Re: psql --command option ignores --variable's - Mailing list pgsql-general

From Adrian Klaver
Subject Re: psql --command option ignores --variable's
Date
Msg-id 5224A8FF.6090500@gmail.com
Whole thread Raw
In response to psql --command option ignores --variable's  (Tim Kane <tim.kane@gmail.com>)
Responses Re: psql --command option ignores --variable's  (Tim Kane <tim.kane@gmail.com>)
List pgsql-general
On 09/02/2013 07:54 AM, Tim Kane wrote:
> Hi all,
>
> It seems the behaviour of the —command / -c option is such that it will
> ignore any variables set on the command-line.
>
>
> :~ psql --variable TESTVAR='123' --command 'select :TESTVAR;'
> ERROR:  syntax error at or near ":"
> LINE 1: select :TESTVAR;
>                 ^
>
> :~$ psql --variable TESTVAR='123'
> psql (9.1.9)
> Type "help" for help.
>
> timk=> select :TESTVAR;
>   ?column?
> ----------
>        123
> (1 row)
>
>
>
> Obviously, there are ways around this, but is this expected behaviour?

See  *--> section below.
http://www.postgresql.org/docs/9.1/interactive/app-psql.html

-c command
--command=command
Specifies that psql is to execute one command string, command, and then
exit. This is useful in shell scripts. Start-up files (psqlrc and
~/.psqlrc) are ignored with this option.

*-->command must be either a command string that is completely parsable
by the server (i.e., it contains no psql-specific features), or a single
backslash command. Thus you cannot mix SQL and psql meta-commands with
this option. To achieve that, you could pipe the string into psql, like
this: echo '\x \\ SELECT * FROM foo;' | psql. (\\ is the separator
meta-command.)
<--*

If the command string contains multiple SQL commands, they are processed
in a single transaction, unless there are explicit BEGIN/COMMIT commands
included in the string to divide it into multiple transactions. This is
different from the behavior when the same string is fed to psql's
standard input. Also, only the result of the last SQL command is returned.



>
>
> Tim


--
Adrian Klaver
adrian.klaver@gmail.com


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: psql --command option ignores --variable's
Next
From: Laszlo Hornyak
Date:
Subject: STABLE marker on functions with single select