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