OSDN Git Service

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