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