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