Thread: How to mix psql commands and SQL commands on the shell command line?
How can I mix psql commands and SQL commands on the shell command line, please? $ psql psql (9.2.1) Type "help" for help. ddcKeyGen=> \timing \\ select count(*) from auth_users; Timing is on. count ------- 276 (1 row) Time: 1.730 ms $ psql -c "\timing \\ select count(*) from auth_users;" Timing is on. $ It seems like psql discards everything after the first psql argument... Is there another way to mix psql and SQL commands on the shell command line? Best, Aleksey
On 12/07/2012 11:58 AM, Aleksey Tsalolikhin wrote: > How can I mix psql commands and SQL commands on the shell command line, please? > > $ psql > psql (9.2.1) > Type "help" for help. > > ddcKeyGen=> \timing \\ select count(*) from auth_users; > Timing is on. > count > ------- > 276 > (1 row) > > Time: 1.730 ms > > $ psql -c "\timing \\ select count(*) from auth_users;" > Timing is on. > $ > > It seems like psql discards everything after the first psql > argument... Is there another way to mix psql and SQL commands on the > shell command line? http://www.postgresql.org/docs/9.2/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. > > Best, > Aleksey > > -- Adrian Klaver adrian.klaver@gmail.com
Hello 2012/12/7 Aleksey Tsalolikhin <atsaloli.tech@gmail.com>: > How can I mix psql commands and SQL commands on the shell command line, please? > > $ psql > psql (9.2.1) > Type "help" for help. > > ddcKeyGen=> \timing \\ select count(*) from auth_users; > Timing is on. > count > ------- > 276 > (1 row) > > Time: 1.730 ms > > $ psql -c "\timing \\ select count(*) from auth_users;" > Timing is on. > $ > you can try pipe pavel@nemesis ~ $ echo $'\\timing\nselect 10;' | psql postgres Timing is on. ?column? ────────── 10 (1 row) Time: 0.478 ms Regards Pavel Stehule > It seems like psql discards everything after the first psql > argument... Is there another way to mix psql and SQL commands on the > shell command line? > > Best, > Aleksey > > > -- > Sent via pgsql-general mailing list (pgsql-general@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-general
Re: How to mix psql commands and SQL commands on the shell command line?
From
Aleksey Tsalolikhin
Date:
Thank you very much, Adrian and Pavel. And I really appreciate the documentation reference, Adrian! Yours fondly, Aleksey