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