@@ -38,9 +38,10 @@ PostgreSQL documentation
3838 <productname>PostgreSQL</productname>. It enables you to type in
3939 queries interactively, issue them to
4040 <productname>PostgreSQL</productname>, and see the query results.
41- Alternatively, input can be from a file. In addition, it provides a
42- number of meta-commands and various shell-like features to
43- facilitate writing scripts and automating a wide variety of tasks.
41+ Alternatively, input can be from a file or from command line
42+ arguments. In addition, it provides a number of meta-commands and various
43+ shell-like features to facilitate writing scripts and automating a wide
44+ variety of tasks.
4445 </para>
4546 </refsect1>
4647
@@ -89,11 +90,10 @@ PostgreSQL documentation
8990 <term><option>--command=<replaceable class="parameter">command</replaceable></></term>
9091 <listitem>
9192 <para>
92- Specifies that <application>psql</application> is to execute one
93- command string, <replaceable class="parameter">command</replaceable>,
94- and then exit. This is useful in shell scripts. Start-up files
95- (<filename>psqlrc</filename> and <filename>~/.psqlrc</filename>) are
96- ignored with this option.
93+ Specifies that <application>psql</application> is to execute the given
94+ command string, <replaceable class="parameter">command</replaceable>.
95+ This option can be repeated and combined in any order with
96+ the <option>-f</option> option.
9797 </para>
9898 <para>
9999 <replaceable class="parameter">command</replaceable> must be either
@@ -102,25 +102,34 @@ PostgreSQL documentation
102102 or a single backslash command. Thus you cannot mix
103103 <acronym>SQL</acronym> and <application>psql</application>
104104 meta-commands with this option. To achieve that, you could
105- pipe the string into <application>psql</application>, for example:
105+ use repeated <option>-c</option> options or pipe the string
106+ into <application>psql</application>, for example:
107+ <literal>psql -c '\x' -c 'SELECT * FROM foo;'</literal> or
106108 <literal>echo '\x \\ SELECT * FROM foo;' | psql</literal>.
107109 (<literal>\\</> is the separator meta-command.)
108110 </para>
109111 <para>
110- If the command string contains multiple SQL commands, they are
111- processed in a single transaction, unless there are explicit
112- <command>BEGIN</>/<command>COMMIT</> commands included in the
113- string to divide it into multiple transactions. This is
114- different from the behavior when the same string is fed to
115- <application>psql</application>'s standard input. Also, only
116- the result of the last SQL command is returned.
112+ Each command string passed to <option>-c</option> is sent to the server
113+ as a single query. Because of this, the server executes it as a single
114+ transaction, even if a command string contains
115+ multiple <acronym>SQL</acronym> commands, unless there are
116+ explicit <command>BEGIN</>/<command>COMMIT</> commands included in the
117+ string to divide it into multiple transactions. Also, the server only
118+ returns the result of the last <acronym>SQL</acronym> command to the
119+ client. This is different from the behavior when the same string with
120+ multiple <acronym>SQL</acronym> commands is fed
121+ to <application>psql</application>'s standard input because
122+ then <application>psql</application> sends each <acronym>SQL</acronym>
123+ command separately.
117124 </para>
118125 <para>
119- Because of these legacy behaviors, putting more than one command in
120- the <option>-c</option> string often has unexpected results. It's
121- better to feed multiple commands to <application>psql</application>'s
122- standard input, either using <application>echo</application> as
123- illustrated above, or via a shell here-document, for example:
126+ Putting more than one command in the <option>-c</option> string often
127+ has unexpected results. This is a result of the fact that the whole
128+ string is sent to the server as a single query.
129+ It's better to use repeated <option>-c</option> commands or feed
130+ multiple commands to <application>psql</application>'s standard input,
131+ either using <application>echo</application> as illustrated above, or
132+ via a shell here-document, for example:
124133<programlisting>
125134psql <<EOF
126135\x
185194 <para>
186195 Use the file <replaceable class="parameter">filename</replaceable>
187196 as the source of commands instead of reading commands interactively.
188- After the file is processed, <application>psql</application>
189- terminates. This is in many ways equivalent to the meta-command
190- <command>\i</command>.
197+ This option can be repeated and combined in any order with
198+ the <option>-c</option> option. After the commands in
199+ every <option>-c</option> command string and <option>-f</option> file
200+ are processed, <application>psql</application> terminates. This option
201+ is in many ways equivalent to the meta-command <command>\i</command>.
191202 </para>
192203
193204 <para>
@@ -539,20 +550,21 @@ EOF
539550 <term><option>--single-transaction</option></term>
540551 <listitem>
541552 <para>
542- When <application>psql</application> executes a script, adding
543- this option wraps <command>BEGIN</>/<command>COMMIT</> around the
544- script to execute it as a single transaction. This ensures that
545- either all the commands complete successfully, or no changes are
546- applied.
553+ When <application>psql</application> executes commands from a script
554+ and/or a <option>-c</option> option, adding this option
555+ wraps <command>BEGIN</>/<command>COMMIT</> around all of those
556+ commands as a whole to execute them as a single transaction. This
557+ ensures that either all the commands complete successfully, or no
558+ changes are applied.
547559 </para>
548560
549561 <para>
550- If the script itself uses <command>BEGIN</>, <command>COMMIT</>,
562+ If the commands themselves
563+ contain <command>BEGIN</>, <command>COMMIT</>,
551564 or <command>ROLLBACK</>, this option will not have the desired
552- effects.
553- Also, if the script contains any command that cannot be executed
554- inside a transaction block, specifying this option will cause that
555- command (and hence the whole transaction) to fail.
565+ effects. Also, if an individual command cannot be executed inside a
566+ transaction block, specifying this option will cause the whole
567+ transaction to fail.
556568 </para>
557569 </listitem>
558570 </varlistentry>
@@ -3725,7 +3737,7 @@ PSQL_EDITOR_LINENUMBER_ARG='--line '
37253737 <term><filename>psqlrc</filename> and <filename>~/.psqlrc</filename></term>
37263738 <listitem>
37273739 <para>
3728- Unless it is passed an <option>-X</option> or <option>-c</option> option,
3740+ Unless it is passed an <option>-X</option> option,
37293741 <application>psql</application> attempts to read and execute commands
37303742 from the system-wide startup file (<filename>psqlrc</filename>) and then
37313743 the user's personal startup file (<filename>~/.psqlrc</filename>), after
@@ -3819,6 +3831,12 @@ PSQL_EDITOR_LINENUMBER_ARG='--line '
38193831 </para>
38203832 </listitem>
38213833
3834+ <listitem>
3835+ <para>
3836+ Before <productname>PostgreSQL</productname> 9.6, <option>-c</option>
3837+ implied <option>-X</option>; this is no longer the case.
3838+ </para>
3839+ </listitem>
38223840 </itemizedlist>
38233841 </refsect1>
38243842
0 commit comments