]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/psql-ref.sgml
Add psql variables to track success/failure of SQL queries.
[postgresql] / doc / src / sgml / ref / psql-ref.sgml
1 <!--
2 doc/src/sgml/ref/psql-ref.sgml
3 PostgreSQL documentation
4 -->
5
6 <refentry id="APP-PSQL">
7  <indexterm zone="app-psql">
8   <primary>psql</primary>
9  </indexterm>
10
11   <refmeta>
12     <refentrytitle><application>psql</application></refentrytitle>
13     <manvolnum>1</manvolnum>
14     <refmiscinfo>Application</refmiscinfo>
15   </refmeta>
16
17   <refnamediv>
18     <refname><application>psql</application></refname>
19     <refpurpose>
20       <productname>PostgreSQL</productname> interactive terminal
21     </refpurpose>
22   </refnamediv>
23
24  <refsynopsisdiv>
25   <cmdsynopsis>
26    <command>psql</command>
27    <arg rep="repeat"><replaceable class="parameter">option</replaceable></arg>
28    <arg choice="opt"><replaceable class="parameter">dbname</replaceable>
29    <arg choice="opt"><replaceable class="parameter">username</replaceable></arg></arg>
30   </cmdsynopsis>
31  </refsynopsisdiv>
32
33  <refsect1>
34   <title>Description</title>
35
36     <para>
37      <application>psql</application> is a terminal-based front-end to
38      <productname>PostgreSQL</productname>. It enables you to type in
39      queries interactively, issue them to
40      <productname>PostgreSQL</productname>, and see the query results.
41      Alternatively, input can be from a file or from command line
42      arguments. In addition, <application>psql</application> provides a
43      number of meta-commands and various shell-like features to
44      facilitate writing scripts and automating a wide variety of tasks.
45     </para>
46  </refsect1>
47
48  <refsect1 id="R1-APP-PSQL-3">
49   <title>Options</title>
50
51   <variablelist>
52     <varlistentry>
53       <term><option>-a</></term>
54       <term><option>--echo-all</></term>
55       <listitem>
56       <para>
57       Print all nonempty input lines to standard output as they are read.
58       (This does not apply to lines read interactively.) This is
59       equivalent to setting the variable <varname>ECHO</varname> to
60       <literal>all</literal>.
61       </para>
62       </listitem>
63     </varlistentry>
64
65     <varlistentry>
66       <term><option>-A</></term>
67       <term><option>--no-align</></term>
68       <listitem>
69       <para>
70       Switches to unaligned output mode. (The default output mode is
71       otherwise aligned.)  This is equivalent to <command>\pset format
72       unaligned</command>.
73       </para>
74       </listitem>
75     </varlistentry>
76
77     <varlistentry>
78       <term><option>-b</></term>
79       <term><option>--echo-errors</></term>
80       <listitem>
81       <para>
82       Print failed SQL commands to standard error output. This is
83       equivalent to setting the variable <varname>ECHO</varname> to
84       <literal>errors</literal>.
85       </para>
86       </listitem>
87     </varlistentry>
88
89     <varlistentry>
90       <term><option>-c <replaceable class="parameter">command</replaceable></></term>
91       <term><option>--command=<replaceable class="parameter">command</replaceable></></term>
92       <listitem>
93       <para>
94        Specifies that <application>psql</application> is to execute the given
95        command string, <replaceable class="parameter">command</replaceable>.
96        This option can be repeated and combined in any order with
97        the <option>-f</option> option.  When either <option>-c</option>
98        or <option>-f</option> is specified, <application>psql</application>
99        does not read commands from standard input; instead it terminates
100        after processing all the <option>-c</option> and <option>-f</option>
101        options in sequence.
102       </para>
103       <para>
104        <replaceable class="parameter">command</replaceable> must be either
105        a command string that is completely parsable by the server (i.e.,
106        it contains no <application>psql</application>-specific features),
107        or a single backslash command. Thus you cannot mix
108        <acronym>SQL</acronym> and <application>psql</application>
109        meta-commands within a <option>-c</option> option. To achieve that,
110        you could use repeated <option>-c</option> options or pipe the string
111        into <application>psql</application>, for example:
112 <programlisting>
113 psql -c '\x' -c 'SELECT * FROM foo;'
114 </programlisting>
115        or
116 <programlisting>
117 echo '\x \\ SELECT * FROM foo;' | psql
118 </programlisting>
119        (<literal>\\</> is the separator meta-command.)
120       </para>
121       <para>
122        Each <acronym>SQL</acronym> command string passed
123        to <option>-c</option> is sent to the server as a single request.
124        Because of this, the server executes it as a single transaction even
125        if the string contains multiple <acronym>SQL</acronym> commands,
126        unless there are explicit <command>BEGIN</>/<command>COMMIT</>
127        commands included in the string to divide it into multiple
128        transactions.  (See <xref linkend="protocol-flow-multi-statement">
129        for more details about how the server handles multi-query strings.)
130        Also, <application>psql</application> only prints the
131        result of the last <acronym>SQL</acronym> command in the string.
132        This is different from the behavior when the same string is read from
133        a file or fed to <application>psql</application>'s standard input,
134        because then <application>psql</application> sends
135        each <acronym>SQL</acronym> command separately.
136       </para>
137       <para>
138        Because of this behavior, putting more than one SQL command in a
139        single <option>-c</option> string often has unexpected results.
140        It's better to use repeated <option>-c</option> commands or feed
141        multiple commands to <application>psql</application>'s standard input,
142        either using <application>echo</application> as illustrated above, or
143        via a shell here-document, for example:
144 <programlisting>
145 psql &lt;&lt;EOF
146 \x
147 SELECT * FROM foo;
148 EOF
149 </programlisting>
150       </para>
151       </listitem>
152     </varlistentry>
153
154     <varlistentry>
155       <term><option>-d <replaceable class="parameter">dbname</replaceable></></term>
156       <term><option>--dbname=<replaceable class="parameter">dbname</replaceable></></term>
157       <listitem>
158       <para>
159        Specifies the name of the database to connect to. This is
160        equivalent to specifying <replaceable
161        class="parameter">dbname</replaceable> as the first non-option
162        argument on the command line.
163       </para>
164       <para>
165        If this parameter contains an <symbol>=</symbol> sign or starts
166        with a valid <acronym>URI</acronym> prefix
167        (<literal>postgresql://</literal>
168        or <literal>postgres://</literal>), it is treated as a
169        <parameter>conninfo</parameter> string. See <xref
170        linkend="libpq-connstring"> for more information.
171       </para>
172       </listitem>
173     </varlistentry>
174
175     <varlistentry>
176       <term><option>-e</></term>
177       <term><option>--echo-queries</></term>
178       <listitem>
179       <para>
180       Copy all SQL commands sent to the server to standard output as well.
181       This is equivalent
182       to setting the variable <varname>ECHO</varname> to
183       <literal>queries</literal>.
184       </para>
185       </listitem>
186     </varlistentry>
187
188     <varlistentry>
189       <term><option>-E</></term>
190       <term><option>--echo-hidden</></term>
191       <listitem>
192       <para>
193       Echo the actual queries generated by <command>\d</command> and other backslash
194       commands. You can use this to study <application>psql</application>'s
195       internal operations. This is equivalent to
196       setting the variable <varname>ECHO_HIDDEN</varname> to <literal>on</>.
197       </para>
198       </listitem>
199     </varlistentry>
200
201     <varlistentry>
202       <term><option>-f <replaceable class="parameter">filename</replaceable></></term>
203       <term><option>--file=<replaceable class="parameter">filename</replaceable></></term>
204       <listitem>
205       <para>
206        Read commands from the
207        file <replaceable class="parameter">filename</replaceable>,
208        rather than standard input.
209        This option can be repeated and combined in any order with
210        the <option>-c</option> option.  When either <option>-c</option>
211        or <option>-f</option> is specified, <application>psql</application>
212        does not read commands from standard input; instead it terminates
213        after processing all the <option>-c</option> and <option>-f</option>
214        options in sequence.
215        Except for that, this option is largely equivalent to the
216        meta-command <command>\i</command>.
217       </para>
218
219       <para>
220        If <replaceable>filename</replaceable> is <literal>-</literal>
221        (hyphen), then standard input is read until an EOF indication
222        or <command>\q</> meta-command.  This can be used to intersperse
223        interactive input with input from files.  Note however that Readline
224        is not used in this case (much as if <option>-n</option> had been
225        specified).
226       </para>
227
228       <para>
229       Using this option is subtly different from writing <literal>psql
230       &lt; <replaceable
231       class="parameter">filename</replaceable></literal>. In general,
232       both will do what you expect, but using <literal>-f</literal>
233       enables some nice features such as error messages with line
234       numbers. There is also a slight chance that using this option will
235       reduce the start-up overhead. On the other hand, the variant using
236       the shell's input redirection is (in theory) guaranteed to yield
237       exactly the same output you would have received had you entered
238       everything by hand.
239       </para>
240       </listitem>
241     </varlistentry>
242
243     <varlistentry>
244       <term><option>-F <replaceable class="parameter">separator</replaceable></></term>
245       <term><option>--field-separator=<replaceable class="parameter">separator</replaceable></></term>
246       <listitem>
247       <para>
248       Use <replaceable class="parameter">separator</replaceable> as the
249       field separator for unaligned output. This is equivalent to
250       <command>\pset fieldsep</command> or <command>\f</command>.
251       </para>
252       </listitem>
253     </varlistentry>
254
255     <varlistentry>
256       <term><option>-h <replaceable class="parameter">hostname</replaceable></></term>
257       <term><option>--host=<replaceable class="parameter">hostname</replaceable></></term>
258       <listitem>
259       <para>
260       Specifies the host name of the machine on which the
261       server is running. If the value begins
262       with a slash, it is used as the directory for the Unix-domain
263       socket.
264       </para>
265       </listitem>
266     </varlistentry>
267
268     <varlistentry>
269       <term><option>-H</></term>
270       <term><option>--html</></term>
271       <listitem>
272       <para>
273       Turn on <acronym>HTML</acronym> tabular output. This is
274       equivalent to <literal>\pset format html</literal> or the
275       <command>\H</command> command.
276       </para>
277       </listitem>
278     </varlistentry>
279
280     <varlistentry>
281       <term><option>-l</></term>
282       <term><option>--list</></term>
283       <listitem>
284       <para>
285       List all available databases, then exit. Other non-connection
286       options are ignored. This is similar to the meta-command
287       <command>\list</command>.
288       </para>
289       </listitem>
290     </varlistentry>
291
292     <varlistentry>
293       <term><option>-L <replaceable class="parameter">filename</replaceable></></term>
294       <term><option>--log-file=<replaceable class="parameter">filename</replaceable></></term>
295       <listitem>
296       <para>
297        Write all query output into file <replaceable
298        class="parameter">filename</replaceable>, in addition to the
299        normal output destination.
300       </para>
301       </listitem>
302     </varlistentry>
303
304     <varlistentry>
305       <term><option>-n</></term>
306       <term><option>--no-readline</></term>
307       <listitem>
308       <para>
309        Do not use <application>Readline</application> for line editing and do
310        not use the command history.
311        This can be useful to turn off tab expansion when cutting and pasting.
312       </para>
313       </listitem>
314     </varlistentry>
315
316     <varlistentry>
317       <term><option>-o <replaceable class="parameter">filename</replaceable></></term>
318       <term><option>--output=<replaceable class="parameter">filename</replaceable></></term>
319       <listitem>
320       <para>
321       Put all query output into file <replaceable
322       class="parameter">filename</replaceable>. This is equivalent to
323       the command <command>\o</command>.
324       </para>
325       </listitem>
326     </varlistentry>
327
328     <varlistentry>
329       <term><option>-p <replaceable class="parameter">port</replaceable></></term>
330       <term><option>--port=<replaceable class="parameter">port</replaceable></></term>
331       <listitem>
332       <para>
333       Specifies the TCP port or the local Unix-domain
334       socket file extension on which the server is listening for
335       connections. Defaults to the value of the <envar>PGPORT</envar>
336       environment variable or, if not set, to the port specified at
337       compile time, usually 5432.
338       </para>
339       </listitem>
340     </varlistentry>
341
342     <varlistentry>
343       <term><option>-P <replaceable class="parameter">assignment</replaceable></></term>
344       <term><option>--pset=<replaceable class="parameter">assignment</replaceable></></term>
345       <listitem>
346       <para>
347       Specifies printing options, in the style of
348       <command>\pset</command>. Note that here you
349       have to separate name and value with an equal sign instead of a
350       space. For example, to set the output format to <application>LaTeX</application>, you could write
351       <literal>-P format=latex</literal>.
352       </para>
353       </listitem>
354     </varlistentry>
355
356     <varlistentry>
357       <term><option>-q</></term>
358       <term><option>--quiet</></term>
359       <listitem>
360       <para>
361       Specifies that <application>psql</application> should do its work
362       quietly. By default, it prints welcome messages and various
363       informational output. If this option is used, none of this
364       happens. This is useful with the <option>-c</option> option.
365       This is equivalent to setting the variable <varname>QUIET</varname>
366       to <literal>on</>.
367       </para>
368       </listitem>
369     </varlistentry>
370
371     <varlistentry>
372       <term><option>-R <replaceable class="parameter">separator</replaceable></></term>
373       <term><option>--record-separator=<replaceable class="parameter">separator</replaceable></></term>
374       <listitem>
375       <para>
376       Use <replaceable class="parameter">separator</replaceable> as the
377       record separator for unaligned output. This is equivalent to
378       <command>\pset recordsep</command>.
379       </para>
380       </listitem>
381     </varlistentry>
382
383     <varlistentry>
384       <term><option>-s</></term>
385       <term><option>--single-step</></term>
386       <listitem>
387       <para>
388       Run in single-step mode. That means the user is prompted before
389       each command is sent to the server, with the option to cancel
390       execution as well. Use this to debug scripts.
391       </para>
392       </listitem>
393     </varlistentry>
394
395     <varlistentry>
396       <term><option>-S</></term>
397       <term><option>--single-line</></term>
398       <listitem>
399       <para>
400       Runs in single-line mode where a newline terminates an SQL command, as a
401       semicolon does.
402       </para>
403
404       <note>
405       <para>
406       This mode is provided for those who insist on it, but you are not
407       necessarily encouraged to use it. In particular, if you mix
408       <acronym>SQL</acronym> and meta-commands on a line the order of
409       execution might not always be clear to the inexperienced user.
410       </para>
411       </note>
412       </listitem>
413     </varlistentry>
414
415     <varlistentry>
416       <term><option>-t</></term>
417       <term><option>--tuples-only</></term>
418       <listitem>
419       <para>
420       Turn off printing of column names and result row count footers,
421       etc. This is equivalent to <command>\t</command> or
422       <command>\pset tuples_only</command>.
423       </para>
424       </listitem>
425     </varlistentry>
426
427     <varlistentry>
428       <term><option>-T <replaceable class="parameter">table_options</replaceable></></term>
429       <term><option>--table-attr=<replaceable class="parameter">table_options</replaceable></></term>
430       <listitem>
431       <para>
432       Specifies options to be placed within the
433       <acronym>HTML</acronym> <sgmltag>table</sgmltag> tag. See
434       <command>\pset tableattr</command> for details.
435       </para>
436       </listitem>
437     </varlistentry>
438
439     <varlistentry>
440       <term><option>-U <replaceable class="parameter">username</replaceable></></term>
441       <term><option>--username=<replaceable class="parameter">username</replaceable></></term>
442       <listitem>
443       <para>
444       Connect to the database as the user <replaceable
445       class="parameter">username</replaceable> instead of the default.
446       (You must have permission to do so, of course.)
447       </para>
448       </listitem>
449     </varlistentry>
450
451     <varlistentry>
452       <term><option>-v <replaceable class="parameter">assignment</replaceable></></term>
453       <term><option>--set=<replaceable class="parameter">assignment</replaceable></></term>
454       <term><option>--variable=<replaceable class="parameter">assignment</replaceable></></term>
455       <listitem>
456       <para>
457       Perform a variable assignment, like the <command>\set</command>
458       meta-command. Note that you must separate name and value, if
459       any, by an equal sign on the command line. To unset a variable,
460       leave off the equal sign. To set a variable with an empty value,
461       use the equal sign but leave off the value. These assignments are
462       done during command line processing, so variables that reflect
463       connection state will get overwritten later.
464       </para>
465       </listitem>
466     </varlistentry>
467
468     <varlistentry>
469       <term><option>-V</></term>
470       <term><option>--version</></term>
471       <listitem>
472       <para>
473       Print the <application>psql</application> version and exit.
474       </para>
475       </listitem>
476     </varlistentry>
477
478     <varlistentry>
479      <term><option>-w</></term>
480      <term><option>--no-password</></term>
481      <listitem>
482       <para>
483        Never issue a password prompt.  If the server requires password
484        authentication and a password is not available by other means
485        such as a <filename>.pgpass</filename> file, the connection
486        attempt will fail.  This option can be useful in batch jobs and
487        scripts where no user is present to enter a password.
488       </para>
489
490       <para>
491        Note that this option will remain set for the entire session,
492        and so it affects uses of the meta-command
493        <command>\connect</command> as well as the initial connection attempt.
494       </para>
495      </listitem>
496     </varlistentry>
497
498     <varlistentry>
499       <term><option>-W</></term>
500       <term><option>--password</></term>
501       <listitem>
502       <para>
503        Force <application>psql</application> to prompt for a
504        password before connecting to a database.
505       </para>
506
507       <para>
508        This option is never essential, since <application>psql</application>
509        will automatically prompt for a password if the server demands
510        password authentication.  However, <application>psql</application>
511        will waste a connection attempt finding out that the server wants a
512        password.  In some cases it is worth typing <option>-W</> to avoid
513        the extra connection attempt.
514       </para>
515
516       <para>
517        Note that this option will remain set for the entire session,
518        and so it affects uses of the meta-command
519        <command>\connect</command> as well as the initial connection attempt.
520       </para>
521       </listitem>
522     </varlistentry>
523
524     <varlistentry>
525       <term><option>-x</></term>
526       <term><option>--expanded</></term>
527       <listitem>
528       <para>
529       Turn on the expanded table formatting mode. This is equivalent to
530       <command>\x</command> or <command>\pset expanded</command>.
531       </para>
532       </listitem>
533     </varlistentry>
534
535     <varlistentry>
536       <term><option>-X,</></term>
537       <term><option>--no-psqlrc</></term>
538       <listitem>
539       <para>
540       Do not read the start-up file (neither the system-wide
541       <filename>psqlrc</filename> file nor the user's
542       <filename>~/.psqlrc</filename> file).
543       </para>
544       </listitem>
545     </varlistentry>
546
547     <varlistentry>
548       <term><option>-z</option></term>
549       <term><option>--field-separator-zero</option></term>
550       <listitem>
551       <para>
552       Set the field separator for unaligned output to a zero byte.  This is
553       equvalent to <command>\pset fieldsep_zero</command>.
554       </para>
555       </listitem>
556     </varlistentry>
557
558     <varlistentry>
559       <term><option>-0</option></term>
560       <term><option>--record-separator-zero</option></term>
561       <listitem>
562       <para>
563       Set the record separator for unaligned output to a zero byte.  This is
564       useful for interfacing, for example, with <literal>xargs -0</literal>.
565       This is equivalent to <command>\pset recordsep_zero</command>.
566       </para>
567       </listitem>
568     </varlistentry>
569
570      <varlistentry>
571       <term><option>-1</option></term>
572       <term><option>--single-transaction</option></term>
573       <listitem>
574        <para>
575         This option can only be used in combination with one or more
576         <option>-c</option> and/or <option>-f</option> options.  It causes
577         <application>psql</application> to issue a <command>BEGIN</> command
578         before the first such option and a <command>COMMIT</> command after
579         the last one, thereby wrapping all the commands into a single
580         transaction.  This ensures that either all the commands complete
581         successfully, or no changes are applied.
582        </para>
583
584        <para>
585         If the commands themselves
586         contain <command>BEGIN</>, <command>COMMIT</>,
587         or <command>ROLLBACK</>, this option will not have the desired
588         effects.  Also, if an individual command cannot be executed inside a
589         transaction block, specifying this option will cause the whole
590         transaction to fail.
591        </para>
592       </listitem>
593      </varlistentry>
594
595     <varlistentry>
596       <term><option>-?</></term>
597       <term><option>--help[=<replaceable class="parameter">topic</>]</option></term>
598       <listitem>
599       <para>
600       Show help about <application>psql</application> and exit. The optional
601       <replaceable class="parameter">topic</> parameter (defaulting
602       to <literal>options</literal>) selects which part of <application>psql</application> is
603       explained: <literal>commands</> describes <application>psql</>'s
604       backslash commands; <literal>options</> describes the command-line
605       options that can be passed to <application>psql</>;
606       and <literal>variables</> shows help about <application>psql</application> configuration
607       variables.
608       </para>
609       </listitem>
610     </varlistentry>
611
612   </variablelist>
613  </refsect1>
614
615
616  <refsect1>
617   <title>Exit Status</title>
618
619   <para>
620    <application>psql</application> returns 0 to the shell if it
621    finished normally, 1 if a fatal error of its own occurs (e.g. out of memory,
622    file not found), 2 if the connection to the server went bad
623    and the session was not interactive, and 3 if an error occurred in a
624    script and the variable <varname>ON_ERROR_STOP</varname> was set.
625   </para>
626  </refsect1>
627
628
629  <refsect1>
630   <title>Usage</title>
631
632   <refsect2 id="R2-APP-PSQL-connecting">
633     <title>Connecting to a Database</title>
634
635     <para>
636     <application>psql</application> is a regular
637     <productname>PostgreSQL</productname> client application. In order
638     to connect to a database you need to know the name of your target
639     database, the host name and port number of the server, and what user
640     name you want to connect as. <application>psql</application> can be
641     told about those parameters via command line options, namely
642     <option>-d</option>, <option>-h</option>, <option>-p</option>, and
643     <option>-U</option> respectively. If an argument is found that does
644     not belong to any option it will be interpreted as the database name
645     (or the user name, if the database name is already given). Not all
646     of these options are required; there are useful defaults. If you omit the host
647     name, <application>psql</> will connect via a Unix-domain socket
648     to a server on the local host, or via TCP/IP to <literal>localhost</> on
649     machines that don't have Unix-domain sockets. The default port number is
650     determined at compile time.
651     Since the database server uses the same default, you will not have
652     to specify the port in most cases. The default user name is your
653     operating-system user name, as is the default database name.
654     Note that you cannot
655     just connect to any database under any user name. Your database
656     administrator should have informed you about your access rights.
657     </para>
658
659     <para>
660     When the defaults aren't quite right, you can save yourself
661     some typing by setting the environment variables
662     <envar>PGDATABASE</envar>, <envar>PGHOST</envar>,
663     <envar>PGPORT</envar> and/or <envar>PGUSER</envar> to appropriate
664     values. (For additional environment variables, see <xref
665     linkend="libpq-envars">.) It is also convenient to have a
666     <filename>~/.pgpass</> file to avoid regularly having to type in
667     passwords. See <xref linkend="libpq-pgpass"> for more information.
668     </para>
669
670     <para>
671      An alternative way to specify connection parameters is in a
672      <parameter>conninfo</parameter> string or
673      a <acronym>URI</acronym>, which is used instead of a database
674      name. This mechanism give you very wide control over the
675      connection. For example:
676 <programlisting>
677 $ <userinput>psql "service=myservice sslmode=require"</userinput>
678 $ <userinput>psql postgresql://dbmaster:5433/mydb?sslmode=require</userinput>
679 </programlisting>
680      This way you can also use <acronym>LDAP</acronym> for connection
681      parameter lookup as described in <xref linkend="libpq-ldap">.
682      See <xref linkend="libpq-paramkeywords"> for more information on all the
683      available connection options.
684     </para>
685
686     <para>
687     If the connection could not be made for any reason (e.g., insufficient
688     privileges, server is not running on the targeted host, etc.),
689     <application>psql</application> will return an error and terminate.
690     </para>
691
692     <para>
693      If both standard input and standard output are a
694      terminal, then <application>psql</application> sets the client
695      encoding to <quote>auto</quote>, which will detect the
696      appropriate client encoding from the locale settings
697      (<envar>LC_CTYPE</envar> environment variable on Unix systems).
698      If this doesn't work out as expected, the client encoding can be
699      overridden using the environment
700      variable <envar>PGCLIENTENCODING</envar>.
701     </para>
702   </refsect2>
703
704   <refsect2 id="R2-APP-PSQL-4">
705     <title>Entering SQL Commands</title>
706
707     <para>
708     In normal operation, <application>psql</application> provides a
709     prompt with the name of the database to which
710     <application>psql</application> is currently connected, followed by
711     the string <literal>=&gt;</literal>. For example:
712 <programlisting>
713 $ <userinput>psql testdb</userinput>
714 psql (&version;)
715 Type "help" for help.
716
717 testdb=&gt;
718 </programlisting>
719     </para>
720
721     <para>
722     At the prompt, the user can type in <acronym>SQL</acronym> commands.
723     Ordinarily, input lines are sent to the server when a
724     command-terminating semicolon is reached. An end of line does not
725     terminate a command.  Thus commands can be spread over several lines for
726     clarity. If the command was sent and executed without error, the results
727     of the command are displayed on the screen.
728     </para>
729
730     <para>
731     Whenever a command is executed, <application>psql</application> also polls
732     for asynchronous notification events generated by
733     <xref linkend="SQL-LISTEN"> and
734     <xref linkend="SQL-NOTIFY">.
735     </para>
736
737     <para>
738     While C-style block comments are passed to the server for
739     processing and removal, SQL-standard comments are removed by
740     <application>psql</application>.
741     </para>
742   </refsect2>
743
744   <refsect2 id="APP-PSQL-meta-commands">
745     <title>Meta-Commands</title>
746
747     <para>
748     Anything you enter in <application>psql</application> that begins
749     with an unquoted backslash is a <application>psql</application>
750     meta-command that is processed by <application>psql</application>
751     itself. These commands make
752     <application>psql</application> more useful for administration or
753     scripting. Meta-commands are often called slash or backslash commands.
754     </para>
755
756     <para>
757     The format of a <application>psql</application> command is the backslash,
758     followed immediately by a command verb, then any arguments. The arguments
759     are separated from the command verb and each other by any number of
760     whitespace characters.
761     </para>
762
763     <para>
764     To include whitespace in an argument you can quote it with
765     single quotes. To include a single quote in an argument,
766     write two single quotes within single-quoted text.
767     Anything contained in single quotes is
768     furthermore subject to C-like substitutions for
769     <literal>\n</literal> (new line), <literal>\t</literal> (tab),
770     <literal>\b</literal> (backspace), <literal>\r</literal> (carriage return),
771     <literal>\f</literal> (form feed),
772     <literal>\</literal><replaceable>digits</replaceable> (octal), and
773     <literal>\x</literal><replaceable>digits</replaceable> (hexadecimal).
774     A backslash preceding any other character within single-quoted text
775     quotes that single character, whatever it is.
776     </para>
777
778     <para>
779     If an unquoted colon (<literal>:</literal>) followed by a
780     <application>psql</> variable name appears within an argument, it is
781     replaced by the variable's value, as described in <xref
782     linkend="APP-PSQL-interpolation" endterm="APP-PSQL-interpolation-title">.
783     The forms <literal>:'<replaceable>variable_name</>'</literal> and
784     <literal>:"<replaceable>variable_name</>"</literal> described there
785     work as well.
786     </para>
787
788     <para>
789     Within an argument, text that is enclosed in backquotes
790     (<literal>`</literal>) is taken as a command line that is passed to the
791     shell.  The output of the command (with any trailing newline removed)
792     replaces the backquoted text.  Within the text enclosed in backquotes,
793     no special quoting or other processing occurs, except that appearances
794     of <literal>:<replaceable>variable_name</></literal> where
795     <replaceable>variable_name</> is a <application>psql</> variable name
796     are replaced by the variable's value.  Also, appearances of
797     <literal>:'<replaceable>variable_name</>'</literal> are replaced by the
798     variable's value suitably quoted to become a single shell command
799     argument.  (The latter form is almost always preferable, unless you are
800     very sure of what is in the variable.)  Because carriage return and line
801     feed characters cannot be safely quoted on all platforms, the
802     <literal>:'<replaceable>variable_name</>'</literal> form prints an
803     error message and does not substitute the variable value when such
804     characters appear in the value.
805     </para>
806
807     <para>
808     Some commands take an <acronym>SQL</acronym> identifier (such as a
809     table name) as argument. These arguments follow the syntax rules
810     of <acronym>SQL</acronym>: Unquoted letters are forced to
811     lowercase, while double quotes (<literal>"</>) protect letters
812     from case conversion and allow incorporation of whitespace into
813     the identifier.  Within double quotes, paired double quotes reduce
814     to a single double quote in the resulting name.  For example,
815     <literal>FOO"BAR"BAZ</> is interpreted as <literal>fooBARbaz</>,
816     and <literal>"A weird"" name"</> becomes <literal>A weird"
817     name</>.
818     </para>
819
820     <para>
821     Parsing for arguments stops at the end of the line, or when another
822     unquoted backslash is found.  An unquoted backslash
823     is taken as the beginning of a new meta-command. The special
824     sequence <literal>\\</literal> (two backslashes) marks the end of
825     arguments and continues parsing <acronym>SQL</acronym> commands, if
826     any. That way <acronym>SQL</acronym> and
827     <application>psql</application> commands can be freely mixed on a
828     line. But in any case, the arguments of a meta-command cannot
829     continue beyond the end of the line.
830     </para>
831
832     <para>
833     Many of the meta-commands act on the <firstterm>current query buffer</>.
834     This is simply a buffer holding whatever SQL command text has been typed
835     but not yet sent to the server for execution.  This will include previous
836     input lines as well as any text appearing before the meta-command on the
837     same line.
838     </para>
839
840     <para>
841     The following meta-commands are defined:
842
843     <variablelist>
844       <varlistentry>
845         <term><literal>\a</literal></term>
846         <listitem>
847         <para>
848         If the current table output format is unaligned, it is switched to aligned.
849         If it is not unaligned, it is set to unaligned. This command is
850         kept for backwards compatibility. See <command>\pset</command> for a
851         more general solution.
852         </para>
853         </listitem>
854       </varlistentry>
855
856       <varlistentry>
857         <term><literal>\c</literal> or <literal>\connect [ -reuse-previous=<replaceable class="parameter">on|off</replaceable> ] [ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] [ <replaceable class="parameter">host</replaceable> ] [ <replaceable class="parameter">port</replaceable> ] | <replaceable class="parameter">conninfo</replaceable> ]</literal></term>
858         <listitem>
859         <para>
860         Establishes a new connection to a <productname>PostgreSQL</>
861         server.  The connection parameters to use can be specified either
862         using a positional syntax, or using <replaceable>conninfo</> connection
863         strings as detailed in <xref linkend="libpq-connstring">.
864         </para>
865
866         <para>
867         Where the command omits database name, user, host, or port, the new
868         connection can reuse values from the previous connection.  By default,
869         values from the previous connection are reused except when processing
870         a <replaceable>conninfo</> string.  Passing a first argument
871         of <literal>-reuse-previous=on</>
872         or <literal>-reuse-previous=off</literal> overrides that default.
873         When the command neither specifies nor reuses a particular parameter,
874         the <application>libpq</application> default is used.  Specifying any
875         of <replaceable class="parameter">dbname</replaceable>,
876         <replaceable class="parameter">username</replaceable>,
877         <replaceable class="parameter">host</replaceable> or
878         <replaceable class="parameter">port</replaceable>
879         as <literal>-</literal> is equivalent to omitting that parameter.
880         </para>
881
882         <para>
883         If the new connection is successfully made, the previous
884         connection is closed.
885         If the connection attempt failed (wrong user name, access
886         denied, etc.), the previous connection will only be kept if
887         <application>psql</application> is in interactive mode. When
888         executing a non-interactive script, processing will
889         immediately stop with an error. This distinction was chosen as
890         a user convenience against typos on the one hand, and a safety
891         mechanism that scripts are not accidentally acting on the
892         wrong database on the other hand.
893         </para>
894
895         <para>
896         Examples:
897         </para>
898 <programlisting>
899 =&gt; \c mydb myuser host.dom 6432
900 =&gt; \c service=foo
901 =&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
902 =&gt; \c postgresql://tom@localhost/mydb?application_name=myapp
903 </programlisting>
904         </listitem>
905       </varlistentry>
906
907       <varlistentry>
908         <term><literal>\C [ <replaceable class="parameter">title</replaceable> ]</literal></term>
909         <listitem>
910         <para>
911         Sets the title of any tables being printed as the result of a
912         query or unset any such title. This command is equivalent to
913         <literal>\pset title <replaceable
914         class="parameter">title</replaceable></literal>. (The name of
915         this command derives from <quote>caption</quote>, as it was
916         previously only used to set the caption in an
917         <acronym>HTML</acronym> table.)
918         </para>
919         </listitem>
920       </varlistentry>
921
922       <varlistentry>
923        <term><literal>\cd [ <replaceable>directory</replaceable> ]</literal></term>
924        <listitem>
925         <para>
926          Changes the current working directory to
927          <replaceable>directory</replaceable>. Without argument, changes
928          to the current user's home directory.
929         </para>
930
931         <tip>
932          <para>
933           To print your current working directory, use <literal>\! pwd</literal>.
934          </para>
935         </tip>
936        </listitem>
937       </varlistentry>
938
939       <varlistentry>
940         <term><literal>\conninfo</literal></term>
941         <listitem>
942         <para>
943         Outputs information about the current database connection.
944         </para>
945         </listitem>
946       </varlistentry>
947
948       <varlistentry id="APP-PSQL-meta-commands-copy">
949         <term><literal>\copy { <replaceable class="parameter">table</replaceable> [ ( <replaceable class="parameter">column_list</replaceable> ) ] | ( <replaceable class="parameter">query</replaceable> ) }
950         { <literal>from</literal> | <literal>to</literal> }
951         { <replaceable class="parameter">'filename'</replaceable> | program <replaceable class="parameter">'command'</replaceable> | stdin | stdout | pstdin | pstdout }
952         [ [ with ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]</literal></term>
953
954         <listitem>
955         <para>
956         Performs a frontend (client) copy. This is an operation that
957         runs an <acronym>SQL</acronym> <xref linkend="SQL-COPY">
958         command, but instead of the server
959         reading or writing the specified file,
960         <application>psql</application> reads or writes the file and
961         routes the data between the server and the local file system.
962         This means that file accessibility and privileges are those of
963         the local user, not the server, and no SQL superuser
964         privileges are required.
965         </para>
966
967         <para>
968         When <literal>program</> is specified,
969         <replaceable class="parameter">command</replaceable> is
970         executed by <application>psql</application> and the data passed from
971         or to <replaceable class="parameter">command</replaceable> is
972         routed between the server and the client.
973         Again, the execution privileges are those of
974         the local user, not the server, and no SQL superuser
975         privileges are required.
976         </para>
977
978         <para>
979         For <literal>\copy ... from stdin</>, data rows are read from the same
980         source that issued the command, continuing until <literal>\.</literal>
981         is read or the stream reaches <acronym>EOF</>. This option is useful
982         for populating tables in-line within a SQL script file.
983         For <literal>\copy ... to stdout</>, output is sent to the same place
984         as <application>psql</> command output, and
985         the <literal>COPY <replaceable>count</></literal> command status is
986         not printed (since it might be confused with a data row).
987         To read/write <application>psql</application>'s standard input or
988         output regardless of the current command source or <literal>\o</>
989         option, write <literal>from pstdin</> or <literal>to pstdout</>.
990         </para>
991
992         <para>
993         The syntax of this command is similar to that of the
994         <acronym>SQL</acronym> <xref linkend="sql-copy">
995         command.  All options other than the data source/destination are
996         as specified for <xref linkend="sql-copy">.
997         Because of this, special parsing rules apply to the <command>\copy</>
998         meta-command.  Unlike most other meta-commands, the entire remainder
999         of the line is always taken to be the arguments of <command>\copy</>,
1000         and neither variable interpolation nor backquote expansion are
1001         performed in the arguments.
1002         </para>
1003
1004         <tip>
1005         <para>
1006         This operation is not as efficient as the <acronym>SQL</acronym>
1007         <command>COPY</command> command because all data must pass
1008         through the client/server connection. For large
1009         amounts of data the <acronym>SQL</acronym> command might be preferable.
1010         </para>
1011         </tip>
1012
1013         </listitem>
1014       </varlistentry>
1015
1016       <varlistentry>
1017         <term><literal>\copyright</literal></term>
1018         <listitem>
1019         <para>
1020         Shows the copyright and distribution terms of
1021         <productname>PostgreSQL</productname>.
1022         </para>
1023         </listitem>
1024       </varlistentry>
1025
1026
1027       <varlistentry id="APP-PSQL-meta-commands-crosstabview">
1028         <term><literal>\crosstabview [
1029             <replaceable class="parameter">colV</replaceable>
1030             [ <replaceable class="parameter">colH</replaceable>
1031             [ <replaceable class="parameter">colD</replaceable>
1032             [ <replaceable class="parameter">sortcolH</replaceable>
1033             ] ] ] ] </literal></term>
1034         <listitem>
1035         <para>
1036         Executes the current query buffer (like <literal>\g</literal>) and
1037         shows the results in a crosstab grid.
1038         The query must return at least three columns.
1039         The output column identified by <replaceable class="parameter">colV</>
1040         becomes a vertical header and the output column identified by
1041         <replaceable class="parameter">colH</replaceable>
1042         becomes a horizontal header.
1043         <replaceable class="parameter">colD</replaceable> identifies
1044         the output column to display within the grid.
1045         <replaceable class="parameter">sortcolH</replaceable> identifies
1046         an optional sort column for the horizontal header.
1047         </para>
1048
1049         <para>
1050         Each column specification can be a column number (starting at 1) or
1051         a column name.  The usual SQL case folding and quoting rules apply to
1052         column names.  If omitted,
1053         <replaceable class="parameter">colV</replaceable> is taken as column 1
1054         and <replaceable class="parameter">colH</replaceable> as column 2.
1055         <replaceable class="parameter">colH</replaceable> must differ from
1056         <replaceable class="parameter">colV</replaceable>.
1057         If <replaceable class="parameter">colD</replaceable> is not
1058         specified, then there must be exactly three columns in the query
1059         result, and the column that is neither
1060         <replaceable class="parameter">colV</replaceable> nor
1061         <replaceable class="parameter">colH</replaceable>
1062         is taken to be <replaceable class="parameter">colD</replaceable>.
1063         </para>
1064
1065         <para>
1066         The vertical header, displayed as the leftmost column, contains the
1067         values found in column <replaceable class="parameter">colV</>, in the
1068         same order as in the query results, but with duplicates removed.
1069         </para>
1070
1071         <para>
1072         The horizontal header, displayed as the first row, contains the values
1073         found in column <replaceable class="parameter">colH</replaceable>,
1074         with duplicates removed.  By default, these appear in the same order
1075         as in the query results.  But if the
1076         optional <replaceable class="parameter">sortcolH</> argument is given,
1077         it identifies a column whose values must be integer numbers, and the
1078         values from <replaceable class="parameter">colH</replaceable> will
1079         appear in the horizontal header sorted according to the
1080         corresponding <replaceable class="parameter">sortcolH</> values.
1081         </para>
1082
1083         <para>
1084         Inside the crosstab grid, for each distinct value <literal>x</literal>
1085         of <replaceable class="parameter">colH</replaceable> and each distinct
1086         value <literal>y</literal>
1087         of <replaceable class="parameter">colV</replaceable>, the cell located
1088         at the intersection <literal>(x,y)</literal> contains the value of
1089         the <literal>colD</literal> column in the query result row for which
1090         the value of <replaceable class="parameter">colH</replaceable>
1091         is <literal>x</literal> and the value
1092         of <replaceable class="parameter">colV</replaceable>
1093         is <literal>y</>.  If there is no such row, the cell is empty.  If
1094         there are multiple such rows, an error is reported.
1095         </para>
1096         </listitem>
1097       </varlistentry>
1098
1099
1100       <varlistentry>
1101         <term><literal>\d[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1102
1103         <listitem>
1104         <para>
1105         For each relation (table, view, materialized view, index, sequence,
1106         or foreign table)
1107         or composite type matching the
1108         <replaceable class="parameter">pattern</replaceable>, show all
1109         columns, their types, the tablespace (if not the default) and any
1110         special attributes such as <literal>NOT NULL</literal> or defaults.
1111         Associated indexes, constraints, rules, and triggers are
1112         also shown.  For foreign tables, the associated foreign
1113         server is shown as well.
1114         (<quote>Matching the pattern</> is defined in
1115         <xref linkend="APP-PSQL-patterns" endterm="APP-PSQL-patterns-title">
1116         below.)
1117         </para>
1118
1119         <para>
1120         For some types of relation, <literal>\d</> shows additional information
1121         for each column: column values for sequences, indexed expressions for
1122         indexes, and foreign data wrapper options for foreign tables.
1123         </para>
1124
1125         <para>
1126         The command form <literal>\d+</literal> is identical, except that
1127         more information is displayed: any comments associated with the
1128         columns of the table are shown, as is the presence of OIDs in the
1129         table, the view definition if the relation is a view, a non-default
1130         <link linkend="SQL-CREATETABLE-REPLICA-IDENTITY">replica
1131         identity</link> setting.
1132         </para>
1133
1134         <para>
1135         By default, only user-created objects are shown;  supply a
1136         pattern or the <literal>S</literal> modifier to include system
1137         objects.
1138         </para>
1139
1140         <note>
1141         <para>
1142         If <command>\d</command> is used without a
1143         <replaceable class="parameter">pattern</replaceable> argument, it is
1144         equivalent to <command>\dtvmsE</command> which will show a list of
1145         all visible tables, views, materialized views, sequences and
1146         foreign tables.
1147         This is purely a convenience measure.
1148         </para>
1149         </note>
1150         </listitem>
1151       </varlistentry>
1152
1153       <varlistentry>
1154         <term><literal>\da[S] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1155
1156         <listitem>
1157         <para>
1158         Lists aggregate functions, together with their
1159         return type and the data types they operate on. If <replaceable
1160         class="parameter">pattern</replaceable>
1161         is specified, only aggregates whose names match the pattern are shown.
1162         By default, only user-created objects are shown;  supply a
1163         pattern or the <literal>S</literal> modifier to include system
1164         objects.
1165         </para>
1166         </listitem>
1167       </varlistentry>
1168
1169       <varlistentry>
1170         <term><literal>\dA[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1171
1172         <listitem>
1173         <para>
1174         Lists access methods. If <replaceable
1175         class="parameter">pattern</replaceable> is specified, only access
1176         methods whose names match the pattern are shown. If
1177         <literal>+</literal> is appended to the command name, each access
1178         method is listed with its associated handler function and description.
1179         </para>
1180         </listitem>
1181       </varlistentry>
1182
1183       <varlistentry>
1184         <term><literal>\db[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1185
1186         <listitem>
1187         <para>
1188         Lists tablespaces. If <replaceable
1189         class="parameter">pattern</replaceable>
1190         is specified, only tablespaces whose names match the pattern are shown.
1191         If <literal>+</literal> is appended to the command name, each tablespace
1192         is listed with its associated options, on-disk size, permissions and
1193         description.
1194         </para>
1195         </listitem>
1196       </varlistentry>
1197
1198
1199       <varlistentry>
1200         <term><literal>\dc[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1201         <listitem>
1202         <para>
1203         Lists conversions between character-set encodings.
1204         If <replaceable class="parameter">pattern</replaceable>
1205         is specified, only conversions whose names match the pattern are
1206         listed.
1207         By default, only user-created objects are shown;  supply a
1208         pattern or the <literal>S</literal> modifier to include system
1209         objects.
1210         If <literal>+</literal> is appended to the command name, each object
1211         is listed with its associated description.
1212         </para>
1213         </listitem>
1214       </varlistentry>
1215
1216
1217       <varlistentry>
1218         <term><literal>\dC[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1219         <listitem>
1220         <para>
1221         Lists type casts.
1222         If <replaceable class="parameter">pattern</replaceable>
1223         is specified, only casts whose source or target types match the
1224         pattern are listed.
1225         If <literal>+</literal> is appended to the command name, each object
1226         is listed with its associated description.
1227         </para>
1228         </listitem>
1229       </varlistentry>
1230
1231
1232       <varlistentry>
1233         <term><literal>\dd[S] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1234         <listitem>
1235         <para>
1236         Shows the descriptions of objects of type <literal>constraint</>,
1237         <literal>operator class</>, <literal>operator family</>,
1238         <literal>rule</>, and <literal>trigger</>. All
1239         other comments may be viewed by the respective backslash commands for
1240         those object types.
1241         </para>
1242
1243         <para><literal>\dd</literal> displays descriptions for objects matching the
1244         <replaceable class="parameter">pattern</replaceable>, or of visible
1245         objects of the appropriate type if no argument is given.  But in either
1246         case, only objects that have a description are listed.
1247         By default, only user-created objects are shown;  supply a
1248         pattern or the <literal>S</literal> modifier to include system
1249         objects.
1250         </para>
1251
1252         <para>
1253         Descriptions for objects can be created with the <xref
1254         linkend="sql-comment">
1255         <acronym>SQL</acronym> command.
1256        </para>
1257         </listitem>
1258       </varlistentry>
1259
1260
1261       <varlistentry>
1262         <term><literal>\dD[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1263         <listitem>
1264         <para>
1265         Lists domains. If <replaceable
1266         class="parameter">pattern</replaceable>
1267         is specified, only domains whose names match the pattern are shown.
1268         By default, only user-created objects are shown;  supply a
1269         pattern or the <literal>S</literal> modifier to include system
1270         objects.
1271         If <literal>+</literal> is appended to the command name, each object
1272         is listed with its associated permissions and description.
1273         </para>
1274         </listitem>
1275       </varlistentry>
1276
1277
1278       <varlistentry>
1279         <term><literal>\ddp [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1280         <listitem>
1281         <para>
1282         Lists default access privilege settings.  An entry is shown for
1283         each role (and schema, if applicable) for which the default
1284         privilege settings have been changed from the built-in defaults.
1285         If <replaceable class="parameter">pattern</replaceable> is
1286         specified, only entries whose role name or schema name matches
1287         the pattern are listed.
1288         </para>
1289
1290         <para>
1291         The <xref linkend="sql-alterdefaultprivileges"> command is used to set
1292         default access privileges.  The meaning of the
1293         privilege display is explained under
1294         <xref linkend="sql-grant">.
1295         </para>
1296         </listitem>
1297       </varlistentry>
1298
1299
1300       <varlistentry>
1301         <term><literal>\dE[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1302         <term><literal>\di[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1303         <term><literal>\dm[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1304         <term><literal>\ds[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1305         <term><literal>\dt[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1306         <term><literal>\dv[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1307
1308         <listitem>
1309         <para>
1310         In this group of commands, the letters <literal>E</literal>,
1311         <literal>i</literal>, <literal>m</literal>, <literal>s</literal>,
1312         <literal>t</literal>, and <literal>v</literal>
1313         stand for foreign table, index, materialized view, sequence, table, and view,
1314         respectively.
1315         You can specify any or all of
1316         these letters, in any order, to obtain a listing of objects
1317         of these types.  For example, <literal>\dit</> lists indexes
1318         and tables.  If <literal>+</literal> is
1319         appended to the command name, each object is listed with its
1320         physical size on disk and its associated description, if any.
1321         If <replaceable class="parameter">pattern</replaceable> is
1322         specified, only objects whose names match the pattern are listed.
1323         By default, only user-created objects are shown; supply a
1324         pattern or the <literal>S</literal> modifier to include system
1325         objects.
1326         </para>
1327         </listitem>
1328       </varlistentry>
1329
1330
1331       <varlistentry>
1332         <term><literal>\des[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1333         <listitem>
1334         <para>
1335         Lists foreign servers (mnemonic: <quote>external
1336         servers</quote>).
1337         If <replaceable class="parameter">pattern</replaceable> is
1338         specified, only those servers whose name matches the pattern
1339         are listed.  If the form <literal>\des+</literal> is used, a
1340         full description of each server is shown, including the
1341         server's ACL, type, version, options, and description.
1342         </para>
1343         </listitem>
1344       </varlistentry>
1345
1346
1347       <varlistentry>
1348         <term><literal>\det[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1349         <listitem>
1350         <para>
1351         Lists foreign tables (mnemonic: <quote>external tables</quote>).
1352         If <replaceable class="parameter">pattern</replaceable> is
1353         specified, only entries whose table name or schema name matches
1354         the pattern are listed.  If the form <literal>\det+</literal>
1355         is used, generic options and the foreign table description
1356         are also displayed.
1357         </para>
1358         </listitem>
1359       </varlistentry>
1360
1361
1362       <varlistentry>
1363         <term><literal>\deu[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1364         <listitem>
1365         <para>
1366         Lists user mappings (mnemonic: <quote>external
1367         users</quote>).
1368         If <replaceable class="parameter">pattern</replaceable> is
1369         specified, only those mappings whose user names match the
1370         pattern are listed.  If the form <literal>\deu+</literal> is
1371         used, additional information about each mapping is shown.
1372         </para>
1373
1374         <caution>
1375         <para>
1376         <literal>\deu+</literal> might also display the user name and
1377         password of the remote user, so care should be taken not to
1378         disclose them.
1379         </para>
1380         </caution>
1381         </listitem>
1382       </varlistentry>
1383
1384
1385       <varlistentry>
1386         <term><literal>\dew[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1387         <listitem>
1388         <para>
1389         Lists foreign-data wrappers (mnemonic: <quote>external
1390         wrappers</quote>).
1391         If <replaceable class="parameter">pattern</replaceable> is
1392         specified, only those foreign-data wrappers whose name matches
1393         the pattern are listed.  If the form <literal>\dew+</literal>
1394         is used, the ACL, options, and description of the foreign-data
1395         wrapper are also shown.
1396         </para>
1397         </listitem>
1398       </varlistentry>
1399
1400
1401       <varlistentry>
1402         <term><literal>\df[antwS+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1403
1404         <listitem>
1405         <para>
1406         Lists functions, together with their result data types, argument data
1407         types, and function types, which are classified as <quote>agg</>
1408         (aggregate), <quote>normal</>, <quote>trigger</>, or <quote>window</>.
1409         To display only functions
1410         of specific type(s), add the corresponding letters <literal>a</>,
1411         <literal>n</>, <literal>t</>, or <literal>w</> to the command.
1412         If <replaceable
1413         class="parameter">pattern</replaceable> is specified, only
1414         functions whose names match the pattern are shown.
1415         By default, only user-created
1416         objects are shown; supply a pattern or the <literal>S</literal>
1417         modifier to include system objects.
1418         If the form <literal>\df+</literal> is used, additional information
1419         about each function is shown, including volatility,
1420         parallel safety, owner, security classification, access privileges,
1421         language, source code and description.
1422         </para>
1423
1424         <tip>
1425         <para>
1426         To look up functions taking arguments or returning values of a specific
1427         data type, use your pager's search capability to scroll through the
1428         <literal>\df</> output.
1429         </para>
1430         </tip>
1431
1432         </listitem>
1433       </varlistentry>
1434
1435       <varlistentry>
1436         <term><literal>\dF[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1437         <listitem>
1438         <para>
1439          Lists text search configurations.
1440          If <replaceable class="parameter">pattern</replaceable> is specified,
1441          only configurations whose names match the pattern are shown.
1442          If the form <literal>\dF+</literal> is used, a full description of
1443          each configuration is shown, including the underlying text search
1444          parser and the dictionary list for each parser token type.
1445         </para>
1446         </listitem>
1447       </varlistentry>
1448
1449       <varlistentry>
1450         <term><literal>\dFd[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1451         <listitem>
1452         <para>
1453          Lists text search dictionaries.
1454          If <replaceable class="parameter">pattern</replaceable> is specified,
1455          only dictionaries whose names match the pattern are shown.
1456          If the form <literal>\dFd+</literal> is used, additional information
1457          is shown about each selected dictionary, including the underlying
1458          text search template and the option values.
1459         </para>
1460         </listitem>
1461       </varlistentry>
1462
1463       <varlistentry>
1464         <term><literal>\dFp[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1465         <listitem>
1466         <para>
1467          Lists text search parsers.
1468          If <replaceable class="parameter">pattern</replaceable> is specified,
1469          only parsers whose names match the pattern are shown.
1470          If the form <literal>\dFp+</literal> is used, a full description of
1471          each parser is shown, including the underlying functions and the
1472          list of recognized token types.
1473         </para>
1474         </listitem>
1475       </varlistentry>
1476
1477       <varlistentry>
1478         <term><literal>\dFt[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1479         <listitem>
1480         <para>
1481          Lists text search templates.
1482          If <replaceable class="parameter">pattern</replaceable> is specified,
1483          only templates whose names match the pattern are shown.
1484          If the form <literal>\dFt+</literal> is used, additional information
1485          is shown about each template, including the underlying function names.
1486         </para>
1487         </listitem>
1488       </varlistentry>
1489
1490
1491       <varlistentry>
1492         <term><literal>\dg[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1493         <listitem>
1494         <para>
1495         Lists database roles.
1496         (Since the concepts of <quote>users</> and <quote>groups</> have been
1497         unified into <quote>roles</>, this command is now equivalent to
1498         <literal>\du</literal>.)
1499         By default, only user-created roles are shown; supply the
1500         <literal>S</literal> modifier to include system roles.
1501         If <replaceable class="parameter">pattern</replaceable> is specified,
1502         only those roles whose names match the pattern are listed.
1503         If the form <literal>\dg+</literal> is used, additional information
1504         is shown about each role; currently this adds the comment for each
1505         role.
1506         </para>
1507         </listitem>
1508       </varlistentry>
1509
1510
1511       <varlistentry>
1512         <term><literal>\dl</literal></term>
1513         <listitem>
1514         <para>
1515         This is an alias for <command>\lo_list</command>, which shows a
1516         list of large objects.
1517         </para>
1518         </listitem>
1519       </varlistentry>
1520
1521       <varlistentry>
1522         <term><literal>\dL[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1523         <listitem>
1524         <para>
1525         Lists procedural languages. If <replaceable
1526         class="parameter">pattern</replaceable>
1527         is specified, only languages whose names match the pattern are listed.
1528         By default, only user-created languages
1529         are shown; supply the <literal>S</literal> modifier to include system
1530         objects. If <literal>+</literal> is appended to the command name, each
1531         language is listed with its call handler, validator, access privileges,
1532         and whether it is a system object.
1533         </para>
1534         </listitem>
1535       </varlistentry>
1536
1537
1538       <varlistentry>
1539         <term><literal>\dn[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1540
1541         <listitem>
1542         <para>
1543         Lists schemas (namespaces). If <replaceable
1544         class="parameter">pattern</replaceable>
1545         is specified, only schemas whose names match the pattern are listed.
1546         By default, only user-created objects are shown; supply a
1547         pattern or the <literal>S</literal> modifier to include system objects.
1548         If <literal>+</literal> is appended to the command name, each object
1549         is listed with its associated permissions and description, if any.
1550         </para>
1551         </listitem>
1552       </varlistentry>
1553
1554
1555       <varlistentry>
1556         <term><literal>\do[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1557         <listitem>
1558         <para>
1559         Lists operators with their operand and result types.
1560         If <replaceable class="parameter">pattern</replaceable> is
1561         specified, only operators whose names match the pattern are listed.
1562         By default, only user-created objects are shown; supply a
1563         pattern or the <literal>S</literal> modifier to include system
1564         objects.
1565         If <literal>+</literal> is appended to the command name,
1566         additional information about each operator is shown, currently just
1567         the name of the underlying function.
1568         </para>
1569         </listitem>
1570       </varlistentry>
1571
1572
1573       <varlistentry>
1574         <term><literal>\dO[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1575         <listitem>
1576         <para>
1577         Lists collations.
1578         If <replaceable class="parameter">pattern</replaceable> is
1579         specified, only collations whose names match the pattern are
1580         listed.  By default, only user-created objects are shown;
1581         supply a pattern or the <literal>S</literal> modifier to
1582         include system objects.  If <literal>+</literal> is appended
1583         to the command name, each collation is listed with its associated
1584         description, if any.
1585         Note that only collations usable with the current database's encoding
1586         are shown, so the results may vary in different databases of the
1587         same installation.
1588         </para>
1589         </listitem>
1590       </varlistentry>
1591
1592
1593       <varlistentry>
1594         <term><literal>\dp [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1595         <listitem>
1596         <para>
1597         Lists tables, views and sequences with their
1598         associated access privileges.
1599         If <replaceable class="parameter">pattern</replaceable> is
1600         specified, only tables, views and sequences whose names match the
1601         pattern are listed.
1602         </para>
1603
1604         <para>
1605         The <xref linkend="sql-grant"> and
1606         <xref linkend="sql-revoke">
1607         commands are used to set access privileges.  The meaning of the
1608         privilege display is explained under
1609         <xref linkend="sql-grant">.
1610         </para>
1611         </listitem>
1612       </varlistentry>
1613
1614       <varlistentry>
1615         <term><literal>\drds [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">role-pattern</replaceable></link> [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">database-pattern</replaceable></link> ] ]</literal></term>
1616         <listitem>
1617         <para>
1618         Lists defined configuration settings.  These settings can be
1619         role-specific, database-specific, or both.
1620         <replaceable>role-pattern</replaceable> and
1621         <replaceable>database-pattern</replaceable> are used to select
1622         specific roles and databases to list, respectively.  If omitted, or if
1623         <literal>*</> is specified, all settings are listed, including those
1624         not role-specific or database-specific, respectively.
1625         </para>
1626
1627         <para>
1628         The <xref linkend="sql-alterrole"> and
1629         <xref linkend="sql-alterdatabase">
1630         commands are used to define per-role and per-database configuration
1631         settings.
1632         </para>
1633         </listitem>
1634       </varlistentry>
1635
1636       <varlistentry>
1637         <term><literal>\dRp[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1638         <listitem>
1639         <para>
1640         Lists replication publications.
1641         If <replaceable class="parameter">pattern</replaceable> is
1642         specified, only those publications whose names match the pattern are
1643         listed.
1644         If <literal>+</literal> is appended to the command name, the tables
1645         associated with each publication are shown as well.
1646         </para>
1647         </listitem>
1648       </varlistentry>
1649
1650       <varlistentry>
1651         <term><literal>\dRs[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1652         <listitem>
1653         <para>
1654         Lists replication subscriptions.
1655         If <replaceable class="parameter">pattern</replaceable> is
1656         specified, only those subscriptions whose names match the pattern are
1657         listed.
1658         If <literal>+</literal> is appended to the command name, additional
1659         properties of the subscriptions are shown.
1660         </para>
1661         </listitem>
1662       </varlistentry>
1663
1664       <varlistentry>
1665         <term><literal>\dT[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1666         <listitem>
1667         <para>
1668         Lists data types.
1669         If <replaceable class="parameter">pattern</replaceable> is
1670         specified, only types whose names match the pattern are listed.
1671         If <literal>+</literal> is appended to the command name, each type is
1672         listed with its internal name and size, its allowed values
1673         if it is an <type>enum</> type, and its associated permissions.
1674         By default, only user-created objects are shown;  supply a
1675         pattern or the <literal>S</literal> modifier to include system
1676         objects.
1677         </para>
1678         </listitem>
1679       </varlistentry>
1680
1681       <varlistentry>
1682         <term><literal>\du[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1683         <listitem>
1684         <para>
1685         Lists database roles.
1686         (Since the concepts of <quote>users</> and <quote>groups</> have been
1687         unified into <quote>roles</>, this command is now equivalent to
1688         <literal>\dg</literal>.)
1689         By default, only user-created roles are shown; supply the
1690         <literal>S</literal> modifier to include system roles.
1691         If <replaceable class="parameter">pattern</replaceable> is specified,
1692         only those roles whose names match the pattern are listed.
1693         If the form <literal>\du+</literal> is used, additional information
1694         is shown about each role; currently this adds the comment for each
1695         role.
1696         </para>
1697         </listitem>
1698       </varlistentry>
1699
1700       <varlistentry>
1701         <term><literal>\dx[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1702         <listitem>
1703         <para>
1704         Lists installed extensions.
1705         If <replaceable class="parameter">pattern</replaceable>
1706         is specified, only those extensions whose names match the pattern
1707         are listed.
1708         If the form <literal>\dx+</literal> is used, all the objects belonging
1709         to each matching extension are listed.
1710         </para>
1711         </listitem>
1712       </varlistentry>
1713
1714       <varlistentry>
1715         <term><literal>\dy[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1716         <listitem>
1717         <para>
1718         Lists event triggers.
1719         If <replaceable class="parameter">pattern</replaceable>
1720         is specified, only those event triggers whose names match the pattern
1721         are listed.
1722         If <literal>+</literal> is appended to the command name, each object
1723         is listed with its associated description.
1724         </para>
1725         </listitem>
1726       </varlistentry>
1727
1728       <varlistentry>
1729         <term><literal>\e</literal> or <literal>\edit</> <literal> <optional> <replaceable class="parameter">filename</> </optional> <optional> <replaceable class="parameter">line_number</> </optional> </literal></term>
1730
1731         <listitem>
1732         <para>
1733         If <replaceable class="parameter">filename</replaceable> is
1734         specified, the file is edited; after the editor exits, the file's
1735         content is copied into the current query buffer. If no <replaceable
1736         class="parameter">filename</replaceable> is given, the current query
1737         buffer is copied to a temporary file which is then edited in the same
1738         fashion.  Or, if the current query buffer is empty, the most recently
1739         executed query is copied to a temporary file and edited in the same
1740         fashion.
1741         </para>
1742
1743         <para>
1744         The new contents of the query buffer are then re-parsed according to
1745         the normal rules of <application>psql</application>, treating the
1746         whole buffer as a single line.  Any complete queries are immediately
1747         executed; that is, if the query buffer contains or ends with a
1748         semicolon, everything up to that point is executed.  Whatever remains
1749         will wait in the query buffer; type semicolon or <literal>\g</> to
1750         send it, or <literal>\r</> to cancel it by clearing the query buffer.
1751         Treating the buffer as a single line primarily affects meta-commands:
1752         whatever is in the buffer after a meta-command will be taken as
1753         argument(s) to the meta-command, even if it spans multiple lines.
1754         (Thus you cannot make meta-command-using scripts this way.
1755         Use <command>\i</command> for that.)
1756         </para>
1757
1758         <para>
1759         If a line number is specified, <application>psql</application> will
1760         position the cursor on the specified line of the file or query buffer.
1761         Note that if a single all-digits argument is given,
1762         <application>psql</application> assumes it is a line number,
1763         not a file name.
1764         </para>
1765
1766         <tip>
1767         <para>
1768         See under <xref linkend="app-psql-environment"
1769         endterm="app-psql-environment-title"> for how to configure and
1770         customize your editor.
1771         </para>
1772         </tip>
1773         </listitem>
1774       </varlistentry>
1775
1776       <varlistentry>
1777         <term><literal>\echo <replaceable class="parameter">text</replaceable> [ ... ]</literal></term>
1778         <listitem>
1779         <para>
1780         Prints the arguments to the standard output, separated by one
1781         space and followed by a newline. This can be useful to
1782         intersperse information in the output of scripts. For example:
1783 <programlisting>
1784 =&gt; <userinput>\echo `date`</userinput>
1785 Tue Oct 26 21:40:57 CEST 1999
1786 </programlisting>
1787         If the first argument is an unquoted <literal>-n</literal> the trailing
1788         newline is not written.
1789         </para>
1790
1791         <tip>
1792         <para>
1793         If you use the <command>\o</command> command to redirect your
1794         query output you might wish to use <command>\qecho</command>
1795         instead of this command.
1796         </para>
1797         </tip>
1798         </listitem>
1799       </varlistentry>
1800
1801       <varlistentry>
1802         <term><literal>\ef <optional> <replaceable class="parameter">function_description</> <optional>  <replaceable class="parameter">line_number</> </optional> </optional> </literal></term>
1803
1804         <listitem>
1805         <para>
1806          This command fetches and edits the definition of the named function,
1807          in the form of a <command>CREATE OR REPLACE FUNCTION</> command.
1808          Editing is done in the same way as for <literal>\edit</>.
1809          After the editor exits, the updated command waits in the query buffer;
1810          type semicolon or <literal>\g</> to send it, or <literal>\r</>
1811          to cancel.
1812         </para>
1813
1814         <para>
1815          The target function can be specified by name alone, or by name
1816          and arguments, for example <literal>foo(integer, text)</>.
1817          The argument types must be given if there is more
1818          than one function of the same name.
1819         </para>
1820
1821         <para>
1822          If no function is specified, a blank <command>CREATE FUNCTION</>
1823          template is presented for editing.
1824         </para>
1825
1826         <para>
1827         If a line number is specified, <application>psql</application> will
1828         position the cursor on the specified line of the function body.
1829         (Note that the function body typically does not begin on the first
1830         line of the file.)
1831         </para>
1832
1833         <para>
1834         Unlike most other meta-commands, the entire remainder of the line is
1835         always taken to be the argument(s) of <command>\ef</>, and neither
1836         variable interpolation nor backquote expansion are performed in the
1837         arguments.
1838         </para>
1839
1840         <tip>
1841         <para>
1842         See under <xref linkend="app-psql-environment"
1843         endterm="app-psql-environment-title"> for how to configure and
1844         customize your editor.
1845         </para>
1846         </tip>
1847         </listitem>
1848       </varlistentry>
1849
1850
1851       <varlistentry>
1852         <term><literal>\encoding [ <replaceable class="parameter">encoding</replaceable> ]</literal></term>
1853
1854         <listitem>
1855         <para>
1856         Sets the client character set encoding.  Without an argument, this command
1857         shows the current encoding.
1858         </para>
1859         </listitem>
1860       </varlistentry>
1861
1862
1863       <varlistentry>
1864         <term><literal>\errverbose</literal></term>
1865
1866         <listitem>
1867         <para>
1868         Repeats the most recent server error message at maximum
1869         verbosity, as though <varname>VERBOSITY</varname> were set
1870         to <literal>verbose</> and <varname>SHOW_CONTEXT</varname> were
1871         set to <literal>always</>.
1872         </para>
1873         </listitem>
1874       </varlistentry>
1875
1876
1877       <varlistentry>
1878         <term><literal>\ev <optional> <replaceable class="parameter">view_name</> <optional>  <replaceable class="parameter">line_number</> </optional> </optional> </literal></term>
1879
1880         <listitem>
1881         <para>
1882          This command fetches and edits the definition of the named view,
1883          in the form of a <command>CREATE OR REPLACE VIEW</> command.
1884          Editing is done in the same way as for <literal>\edit</>.
1885          After the editor exits, the updated command waits in the query buffer;
1886          type semicolon or <literal>\g</> to send it, or <literal>\r</>
1887          to cancel.
1888         </para>
1889
1890         <para>
1891          If no view is specified, a blank <command>CREATE VIEW</>
1892          template is presented for editing.
1893         </para>
1894
1895         <para>
1896          If a line number is specified, <application>psql</application> will
1897          position the cursor on the specified line of the view definition.
1898         </para>
1899
1900         <para>
1901         Unlike most other meta-commands, the entire remainder of the line is
1902         always taken to be the argument(s) of <command>\ev</>, and neither
1903         variable interpolation nor backquote expansion are performed in the
1904         arguments.
1905         </para>
1906         </listitem>
1907       </varlistentry>
1908
1909
1910       <varlistentry>
1911         <term><literal>\f [ <replaceable class="parameter">string</replaceable> ]</literal></term>
1912
1913         <listitem>
1914         <para>
1915         Sets the field separator for unaligned query output. The default
1916         is the vertical bar (<literal>|</literal>). It is equivalent to
1917         <command>\pset fieldsep</command>.
1918         </para>
1919         </listitem>
1920       </varlistentry>
1921
1922
1923       <varlistentry>
1924         <term><literal>\g [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
1925         <term><literal>\g [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
1926         <listitem>
1927         <para>
1928         Sends the current query buffer to the server for execution.
1929         If an argument is given, the query's output is written to the named
1930         file or piped to the given shell command, instead of displaying it as
1931         usual.  The file or command is written to only if the query
1932         successfully returns zero or more tuples, not if the query fails or
1933         is a non-data-returning SQL command.
1934         </para>
1935         <para>
1936         If the current query buffer is empty, the most recently sent query is
1937         re-executed instead.  Except for that behavior, <literal>\g</literal>
1938         without an argument is essentially equivalent to a semicolon.
1939         A <literal>\g</literal> with argument is a <quote>one-shot</quote>
1940         alternative to the <command>\o</command> command.
1941         </para>
1942         <para>
1943         If the argument begins with <literal>|</>, then the entire remainder
1944         of the line is taken to be
1945         the <replaceable class="parameter">command</replaceable> to execute,
1946         and neither variable interpolation nor backquote expansion are
1947         performed in it.  The rest of the line is simply passed literally to
1948         the shell.
1949         </para>
1950         </listitem>
1951       </varlistentry>
1952
1953
1954       <varlistentry>
1955         <term><literal>\gdesc</literal></term>
1956
1957         <listitem>
1958         <para>
1959          Shows the description (that is, the column names and data types)
1960          of the result of the current query buffer.  The query is not
1961          actually executed; however, if it contains some type of syntax
1962          error, that error will be reported in the normal way.
1963         </para>
1964
1965         <para>
1966          If the current query buffer is empty, the most recently sent query
1967          is described instead.
1968         </para>
1969         </listitem>
1970       </varlistentry>
1971
1972
1973       <varlistentry>
1974         <term><literal>\gexec</literal></term>
1975
1976         <listitem>
1977         <para>
1978          Sends the current query buffer to the server, then treats
1979          each column of each row of the query's output (if any) as a SQL
1980          statement to be executed.  For example, to create an index on each
1981          column of <structname>my_table</>:
1982 <programlisting>
1983 =&gt; <userinput>SELECT format('create index on my_table(%I)', attname)</>
1984 -&gt; <userinput>FROM pg_attribute</>
1985 -&gt; <userinput>WHERE attrelid = 'my_table'::regclass AND attnum &gt; 0</>
1986 -&gt; <userinput>ORDER BY attnum</>
1987 -&gt; <userinput>\gexec</>
1988 CREATE INDEX
1989 CREATE INDEX
1990 CREATE INDEX
1991 CREATE INDEX
1992 </programlisting>
1993         </para>
1994
1995         <para>
1996          The generated queries are executed in the order in which the rows
1997          are returned, and left-to-right within each row if there is more
1998          than one column.  NULL fields are ignored.  The generated queries
1999          are sent literally to the server for processing, so they cannot be
2000          <application>psql</> meta-commands nor contain <application>psql</>
2001          variable references.  If any individual query fails, execution of
2002          the remaining queries continues
2003          unless <varname>ON_ERROR_STOP</varname> is set.  Execution of each
2004          query is subject to <varname>ECHO</varname> processing.
2005          (Setting <varname>ECHO</varname> to <literal>all</literal>
2006          or <literal>queries</literal> is often advisable when
2007          using <command>\gexec</>.)  Query logging, single-step mode,
2008          timing, and other query execution features apply to each generated
2009          query as well.
2010         </para>
2011         <para>
2012          If the current query buffer is empty, the most recently sent query
2013          is re-executed instead.
2014         </para>
2015         </listitem>
2016       </varlistentry>
2017
2018
2019       <varlistentry>
2020         <term><literal>\gset [ <replaceable class="parameter">prefix</replaceable> ]</literal></term>
2021
2022         <listitem>
2023         <para>
2024          Sends the current query buffer to the server and stores the
2025          query's output into <application>psql</> variables (see <xref
2026          linkend="APP-PSQL-variables" endterm="APP-PSQL-variables-title">).
2027          The query to be executed must return exactly one row.  Each column of
2028          the row is stored into a separate variable, named the same as the
2029          column.  For example:
2030 <programlisting>
2031 =&gt; <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput>
2032 -&gt; <userinput>\gset</userinput>
2033 =&gt; <userinput>\echo :var1 :var2</userinput>
2034 hello 10
2035 </programlisting>
2036         </para>
2037         <para>
2038          If you specify a <replaceable class="parameter">prefix</replaceable>,
2039          that string is prepended to the query's column names to create the
2040          variable names to use:
2041 <programlisting>
2042 =&gt; <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput>
2043 -&gt; <userinput>\gset result_</userinput>
2044 =&gt; <userinput>\echo :result_var1 :result_var2</userinput>
2045 hello 10
2046 </programlisting>
2047         </para>
2048         <para>
2049          If a column result is NULL, the corresponding variable is unset
2050          rather than being set.
2051         </para>
2052         <para>
2053          If the query fails or does not return one row,
2054          no variables are changed.
2055         </para>
2056         <para>
2057          If the current query buffer is empty, the most recently sent query
2058          is re-executed instead.
2059         </para>
2060         </listitem>
2061       </varlistentry>
2062
2063
2064       <varlistentry>
2065         <term><literal>\gx [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2066         <term><literal>\gx [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
2067         <listitem>
2068         <para>
2069         <literal>\gx</literal> is equivalent to <literal>\g</literal>, but
2070         forces expanded output mode for this query.  See <literal>\x</literal>.
2071         </para>
2072         </listitem>
2073       </varlistentry>
2074
2075
2076       <varlistentry>
2077         <term><literal>\h</literal> or <literal>\help</literal> <literal>[ <replaceable class="parameter">command</replaceable> ]</literal></term>
2078         <listitem>
2079         <para>
2080         Gives syntax help on the specified <acronym>SQL</acronym>
2081         command. If <replaceable class="parameter">command</replaceable>
2082         is not specified, then <application>psql</application> will list
2083         all the commands for which syntax help is available. If
2084         <replaceable class="parameter">command</replaceable> is an
2085         asterisk (<literal>*</literal>), then syntax help on all
2086         <acronym>SQL</acronym> commands is shown.
2087         </para>
2088
2089         <para>
2090         Unlike most other meta-commands, the entire remainder of the line is
2091         always taken to be the argument(s) of <command>\help</>, and neither
2092         variable interpolation nor backquote expansion are performed in the
2093         arguments.
2094         </para>
2095
2096         <note>
2097         <para>
2098         To simplify typing, commands that consists of several words do
2099         not have to be quoted. Thus it is fine to type <userinput>\help
2100         alter table</userinput>.
2101         </para>
2102         </note>
2103         </listitem>
2104       </varlistentry>
2105
2106
2107       <varlistentry>
2108         <term><literal>\H</literal> or <literal>\html</literal></term>
2109         <listitem>
2110         <para>
2111         Turns on <acronym>HTML</acronym> query output format. If the
2112         <acronym>HTML</acronym> format is already on, it is switched
2113         back to the default aligned text format. This command is for
2114         compatibility and convenience, but see <command>\pset</command>
2115         about setting other output options.
2116         </para>
2117         </listitem>
2118       </varlistentry>
2119
2120
2121       <varlistentry>
2122         <term><literal>\i</literal> or <literal>\include</literal> <replaceable class="parameter">filename</replaceable></term>
2123         <listitem>
2124         <para>
2125         Reads input from the file <replaceable
2126         class="parameter">filename</replaceable> and executes it as
2127         though it had been typed on the keyboard.
2128         </para>
2129         <para>
2130         If <replaceable>filename</replaceable> is <literal>-</literal>
2131         (hyphen), then standard input is read until an EOF indication
2132         or <command>\q</> meta-command.  This can be used to intersperse
2133         interactive input with input from files.  Note that Readline behavior
2134         will be used only if it is active at the outermost level.
2135         </para>
2136         <note>
2137         <para>
2138         If you want to see the lines on the screen as they are read you
2139         must set the variable <varname>ECHO</varname> to
2140         <literal>all</literal>.
2141         </para>
2142         </note>
2143         </listitem>
2144       </varlistentry>
2145
2146
2147       <varlistentry>
2148         <term><literal>\if</literal> <replaceable class="parameter">expression</replaceable></term>
2149         <term><literal>\elif</literal> <replaceable class="parameter">expression</replaceable></term>
2150         <term><literal>\else</literal></term>
2151         <term><literal>\endif</literal></term>
2152         <listitem>
2153         <para>
2154         This group of commands implements nestable conditional blocks.
2155         A conditional block must begin with an <command>\if</command> and end
2156         with an <command>\endif</command>.  In between there may be any number
2157         of <command>\elif</command> clauses, which may optionally be followed
2158         by a single <command>\else</command> clause.  Ordinary queries and
2159         other types of backslash commands may (and usually do) appear between
2160         the commands forming a conditional block.
2161         </para>
2162         <para>
2163         The <command>\if</command> and <command>\elif</command> commands read
2164         their argument(s) and evaluate them as a boolean expression.  If the
2165         expression yields <literal>true</literal> then processing continues
2166         normally; otherwise, lines are skipped until a
2167         matching <command>\elif</command>, <command>\else</command>,
2168         or <command>\endif</command> is reached.  Once
2169         an <command>\if</command> or <command>\elif</command> test has
2170         succeeded, the arguments of later <command>\elif</command> commands in
2171         the same block are not evaluated but are treated as false.  Lines
2172         following an <command>\else</command> are processed only if no earlier
2173         matching <command>\if</command> or <command>\elif</command> succeeded.
2174         </para>
2175         <para>
2176         The <replaceable class="parameter">expression</replaceable> argument
2177         of an <command>\if</command> or <command>\elif</command> command
2178         is subject to variable interpolation and backquote expansion, just
2179         like any other backslash command argument.  After that it is evaluated
2180         like the value of an on/off option variable.  So a valid value
2181         is any unambiguous case-insensitive match for one of:
2182         <literal>true</literal>, <literal>false</literal>, <literal>1</literal>,
2183         <literal>0</literal>, <literal>on</literal>, <literal>off</literal>,
2184         <literal>yes</literal>, <literal>no</literal>.  For example,
2185         <literal>t</literal>, <literal>T</literal>, and <literal>tR</literal>
2186         will all be considered to be <literal>true</literal>.
2187         </para>
2188         <para>
2189         Expressions that do not properly evaluate to true or false will
2190         generate a warning and be treated as false.
2191         </para>
2192         <para>
2193         Lines being skipped are parsed normally to identify queries and
2194         backslash commands, but queries are not sent to the server, and
2195         backslash commands other than conditionals
2196         (<command>\if</command>, <command>\elif</command>,
2197         <command>\else</command>, <command>\endif</command>) are
2198         ignored.  Conditional commands are checked only for valid nesting.
2199         Variable references in skipped lines are not expanded, and backquote
2200         expansion is not performed either.
2201         </para>
2202         <para>
2203         All the backslash commands of a given conditional block must appear in
2204         the same source file. If EOF is reached on the main input file or an
2205         <command>\include</command>-ed file before all local
2206         <command>\if</command>-blocks have been closed,
2207         then <application>psql</> will raise an error.
2208         </para>
2209         <para>
2210          Here is an example:
2211         </para>
2212 <programlisting>
2213 -- check for the existence of two separate records in the database and store
2214 -- the results in separate psql variables
2215 SELECT
2216     EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer,
2217     EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee
2218 \gset
2219 \if :is_customer
2220     SELECT * FROM customer WHERE customer_id = 123;
2221 \elif :is_employee
2222     \echo 'is not a customer but is an employee'
2223     SELECT * FROM employee WHERE employee_id = 456;
2224 \else
2225     \if yes
2226         \echo 'not a customer or employee'
2227     \else
2228         \echo 'this will never print'
2229     \endif
2230 \endif
2231 </programlisting>
2232         </listitem>
2233       </varlistentry>
2234
2235
2236       <varlistentry>
2237         <term><literal>\ir</literal> or <literal>\include_relative</literal> <replaceable class="parameter">filename</replaceable></term>
2238         <listitem>
2239         <para>
2240         The <literal>\ir</> command is similar to <literal>\i</>, but resolves
2241         relative file names differently.  When executing in interactive mode,
2242         the two commands behave identically.  However, when invoked from a
2243         script, <literal>\ir</literal> interprets file names relative to the
2244         directory in which the script is located, rather than the current
2245         working directory.
2246         </para>
2247         </listitem>
2248       </varlistentry>
2249
2250
2251       <varlistentry>
2252         <term><literal>\l[+]</literal> or <literal>\list[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2253         <listitem>
2254         <para>
2255         List the databases in the server and show their names, owners,
2256         character set encodings, and access privileges.
2257         If <replaceable class="parameter">pattern</replaceable> is specified,
2258         only databases whose names match the pattern are listed.
2259         If <literal>+</literal> is appended to the command name, database
2260         sizes, default tablespaces, and descriptions are also displayed.
2261         (Size information is only available for databases that the current
2262         user can connect to.)
2263         </para>
2264         </listitem>
2265       </varlistentry>
2266
2267
2268       <varlistentry>
2269         <term><literal>\lo_export <replaceable class="parameter">loid</replaceable> <replaceable class="parameter">filename</replaceable></literal></term>
2270
2271         <listitem>
2272         <para>
2273         Reads the large object with <acronym>OID</acronym> <replaceable
2274         class="parameter">loid</replaceable> from the database and
2275         writes it to <replaceable
2276         class="parameter">filename</replaceable>. Note that this is
2277         subtly different from the server function
2278         <function>lo_export</function>, which acts with the permissions
2279         of the user that the database server runs as and on the server's
2280         file system.
2281         </para>
2282         <tip>
2283         <para>
2284         Use <command>\lo_list</command> to find out the large object's
2285         <acronym>OID</acronym>.
2286         </para>
2287         </tip>
2288         </listitem>
2289       </varlistentry>
2290
2291
2292       <varlistentry>
2293         <term><literal>\lo_import <replaceable class="parameter">filename</replaceable> [ <replaceable class="parameter">comment</replaceable> ]</literal></term>
2294
2295         <listitem>
2296         <para>
2297         Stores the file into a <productname>PostgreSQL</productname>
2298         large object. Optionally, it associates the given
2299         comment with the object. Example:
2300 <programlisting>
2301 foo=&gt; <userinput>\lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'</userinput>
2302 lo_import 152801
2303 </programlisting>
2304         The response indicates that the large object received object
2305         ID 152801, which can be used to access the newly-created large
2306         object in the future. For the sake of readability, it is
2307         recommended to always associate a human-readable comment with
2308         every object. Both OIDs and comments can be viewed with the
2309         <command>\lo_list</command> command.
2310         </para>
2311
2312         <para>
2313         Note that this command is subtly different from the server-side
2314         <function>lo_import</function> because it acts as the local user
2315         on the local file system, rather than the server's user and file
2316         system.
2317         </para>
2318         </listitem>
2319       </varlistentry>
2320
2321       <varlistentry>
2322         <term><literal>\lo_list</literal></term>
2323         <listitem>
2324         <para>
2325         Shows a list of all <productname>PostgreSQL</productname>
2326         large objects currently stored in the database,
2327         along with any comments provided for them.
2328         </para>
2329         </listitem>
2330       </varlistentry>
2331
2332       <varlistentry>
2333         <term><literal>\lo_unlink <replaceable class="parameter">loid</replaceable></literal></term>
2334
2335         <listitem>
2336         <para>
2337         Deletes the large object with <acronym>OID</acronym>
2338         <replaceable class="parameter">loid</replaceable> from the
2339         database.
2340         </para>
2341
2342         <tip>
2343         <para>
2344         Use <command>\lo_list</command> to find out the large object's
2345         <acronym>OID</acronym>.
2346         </para>
2347         </tip>
2348         </listitem>
2349       </varlistentry>
2350
2351
2352       <varlistentry>
2353         <term><literal>\o</literal> or <literal>\out [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2354         <term><literal>\o</literal> or <literal>\out [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
2355         <listitem>
2356         <para>
2357         Arranges to save future query results to the file <replaceable
2358         class="parameter">filename</replaceable> or pipe future results
2359         to the shell command <replaceable
2360         class="parameter">command</replaceable>. If no argument is
2361         specified, the query output is reset to the standard output.
2362         </para>
2363
2364         <para>
2365         If the argument begins with <literal>|</>, then the entire remainder
2366         of the line is taken to be
2367         the <replaceable class="parameter">command</replaceable> to execute,
2368         and neither variable interpolation nor backquote expansion are
2369         performed in it.  The rest of the line is simply passed literally to
2370         the shell.
2371         </para>
2372
2373         <para>
2374         <quote>Query results</quote> includes all tables, command
2375         responses, and notices obtained from the database server, as
2376         well as output of various backslash commands that query the
2377         database (such as <command>\d</command>); but not error
2378         messages.
2379         </para>
2380
2381         <tip>
2382         <para>
2383         To intersperse text output in between query results, use
2384         <command>\qecho</command>.
2385         </para>
2386         </tip>
2387         </listitem>
2388       </varlistentry>
2389
2390
2391       <varlistentry>
2392         <term><literal>\p</literal> or <literal>\print</literal></term>
2393         <listitem>
2394         <para>
2395         Print the current query buffer to the standard output.
2396         If the current query buffer is empty, the most recently executed query
2397         is printed instead.
2398         </para>
2399         </listitem>
2400       </varlistentry>
2401
2402       <varlistentry>
2403         <term><literal>\password [ <replaceable class="parameter">username</replaceable> ]</literal></term>
2404         <listitem>
2405         <para>
2406         Changes the password of the specified user (by default, the current
2407         user).  This command prompts for the new password, encrypts it, and
2408         sends it to the server as an <command>ALTER ROLE</> command.  This
2409         makes sure that the new password does not appear in cleartext in the
2410         command history, the server log, or elsewhere.
2411         </para>
2412         </listitem>
2413       </varlistentry>
2414
2415       <varlistentry>
2416         <term><literal>\prompt [ <replaceable class="parameter">text</replaceable> ] <replaceable class="parameter">name</replaceable></literal></term>
2417         <listitem>
2418         <para>
2419          Prompts the user to supply text, which is assigned to the variable
2420          <replaceable class="parameter">name</>.
2421          An optional prompt string, <replaceable
2422          class="parameter">text</>, can be specified.  (For multiword
2423          prompts, surround the text with single quotes.)
2424         </para>
2425
2426         <para>
2427          By default, <literal>\prompt</> uses the terminal for input and
2428          output.  However, if the <option>-f</> command line switch was
2429          used, <literal>\prompt</> uses standard input and standard output.
2430         </para>
2431         </listitem>
2432       </varlistentry>
2433
2434       <varlistentry>
2435         <term><literal>\pset [ <replaceable class="parameter">option</replaceable> [ <replaceable class="parameter">value</replaceable> ] ]</literal></term>
2436
2437         <listitem>
2438         <para>
2439         This command sets options affecting the output of query result tables.
2440         <replaceable class="parameter">option</replaceable>
2441         indicates which option is to be set. The semantics of
2442         <replaceable class="parameter">value</replaceable> vary depending
2443         on the selected option.  For some options, omitting <replaceable
2444         class="parameter">value</replaceable> causes the option to be toggled
2445         or unset, as described under the particular option.  If no such
2446         behavior is mentioned, then omitting
2447         <replaceable class="parameter">value</replaceable> just results in
2448         the current setting being displayed.
2449         </para>
2450
2451         <para>
2452         <command>\pset</command> without any arguments displays the current status
2453         of all printing options.
2454         </para>
2455
2456         <para>
2457         Adjustable printing options are:
2458         <variablelist>
2459           <varlistentry>
2460           <term><literal>border</literal></term>
2461           <listitem>
2462           <para>
2463           The <replaceable class="parameter">value</replaceable> must be a
2464           number. In general, the higher
2465           the number the more borders and lines the tables will have,
2466           but details depend on the particular format.
2467           In <acronym>HTML</acronym> format, this will translate directly
2468           into the <literal>border=...</literal> attribute.
2469           In most other formats only values 0 (no border), 1 (internal
2470           dividing lines), and 2 (table frame) make sense, and values above 2
2471           will be treated the same as <literal>border = 2</literal>.
2472           The <literal>latex</literal> and <literal>latex-longtable</literal>
2473           formats additionally allow a value of 3 to add dividing lines
2474           between data rows.
2475           </para>
2476           </listitem>
2477           </varlistentry>
2478
2479           <varlistentry>
2480           <term><literal>columns</literal></term>
2481           <listitem>
2482           <para>
2483           Sets the target width for the <literal>wrapped</> format, and also
2484           the width limit for determining whether output is wide enough to
2485           require the pager or switch to the vertical display in expanded auto
2486           mode.
2487           Zero (the default) causes the target width to be controlled by the
2488           environment variable <envar>COLUMNS</>, or the detected screen width
2489           if <envar>COLUMNS</> is not set.
2490           In addition, if <literal>columns</> is zero then the
2491           <literal>wrapped</> format only affects screen output.
2492           If <literal>columns</> is nonzero then file and pipe output is
2493           wrapped to that width as well.
2494           </para>
2495           </listitem>
2496           </varlistentry>
2497
2498           <varlistentry>
2499           <term><literal>expanded</literal> (or <literal>x</literal>)</term>
2500           <listitem>
2501           <para>
2502           If <replaceable class="parameter">value</replaceable> is specified it
2503           must be either <literal>on</literal> or <literal>off</literal>, which
2504           will enable or disable expanded mode, or <literal>auto</literal>.
2505           If <replaceable class="parameter">value</replaceable> is omitted the
2506           command toggles between the on and off settings.  When expanded mode
2507           is enabled, query results are displayed in two columns, with the
2508           column name on the left and the data on the right. This mode is
2509           useful if the data wouldn't fit on the screen in the
2510           normal <quote>horizontal</quote> mode.  In the auto setting, the
2511           expanded mode is used whenever the query output has more than one
2512           column and is wider than the screen; otherwise, the regular mode is
2513           used.  The auto setting is only
2514           effective in the aligned and wrapped formats.  In other formats, it
2515           always behaves as if the expanded mode is off.
2516           </para>
2517           </listitem>
2518           </varlistentry>
2519
2520           <varlistentry>
2521           <term><literal>fieldsep</literal></term>
2522           <listitem>
2523           <para>
2524           Specifies the field separator to be used in unaligned output
2525           format. That way one can create, for example, tab- or
2526           comma-separated output, which other programs might prefer. To
2527           set a tab as field separator, type <literal>\pset fieldsep
2528           '\t'</literal>. The default field separator is
2529           <literal>'|'</literal> (a vertical bar).
2530           </para>
2531           </listitem>
2532           </varlistentry>
2533
2534           <varlistentry>
2535           <term><literal>fieldsep_zero</literal></term>
2536           <listitem>
2537           <para>
2538           Sets the field separator to use in unaligned output format to a zero
2539           byte.
2540           </para>
2541           </listitem>
2542           </varlistentry>
2543
2544           <varlistentry>
2545           <term><literal>footer</literal></term>
2546           <listitem>
2547           <para>
2548           If <replaceable class="parameter">value</replaceable> is specified
2549           it must be either <literal>on</literal> or <literal>off</literal>
2550           which will enable or disable display of the table footer
2551           (the <literal>(<replaceable>n</> rows)</literal> count).
2552           If <replaceable class="parameter">value</replaceable> is omitted the
2553           command toggles footer display on or off.
2554           </para>
2555           </listitem>
2556           </varlistentry>
2557
2558           <varlistentry>
2559           <term><literal>format</literal></term>
2560           <listitem>
2561           <para>
2562           Sets the output format to one of <literal>unaligned</literal>,
2563           <literal>aligned</literal>, <literal>wrapped</literal>,
2564           <literal>html</literal>, <literal>asciidoc</literal>,
2565           <literal>latex</literal> (uses <literal>tabular</literal>),
2566           <literal>latex-longtable</literal>, or
2567           <literal>troff-ms</literal>.
2568           Unique abbreviations are allowed.  (That would mean one letter
2569           is enough.)
2570           </para>
2571
2572           <para><literal>unaligned</> format writes all columns of a row on one
2573           line, separated by the currently active field separator. This
2574           is useful for creating output that might be intended to be read
2575           in by other programs (for example, tab-separated or comma-separated
2576           format).
2577           </para>
2578
2579           <para><literal>aligned</literal> format is the standard, human-readable,
2580           nicely formatted text output;  this is the default.
2581           </para>
2582
2583           <para><literal>wrapped</> format is like <literal>aligned</> but wraps
2584           wide data values across lines to make the output fit in the target
2585           column width.  The target width is determined as described under
2586           the <literal>columns</> option.  Note that <application>psql</> will
2587           not attempt to wrap column header titles; therefore,
2588           <literal>wrapped</> format behaves the same as <literal>aligned</>
2589           if the total width needed for column headers exceeds the target.
2590           </para>
2591
2592           <para>
2593           The <literal>html</>, <literal>asciidoc</>, <literal>latex</>,
2594           <literal>latex-longtable</literal>, and <literal>troff-ms</>
2595           formats put out tables that are intended to
2596           be included in documents using the respective mark-up
2597           language. They are not complete documents! This might not be
2598           necessary in <acronym>HTML</acronym>, but in
2599           <application>LaTeX</application> you must have a complete
2600           document wrapper.  <literal>latex-longtable</literal>
2601           also requires the <application>LaTeX</application>
2602           <literal>longtable</literal> and <literal>booktabs</> packages.
2603           </para>
2604           </listitem>
2605           </varlistentry>
2606
2607           <varlistentry>
2608           <term><literal>linestyle</literal></term>
2609           <listitem>
2610           <para>
2611           Sets the border line drawing style to one
2612           of <literal>ascii</literal>, <literal>old-ascii</literal>,
2613           or <literal>unicode</literal>.
2614           Unique abbreviations are allowed.  (That would mean one
2615           letter is enough.)
2616           The default setting is <literal>ascii</>.
2617           This option only affects the <literal>aligned</> and
2618           <literal>wrapped</> output formats.
2619           </para>
2620
2621           <para><literal>ascii</literal> style uses plain <acronym>ASCII</acronym>
2622           characters.  Newlines in data are shown using
2623           a <literal>+</literal> symbol in the right-hand margin.
2624           When the <literal>wrapped</literal> format wraps data from
2625           one line to the next without a newline character, a dot
2626           (<literal>.</>) is shown in the right-hand margin of the first line,
2627           and again in the left-hand margin of the following line.
2628           </para>
2629
2630           <para><literal>old-ascii</literal> style uses plain <acronym>ASCII</>
2631           characters, using the formatting style used
2632           in <productname>PostgreSQL</productname> 8.4 and earlier.
2633           Newlines in data are shown using a <literal>:</literal>
2634           symbol in place of the left-hand column separator.
2635           When the data is wrapped from one line
2636           to the next without a newline character, a <literal>;</>
2637           symbol is used in place of the left-hand column separator.
2638           </para>
2639
2640           <para><literal>unicode</literal> style uses Unicode box-drawing characters.
2641           Newlines in data are shown using a carriage return symbol
2642           in the right-hand margin.  When the data is wrapped from one line
2643           to the next without a newline character, an ellipsis symbol
2644           is shown in the right-hand margin of the first line, and
2645           again in the left-hand margin of the following line.
2646           </para>
2647
2648           <para>
2649           When the <literal>border</> setting is greater than zero,
2650           the <literal>linestyle</literal> option also determines the
2651           characters with which the border lines are drawn.
2652           Plain <acronym>ASCII</acronym> characters work everywhere, but
2653           Unicode characters look nicer on displays that recognize them.
2654           </para>
2655           </listitem>
2656           </varlistentry>
2657
2658           <varlistentry>
2659           <term><literal>null</literal></term>
2660           <listitem>
2661           <para>
2662           Sets the string to be printed in place of a null value.
2663           The default is to print nothing, which can easily be mistaken for
2664           an empty string. For example, one might prefer <literal>\pset null
2665           '(null)'</literal>.
2666           </para>
2667           </listitem>
2668           </varlistentry>
2669
2670           <varlistentry>
2671           <term><literal>numericlocale</literal></term>
2672           <listitem>
2673           <para>
2674           If <replaceable class="parameter">value</replaceable> is specified
2675           it must be either <literal>on</literal> or <literal>off</literal>
2676           which will enable or disable display of a locale-specific character
2677           to separate groups of digits to the left of the decimal marker.
2678           If <replaceable class="parameter">value</replaceable> is omitted the
2679           command toggles between regular and locale-specific numeric output.
2680           </para>
2681           </listitem>
2682           </varlistentry>
2683
2684           <varlistentry>
2685           <term><literal>pager</literal></term>
2686           <listitem>
2687           <para>
2688           Controls use of a pager program for query and <application>psql</>
2689           help output.  If the environment variable <envar>PSQL_PAGER</envar>
2690           or <envar>PAGER</envar> is set, the output is piped to the
2691           specified program.  Otherwise a platform-dependent default program
2692           (such as <filename>more</filename>) is used.
2693           </para>
2694
2695           <para>
2696           When the <literal>pager</> option is <literal>off</>, the pager
2697           program is not used. When the <literal>pager</> option is
2698           <literal>on</>, the pager is used when appropriate, i.e., when the
2699           output is to a terminal and will not fit on the screen.
2700           The <literal>pager</> option can also be set to <literal>always</>,
2701           which causes the pager to be used for all terminal output regardless
2702           of whether it fits on the screen.  <literal>\pset pager</>
2703           without a <replaceable class="parameter">value</replaceable>
2704           toggles pager use on and off.
2705           </para>
2706           </listitem>
2707           </varlistentry>
2708
2709           <varlistentry>
2710           <term><literal>pager_min_lines</literal></term>
2711           <listitem>
2712           <para>
2713           If <literal>pager_min_lines</> is set to a number greater than the
2714           page height, the pager program will not be called unless there are
2715           at least this many lines of output to show. The default setting
2716           is 0.
2717           </para>
2718           </listitem>
2719           </varlistentry>
2720
2721           <varlistentry>
2722           <term><literal>recordsep</literal></term>
2723           <listitem>
2724           <para>
2725           Specifies the record (line) separator to use in unaligned
2726           output format. The default is a newline character.
2727           </para>
2728           </listitem>
2729           </varlistentry>
2730
2731           <varlistentry>
2732           <term><literal>recordsep_zero</literal></term>
2733           <listitem>
2734           <para>
2735           Sets the record separator to use in unaligned output format to a zero
2736           byte.
2737           </para>
2738           </listitem>
2739           </varlistentry>
2740
2741           <varlistentry>
2742           <term><literal>tableattr</literal> (or <literal>T</literal>)</term>
2743           <listitem>
2744           <para>
2745           In <acronym>HTML</acronym> format, this specifies attributes
2746           to be placed inside the <sgmltag>table</sgmltag> tag.  This
2747           could for example be <literal>cellpadding</literal> or
2748           <literal>bgcolor</literal>. Note that you probably don't want
2749           to specify <literal>border</literal> here, as that is already
2750           taken care of by <literal>\pset border</literal>.
2751           If no
2752           <replaceable class="parameter">value</replaceable> is given,
2753           the table attributes are unset.
2754           </para>
2755           <para>
2756           In <literal>latex-longtable</literal> format, this controls
2757           the proportional width of each column containing a left-aligned
2758           data type.  It is specified as a whitespace-separated list of values,
2759           e.g. <literal>'0.2 0.2 0.6'</>.  Unspecified output columns
2760           use the last specified value.
2761           </para>
2762           </listitem>
2763           </varlistentry>
2764
2765           <varlistentry>
2766           <term><literal>title</literal> (or <literal>C</literal>)</term>
2767           <listitem>
2768           <para>
2769           Sets the table title for any subsequently printed tables. This
2770           can be used to give your output descriptive tags. If no
2771           <replaceable class="parameter">value</replaceable> is given,
2772           the title is unset.
2773           </para>
2774           </listitem>
2775           </varlistentry>
2776
2777           <varlistentry>
2778           <term><literal>tuples_only</literal> (or <literal>t</literal>)</term>
2779           <listitem>
2780           <para>
2781           If <replaceable class="parameter">value</replaceable> is specified
2782           it must be either <literal>on</literal> or <literal>off</literal>
2783           which will enable or disable tuples-only mode.
2784           If <replaceable class="parameter">value</replaceable> is omitted the
2785           command toggles between regular and tuples-only output.
2786           Regular output includes extra information such
2787           as column headers, titles, and various footers. In tuples-only
2788           mode, only actual table data is shown.
2789           </para>
2790           </listitem>
2791           </varlistentry>
2792
2793           <varlistentry>
2794           <term><literal>unicode_border_linestyle</literal></term>
2795           <listitem>
2796           <para>
2797           Sets the border drawing style for the <literal>unicode</literal>
2798           line style to one of <literal>single</literal>
2799           or <literal>double</literal>.
2800           </para>
2801           </listitem>
2802           </varlistentry>
2803
2804           <varlistentry>
2805           <term><literal>unicode_column_linestyle</literal></term>
2806           <listitem>
2807           <para>
2808           Sets the column drawing style for the <literal>unicode</literal>
2809           line style to one of <literal>single</literal>
2810           or <literal>double</literal>.
2811           </para>
2812           </listitem>
2813           </varlistentry>
2814
2815           <varlistentry>
2816           <term><literal>unicode_header_linestyle</literal></term>
2817           <listitem>
2818           <para>
2819           Sets the header drawing style for the <literal>unicode</literal>
2820           line style to one of <literal>single</literal>
2821           or <literal>double</literal>.
2822           </para>
2823           </listitem>
2824           </varlistentry>
2825         </variablelist>
2826         </para>
2827
2828         <para>
2829         Illustrations of how these different formats look can be seen in
2830         the <xref linkend="APP-PSQL-examples"
2831         endterm="APP-PSQL-examples-title"> section.
2832         </para>
2833
2834         <tip>
2835         <para>
2836         There are various shortcut commands for <command>\pset</command>. See
2837         <command>\a</command>, <command>\C</command>, <command>\f</command>,
2838         <command>\H</command>, <command>\t</command>, <command>\T</command>,
2839         and <command>\x</command>.
2840         </para>
2841         </tip>
2842
2843         </listitem>
2844       </varlistentry>
2845
2846
2847       <varlistentry>
2848         <term><literal>\q</literal> or <literal>\quit</literal></term>
2849         <listitem>
2850         <para>
2851         Quits the <application>psql</application> program.
2852         In a script file, only execution of that script is terminated.
2853         </para>
2854         </listitem>
2855       </varlistentry>
2856
2857
2858       <varlistentry>
2859         <term><literal>\qecho <replaceable class="parameter">text</replaceable> [ ... ] </literal></term>
2860         <listitem>
2861         <para>
2862         This command is identical to <command>\echo</command> except
2863         that the output will be written to the query output channel, as
2864         set by <command>\o</command>.
2865         </para>
2866         </listitem>
2867       </varlistentry>
2868
2869
2870       <varlistentry>
2871         <term><literal>\r</literal> or <literal>\reset</literal></term>
2872         <listitem>
2873         <para>
2874         Resets (clears) the query buffer.
2875         </para>
2876         </listitem>
2877       </varlistentry>
2878
2879
2880       <varlistentry>
2881         <term><literal>\s [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2882         <listitem>
2883         <para>
2884         Print <application>psql</application>'s command line history
2885         to <replaceable class="parameter">filename</replaceable>.
2886         If <replaceable class="parameter">filename</replaceable> is omitted,
2887         the history is written to the standard output (using the pager if
2888         appropriate).  This command is not available
2889         if <application>psql</application> was built
2890         without <application>Readline</application> support.
2891         </para>
2892         </listitem>
2893       </varlistentry>
2894
2895
2896       <varlistentry>
2897         <term><literal>\set [ <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> [ ... ] ] ]</literal></term>
2898
2899         <listitem>
2900         <para>
2901         Sets the <application>psql</> variable <replaceable
2902         class="parameter">name</replaceable> to <replaceable
2903         class="parameter">value</replaceable>, or if more than one value
2904         is given, to the concatenation of all of them. If only one
2905         argument is given, the variable is set to an empty-string value. To
2906         unset a variable, use the <command>\unset</command> command.
2907         </para>
2908
2909         <para><command>\set</> without any arguments displays the names and values
2910         of all currently-set <application>psql</> variables.
2911         </para>
2912
2913         <para>
2914         Valid variable names can contain letters, digits, and
2915         underscores. See the section <xref
2916         linkend="APP-PSQL-variables"
2917         endterm="APP-PSQL-variables-title"> below for details.
2918         Variable names are case-sensitive.
2919         </para>
2920
2921         <para>
2922         Certain variables are special, in that they
2923         control <application>psql</application>'s behavior or are
2924         automatically set to reflect connection state.  These variables are
2925         documented in <xref linkend="APP-PSQL-variables"
2926         endterm="APP-PSQL-variables-title">, below.
2927         </para>
2928
2929         <note>
2930         <para>
2931         This command is unrelated to the <acronym>SQL</acronym>
2932         command <xref linkend="SQL-SET">.
2933         </para>
2934         </note>
2935         </listitem>
2936       </varlistentry>
2937
2938
2939       <varlistentry>
2940         <term><literal>\setenv <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> ]</literal></term>
2941
2942         <listitem>
2943         <para>
2944         Sets the environment variable <replaceable
2945         class="parameter">name</replaceable> to <replaceable
2946         class="parameter">value</replaceable>, or if the
2947         <replaceable class="parameter">value</replaceable> is
2948         not supplied, unsets the environment variable. Example:
2949 <programlisting>
2950 testdb=&gt; <userinput>\setenv PAGER less</userinput>
2951 testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
2952 </programlisting></para>
2953         </listitem>
2954       </varlistentry>
2955
2956       <varlistentry>
2957         <term><literal>\sf[+] <replaceable class="parameter">function_description</> </literal></term>
2958
2959         <listitem>
2960         <para>
2961          This command fetches and shows the definition of the named function,
2962          in the form of a <command>CREATE OR REPLACE FUNCTION</> command.
2963          The definition is printed to the current query output channel,
2964          as set by <command>\o</command>.
2965         </para>
2966
2967         <para>
2968          The target function can be specified by name alone, or by name
2969          and arguments, for example <literal>foo(integer, text)</>.
2970          The argument types must be given if there is more
2971          than one function of the same name.
2972         </para>
2973
2974         <para>
2975          If <literal>+</literal> is appended to the command name, then the
2976          output lines are numbered, with the first line of the function body
2977          being line 1.
2978         </para>
2979
2980         <para>
2981         Unlike most other meta-commands, the entire remainder of the line is
2982         always taken to be the argument(s) of <command>\sf</>, and neither
2983         variable interpolation nor backquote expansion are performed in the
2984         arguments.
2985         </para>
2986         </listitem>
2987       </varlistentry>
2988
2989
2990       <varlistentry>
2991         <term><literal>\sv[+] <replaceable class="parameter">view_name</> </literal></term>
2992
2993         <listitem>
2994          <para>
2995           This command fetches and shows the definition of the named view,
2996           in the form of a <command>CREATE OR REPLACE VIEW</> command.
2997           The definition is printed to the current query output channel,
2998           as set by <command>\o</command>.
2999          </para>
3000
3001          <para>
3002           If <literal>+</literal> is appended to the command name, then the
3003           output lines are numbered from 1.
3004          </para>
3005
3006         <para>
3007         Unlike most other meta-commands, the entire remainder of the line is
3008         always taken to be the argument(s) of <command>\sv</>, and neither
3009         variable interpolation nor backquote expansion are performed in the
3010         arguments.
3011         </para>
3012         </listitem>
3013       </varlistentry>
3014
3015
3016       <varlistentry>
3017         <term><literal>\t</literal></term>
3018         <listitem>
3019         <para>
3020         Toggles the display of output column name headings and row count
3021         footer. This command is equivalent to <literal>\pset
3022         tuples_only</literal> and is provided for convenience.
3023         </para>
3024         </listitem>
3025       </varlistentry>
3026
3027
3028       <varlistentry>
3029         <term><literal>\T <replaceable class="parameter">table_options</replaceable></literal></term>
3030         <listitem>
3031         <para>
3032         Specifies attributes to be placed within the
3033         <sgmltag>table</sgmltag> tag in <acronym>HTML</acronym>
3034         output format. This command is equivalent to <literal>\pset
3035         tableattr <replaceable
3036         class="parameter">table_options</replaceable></literal>.
3037         </para>
3038         </listitem>
3039       </varlistentry>
3040
3041
3042       <varlistentry>
3043        <term><literal>\timing [ <replaceable class="parameter">on</replaceable> | <replaceable class="parameter">off</replaceable> ]</literal></term>
3044         <listitem>
3045         <para>
3046          With a parameter, turns displaying of how long each SQL statement
3047          takes on or off.  Without a parameter, toggles the display between
3048          on and off.  The display is in milliseconds; intervals longer than
3049          1 second are also shown in minutes:seconds format, with hours and
3050          days fields added if needed.
3051         </para>
3052        </listitem>
3053       </varlistentry>
3054
3055
3056       <varlistentry>
3057         <term><literal>\unset <replaceable class="parameter">name</replaceable></literal></term>
3058
3059         <listitem>
3060         <para>
3061         Unsets (deletes) the <application>psql</> variable <replaceable
3062         class="parameter">name</replaceable>.
3063         </para>
3064
3065         <para>
3066         Most variables that control <application>psql</application>'s behavior
3067         cannot be unset; instead, an <literal>\unset</> command is interpreted
3068         as setting them to their default values.
3069         See <xref linkend="APP-PSQL-variables"
3070         endterm="APP-PSQL-variables-title">, below.
3071         </para>
3072         </listitem>
3073       </varlistentry>
3074
3075
3076       <varlistentry>
3077         <term><literal>\w</literal> or <literal>\write</literal> <replaceable class="parameter">filename</replaceable></term>
3078         <term><literal>\w</literal> or <literal>\write</literal> <literal>|</><replaceable class="parameter">command</replaceable></term>
3079         <listitem>
3080         <para>
3081         Writes the current query buffer to the file <replaceable
3082         class="parameter">filename</replaceable> or pipes it to the shell
3083         command <replaceable class="parameter">command</replaceable>.
3084         If the current query buffer is empty, the most recently executed query
3085         is written instead.
3086         </para>
3087
3088         <para>
3089         If the argument begins with <literal>|</>, then the entire remainder
3090         of the line is taken to be
3091         the <replaceable class="parameter">command</replaceable> to execute,
3092         and neither variable interpolation nor backquote expansion are
3093         performed in it.  The rest of the line is simply passed literally to
3094         the shell.
3095         </para>
3096         </listitem>
3097       </varlistentry>
3098
3099
3100       <varlistentry>
3101         <term><literal>\watch [ <replaceable class="parameter">seconds</replaceable> ]</literal></term>
3102         <listitem>
3103         <para>
3104         Repeatedly execute the current query buffer (as <literal>\g</> does)
3105         until interrupted or the query fails.  Wait the specified number of
3106         seconds (default 2) between executions.  Each query result is
3107         displayed with a header that includes the <literal>\pset title</>
3108         string (if any), the time as of query start, and the delay interval.
3109         </para>
3110         <para>
3111         If the current query buffer is empty, the most recently sent query
3112         is re-executed instead.
3113         </para>
3114         </listitem>
3115       </varlistentry>
3116
3117
3118       <varlistentry>
3119         <term><literal>\x [ <replaceable class="parameter">on</replaceable> | <replaceable class="parameter">off</replaceable> | <replaceable class="parameter">auto</replaceable> ]</literal></term>
3120         <listitem>
3121         <para>
3122         Sets or toggles expanded table formatting mode. As such it is equivalent to
3123         <literal>\pset expanded</literal>.
3124        </para>
3125        </listitem>
3126       </varlistentry>
3127
3128
3129       <varlistentry>
3130         <term><literal>\z [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
3131         <listitem>
3132         <para>
3133         Lists tables, views and sequences with their
3134         associated access privileges.
3135         If a <replaceable class="parameter">pattern</replaceable> is
3136         specified, only tables, views and sequences whose names match the
3137         pattern are listed.
3138         </para>
3139
3140         <para>
3141         This is an alias for <command>\dp</command> (<quote>display
3142         privileges</quote>).
3143         </para>
3144         </listitem>
3145       </varlistentry>
3146
3147
3148       <varlistentry>
3149         <term><literal>\! [ <replaceable class="parameter">command</replaceable> ]</literal></term>
3150         <listitem>
3151         <para>
3152         With no argument, escapes to a sub-shell; <application>psql</>
3153         resumes when the sub-shell exits.  With an argument, executes the
3154         shell command <replaceable class="parameter">command</replaceable>.
3155         </para>
3156
3157         <para>
3158         Unlike most other meta-commands, the entire remainder of the line is
3159         always taken to be the argument(s) of <command>\!</>, and neither
3160         variable interpolation nor backquote expansion are performed in the
3161         arguments.  The rest of the line is simply passed literally to the
3162         shell.
3163         </para>
3164         </listitem>
3165       </varlistentry>
3166
3167
3168       <varlistentry>
3169         <term><literal>\? [ <replaceable class="parameter">topic</> ]</literal></term>
3170         <listitem>
3171         <para>
3172         Shows help information. The optional
3173         <replaceable class="parameter">topic</> parameter
3174         (defaulting to <literal>commands</>) selects which part of <application>psql</application> is
3175         explained: <literal>commands</> describes <application>psql</>'s
3176         backslash commands; <literal>options</> describes the command-line
3177         options that can be passed to <application>psql</>;
3178         and <literal>variables</> shows help about <application>psql</application> configuration
3179         variables.
3180         </para>
3181         </listitem>
3182       </varlistentry>
3183
3184
3185       <varlistentry>
3186         <term><literal>\;</literal></term>
3187         <listitem>
3188         <para>
3189         Backslash-semicolon is not a meta-command in the same way as the
3190         preceding commands; rather, it simply causes a semicolon to be
3191         added to the query buffer without any further processing.
3192         </para>
3193
3194         <para>
3195         Normally, <application>psql</> will dispatch a SQL command to the
3196         server as soon as it reaches the command-ending semicolon, even if
3197         more input remains on the current line.  Thus for example entering
3198 <programlisting>
3199 select 1; select 2; select 3;
3200 </programlisting>
3201         will result in the three SQL commands being individually sent to
3202         the server, with each one's results being displayed before
3203         continuing to the next command.  However, a semicolon entered
3204         as <literal>\;</> will not trigger command processing, so that the
3205         command before it and the one after are effectively combined and
3206         sent to the server in one request.  So for example
3207 <programlisting>
3208 select 1\; select 2\; select 3;
3209 </programlisting>
3210         results in sending the three SQL commands to the server in a single
3211         request, when the non-backslashed semicolon is reached.
3212         The server executes such a request as a single transaction,
3213         unless there are explicit <command>BEGIN</>/<command>COMMIT</>
3214         commands included in the string to divide it into multiple
3215         transactions.  (See <xref linkend="protocol-flow-multi-statement">
3216         for more details about how the server handles multi-query strings.)
3217         <application>psql</application> prints only the last query result
3218         it receives for each request; in this example, although all
3219         three <command>SELECT</>s are indeed executed, <application>psql</>
3220         only prints the <literal>3</>.
3221         </para>
3222         </listitem>
3223       </varlistentry>
3224
3225     </variablelist>
3226   </para>
3227
3228   <refsect3 id="APP-PSQL-patterns">
3229    <title id="APP-PSQL-patterns-title">Patterns</title>
3230
3231    <indexterm>
3232     <primary>patterns</primary>
3233     <secondary>in psql and pg_dump</secondary>
3234    </indexterm>
3235
3236   <para>
3237    The various <literal>\d</> commands accept a <replaceable
3238    class="parameter">pattern</replaceable> parameter to specify the
3239    object name(s) to be displayed.  In the simplest case, a pattern
3240    is just the exact name of the object.  The characters within a
3241    pattern are normally folded to lower case, just as in SQL names;
3242    for example, <literal>\dt FOO</> will display the table named
3243    <literal>foo</>.  As in SQL names, placing double quotes around
3244    a pattern stops folding to lower case.  Should you need to include
3245    an actual double quote character in a pattern, write it as a pair
3246    of double quotes within a double-quote sequence; again this is in
3247    accord with the rules for SQL quoted identifiers.  For example,
3248    <literal>\dt "FOO""BAR"</> will display the table named
3249    <literal>FOO"BAR</> (not <literal>foo"bar</>).  Unlike the normal
3250    rules for SQL names, you can put double quotes around just part
3251    of a pattern, for instance <literal>\dt FOO"FOO"BAR</> will display
3252    the table named <literal>fooFOObar</>.
3253   </para>
3254
3255   <para>
3256    Whenever the <replaceable class="parameter">pattern</replaceable> parameter
3257    is omitted completely, the <literal>\d</> commands display all objects
3258    that are visible in the current schema search path &mdash; this is
3259    equivalent to using <literal>*</> as the pattern.
3260    (An object is said to be <firstterm>visible</> if its
3261    containing schema is in the search path and no object of the same
3262    kind and name appears earlier in the search path. This is equivalent to the
3263    statement that the object can be referenced by name without explicit
3264    schema qualification.)
3265    To see all objects in the database regardless of visibility,
3266    use <literal>*.*</> as the pattern.
3267   </para>
3268
3269   <para>
3270    Within a pattern, <literal>*</> matches any sequence of characters
3271    (including no characters) and <literal>?</> matches any single character.
3272    (This notation is comparable to Unix shell file name patterns.)
3273    For example, <literal>\dt int*</> displays tables whose names
3274    begin with <literal>int</>.  But within double quotes, <literal>*</>
3275    and <literal>?</> lose these special meanings and are just matched
3276    literally.
3277   </para>
3278
3279   <para>
3280    A pattern that contains a dot (<literal>.</>) is interpreted as a schema
3281    name pattern followed by an object name pattern.  For example,
3282    <literal>\dt foo*.*bar*</> displays all tables whose table name
3283    includes <literal>bar</> that are in schemas whose schema name
3284    starts with <literal>foo</>.  When no dot appears, then the pattern
3285    matches only objects that are visible in the current schema search path.
3286    Again, a dot within double quotes loses its special meaning and is matched
3287    literally.
3288   </para>
3289
3290   <para>
3291    Advanced users can use regular-expression notations such as character
3292    classes, for example <literal>[0-9]</> to match any digit.  All regular
3293    expression special characters work as specified in
3294    <xref linkend="functions-posix-regexp">, except for <literal>.</> which
3295    is taken as a separator as mentioned above, <literal>*</> which is
3296    translated to the regular-expression notation <literal>.*</>,
3297    <literal>?</> which is translated to <literal>.</>, and
3298    <literal>$</> which is matched literally.  You can emulate
3299    these pattern characters at need by writing
3300    <literal>?</> for <literal>.</>,
3301    <literal>(<replaceable class="parameter">R</replaceable>+|)</literal> for
3302    <literal><replaceable class="parameter">R</replaceable>*</literal>, or
3303    <literal>(<replaceable class="parameter">R</replaceable>|)</literal> for
3304    <literal><replaceable class="parameter">R</replaceable>?</literal>.
3305    <literal>$</> is not needed as a regular-expression character since
3306    the pattern must match the whole name, unlike the usual
3307    interpretation of regular expressions (in other words, <literal>$</>
3308    is automatically appended to your pattern).  Write <literal>*</> at the
3309    beginning and/or end if you don't wish the pattern to be anchored.
3310    Note that within double quotes, all regular expression special characters
3311    lose their special meanings and are matched literally.  Also, the regular
3312    expression special characters are matched literally in operator name
3313    patterns (i.e., the argument of <literal>\do</>).
3314   </para>
3315   </refsect3>
3316  </refsect2>
3317
3318  <refsect2>
3319   <title>Advanced Features</title>
3320
3321    <refsect3 id="APP-PSQL-variables">
3322     <title id="APP-PSQL-variables-title">Variables</title>
3323
3324     <para>
3325     <application>psql</application> provides variable substitution
3326     features similar to common Unix command shells.
3327     Variables are simply name/value pairs, where the value
3328     can be any string of any length.  The name must consist of letters
3329     (including non-Latin letters), digits, and underscores.
3330     </para>
3331
3332     <para>
3333     To set a variable, use the <application>psql</application> meta-command
3334     <command>\set</command>.  For example,
3335 <programlisting>
3336 testdb=&gt; <userinput>\set foo bar</userinput>
3337 </programlisting>
3338     sets the variable <literal>foo</literal> to the value
3339     <literal>bar</literal>. To retrieve the content of the variable, precede
3340     the name with a colon, for example:
3341 <programlisting>
3342 testdb=&gt; <userinput>\echo :foo</userinput>
3343 bar
3344 </programlisting>
3345     This works in both regular SQL commands and meta-commands; there is
3346     more detail in <xref linkend="APP-PSQL-interpolation"
3347     endterm="APP-PSQL-interpolation-title">, below.
3348     </para>
3349
3350     <para>
3351     If you call <command>\set</command> without a second argument, the
3352     variable is set to an empty-string value. To unset (i.e., delete)
3353     a variable, use the command <command>\unset</command>.  To show the
3354     values of all variables, call <command>\set</command> without any argument.
3355     </para>
3356
3357     <note>
3358     <para>
3359     The arguments of <command>\set</command> are subject to the same
3360     substitution rules as with other commands. Thus you can construct
3361     interesting references such as <literal>\set :foo
3362     'something'</literal> and get <quote>soft links</quote> or
3363     <quote>variable variables</quote> of <productname>Perl</productname>
3364     or <productname><acronym>PHP</acronym></productname> fame,
3365     respectively. Unfortunately (or fortunately?), there is no way to do
3366     anything useful with these constructs. On the other hand,
3367     <literal>\set bar :foo</literal> is a perfectly valid way to copy a
3368     variable.
3369     </para>
3370     </note>
3371
3372     <para>
3373     A number of these variables are treated specially
3374     by <application>psql</application>. They represent certain option
3375     settings that can be changed at run time by altering the value of
3376     the variable, or in some cases represent changeable state of
3377     <application>psql</application>.
3378     By convention, all specially treated variables' names
3379     consist of all upper-case ASCII letters (and possibly digits and
3380     underscores). To ensure maximum compatibility in the future, avoid
3381     using such variable names for your own purposes.
3382    </para>
3383
3384    <para>
3385     Variables that control <application>psql</application>'s behavior
3386     generally cannot be unset or set to invalid values.  An <literal>\unset</>
3387     command is allowed but is interpreted as setting the variable to its
3388     default value.  A <literal>\set</> command without a second argument is
3389     interpreted as setting the variable to <literal>on</>, for control
3390     variables that accept that value, and is rejected for others.  Also,
3391     control variables that accept the values <literal>on</>
3392     and <literal>off</> will also accept other common spellings of Boolean
3393     values, such as <literal>true</> and <literal>false</>.
3394    </para>
3395
3396    <para>
3397     The specially treated variables are:
3398    </para>
3399
3400     <variablelist>
3401       <varlistentry>
3402       <term>
3403        <varname>AUTOCOMMIT</varname>
3404        <indexterm>
3405         <primary>autocommit</primary>
3406         <secondary>psql</secondary>
3407        </indexterm>
3408       </term>
3409         <listitem>
3410         <para>
3411         When <literal>on</> (the default), each SQL command is automatically
3412         committed upon successful completion.  To postpone commit in this
3413         mode, you must enter a <command>BEGIN</> or <command>START
3414         TRANSACTION</> SQL command.  When <literal>off</> or unset, SQL
3415         commands are not committed until you explicitly issue
3416         <command>COMMIT</> or <command>END</>.  The autocommit-off
3417         mode works by issuing an implicit <command>BEGIN</> for you, just
3418         before any command that is not already in a transaction block and
3419         is not itself a <command>BEGIN</> or other transaction-control
3420         command, nor a command that cannot be executed inside a transaction
3421         block (such as <command>VACUUM</>).
3422         </para>
3423
3424         <note>
3425         <para>
3426          In autocommit-off mode, you must explicitly abandon any failed
3427          transaction by entering <command>ABORT</> or <command>ROLLBACK</>.
3428          Also keep in mind that if you exit the session
3429          without committing, your work will be lost.
3430         </para>
3431         </note>
3432
3433         <note>
3434         <para>
3435          The autocommit-on mode is <productname>PostgreSQL</>'s traditional
3436          behavior, but autocommit-off is closer to the SQL spec.  If you
3437          prefer autocommit-off, you might wish to set it in the system-wide
3438          <filename>psqlrc</filename> file or your
3439          <filename>~/.psqlrc</filename> file.
3440         </para>
3441         </note>
3442         </listitem>
3443       </varlistentry>
3444
3445       <varlistentry>
3446         <term><varname>COMP_KEYWORD_CASE</varname></term>
3447         <listitem>
3448         <para>
3449         Determines which letter case to use when completing an SQL key word.
3450         If set to <literal>lower</literal> or <literal>upper</literal>, the
3451         completed word will be in lower or upper case, respectively.  If set
3452         to <literal>preserve-lower</literal>
3453         or <literal>preserve-upper</literal> (the default), the completed word
3454         will be in the case of the word already entered, but words being
3455         completed without anything entered will be in lower or upper case,
3456         respectively.
3457         </para>
3458         </listitem>
3459       </varlistentry>
3460
3461       <varlistentry>
3462         <term><varname>DBNAME</varname></term>
3463         <listitem>
3464         <para>
3465         The name of the database you are currently connected to. This is
3466         set every time you connect to a database (including program
3467         start-up), but can be changed or unset.
3468         </para>
3469         </listitem>
3470       </varlistentry>
3471
3472       <varlistentry>
3473         <term><varname>ECHO</varname></term>
3474         <listitem>
3475         <para>
3476         If set to <literal>all</literal>, all nonempty input lines are printed
3477         to standard output as they are read.  (This does not apply to lines
3478         read interactively.)  To select this behavior on program
3479         start-up, use the switch <option>-a</option>. If set to
3480         <literal>queries</literal>,
3481         <application>psql</application> prints each query to standard output
3482         as it is sent to the server. The switch to select this behavior is
3483         <option>-e</option>. If set to <literal>errors</literal>, then only
3484         failed queries are displayed on standard error output. The switch
3485         for this behavior is <option>-b</option>. If set to
3486         <literal>none</literal> (the default), then no queries are displayed.
3487         </para>
3488         </listitem>
3489       </varlistentry>
3490
3491       <varlistentry>
3492         <term><varname>ECHO_HIDDEN</varname></term>
3493         <listitem>
3494         <para>
3495         When this variable is set to <literal>on</> and a backslash command
3496         queries the database, the query is first shown.
3497         This feature helps you to study
3498         <productname>PostgreSQL</productname> internals and provide
3499         similar functionality in your own programs. (To select this behavior
3500         on program start-up, use the switch <option>-E</option>.)  If you set
3501         this variable to the value <literal>noexec</literal>, the queries are
3502         just shown but are not actually sent to the server and executed.
3503         The default value is <literal>off</>.
3504         </para>
3505         </listitem>
3506       </varlistentry>
3507
3508       <varlistentry>
3509         <term><varname>ENCODING</varname></term>
3510         <listitem>
3511         <para>
3512         The current client character set encoding.
3513         This is set every time you connect to a database (including
3514         program start-up), and when you change the encoding
3515         with <literal>\encoding</>, but it can be changed or unset.
3516         </para>
3517         </listitem>
3518       </varlistentry>
3519
3520       <varlistentry>
3521        <term><varname>ERROR</varname></term>
3522        <listitem>
3523         <para>
3524          <literal>true</> if the last SQL query failed, <literal>false</> if
3525          it succeeded.  See also <varname>SQLSTATE</>.
3526         </para>
3527        </listitem>
3528       </varlistentry>
3529
3530       <varlistentry>
3531         <term><varname>FETCH_COUNT</varname></term>
3532         <listitem>
3533         <para>
3534         If this variable is set to an integer value greater than zero,
3535         the results of <command>SELECT</command> queries are fetched
3536         and displayed in groups of that many rows, rather than the
3537         default behavior of collecting the entire result set before
3538         display.  Therefore only a
3539         limited amount of memory is used, regardless of the size of
3540         the result set.  Settings of 100 to 1000 are commonly used
3541         when enabling this feature.
3542         Keep in mind that when using this feature, a query might
3543         fail after having already displayed some rows.
3544         </para>
3545
3546         <tip>
3547         <para>
3548         Although you can use any output format with this feature,
3549         the default <literal>aligned</> format tends to look bad
3550         because each group of <varname>FETCH_COUNT</varname> rows
3551         will be formatted separately, leading to varying column
3552         widths across the row groups.  The other output formats work better.
3553         </para>
3554         </tip>
3555         </listitem>
3556       </varlistentry>
3557
3558       <varlistentry>
3559         <term><varname>HISTCONTROL</varname></term>
3560         <listitem>
3561         <para>
3562          If this variable is set to <literal>ignorespace</literal>,
3563          lines which begin with a space are not entered into the history
3564          list. If set to a value of <literal>ignoredups</literal>, lines
3565          matching the previous history line are not entered. A value of
3566          <literal>ignoreboth</literal> combines the two options. If
3567          set to <literal>none</literal> (the default), all lines
3568          read in interactive mode are saved on the history list.
3569         </para>
3570         <note>
3571         <para>
3572         This feature was shamelessly plagiarized from
3573         <application>Bash</application>.
3574         </para>
3575         </note>
3576         </listitem>
3577       </varlistentry>
3578
3579       <varlistentry>
3580         <term><varname>HISTFILE</varname></term>
3581         <listitem>
3582         <para>
3583         The file name that will be used to store the history list.  If unset,
3584         the file name is taken from the <envar>PSQL_HISTORY</envar>
3585         environment variable.  If that is not set either, the default
3586         is <filename>~/.psql_history</filename>,
3587         or <filename>%APPDATA%\postgresql\psql_history</filename> on Windows.
3588         For example, putting:
3589 <programlisting>
3590 \set HISTFILE ~/.psql_history- :DBNAME
3591 </programlisting>
3592         in <filename>~/.psqlrc</filename> will cause
3593         <application>psql</application> to maintain a separate history for
3594         each database.
3595         </para>
3596         <note>
3597         <para>
3598         This feature was shamelessly plagiarized from
3599         <application>Bash</application>.
3600         </para>
3601         </note>
3602         </listitem>
3603       </varlistentry>
3604
3605       <varlistentry>
3606         <term><varname>HISTSIZE</varname></term>
3607         <listitem>
3608         <para>
3609         The maximum number of commands to store in the command history
3610         (default 500).  If set to a negative value, no limit is applied.
3611         </para>
3612         <note>
3613         <para>
3614         This feature was shamelessly plagiarized from
3615         <application>Bash</application>.
3616         </para>
3617         </note>
3618         </listitem>
3619       </varlistentry>
3620
3621       <varlistentry>
3622         <term><varname>HOST</varname></term>
3623         <listitem>
3624         <para>
3625         The database server host you are currently connected to. This is
3626         set every time you connect to a database (including program
3627         start-up), but can be changed or unset.
3628         </para>
3629         </listitem>
3630       </varlistentry>
3631
3632       <varlistentry>
3633         <term><varname>IGNOREEOF</varname></term>
3634         <listitem>
3635         <para>
3636          If set to 1 or less, sending an <acronym>EOF</> character (usually
3637          <keycombo action="simul"><keycap>Control</><keycap>D</></>)
3638          to an interactive session of <application>psql</application>
3639          will terminate the application.  If set to a larger numeric value,
3640          that many consecutive <acronym>EOF</> characters must be typed to
3641          make an interactive session terminate.  If the variable is set to a
3642          non-numeric value, it is interpreted as 10.  The default is 0.
3643         </para>
3644         <note>
3645         <para>
3646         This feature was shamelessly plagiarized from
3647         <application>Bash</application>.
3648         </para>
3649         </note>
3650         </listitem>
3651       </varlistentry>
3652
3653       <varlistentry>
3654         <term><varname>LASTOID</varname></term>
3655         <listitem>
3656         <para>
3657         The value of the last affected OID, as returned from an
3658         <command>INSERT</command> or <command>\lo_import</command>
3659         command. This variable is only guaranteed to be valid until
3660         after the result of the next <acronym>SQL</acronym> command has
3661         been displayed.
3662         </para>
3663         </listitem>
3664       </varlistentry>
3665
3666       <varlistentry>
3667        <term><varname>LAST_ERROR_MESSAGE</varname></term>
3668        <term><varname>LAST_ERROR_SQLSTATE</varname></term>
3669        <listitem>
3670         <para>
3671          The primary error message and associated SQLSTATE code for the most
3672          recent failed query in the current <application>psql</> session, or
3673          an empty string and <literal>00000</> if no error has occurred in
3674          the current session.
3675         </para>
3676        </listitem>
3677       </varlistentry>
3678
3679       <varlistentry>
3680       <term>
3681        <varname>ON_ERROR_ROLLBACK</varname>
3682        <indexterm>
3683         <primary>rollback</primary>
3684         <secondary>psql</secondary>
3685        </indexterm>
3686       </term>
3687         <listitem>
3688         <para>
3689         When set to <literal>on</>, if a statement in a transaction block
3690         generates an error, the error is ignored and the transaction
3691         continues. When set to <literal>interactive</>, such errors are only
3692         ignored in interactive sessions, and not when reading script
3693         files. When set to <literal>off</> (the default), a statement in a
3694         transaction block that generates an error aborts the entire
3695         transaction. The error rollback mode works by issuing an
3696         implicit <command>SAVEPOINT</> for you, just before each command
3697         that is in a transaction block, and then rolling back to the
3698         savepoint if the command fails.
3699         </para>
3700         </listitem>
3701       </varlistentry>
3702
3703       <varlistentry>
3704         <term><varname>ON_ERROR_STOP</varname></term>
3705         <listitem>
3706         <para>
3707         By default, command processing continues after an error.  When this
3708         variable is set to <literal>on</>, processing will instead stop
3709         immediately.  In interactive mode,
3710         <application>psql</application> will return to the command prompt;
3711         otherwise, <application>psql</application> will exit, returning
3712         error code 3 to distinguish this case from fatal error
3713         conditions, which are reported using error code 1.  In either case,
3714         any currently running scripts (the top-level script, if any, and any
3715         other scripts which it may have in invoked) will be terminated
3716         immediately.  If the top-level command string contained multiple SQL
3717         commands, processing will stop with the current command.
3718         </para>
3719         </listitem>
3720       </varlistentry>
3721
3722       <varlistentry>
3723         <term><varname>PORT</varname></term>
3724         <listitem>
3725         <para>
3726         The database server port to which you are currently connected.
3727         This is set every time you connect to a database (including
3728         program start-up), but can be changed or unset.
3729         </para>
3730         </listitem>
3731       </varlistentry>
3732
3733       <varlistentry>
3734         <term><varname>PROMPT1</varname></term>
3735         <term><varname>PROMPT2</varname></term>
3736         <term><varname>PROMPT3</varname></term>
3737         <listitem>
3738         <para>
3739         These specify what the prompts <application>psql</application>
3740         issues should look like. See <xref
3741         linkend="APP-PSQL-prompting"
3742         endterm="APP-PSQL-prompting-title"> below.
3743         </para>
3744         </listitem>
3745       </varlistentry>
3746
3747       <varlistentry>
3748         <term><varname>QUIET</varname></term>
3749         <listitem>
3750         <para>
3751         Setting this variable to <literal>on</> is equivalent to the command
3752         line option <option>-q</option>. It is probably not too useful in
3753         interactive mode.
3754         </para>
3755         </listitem>
3756       </varlistentry>
3757
3758       <varlistentry>
3759        <term><varname>ROW_COUNT</varname></term>
3760        <listitem>
3761         <para>
3762          The number of rows returned or affected by the last SQL query, or 0
3763          if the query failed or did not report a row count.
3764         </para>
3765        </listitem>
3766       </varlistentry>
3767
3768       <varlistentry>
3769         <term><varname>SERVER_VERSION_NAME</varname></term>
3770         <term><varname>SERVER_VERSION_NUM</varname></term>
3771         <listitem>
3772         <para>
3773         The server's version number as a string, for
3774         example <literal>9.6.2</>, <literal>10.1</> or <literal>11beta1</>,
3775         and in numeric form, for
3776         example <literal>90602</> or <literal>100001</>.
3777         These are set every time you connect to a database
3778         (including program start-up), but can be changed or unset.
3779         </para>
3780         </listitem>
3781       </varlistentry>
3782
3783       <varlistentry>
3784         <term><varname>SHOW_CONTEXT</varname></term>
3785         <listitem>
3786         <para>
3787         This variable can be set to the
3788         values <literal>never</>, <literal>errors</>, or <literal>always</>
3789         to control whether <literal>CONTEXT</> fields are displayed in
3790         messages from the server. The default is <literal>errors</> (meaning
3791         that context will be shown in error messages, but not in notice or
3792         warning messages).  This setting has no effect
3793         when <varname>VERBOSITY</> is set to <literal>terse</>.
3794         (See also <command>\errverbose</>, for use when you want a verbose
3795         version of the error you just got.)
3796         </para>
3797         </listitem>
3798       </varlistentry>
3799
3800       <varlistentry>
3801         <term><varname>SINGLELINE</varname></term>
3802         <listitem>
3803         <para>
3804         Setting this variable to <literal>on</> is equivalent to the command
3805         line option <option>-S</option>.
3806         </para>
3807         </listitem>
3808       </varlistentry>
3809
3810       <varlistentry>
3811         <term><varname>SINGLESTEP</varname></term>
3812         <listitem>
3813         <para>
3814         Setting this variable to <literal>on</> is equivalent to the command
3815         line option <option>-s</option>.
3816         </para>
3817         </listitem>
3818       </varlistentry>
3819
3820       <varlistentry>
3821        <term><varname>SQLSTATE</varname></term>
3822        <listitem>
3823         <para>
3824          The error code (see <xref linkend="errcodes-appendix">) associated
3825          with the last SQL query's failure, or <literal>00000</> if it
3826          succeeded.
3827         </para>
3828        </listitem>
3829       </varlistentry>
3830
3831       <varlistentry>
3832         <term><varname>USER</varname></term>
3833         <listitem>
3834         <para>
3835         The database user you are currently connected as. This is set
3836         every time you connect to a database (including program
3837         start-up), but can be changed or unset.
3838         </para>
3839         </listitem>
3840       </varlistentry>
3841
3842       <varlistentry>
3843         <term><varname>VERBOSITY</varname></term>
3844         <listitem>
3845         <para>
3846         This variable can be set to the values <literal>default</>,
3847         <literal>verbose</>, or <literal>terse</> to control the verbosity
3848         of error reports.
3849         (See also <command>\errverbose</>, for use when you want a verbose
3850         version of the error you just got.)
3851         </para>
3852         </listitem>
3853       </varlistentry>
3854
3855       <varlistentry>
3856         <term><varname>VERSION</varname></term>
3857         <term><varname>VERSION_NAME</varname></term>
3858         <term><varname>VERSION_NUM</varname></term>
3859         <listitem>
3860         <para>
3861         These variables are set at program start-up to reflect
3862         <application>psql</>'s version, respectively as a verbose string,
3863         a short string (e.g., <literal>9.6.2</>, <literal>10.1</>,
3864         or <literal>11beta1</>), and a number (e.g., <literal>90602</>
3865         or <literal>100001</>).  They can be changed or unset.
3866         </para>
3867         </listitem>
3868       </varlistentry>
3869
3870     </variablelist>
3871
3872    </refsect3>
3873
3874    <refsect3 id="APP-PSQL-interpolation">
3875     <title id="APP-PSQL-interpolation-title"><acronym>SQL</acronym> Interpolation</title>
3876
3877     <para>
3878     A key feature of <application>psql</application>
3879     variables is that you can substitute (<quote>interpolate</quote>)
3880     them into regular <acronym>SQL</acronym> statements, as well as the
3881     arguments of meta-commands.  Furthermore,
3882     <application>psql</application> provides facilities for
3883     ensuring that variable values used as SQL literals and identifiers are
3884     properly quoted.  The syntax for interpolating a value without
3885     any quoting is to prepend the variable name with a colon
3886     (<literal>:</literal>).  For example,
3887 <programlisting>
3888 testdb=&gt; <userinput>\set foo 'my_table'</userinput>
3889 testdb=&gt; <userinput>SELECT * FROM :foo;</userinput>
3890 </programlisting>
3891     would query the table <literal>my_table</literal>. Note that this
3892     may be unsafe: the value of the variable is copied literally, so it can
3893     contain unbalanced quotes, or even backslash commands. You must make sure
3894     that it makes sense where you put it.
3895     </para>
3896
3897     <para>
3898     When a value is to be used as an SQL literal or identifier, it is
3899     safest to arrange for it to be quoted.  To quote the value of
3900     a variable as an SQL literal, write a colon followed by the variable
3901     name in single quotes.  To quote the value as an SQL identifier, write
3902     a colon followed by the variable name in double quotes.
3903     These constructs deal correctly with quotes and other special
3904     characters embedded within the variable value.
3905     The previous example would be more safely written this way:
3906 <programlisting>
3907 testdb=&gt; <userinput>\set foo 'my_table'</userinput>
3908 testdb=&gt; <userinput>SELECT * FROM :"foo";</userinput>
3909 </programlisting>
3910     </para>
3911
3912     <para>
3913     Variable interpolation will not be performed within quoted
3914     <acronym>SQL</acronym> literals and identifiers.  Therefore, a
3915     construction such as <literal>':foo'</> doesn't work to produce a quoted
3916     literal from a variable's value (and it would be unsafe if it did work,
3917     since it wouldn't correctly handle quotes embedded in the value).
3918     </para>
3919
3920     <para>
3921     One example use of this mechanism is to
3922     copy the contents of a file into a table column.
3923     First load the file into a variable and then interpolate the variable's
3924     value as a quoted string:
3925 <programlisting>
3926 testdb=&gt; <userinput>\set content `cat my_file.txt`</userinput>
3927 testdb=&gt; <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
3928 </programlisting>
3929     (Note that this still won't work if <filename>my_file.txt</filename> contains NUL bytes.
3930     <application>psql</application> does not support embedded NUL bytes in variable values.)
3931     </para>
3932
3933     <para>
3934     Since colons can legally appear in SQL commands, an apparent attempt
3935     at interpolation (that is, <literal>:name</literal>,
3936     <literal>:'name'</literal>, or <literal>:"name"</literal>) is not
3937     replaced unless the named variable is currently set. In any case, you
3938     can escape a colon with a backslash to protect it from substitution.
3939     </para>
3940
3941     <para>
3942     The colon syntax for variables is standard <acronym>SQL</acronym> for
3943     embedded query languages, such as <application>ECPG</application>.
3944     The colon syntaxes for array slices and type casts are
3945     <productname>PostgreSQL</productname> extensions, which can sometimes
3946     conflict with the standard usage.  The colon-quote syntax for escaping a
3947     variable's value as an SQL literal or identifier is a
3948     <application>psql</application> extension.
3949     </para>
3950
3951    </refsect3>
3952
3953    <refsect3 id="APP-PSQL-prompting">
3954     <title id="APP-PSQL-prompting-title">Prompting</title>
3955
3956     <para>
3957     The prompts <application>psql</application> issues can be customized
3958     to your preference. The three variables <varname>PROMPT1</varname>,
3959     <varname>PROMPT2</varname>, and <varname>PROMPT3</varname> contain strings
3960     and special escape sequences that describe the appearance of the
3961     prompt. Prompt 1 is the normal prompt that is issued when
3962     <application>psql</application> requests a new command. Prompt 2 is
3963     issued when more input is expected during command entry, for example
3964     because the command was not terminated with a semicolon or a quote
3965     was not closed.
3966     Prompt 3 is issued when you are running an <acronym>SQL</acronym>
3967     <command>COPY FROM STDIN</command> command and you need to type in
3968     a row value on the terminal.
3969     </para>
3970
3971     <para>
3972     The value of the selected prompt variable is printed literally,
3973     except where a percent sign (<literal>%</literal>) is encountered.
3974     Depending on the next character, certain other text is substituted
3975     instead. Defined substitutions are:
3976
3977     <variablelist>
3978       <varlistentry>
3979         <term><literal>%M</literal></term>
3980         <listitem>
3981          <para>
3982           The full host name (with domain name) of the database server,
3983           or <literal>[local]</literal> if the connection is over a Unix
3984           domain socket, or
3985           <literal>[local:<replaceable>/dir/name</replaceable>]</literal>,
3986           if the Unix domain socket is not at the compiled in default
3987           location.
3988         </para>
3989        </listitem>
3990       </varlistentry>
3991
3992       <varlistentry>
3993         <term><literal>%m</literal></term>
3994         <listitem>
3995          <para>
3996           The host name of the database server, truncated at the
3997           first dot, or <literal>[local]</literal> if the connection is
3998           over a Unix domain socket.
3999          </para>
4000         </listitem>
4001       </varlistentry>
4002
4003       <varlistentry>
4004         <term><literal>%&gt;</literal></term>
4005         <listitem><para>The port number at which the database server is listening.</para></listitem>
4006       </varlistentry>
4007
4008       <varlistentry>
4009         <term><literal>%n</literal></term>
4010         <listitem>
4011          <para>
4012           The database session user name.  (The expansion of this
4013           value might change during a database session as the result
4014           of the command <command>SET SESSION
4015           AUTHORIZATION</command>.)
4016          </para>
4017         </listitem>
4018       </varlistentry>
4019
4020       <varlistentry>
4021         <term><literal>%/</literal></term>
4022         <listitem><para>The name of the current database.</para></listitem>
4023       </varlistentry>
4024
4025       <varlistentry>
4026         <term><literal>%~</literal></term>
4027         <listitem><para>Like <literal>%/</literal>, but the output is <literal>~</literal>
4028          (tilde) if the database is your default database.</para></listitem>
4029       </varlistentry>
4030
4031       <varlistentry>
4032         <term><literal>%#</literal></term>
4033         <listitem>
4034          <para>
4035           If the session user is a database superuser, then a
4036           <literal>#</literal>, otherwise a <literal>&gt;</literal>.
4037           (The expansion of this value might change during a database
4038           session as the result of the command <command>SET SESSION
4039           AUTHORIZATION</command>.)
4040          </para>
4041         </listitem>
4042       </varlistentry>
4043
4044       <varlistentry>
4045         <term><literal>%p</literal></term>
4046         <listitem>
4047          <para>The process ID of the backend currently connected to.</para>
4048         </listitem>
4049       </varlistentry>
4050
4051       <varlistentry>
4052         <term><literal>%R</literal></term>
4053         <listitem>
4054         <para>
4055         In prompt 1 normally <literal>=</literal>,
4056         but <literal>@</literal> if the session is in an inactive branch of a
4057         conditional block, or <literal>^</literal> if in single-line mode,
4058         or <literal>!</literal> if the session is disconnected from the
4059         database (which can happen if <command>\connect</command> fails).
4060         In prompt 2 <literal>%R</literal> is replaced by a character that
4061         depends on why <application>psql</application> expects more input:
4062         <literal>-</literal> if the command simply wasn't terminated yet,
4063         but <literal>*</literal> if there is an unfinished
4064         <literal>/* ... */</literal> comment,
4065         a single quote if there is an unfinished quoted string,
4066         a double quote if there is an unfinished quoted identifier,
4067         a dollar sign if there is an unfinished dollar-quoted string,
4068         or <literal>(</literal> if there is an unmatched left parenthesis.
4069         In prompt 3 <literal>%R</literal> doesn't produce anything.
4070         </para>
4071         </listitem>
4072       </varlistentry>
4073
4074       <varlistentry>
4075         <term><literal>%x</literal></term>
4076         <listitem>
4077         <para>
4078         Transaction status: an empty string when not in a transaction
4079         block, or <literal>*</> when in a transaction block, or
4080         <literal>!</> when in a failed transaction block, or <literal>?</>
4081         when the transaction state is indeterminate (for example, because
4082         there is no connection).
4083         </para>
4084         </listitem>
4085       </varlistentry>
4086
4087       <varlistentry>
4088         <term><literal>%l</literal></term>
4089         <listitem>
4090          <para>
4091           The line number inside the current statement, starting from <literal>1</>.
4092          </para>
4093         </listitem>
4094       </varlistentry>
4095
4096       <varlistentry>
4097         <term><literal>%</literal><replaceable class="parameter">digits</replaceable></term>
4098         <listitem>
4099         <para>
4100         The character with the indicated octal code is substituted.
4101         </para>
4102         </listitem>
4103       </varlistentry>
4104
4105       <varlistentry>
4106         <term><literal>%:</literal><replaceable class="parameter">name</replaceable><literal>:</literal></term>
4107         <listitem>
4108         <para>
4109         The value of the <application>psql</application> variable
4110         <replaceable class="parameter">name</replaceable>. See the
4111         section <xref linkend="APP-PSQL-variables"
4112         endterm="APP-PSQL-variables-title"> for details.
4113         </para>
4114         </listitem>
4115       </varlistentry>
4116
4117       <varlistentry>
4118         <term><literal>%`</literal><replaceable class="parameter">command</replaceable><literal>`</literal></term>
4119         <listitem>
4120         <para>
4121         The output of <replaceable
4122         class="parameter">command</replaceable>, similar to ordinary
4123         <quote>back-tick</quote> substitution.
4124         </para>
4125         </listitem>
4126       </varlistentry>
4127
4128       <varlistentry>
4129         <term><literal>%[</literal> ... <literal>%]</literal></term>
4130         <listitem>
4131          <para>
4132          Prompts can contain terminal control characters which, for
4133          example, change the color, background, or style of the prompt
4134          text, or change the title of the terminal window. In order for
4135          the line editing features of <application>Readline</application> to work properly, these
4136          non-printing control characters must be designated as invisible
4137          by surrounding them with <literal>%[</literal> and
4138          <literal>%]</literal>. Multiple pairs of these can occur within
4139          the prompt.  For example:
4140 <programlisting>
4141 testdb=&gt; \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '
4142 </programlisting>
4143          results in a boldfaced (<literal>1;</literal>) yellow-on-black
4144          (<literal>33;40</literal>) prompt on VT100-compatible, color-capable
4145          terminals.
4146         </para>
4147         </listitem>
4148       </varlistentry>
4149
4150     </variablelist>
4151
4152     To insert a percent sign into your prompt, write
4153     <literal>%%</literal>. The default prompts are
4154     <literal>'%/%R%# '</literal> for prompts 1 and 2, and
4155     <literal>'&gt;&gt; '</literal> for prompt 3.
4156     </para>
4157
4158     <note>
4159     <para>
4160     This feature was shamelessly plagiarized from
4161     <application>tcsh</application>.
4162     </para>
4163     </note>
4164
4165    </refsect3>
4166
4167    <refsect3>
4168     <title>Command-Line Editing</title>
4169
4170     <para>
4171     <application>psql</application> supports the <application>Readline</application>
4172     library for convenient line editing and retrieval. The command
4173     history is automatically saved when <application>psql</application>
4174     exits and is reloaded when
4175     <application>psql</application> starts up. Tab-completion is also
4176     supported, although the completion logic makes no claim to be an
4177     <acronym>SQL</acronym> parser.  The queries generated by tab-completion
4178     can also interfere with other SQL commands, e.g. <literal>SET
4179     TRANSACTION ISOLATION LEVEL</>.
4180     If for some reason you do not like the tab completion, you
4181     can turn it off by putting this in a file named
4182     <filename>.inputrc</filename> in your home directory:
4183 <programlisting>
4184 $if psql
4185 set disable-completion on
4186 $endif
4187 </programlisting>
4188     (This is not a <application>psql</application> but a
4189     <application>Readline</application> feature. Read its documentation
4190     for further details.)
4191     </para>
4192    </refsect3>
4193   </refsect2>
4194  </refsect1>
4195
4196
4197  <refsect1 id="app-psql-environment">
4198   <title id="app-psql-environment-title">Environment</title>
4199
4200   <variablelist>
4201
4202    <varlistentry>
4203     <term><envar>COLUMNS</envar></term>
4204
4205     <listitem>
4206      <para>
4207       If <literal>\pset columns</> is zero, controls the
4208       width for the <literal>wrapped</> format and width for determining
4209       if wide output requires the pager or should be switched to the
4210       vertical format in expanded auto mode.
4211      </para>
4212     </listitem>
4213    </varlistentry>
4214
4215    <varlistentry>
4216     <term><envar>PGDATABASE</envar></term>
4217     <term><envar>PGHOST</envar></term>
4218     <term><envar>PGPORT</envar></term>
4219     <term><envar>PGUSER</envar></term>
4220
4221     <listitem>
4222      <para>
4223       Default connection parameters (see <xref linkend="libpq-envars">).
4224      </para>
4225     </listitem>
4226    </varlistentry>
4227
4228    <varlistentry>
4229     <term><envar>PSQL_EDITOR</envar></term>
4230     <term><envar>EDITOR</envar></term>
4231     <term><envar>VISUAL</envar></term>
4232
4233     <listitem>
4234      <para>
4235       Editor used by the <command>\e</command>, <command>\ef</command>,
4236       and <command>\ev</command> commands.
4237       These variables are examined in the order listed;
4238       the first that is set is used.
4239       If none of them is set, the default is to use <filename>vi</filename>
4240       on Unix systems or <filename>notepad.exe</filename> on Windows systems.
4241      </para>
4242     </listitem>
4243    </varlistentry>
4244
4245    <varlistentry>
4246     <term><envar>PSQL_EDITOR_LINENUMBER_ARG</envar></term>
4247
4248     <listitem>
4249      <para>
4250       When <command>\e</command>, <command>\ef</command>, or
4251       <command>\ev</command> is used
4252       with a line number argument, this variable specifies the
4253       command-line argument used to pass the starting line number to
4254       the user's editor.  For editors such as <productname>Emacs</> or
4255       <productname>vi</>, this is a plus sign.  Include a trailing
4256       space in the value of the variable if there needs to be space
4257       between the option name and the line number.  Examples:
4258 <programlisting>
4259 PSQL_EDITOR_LINENUMBER_ARG='+'
4260 PSQL_EDITOR_LINENUMBER_ARG='--line '
4261 </programlisting>
4262      </para>
4263
4264      <para>
4265       The default is <literal>+</literal> on Unix systems
4266       (corresponding to the default editor <filename>vi</filename>,
4267       and useful for many other common editors); but there is no
4268       default on Windows systems.
4269      </para>
4270     </listitem>
4271    </varlistentry>
4272
4273    <varlistentry>
4274     <term><envar>PSQL_HISTORY</envar></term>
4275
4276     <listitem>
4277      <para>
4278       Alternative location for the command history file. Tilde (<literal>~</literal>) expansion is performed.
4279      </para>
4280     </listitem>
4281    </varlistentry>
4282
4283    <varlistentry>
4284     <term><envar>PSQL_PAGER</envar></term>
4285     <term><envar>PAGER</envar></term>
4286
4287     <listitem>
4288      <para>
4289       If a query's results do not fit on the screen, they are piped
4290       through this command. Typical values are <literal>more</literal>
4291       or <literal>less</literal>.
4292       Use of the pager can be disabled by setting <envar>PSQL_PAGER</envar>
4293       or <envar>PAGER</envar> to an empty string, or by adjusting the
4294       pager-related options of the <command>\pset</command> command.
4295       These variables are examined in the order listed;
4296       the first that is set is used.
4297       If none of them is set, the default is to use <literal>more</> on most
4298       platforms, but <literal>less</> on Cygwin.
4299      </para>
4300
4301     </listitem>
4302    </varlistentry>
4303
4304    <varlistentry>
4305     <term><envar>PSQLRC</envar></term>
4306
4307     <listitem>
4308      <para>
4309       Alternative location of the user's <filename>.psqlrc</filename> file. Tilde (<literal>~</literal>) expansion is performed.
4310      </para>
4311     </listitem>
4312    </varlistentry>
4313
4314    <varlistentry>
4315     <term><envar>SHELL</envar></term>
4316
4317     <listitem>
4318      <para>
4319       Command executed by the <command>\!</command> command.
4320      </para>
4321     </listitem>
4322    </varlistentry>
4323
4324    <varlistentry>
4325     <term><envar>TMPDIR</envar></term>
4326
4327     <listitem>
4328      <para>
4329       Directory for storing temporary files.  The default is
4330       <filename>/tmp</filename>.
4331      </para>
4332     </listitem>
4333    </varlistentry>
4334   </variablelist>
4335
4336   <para>
4337    This utility, like most other <productname>PostgreSQL</> utilities,
4338    also uses the environment variables supported by <application>libpq</>
4339    (see <xref linkend="libpq-envars">).
4340   </para>
4341
4342  </refsect1>
4343
4344
4345  <refsect1>
4346   <title>Files</title>
4347
4348  <variablelist>
4349   <varlistentry>
4350    <term><filename>psqlrc</filename> and <filename>~/.psqlrc</filename></term>
4351    <listitem>
4352     <para>
4353      Unless it is passed an <option>-X</option> option,
4354      <application>psql</application> attempts to read and execute commands
4355      from the system-wide startup file (<filename>psqlrc</filename>) and then
4356      the user's personal startup file (<filename>~/.psqlrc</filename>), after
4357      connecting to the database but before accepting normal commands.
4358      These files can be used to set up the client and/or the server to taste,
4359      typically with <command>\set</command> and <command>SET</command>
4360      commands.
4361     </para>
4362     <para>
4363      The system-wide startup file is named <filename>psqlrc</filename> and is
4364      sought in the installation's <quote>system configuration</> directory,
4365      which is most reliably identified by running <literal>pg_config
4366      --sysconfdir</>.  By default this directory will be <filename>../etc/</>
4367      relative to the directory containing
4368      the <productname>PostgreSQL</productname> executables.  The name of this
4369      directory can be set explicitly via the <envar>PGSYSCONFDIR</envar>
4370      environment variable.
4371     </para>
4372     <para>
4373      The user's personal startup file is named <filename>.psqlrc</filename>
4374      and is sought in the invoking user's home directory.  On Windows, which
4375      lacks such a concept, the personal startup file is named
4376      <filename>%APPDATA%\postgresql\psqlrc.conf</filename>.
4377      The location of the user's startup file can be set explicitly via
4378      the <envar>PSQLRC</envar> environment variable.
4379     </para>
4380     <para>
4381      Both the system-wide startup file and the user's personal startup file
4382      can be made <application>psql</application>-version-specific
4383      by appending a dash and the <productname>PostgreSQL</productname>
4384      major or minor release number to the file name,
4385      for example <filename>~/.psqlrc-9.2</filename> or
4386      <filename>~/.psqlrc-9.2.5</filename>.  The most specific
4387      version-matching file will be read in preference to a
4388      non-version-specific file.
4389     </para>
4390    </listitem>
4391   </varlistentry>
4392
4393   <varlistentry>
4394    <term><filename>.psql_history</filename></term>
4395    <listitem>
4396     <para>
4397      The command-line history is stored in the file
4398      <filename>~/.psql_history</filename>, or
4399      <filename>%APPDATA%\postgresql\psql_history</filename> on Windows.
4400     </para>
4401     <para>
4402      The location of the history file can be set explicitly via
4403      the <varname>HISTFILE</varname> <application>psql</> variable or
4404      the <envar>PSQL_HISTORY</envar> environment variable.
4405     </para>
4406    </listitem>
4407   </varlistentry>
4408  </variablelist>
4409  </refsect1>
4410
4411
4412  <refsect1>
4413   <title>Notes</title>
4414
4415     <itemizedlist>
4416       <listitem>
4417       <para><application>psql</application> works best with servers of the same
4418        or an older major version.  Backslash commands are particularly likely
4419        to fail if the server is of a newer version than <application>psql</>
4420        itself.  However, backslash commands of the <literal>\d</> family should
4421        work with servers of versions back to 7.4, though not necessarily with
4422        servers newer than <application>psql</> itself.  The general
4423        functionality of running SQL commands and displaying query results
4424        should also work with servers of a newer major version, but this cannot
4425        be guaranteed in all cases.
4426       </para>
4427       <para>
4428        If you want to use <application>psql</application> to connect to several
4429        servers of different major versions, it is recommended that you use the
4430        newest version of <application>psql</application>.  Alternatively, you
4431        can keep around a copy of <application>psql</application> from each
4432        major version and be sure to use the version that matches the
4433        respective server.  But in practice, this additional complication should
4434        not be necessary.
4435       </para>
4436       </listitem>
4437
4438       <listitem>
4439       <para>
4440        Before <productname>PostgreSQL</productname> 9.6,
4441        the <option>-c</option> option implied <option>-X</option>
4442        (<option>--no-psqlrc</>); this is no longer the case.
4443       </para>
4444       </listitem>
4445
4446       <listitem>
4447       <para>
4448        Before <productname>PostgreSQL</productname> 8.4,
4449        <application>psql</application> allowed the
4450        first argument of a single-letter backslash command to start
4451        directly after the command, without intervening whitespace.
4452        Now, some whitespace is required.
4453       </para>
4454       </listitem>
4455     </itemizedlist>
4456  </refsect1>
4457
4458
4459  <refsect1>
4460   <title>Notes for Windows Users</title>
4461
4462  <para>
4463   <application>psql</application> is built as a <quote>console
4464   application</>.  Since the Windows console windows use a different
4465   encoding than the rest of the system, you must take special care
4466   when using 8-bit characters within <application>psql</application>.
4467   If <application>psql</application> detects a problematic
4468   console code page, it will warn you at startup. To change the
4469   console code page, two things are necessary:
4470
4471    <itemizedlist>
4472     <listitem>
4473      <para>
4474       Set the code page by entering <userinput>cmd.exe /c chcp
4475       1252</userinput>. (1252 is a code page that is appropriate for
4476       German; replace it with your value.) If you are using Cygwin,
4477       you can put this command in <filename>/etc/profile</filename>.
4478      </para>
4479     </listitem>
4480
4481     <listitem>
4482      <para>
4483       Set the console font to <literal>Lucida Console</>, because the
4484       raster font does not work with the ANSI code page.
4485      </para>
4486     </listitem>
4487    </itemizedlist></para>
4488
4489  </refsect1>
4490
4491
4492  <refsect1 id="APP-PSQL-examples">
4493   <title id="APP-PSQL-examples-title">Examples</title>
4494
4495   <para>
4496   The first example shows how to spread a command over several lines of
4497   input. Notice the changing prompt:
4498 <programlisting>
4499 testdb=&gt; <userinput>CREATE TABLE my_table (</userinput>
4500 testdb(&gt; <userinput> first integer not null default 0,</userinput>
4501 testdb(&gt; <userinput> second text)</userinput>
4502 testdb-&gt; <userinput>;</userinput>
4503 CREATE TABLE
4504 </programlisting>
4505   Now look at the table definition again:
4506 <programlisting>
4507 testdb=&gt; <userinput>\d my_table</userinput>
4508               Table "public.my_table"
4509  Column |  Type   | Collation | Nullable | Default
4510 --------+---------+-----------+----------+---------
4511  first  | integer |           | not null | 0
4512  second | text    |           |          | 
4513 </programlisting>
4514   Now we change the prompt to something more interesting:
4515 <programlisting>
4516 testdb=&gt; <userinput>\set PROMPT1 '%n@%m %~%R%# '</userinput>
4517 peter@localhost testdb=&gt;
4518 </programlisting>
4519   Let's assume you have filled the table with data and want to take a
4520   look at it:
4521 <programlisting>
4522 peter@localhost testdb=&gt; SELECT * FROM my_table;
4523  first | second
4524 -------+--------
4525      1 | one
4526      2 | two
4527      3 | three
4528      4 | four
4529 (4 rows)
4530
4531 </programlisting>
4532   You can display tables in different ways by using the
4533   <command>\pset</command> command:
4534 <programlisting>
4535 peter@localhost testdb=&gt; <userinput>\pset border 2</userinput>
4536 Border style is 2.
4537 peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table;</userinput>
4538 +-------+--------+
4539 | first | second |
4540 +-------+--------+
4541 |     1 | one    |
4542 |     2 | two    |
4543 |     3 | three  |
4544 |     4 | four   |
4545 +-------+--------+
4546 (4 rows)
4547
4548 peter@localhost testdb=&gt; <userinput>\pset border 0</userinput>
4549 Border style is 0.
4550 peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table;</userinput>
4551 first second
4552 ----- ------
4553     1 one
4554     2 two
4555     3 three
4556     4 four
4557 (4 rows)
4558
4559 peter@localhost testdb=&gt; <userinput>\pset border 1</userinput>
4560 Border style is 1.
4561 peter@localhost testdb=&gt; <userinput>\pset format unaligned</userinput>
4562 Output format is unaligned.
4563 peter@localhost testdb=&gt; <userinput>\pset fieldsep ","</userinput>
4564 Field separator is ",".
4565 peter@localhost testdb=&gt; <userinput>\pset tuples_only</userinput>
4566 Showing only tuples.
4567 peter@localhost testdb=&gt; <userinput>SELECT second, first FROM my_table;</userinput>
4568 one,1
4569 two,2
4570 three,3
4571 four,4
4572 </programlisting>
4573   Alternatively, use the short commands:
4574 <programlisting>
4575 peter@localhost testdb=&gt; <userinput>\a \t \x</userinput>
4576 Output format is aligned.
4577 Tuples only is off.
4578 Expanded display is on.
4579 peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table;</userinput>
4580 -[ RECORD 1 ]-
4581 first  | 1
4582 second | one
4583 -[ RECORD 2 ]-
4584 first  | 2
4585 second | two
4586 -[ RECORD 3 ]-
4587 first  | 3
4588 second | three
4589 -[ RECORD 4 ]-
4590 first  | 4
4591 second | four
4592 </programlisting></para>
4593
4594 <para>
4595   When suitable, query results can be shown in a crosstab representation
4596   with the <command>\crosstabview</command> command:
4597 <programlisting>
4598 testdb=&gt; <userinput>SELECT first, second, first &gt; 2 AS gt2 FROM my_table;</userinput>
4599  first | second | gt2 
4600 -------+--------+-----
4601      1 | one    | f
4602      2 | two    | f
4603      3 | three  | t
4604      4 | four   | t
4605 (4 rows)
4606
4607 testdb=&gt; <userinput>\crosstabview first second</userinput>
4608  first | one | two | three | four 
4609 -------+-----+-----+-------+------
4610      1 | f   |     |       | 
4611      2 |     | f   |       | 
4612      3 |     |     | t     | 
4613      4 |     |     |       | t
4614 (4 rows)
4615 </programlisting>
4616
4617 This second example shows a multiplication table with rows sorted in reverse
4618 numerical order and columns with an independent, ascending numerical order.
4619 <programlisting>
4620 testdb=&gt; <userinput>SELECT t1.first as "A", t2.first+100 AS "B", t1.first*(t2.first+100) as "AxB",</userinput>
4621 testdb(&gt; <userinput>row_number() over(order by t2.first) AS ord</userinput>
4622 testdb(&gt; <userinput>FROM my_table t1 CROSS JOIN my_table t2 ORDER BY 1 DESC</userinput>
4623 testdb(&gt; <userinput>\crosstabview "A" "B" "AxB" ord</userinput>
4624  A | 101 | 102 | 103 | 104 
4625 ---+-----+-----+-----+-----
4626  4 | 404 | 408 | 412 | 416
4627  3 | 303 | 306 | 309 | 312
4628  2 | 202 | 204 | 206 | 208
4629  1 | 101 | 102 | 103 | 104
4630 (4 rows)
4631 </programlisting>
4632
4633 </para>
4634
4635  </refsect1>
4636
4637 </refentry>