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