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