jian
jian

Reputation: 4867

How to use PSQL_EDITOR_LINENUMBER_ARG in Windows version of postgresql

wondering around psql.

postgres-# \ev staff_list 2

environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number

https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-PATTERNS

as the psql cli shows, in order to execute \ev staff_list 2 I need know about PSQL_EDITOR_LINENUMBER_ARG.
But I don't know how to set environment variable: PSQL_EDITOR_LINENUMBER_ARG.
So how to make \ev staff_list 2 works in windows?

Upvotes: 1

Views: 234

Answers (2)

Randall
Randall

Reputation: 3034

I hit this in Linux, too (Ubuntu, to be specific).

@Laurenz Albe is spot on with the solution; just a few more points I think might be worth adding.

  • Using \ef with [ line_number ] is the only reliable way I have found to actually get to the line causing the problem. If I jump to line 39 in my (vim) editor, I'm usually a few lines too early1.   So, I've found it invaluable.

  • From within psql, you can just use psql's \setenv
    \setenv PSQL_EDITOR_LINENUMBER_ARG "+"

  • In psql 13.9, if you don't have PSQL_EDITOR set, you get a list of options:

    Select an editor. To change later, run 'select-editor'.
    1 /usr/bin/nedit
    2 /bin/nano <---- easiest
    3 /usr/bin/vim.basic
    4 /usr/bin/vim.tiny
    5 /usr/bin/emacs
    6 /bin/ed

    Choose 1-6 [2]: 3

    So, it will default to nano if you're not paying close attention, and wanted vim.

  • When you \quit vim, you are at the continuation-line prompt (->). A semicolon will run the function as you just edited it; ctrl-c will abort. (Usual psql behavior, but not quite intuitive after the \ef finishes.)


1 I presume PostgreSQL is not counting some blank lines or comments in my source, or it's changing wrapping, so that the lines it's executing are shifted slightly from my source code.

Upvotes: 0

Laurenz Albe
Laurenz Albe

Reputation: 247320

PostgreSQL defines a default editor in src/bin/psql/settings.h: for Windows, that is notepad.exe, everywhere else it is vi.

Now vi has an option to start the editor with the cursor on a certain line:

vi +10 myfile

But notepad.exe has no such option, hence the error message.

You have two things you can do:

  1. don't try to place the cursor on a specific line in the editor:

    \ev staff_list
    
  2. Use a different editor and tell it how to position the cursor:

    SET PSQL_EDITOR=vim.exe
    SET PSQL_EDITOR_LINENUMBER_ARG="+"
    psql -c "\ev staff_list 2"
    

Upvotes: 2

Related Questions