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