Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/ref/psql-ref.sgml`
002f5770cfa26b70466da51ea104de460fe7078892382dbd0000000100000fa7
 class="parameter">command</replaceable></option></term>
      <listitem>
      <para>
       Specifies that <application>psql</application> is to execute the given
       command string, <replaceable class="parameter">command</replaceable>.
       This option can be repeated and combined in any order with
       the <option>-f</option> option.  When either <option>-c</option>
       or <option>-f</option> is specified, <application>psql</application>
       does not read commands from standard input; instead it terminates
       after processing all the <option>-c</option> and <option>-f</option>
       options in sequence.
      </para>
      <para>
       <replaceable class="parameter">command</replaceable> must be either
       a command string that is completely parsable by the server (i.e.,
       it contains no <application>psql</application>-specific features),
       or a single backslash command. Thus you cannot mix
       <acronym>SQL</acronym> and <application>psql</application>
       meta-commands within a <option>-c</option> option. To achieve that,
       you could use repeated <option>-c</option> options or pipe the string
       into <application>psql</application>, for example:
<programlisting>
psql -c '\x' -c 'SELECT * FROM foo;'
</programlisting>
       or
<programlisting>
echo '\x \\ SELECT * FROM foo;' | psql
</programlisting>
       (<literal>\\</literal> is the separator meta-command.)
      </para>
      <para>
       Each <acronym>SQL</acronym> command string passed
       to <option>-c</option> is sent to the server as a single request.
       Because of this, the server executes it as a single transaction even
       if the string contains multiple <acronym>SQL</acronym> commands,
       unless there are explicit <command>BEGIN</command>/<command>COMMIT</command>
       commands included in the string to divide it into multiple
       transactions.  (See <xref linkend="protocol-flow-multi-statement"/>
       for more details about how the server handles multi-query strings.)
      </para>
      <para>
       If having several commands executed in one transaction is not desired,
       use repeated <option>-c</option> commands or feed multiple commands to
       <application>psql</application>'s standard input,
       either using <application>echo</application> as illustrated above, or
       via a shell here-document, for example:
<programlisting>
psql &lt;&lt;EOF
\x
SELECT * FROM foo;
EOF
</programlisting></para>
      </listitem>
    </varlistentry>

    <varlistentry id="app-psql-option-csv">
      <term><option>--csv</option></term>
      <listitem>
      <para>
      Switches to <acronym>CSV</acronym> (Comma-Separated Values) output
      mode.  This is equivalent to <command>\pset format csv</command>.
      </para>
      </listitem>
    </varlistentry>

    <varlistentry id="app-psql-option-dbname">
      <term><option>-d <replaceable class="parameter">dbname</replaceable></option></term>
      <term><option>--dbname=<replaceable class="parameter">dbname</replaceable></option></term>
      <listitem>
      <para>
       Specifies the name of the database to connect to. This is
       equivalent to specifying <replaceable
       class="parameter">dbname</replaceable> as the first non-option
       argument on the command line.  The <replaceable>dbname</replaceable>
       can be a <link linkend="libpq-connstring">connection string</link>.
       If so, connection string parameters will override any conflicting
       command line options.
      </para>
      </listitem>
    </varlistentry>

    <varlistentry id="app-psql-option-echo-queries">
      <term><option>-e</option></term>
      <term><option>--echo-queries</option></term>
      <listitem>
      <para>
      Copy all SQL commands sent to the server to standard output as well.
      This is equivalent
      to setting the variable <varname>ECHO</varname> to
      <literal>queries</literal>.
      </para>
      </listitem>
    </varlistentry>

Title: psql Options: -c, --csv, -d, and -e
Summary
This section details the command-line options for psql, focusing on: - `-c` (or `--command`): Executes a given command string. It can be repeated and combined with `-f`. When used, psql doesn't read from standard input. The command must be either a server-parsable SQL command or a single backslash command. Multiple commands in a single `-c` are treated as one transaction, unless BEGIN/COMMIT are specified. It also shows how to execute multiple commands using repeated `-c` or piping into psql. - `--csv`: Sets the output mode to CSV (Comma-Separated Values). - `-d` (or `--dbname`): Specifies the database to connect to, which can be a connection string. Connection string parameters override command line options. - `-e` (or `--echo-queries`): Copies all SQL commands sent to the server to standard output.