]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/psql-ref.sgml
TODO marked as done:
[postgresql] / doc / src / sgml / ref / psql-ref.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.81 2002/12/12 21:02:19 momjian Exp $
3 PostgreSQL documentation
4 -->
5
6 <refentry id="APP-PSQL">
7   <refmeta>
8     <refentrytitle id="app-psql-title"><application>psql</application></refentrytitle>
9     <manvolnum>1</manvolnum>
10     <refmiscinfo>Application</refmiscinfo>
11   </refmeta>
12
13   <refnamediv>
14     <refname><application>psql</application></refname>
15     <refpurpose>
16       <productname>PostgreSQL</productname> interactive terminal
17     </refpurpose>
18   </refnamediv>
19
20  <refsynopsisdiv>
21   <cmdsynopsis>
22    <command>psql</command>
23    <arg><replaceable class="parameter">options</replaceable></arg>
24    <arg><replaceable class="parameter">dbname</replaceable>
25    <arg><replaceable class="parameter">user</replaceable></arg></arg>
26   </cmdsynopsis>
27  </refsynopsisdiv>
28
29  <refsect1>
30   <title>Description</title>
31
32     <para>
33      <application>psql</application> is a terminal-based front-end to
34      <productname>PostgreSQL</productname>. It enables you to type in
35      queries interactively, issue them to
36      <productname>PostgreSQL</productname>, and see the query results.
37      Alternatively, input can be from a file. In addition, it provides a
38      number of meta-commands and various shell-like features to
39      facilitate writing scripts and automating a wide variety of tasks.
40     </para>
41  </refsect1>
42
43  <refsect1 id="R1-APP-PSQL-3">
44   <title>Options</title>
45
46   <variablelist>
47     <varlistentry>
48       <term><option>-a</></term>
49       <term><option>--echo-all</></term>
50       <listitem>
51       <para>
52       Print all the lines to the screen as they are read. This is more
53       useful for script processing rather than interactive mode. This is
54       equivalent to setting the variable <varname>ECHO</varname> to
55       <literal>all</literal>.
56       </para>
57       </listitem>
58     </varlistentry>
59
60     <varlistentry>
61       <term><option>-A</></term>
62       <term><option>--no-align</></term>
63       <listitem>
64       <para>
65       Switches to unaligned output mode. (The default output mode is
66       otherwise aligned.)
67       </para>
68       </listitem>
69     </varlistentry>
70         
71     <varlistentry>
72       <term><option>-c <replaceable class="parameter">query</replaceable></></term>
73       <term><option>--command <replaceable class="parameter">query</replaceable></></term>
74       <listitem>
75       <para>
76       Specifies that <application>psql</application> is to execute one
77       query string, <replaceable class="parameter">query</replaceable>,
78       and then exit. This is useful in shell scripts.
79       </para>
80       <para>
81       <replaceable class="parameter">query</replaceable> must be either
82       a query string that is completely parsable by the backend (i.e.,
83       it contains no <application>psql</application> specific features),
84       or it is a single backslash command. Thus you cannot mix
85       <acronym>SQL</acronym> and <application>psql</application>
86       meta-commands. To achieve that, you could pipe the string into
87       <application>psql</application>, like this: <literal>echo "\x \\
88       select * from foo;" | psql</literal>.
89       </para>
90       </listitem>
91     </varlistentry>
92
93     <varlistentry>
94       <term><option>-d <replaceable class="parameter">dbname</replaceable></></term>
95       <term><option>--dbname <replaceable class="parameter">dbname</replaceable></></term>
96       <listitem>
97       <para>
98       Specifies the name of the database to connect to. This is
99       equivalent to specifying <replaceable
100       class="parameter">dbname</replaceable> as the first non-option
101       argument on the command line.
102       </para>
103       </listitem>
104     </varlistentry>
105
106     <varlistentry>
107       <term><option>-e</></term>
108       <term><option>--echo-queries</></term>
109       <listitem>
110       <para>
111       Show all queries that are sent to the backend. This is equivalent
112       to setting the variable <varname>ECHO</varname> to
113       <literal>queries</literal>.
114       </para>
115       </listitem>
116     </varlistentry>
117
118     <varlistentry>
119       <term><option>-E</></term>
120       <term><option>--echo-hidden</></term>
121       <listitem>
122       <para>
123       Echoes the actual queries generated by \d and other backslash
124       commands. You can use this if you wish to include similar
125       functionality into your own programs. This is equivalent to
126       setting the variable <varname>ECHO_HIDDEN</varname> from within
127       <application>psql</application>.
128       </para>
129       </listitem>
130     </varlistentry>
131
132     <varlistentry>
133       <term><option>-f <replaceable class="parameter">filename</replaceable></></term>
134       <term><option>--file <replaceable class="parameter">filename</replaceable></></term>
135       <listitem>
136       <para>
137       Use the file <replaceable class="parameter">filename</replaceable>
138       as the source of queries instead of reading queries interactively.
139       After the file is processed, <application>psql</application>
140       terminates. This is in many ways equivalent to the internal
141       command <command>\i</command>.
142       </para>
143
144       <para>
145        If <replaceable>filename</replaceable> is <literal>-</literal>
146        (hyphen), then standard input is read.
147       </para>
148
149       <para>
150       Using this option is subtly different from writing <literal>psql
151       &lt; <replaceable
152       class="parameter">filename</replaceable></literal>. In general,
153       both will do what you expect, but using <literal>-f</literal>
154       enables some nice features such as error messages with line
155       numbers. There is also a slight chance that using this option will
156       reduce the start-up overhead. On the other hand, the variant using
157       the shell's input redirection is (in theory) guaranteed to yield
158       exactly the same output that you would have gotten had you entered
159       everything by hand.
160       </para>
161       </listitem>
162     </varlistentry>
163
164     <varlistentry>
165       <term><option>-F <replaceable class="parameter">separator</replaceable></></term>
166       <term><option>--field-separator <replaceable class="parameter">separator</replaceable></></term>
167       <listitem>
168       <para>
169       Use <replaceable class="parameter">separator</replaceable> as the
170       field separator. This is equivalent to <command>\pset
171       fieldsep</command> or <command>\f</command>.
172       </para>
173       </listitem>
174     </varlistentry>
175
176     <varlistentry>
177       <term><option>-h <replaceable class="parameter">hostname</replaceable></></term>
178       <term><option>--host <replaceable class="parameter">hostname</replaceable></></term>
179       <listitem>
180       <para>
181       Specifies the host name of the machine on which the
182       <application>postmaster</application> is running. If host begins
183       with a slash, it is used as the directory for the Unix-domain
184       socket.
185       </para>
186       </listitem>
187     </varlistentry>
188
189     <varlistentry>
190       <term><option>-H</></term>
191       <term><option>--html</></term>
192       <listitem>
193       <para>
194       Turns on <acronym>HTML</acronym> tabular output. This is
195       equivalent to <literal>\pset format html</literal> or the
196       <command>\H</command> command.
197       </para>
198       </listitem>
199     </varlistentry>
200  
201     <varlistentry>
202       <term><option>-l</></term>
203       <term><option>--list</></term>
204       <listitem>
205       <para>
206       Lists all available databases, then exits. Other non-connection
207       options are ignored. This is similar to the internal command
208       <command>\list</command>.
209       </para>
210       </listitem>
211     </varlistentry>
212
213     <varlistentry>
214       <term><option>-o <replaceable class="parameter">filename</replaceable></></term>
215       <term><option>--output <replaceable class="parameter">filename</replaceable></></term>
216       <listitem>
217       <para>
218       Put all query output into file <replaceable
219       class="parameter">filename</replaceable>. This is equivalent to
220       the command <command>\o</command>.
221       </para>
222       </listitem>
223     </varlistentry>
224
225     <varlistentry>
226       <term><option>-p <replaceable class="parameter">port</replaceable></></term>
227       <term><option>--port <replaceable class="parameter">port</replaceable></></term>
228       <listitem>
229       <para>
230       Specifies the TCP/IP port or, by omission, the local Unix domain
231       socket file extension on which the
232       <application>postmaster</application> is listening for
233       connections. Defaults to the value of the <envar>PGPORT</envar>
234       environment variable or, if not set, to the port specified at
235       compile time, usually 5432.
236       </para>
237       </listitem>
238     </varlistentry>
239
240     <varlistentry>
241       <term><option>-P <replaceable class="parameter">assignment</replaceable></></term>
242       <term><option>--pset <replaceable class="parameter">assignment</replaceable></></term>
243       <listitem>
244       <para>
245       Allows you to specify printing options in the style of
246       <command>\pset</command> on the command line. Note that here you
247       have to separate name and value with an equal sign instead of a
248       space. Thus to set the output format to LaTeX, you could write
249       <literal>-P format=latex</literal>.
250       </para>
251       </listitem>
252     </varlistentry>
253
254     <varlistentry>
255       <term><option>-q</></term>
256       <term><option>--quiet</></term>
257       <listitem>
258       <para>
259       Specifies that <application>psql</application> should do its work
260       quietly. By default, it prints welcome messages and various
261       informational output. If this option is used, none of this
262       happens. This is useful with the <option>-c</option> option.
263       Within <application>psql</application> you can also set the
264       <varname>QUIET</varname> variable to achieve the same effect.
265       </para>
266       </listitem>
267     </varlistentry>
268
269     <varlistentry>
270       <term><option>-R <replaceable class="parameter">separator</replaceable></></term>
271       <term><option>--record-separator <replaceable class="parameter">separator</replaceable></></term>
272       <listitem>
273       <para>
274       Use <replaceable class="parameter">separator</replaceable> as the
275       record separator. This is equivalent to the <command>\pset
276       recordsep</command> command.
277       </para>
278       </listitem>
279     </varlistentry>
280  
281     <varlistentry>
282       <term><option>-s</></term>
283       <term><option>--single-step</></term>
284       <listitem>
285       <para>
286       Run in single-step mode. That means the user is prompted before
287       each query is sent to the backend, with the option to cancel
288       execution as well. Use this to debug scripts.
289       </para>
290       </listitem>
291     </varlistentry>
292
293     <varlistentry>
294       <term><option>-S</></term>
295       <term><option>--single-line</></term>
296       <listitem>
297       <para>
298       Runs in single-line mode where a newline terminates a query, as a
299       semicolon does.
300       </para>
301
302       <note>
303       <para>
304       This mode is provided for those who insist on it, but you are not
305       necessarily encouraged to use it. In particular, if you mix
306       <acronym>SQL</acronym> and meta-commands on a line the order of
307       execution might not always be clear to the inexperienced user.
308       </para>
309       </note>
310       </listitem>
311     </varlistentry>
312
313     <varlistentry>
314       <term><option>-t</></term>
315       <term><option>--tuples-only</></term>
316       <listitem>
317       <para>
318       Turn off printing of column names and result row count footers,
319       etc. It is completely equivalent to the <command>\t</command>
320       meta-command.
321       </para>
322       </listitem>
323     </varlistentry>
324
325     <varlistentry>
326       <term><option>-T <replaceable class="parameter">table_options</replaceable></></term>
327       <term><option>--table-attr <replaceable class="parameter">table_options</replaceable></></term>
328       <listitem>
329       <para>
330       Allows you to specify options to be placed within the
331       <acronym>HTML</acronym> <sgmltag>table</sgmltag> tag. See
332       <command>\pset</command> for details.
333       </para>
334       </listitem>
335     </varlistentry>
336  
337     <varlistentry>
338       <term><option>-u</></term>
339       <listitem>
340       <para>
341       Makes <application>psql</application> prompt for the user name and
342       password before connecting to the database.
343       </para>
344
345       <para>
346       This option is deprecated, as it is conceptually flawed.
347       (Prompting for a non-default user name and prompting for a
348       password because the backend requires it are really two different
349       things.) You are encouraged to look at the <option>-U</option> and
350       <option>-W</option> options instead.
351       </para>
352       </listitem>
353     </varlistentry>
354
355     <varlistentry>
356       <term><option>-U <replaceable class="parameter">username</replaceable></></term>
357       <term><option>--username <replaceable class="parameter">username</replaceable></></term>
358       <listitem>
359       <para>
360       Connects to the database as the user <replaceable
361       class="parameter">username</replaceable> instead of the default.
362       (You must have permission to do so, of course.)
363       </para> 
364       </listitem>
365     </varlistentry>
366
367     <varlistentry>
368       <term><option>-v <replaceable class="parameter">assignment</replaceable></></term>
369       <term><option>--set <replaceable class="parameter">assignment</replaceable></></term>
370       <term><option>--variable <replaceable class="parameter">assignment</replaceable></></term>
371       <listitem>
372       <para>
373       Performs a variable assignment, like the <command>\set</command>
374       internal command. Note that you must separate name and value, if
375       any, by an equal sign on the command line. To unset a variable,
376       leave off the equal sign. To just set a variable without a value,
377       use the equal sign but leave off the value. These assignments are
378       done during a very early stage of start-up, so variables reserved
379       for internal purposes might get overwritten later.
380       </para>
381       </listitem>
382     </varlistentry>
383
384     <varlistentry>
385       <term><option>-V</></term>
386       <term><option>--version</></term>
387       <listitem>
388       <para>
389       Shows the <application>psql</application> version.
390       </para>
391       </listitem>
392     </varlistentry>
393
394     <varlistentry>
395       <term><option>-W</></term>
396       <term><option>--password</></term>
397       <listitem>
398       <para>
399       Requests that <application>psql</application> should prompt for a
400       password before connecting to a database. This will remain set for
401       the entire session, even if you change the database connection
402       with the meta-command <command>\connect</command>.
403       </para>
404
405       <para>
406       In the current version, <application>psql</application>
407       automatically issues a password prompt whenever the backend
408       requests password authentication. Because this is currently based
409       on a hack, the automatic recognition might mysteriously fail,
410       hence this option to force a prompt. If no password prompt is
411       issued and the backend requires password authentication the
412       connection attempt will fail.
413       </para>
414       </listitem>
415     </varlistentry>
416
417     <varlistentry>
418       <term><option>-x</></term>
419       <term><option>--expanded</></term>
420       <listitem>
421       <para>
422       Turns on extended row format mode. This is equivalent to the
423       command <command>\x</command>.
424       </para>
425       </listitem>
426     </varlistentry>
427
428     <varlistentry>
429       <term><option>-X,</></term>
430       <term><option>--no-psqlrc</></term>
431       <listitem>
432       <para>
433       Do not read the start-up file <filename>~/.psqlrc</filename>.
434       </para>
435       </listitem>
436     </varlistentry>
437
438     <varlistentry>
439       <term><option>-?</></term>
440       <term><option>--help</></term>
441       <listitem>
442       <para>
443       Shows help about <application>psql</application> command line
444       arguments.
445       </para>
446       </listitem>
447     </varlistentry>
448   </variablelist>
449
450   <para>
451    Long options are not available on all platforms.
452   </para>
453  </refsect1>
454
455
456  <refsect1>
457   <title>Exit Status</title>
458
459   <para>
460    <application>psql</application> returns 0 to the shell if it
461    finished normally, 1 if a fatal error of its own (out of memory,
462    file not found) occurs, 2 if the connection to the backend went bad
463    and the session is not interactive, and 3 if an error occurred in a
464    script and the variable <varname>ON_ERROR_STOP</varname> was set.
465   </para>
466  </refsect1>
467
468
469  <refsect1>
470   <title>Usage</title>
471
472   <refsect2 id="R2-APP-PSQL-connecting">
473     <title>Connecting To A Database</title>
474
475     <para>
476     <application>psql</application> is a regular
477     <productname>PostgreSQL</productname> client application. In order
478     to connect to a database you need to know the name of your target
479     database, the host name and port number of the server and what user
480     name you want to connect as. <application>psql</application> can be
481     told about those parameters via command line options, namely
482     <option>-d</option>, <option>-h</option>, <option>-p</option>, and
483     <option>-U</option> respectively. If an argument is found that does
484     not belong to any option it will be interpreted as the database name
485     (or the user name, if the database name is also given). Not all
486     these options are required, defaults do apply. If you omit the host
487     name, <application>psql</> will connect via a Unix domain socket to a server on the
488     local host. The default port number is compile-time determined.
489     Since the database server uses the same default, you will not have
490     to specify the port in most cases. The default user name is your
491     Unix user name, as is the default database name. Note that you can't
492     just connect to any database under any user name. Your database
493     administrator should have informed you about your access rights. To
494     save you some typing you can also set the environment variables
495     <envar>PGDATABASE</envar>, <envar>PGHOST</envar>,
496     <envar>PGPORT</envar> and <envar>PGUSER</envar> to appropriate
497     values.
498     </para>
499
500     <para>
501     If the connection could not be made for any reason (e.g., insufficient
502     privileges, postmaster is not running on the server, etc.),
503     <application>psql</application> will return an error and terminate.
504     </para>
505   </refsect2>
506
507   <refsect2 id="R2-APP-PSQL-4">
508     <title>Entering Queries</title>
509
510     <para>
511     In normal operation, <application>psql</application> provides a
512     prompt with the name of the database to which
513     <application>psql</application> is currently connected, followed by
514     the string <literal>=&gt;</literal>. For example,
515 <programlisting>
516 $ <userinput>psql testdb</userinput>
517 Welcome to psql &version;, the PostgreSQL interactive terminal.
518
519 Type:  \copyright for distribution terms
520        \h for help with SQL commands
521        \? for help on internal slash commands
522        \g or terminate with semicolon to execute query
523        \q to quit
524
525 testdb=>
526 </programlisting>
527     </para>
528
529     <para>
530     At the prompt, the user may type in <acronym>SQL</acronym> queries.
531     Ordinarily, input lines are sent to the backend when a
532     query-terminating semicolon is reached. An end of line does not
533     terminate a query! Thus queries can be spread over several lines for
534     clarity. If the query was sent and without error, the query results
535     are displayed on the screen.
536     </para>
537
538     <para>
539     Whenever a query is executed, <application>psql</application> also polls
540     for asynchronous notification events generated by
541     <xref linkend="SQL-LISTEN" endterm="SQL-LISTEN-title"> and
542     <xref linkend="SQL-NOTIFY" endterm="SQL-NOTIFY-title">.
543     </para>
544   </refsect2>
545
546   <refsect2>
547     <title>Meta-Commands</title>
548
549     <para>
550     Anything you enter in <application>psql</application> that begins
551     with an unquoted backslash is a <application>psql</application>
552     meta-command that is processed by <application>psql</application>
553     itself. These commands are what makes
554     <application>psql</application> interesting for administration or
555     scripting. Meta-commands are more commonly called slash or backslash
556     commands.
557     </para>
558
559     <para>
560     The format of a <application>psql</application> command is the backslash, 
561     followed immediately by a command verb, then any arguments. The arguments
562     are separated from the command verb and each other by any number of 
563     whitespace characters.
564     </para>
565
566     <para>
567     To include whitespace into an argument you may quote it with a
568     single quote. To include a single quote into such an argument,
569     precede it by a backslash. Anything contained in single quotes is
570     furthermore subject to C-like substitutions for
571     <literal>\n</literal> (new line), <literal>\t</literal> (tab),
572     <literal>\</literal><replaceable>digits</replaceable>,
573     <literal>\0</literal><replaceable>digits</replaceable>, and
574     <literal>\0x</literal><replaceable>digits</replaceable> (the
575     character with the given decimal, octal, or hexadecimal code).
576     </para>
577
578     <para>
579     If an unquoted argument begins with a colon (<literal>:</literal>),
580     it is taken as a <application>psql</> variable and the value of the
581     variable is used as the argument instead.
582     </para>
583
584     <para>
585     Arguments that are enclosed in backquotes (<literal>`</literal>)
586     are taken as a command line that is passed to the shell. The
587     output of the command (with any trailing newline removed) is taken
588     as the argument value. The above escape sequences also apply in
589     backquotes.
590     </para>
591
592     <para>
593     Some commands take an <acronym>SQL</acronym> identifier
594     (such as a table name) as argument. These arguments follow the
595     syntax rules of <acronym>SQL</acronym> regarding double quotes: an
596     identifier without double quotes is coerced to lower-case, while
597     whitespace within double quotes is included in the argument.
598     </para>
599
600     <para>
601     Parsing for arguments stops when another unquoted backslash occurs.
602     This is taken as the beginning of a new meta-command. The special
603     sequence <literal>\\</literal> (two backslashes) marks the end of
604     arguments and continues parsing <acronym>SQL</acronym> queries, if
605     any. That way <acronym>SQL</acronym> and
606     <application>psql</application> commands can be freely mixed on a
607     line. But in any case, the arguments of a meta-command cannot
608     continue beyond the end of the line.
609     </para>
610
611     <para>
612     The following meta-commands are defined:
613
614     <variablelist>
615       <varlistentry>
616         <term><literal>\a</literal></term>
617         <listitem>
618         <para>
619         If the current table output format is unaligned, switch to aligned.
620         If it is not unaligned, set it to unaligned. This command is
621         kept for backwards compatibility. See <command>\pset</command> for a
622         general solution.
623         </para>
624         </listitem>
625       </varlistentry>
626
627       <varlistentry>
628        <term><literal>\cd</literal> <optional><replaceable>directory</replaceable></optional></term>
629        <listitem>
630         <para>
631          Change the current working directory to
632          <replaceable>directory</replaceable>. Without argument, change
633          to the current user's home directory.
634         </para>
635
636         <tip>
637          <para>
638           To print your current working directory, use <literal>\!pwd</literal>.
639          </para>
640         </tip>
641        </listitem>
642       </varlistentry>
643
644       <varlistentry>
645         <term><literal>\C</literal> [ <replaceable class="parameter">title</replaceable> ]</term>
646         <listitem>
647         <para>
648         Set the title of any tables being printed as the result of a
649         query or unset any such title. This command is equivalent to
650         <literal>\pset title <replaceable
651         class="parameter">title</replaceable></literal>. (The name of
652         this command derives from <quote>caption</quote>, as it was
653         previously only used to set the caption in an
654         <acronym>HTML</acronym> table.)
655         </para>
656         </listitem>
657       </varlistentry>
658
659       <varlistentry>
660         <term><literal>\connect</literal> (or <literal>\c</literal>) [ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] ]</term>
661         <listitem>
662         <para>
663         Establishes a connection to a new database and/or under a user
664         name. The previous connection is closed. If <replaceable
665         class="parameter">dbname</replaceable> is <literal>-</literal>
666         the current database name is assumed.
667         </para>
668
669         <para>
670         If <replaceable class="parameter">username</replaceable> is
671         omitted the current user name is assumed. </para>
672
673         <para>
674         As a special rule, <command>\connect</command> without any
675         arguments will connect to the default database as the default
676         user (as you would have gotten by starting
677         <application>psql</application> without any arguments).
678         </para>
679
680         <para>
681         If the connection attempt failed (wrong user name, access
682         denied, etc.), the previous connection will be kept if and only
683         if <application>psql</application> is in interactive mode. When
684         executing a non-interactive script, processing will immediately
685         stop with an error. This distinction was chosen as a user
686         convenience against typos on the one hand, and a safety
687         mechanism that scripts are not accidentally acting on the wrong
688         database on the other hand.
689         </para>
690         </listitem>
691       </varlistentry>
692
693       <varlistentry>
694         <term><literal>\copy <replaceable class="parameter">table</replaceable>
695         [ ( <replaceable class="parameter">column_list</replaceable> ) ]
696         { <literal>from</literal> | <literal>to</literal> }
697         <replaceable class="parameter">filename</replaceable> | stdin | stdout
698         [ <literal>with</literal> ] 
699             [ <literal>oids</literal> ] 
700             [ <literal>delimiter [as] </literal> '<replaceable class="parameter">character</replaceable>' ]
701             [ <literal>null [as] </literal> '<replaceable class="parameter">string</replaceable>' ]</literal>
702         </term>
703
704         <listitem>
705         <para>
706         Performs a frontend (client) copy. This is an operation that
707         runs an <acronym>SQL</acronym> <xref linkend="SQL-COPY"
708         endterm="SQL-COPY-title"> command, but instead of the backend's
709         reading or writing the specified file,
710         <application>psql</application> reads or writes the file and
711         routes the data between the backend and the local file system.
712         This means that file accessibility and privileges are those
713         of the local user, not the server, and no SQL superuser
714         privileges are required.
715         </para>
716
717         <para>
718         The syntax of the command is similar to that of the
719         <acronym>SQL</acronym> <command>COPY</command> command (see its
720         description for the details). Note that, because of this,
721         special parsing rules apply to the <command>\copy</command>
722         command. In particular, the variable substitution rules and
723         backslash escapes do not apply.
724         </para>
725
726         <tip>
727         <para>
728         This operation is not as efficient as the <acronym>SQL</acronym>
729         <command>COPY</command> command because all data must pass
730         through the client/server IP or socket connection. For large
731         amounts of data the other technique may be preferable.
732         </para>
733         </tip>
734
735         <note>
736         <para>
737         Note the difference in interpretation of
738         <literal>stdin</literal> and <literal>stdout</literal> between
739         frontend and backend copies: in a frontend copy these always
740         refer to <application>psql</application>'s input and output
741         stream. On a backend copy <literal>stdin</literal> comes from
742         wherever the <command>COPY</command> itself came from (for
743         example, a script run with the <option>-f</option> option), and
744         <literal>stdout</literal> refers to the query output stream (see
745         <command>\o</command> meta-command below).
746         </para>
747         </note>
748         </listitem>
749       </varlistentry>
750
751       <varlistentry>
752         <term><literal>\copyright</literal></term>
753         <listitem>
754         <para>
755         Shows the copyright and distribution terms of
756         <application>PostgreSQL</application>.
757         </para>
758         </listitem>
759       </varlistentry>
760
761       <varlistentry>
762         <term><literal>\d</literal> [ <replaceable class="parameter">pattern</replaceable> ]</term>
763
764         <listitem>
765         <para>
766         For each relation (table, view, index, or sequence) matching the
767         <replaceable class="parameter">pattern</replaceable>, show all
768         columns, their types, and any special
769         attributes such as <literal>NOT NULL</literal> or defaults, if
770         any. Associated indexes, constraints, rules, and triggers are
771         also shown, as is the view definition if the relation is a view.
772         (<quote>Matching the pattern</> is defined below.)
773         </para>
774
775         <para>
776         The command form <literal>\d+</literal> is identical, but any
777         comments associated with the table columns are shown as well.
778         </para>
779
780         <note>
781         <para>
782         If <command>\d</command> is used without a
783         <replaceable class="parameter">pattern</replaceable> argument, it is
784         equivalent to <command>\dtvs</command> which will show a list of
785         all tables, views, and sequences. This is purely a convenience
786         measure.
787         </para>
788         </note>
789         </listitem>
790       </varlistentry>
791
792       <varlistentry>
793         <term><literal>\da</literal> [ <replaceable class="parameter">pattern</replaceable> ]</term>
794
795         <listitem>
796         <para>
797         Lists all available aggregate functions, together with the data
798         type they operate on. If <replaceable
799         class="parameter">pattern</replaceable> (a regular expression)
800         is specified, only matching aggregates are shown.
801         </para>
802         </listitem>
803       </varlistentry>
804
805       <varlistentry>
806         <term><literal>\dd</literal> [ <replaceable class="parameter">pattern</replaceable> ]</term>
807         <listitem>
808         <para>
809         Shows the descriptions of objects matching the <replaceable
810         class="parameter">pattern</replaceable>, or of all visible objects if
811         no argument is given.  But in either case, only objects that have
812         a description are listed.
813         (<quote>Object</quote> covers aggregates, functions, operators,
814         types, relations (tables, views, indexes, sequences, large
815         objects), rules, and triggers.) For example:
816 <programlisting>
817 => <userinput>\dd version</userinput>
818                      Object descriptions
819    Schema   |  Name   |  Object  |        Description
820 ------------+---------+----------+---------------------------
821  pg_catalog | version | function | PostgreSQL version string
822 (1 row)
823 </programlisting>
824         </para>
825
826         <para>
827         Descriptions for objects can be created with the
828         <command>COMMENT ON</command> <acronym>SQL</acronym> command.
829         </para>
830
831         <note>
832         <para>
833         <productname>PostgreSQL</productname> stores the object
834         descriptions in the <structname>pg_description</> system table.
835         </para>
836         </note>
837
838         </listitem>
839       </varlistentry>
840
841
842       <varlistentry>
843         <term><literal>\dD</literal> [ <replaceable class="parameter">pattern</replaceable> ]</term>
844         <listitem>
845         <para>
846         Lists all available domains (derived types). If <replaceable
847         class="parameter">pattern</replaceable>
848         is specified, only matching domains are shown.
849         </para>
850         </listitem>
851       </varlistentry>
852
853
854       <varlistentry>
855         <term><literal>\dc</literal> [ <replaceable class="parameter">pattern</replaceable> ]</term>
856         <listitem>
857         <para>
858         Lists all available conversions (between encodings). If <replaceable
859         class="parameter">pattern</replaceable>
860         is specified, only matching conversions are shown.
861         </para>
862         </listitem>
863       </varlistentry>
864
865
866       <varlistentry>
867         <term><literal>\dC</literal></term>
868         <listitem>
869         <para>
870         Lists all available type casts.  Casts can be explicit, explicit and assignment
871         or implicit, and are used to change a variable from one type to another.
872         </para>
873         </listitem>
874       </varlistentry>
875
876
877       <varlistentry>
878         <term><literal>\df [ <replaceable class="parameter">pattern</replaceable> ]</literal></term>
879
880         <listitem>
881         <para>
882         Lists available functions, together with their argument and
883         return types. If <replaceable
884         class="parameter">pattern</replaceable>
885         is specified, only matching functions are shown. If the form
886         <literal>\df+</literal> is used, additional information about
887         each function, including language and description, is shown.
888         </para>
889
890         <note>
891         <para>
892         To reduce clutter, <literal>\df</> does not show data type I/O
893         functions.  This is implemented by ignoring functions that accept
894         or return type <type>cstring</>.
895         </para>
896         </note>
897
898         </listitem>
899       </varlistentry>
900
901
902       <varlistentry>
903         <term><literal>\distvS [ <replaceable class="parameter">pattern</replaceable> ]</literal></term>
904
905         <listitem>
906         <para>
907         This is not the actual command name: the letters i, s, t, v, S
908         stand for index, sequence, table, view, and system table,
909         respectively. You can specify any or all of these letters, in any
910         order, to obtain a listing of all the matching objects.  The letter
911         S restricts the listing to system objects; without S, only non-system
912         objects are shown.
913         If <quote>+</quote> is appended to the command name, each object is
914         listed with its associated description, if any.
915         </para>
916
917         <para>
918         If a <replaceable class="parameter">pattern</replaceable> is
919         specified, only objects whose name matches the pattern are listed.
920         </para>
921         </listitem>
922       </varlistentry>
923
924
925       <varlistentry>
926         <term><literal>\dl</literal></term>
927         <listitem>
928         <para>
929         This is an alias for <command>\lo_list</command>, which shows a
930         list of large objects.
931         </para>
932         </listitem>
933       </varlistentry>
934
935
936       <varlistentry>
937         <term><literal>\do [ <replaceable class="parameter">pattern</replaceable> ]</literal></term>
938         <listitem>
939         <para>
940         Lists available operators with their operand and return types.
941         If a <replaceable class="parameter">pattern</replaceable> is
942         specified, only operators whose name matches the pattern are listed.
943         </para>
944         </listitem>
945       </varlistentry>
946
947
948       <varlistentry>
949         <term><literal>\dp</literal> [ <replaceable class="parameter">pattern</replaceable> ]</term>
950         <listitem>
951         <para>
952         Produces a list of all available tables with their
953         associated access permissions.
954         If a <replaceable class="parameter">pattern</replaceable> is
955         specified, only tables whose name matches the pattern are listed.
956         </para>
957
958         <para>
959         The commands <xref linkend="SQL-GRANT"> and
960         <xref linkend="SQL-REVOKE">
961         are used to set access permissions.  See <xref linkend="SQL-GRANT">
962         for more information.
963         </para>
964         </listitem>
965       </varlistentry>
966
967
968       <varlistentry>
969         <term><literal>\dT [ <replaceable class="parameter">pattern</replaceable> ]</literal></term>
970         <listitem>
971         <para>
972         Lists all data types or only those that match <replaceable
973         class="parameter">pattern</replaceable>. The command form
974         <literal>\dT+</literal> shows extra information.
975         </para>
976         </listitem>
977       </varlistentry>
978
979
980       <varlistentry>
981         <term><literal>\du [ <replaceable class="parameter">pattern</replaceable> ]</literal></term>
982         <listitem>
983         <para>
984         Lists all database users, or only those that match <replaceable
985         class="parameter">pattern</replaceable>.
986         </para>
987         </listitem>
988       </varlistentry>
989
990
991       <varlistentry>
992         <term><literal>\edit</literal> (or <literal>\e</literal>) [ <replaceable class="parameter">filename</replaceable> ]</term>
993
994         <listitem>
995         <para>
996         If <replaceable class="parameter">filename</replaceable> is
997         specified, the file is edited; after the editor exits, its
998         content is copied back to the query buffer. If no argument is
999         given, the current query buffer is copied to a temporary file
1000         which is then edited in the same fashion.
1001         </para>
1002
1003         <para>
1004         The new query buffer is then re-parsed according to the normal
1005         rules of <application>psql</application>, where the whole buffer
1006         is treated as a single line. (Thus you cannot make scripts this
1007         way. Use <command>\i</command> for that.) This means also that
1008         if the query ends with (or rather contains) a semicolon, it is
1009         immediately executed. In other cases it will merely wait in the
1010         query buffer.
1011         </para>
1012
1013         <tip>
1014         <para>
1015         <application>psql</application> searches the environment
1016         variables <envar>PSQL_EDITOR</envar>, <envar>EDITOR</envar>, and
1017         <envar>VISUAL</envar> (in that order) for an editor to use. If
1018         all of them are unset, <filename>/bin/vi</filename> is run.
1019         </para>
1020         </tip>
1021         </listitem>
1022       </varlistentry>
1023
1024
1025       <varlistentry>
1026         <term><literal>\echo</literal> <replaceable class="parameter">text</replaceable> [ ... ]</term>
1027         <listitem>
1028         <para>
1029         Prints the arguments to the standard output, separated by one
1030         space and followed by a newline. This can be useful to
1031         intersperse information in the output of scripts. For example:
1032 <programlisting>
1033 => <userinput>\echo `date`</userinput>
1034 Tue Oct 26 21:40:57 CEST 1999
1035 </programlisting>
1036         If the first argument is an unquoted <literal>-n</literal> the the trailing
1037         newline is not written.
1038         </para>
1039
1040         <tip>
1041         <para>
1042         If you use the <command>\o</command> command to redirect your
1043         query output you may wish to use <command>\qecho</command>
1044         instead of this command.
1045         </para>
1046         </tip>
1047         </listitem>
1048       </varlistentry>
1049
1050
1051       <varlistentry>
1052         <term><literal>\encoding</literal> [ <replaceable class="parameter">encoding</replaceable> ]</term>
1053
1054         <listitem>
1055         <para>
1056         Sets the client encoding, if you are using multibyte encodings.
1057         Without an argument, this command shows the current encoding.
1058         </para>
1059         </listitem>
1060       </varlistentry>
1061
1062
1063       <varlistentry>
1064         <term><literal>\f</literal> [ <replaceable class="parameter">string</replaceable> ]</term>
1065
1066         <listitem>
1067         <para>
1068         Sets the field separator for unaligned query output. The default
1069         is pipe (<literal>|</literal>). See also
1070         <command>\pset</command> for a generic way of setting output
1071         options.
1072         </para>
1073         </listitem>
1074       </varlistentry>
1075
1076
1077       <varlistentry>
1078         <term><literal>\g</literal> [ { <replaceable class="parameter">filename</replaceable> | <literal>|</literal><replaceable class="parameter">command</replaceable> } ]</term>
1079
1080         <listitem>
1081         <para>
1082         Sends the current query input buffer to the backend and
1083         optionally saves the output in <replaceable
1084         class="parameter">filename</replaceable> or pipes the output
1085         into a separate Unix shell to execute <replaceable
1086         class="parameter">command</replaceable>. A bare
1087         <literal>\g</literal> is virtually equivalent to a semicolon. A
1088         <literal>\g</literal> with argument is a <quote>one-shot</quote>
1089         alternative to the <command>\o</command> command.
1090         </para>
1091         </listitem>
1092       </varlistentry>
1093
1094       <varlistentry>
1095         <term><literal>\help</literal> (or <literal>\h</literal>) [ <replaceable class="parameter">command</replaceable> ]</term>
1096         <listitem>
1097         <para>
1098         Give syntax help on the specified <acronym>SQL</acronym>
1099         command. If <replaceable class="parameter">command</replaceable>
1100         is not specified, then <application>psql</application> will list
1101         all the commands for which syntax help is available. If
1102         <replaceable class="parameter">command</replaceable> is an
1103         asterisk (<quote>*</quote>), then syntax help on all
1104         <acronym>SQL</acronym> commands is shown.
1105         </para>
1106
1107         <note>
1108         <para>
1109         To simplify typing, commands that consists of several words do
1110         not have to be quoted. Thus it is fine to type <userinput>\help
1111         alter table</userinput>.
1112         </para>
1113         </note> 
1114         </listitem>
1115       </varlistentry>
1116
1117
1118       <varlistentry>
1119         <term><literal>\H</literal></term>
1120         <listitem>
1121         <para>
1122         Turns on <acronym>HTML</acronym> query output format. If the
1123         <acronym>HTML</acronym> format is already on, it is switched
1124         back to the default aligned text format. This command is for
1125         compatibility and convenience, but see <command>\pset</command>
1126         about setting other output options.
1127         </para>
1128         </listitem>
1129       </varlistentry>
1130
1131
1132       <varlistentry>
1133         <term><literal>\i</literal> <replaceable class="parameter">filename</replaceable></term>
1134         <listitem>
1135         <para>
1136         Reads input from the file <replaceable
1137         class="parameter">filename</replaceable> and executes it as
1138         though it had been typed on the keyboard.
1139         </para>
1140         <note>
1141         <para>
1142         If you want to see the lines on the screen as they are read you
1143         must set the variable <varname>ECHO</varname> to
1144         <literal>all</literal>.
1145         </para>
1146         </note>
1147         </listitem>
1148       </varlistentry>
1149
1150
1151       <varlistentry>
1152         <term><literal>\l</literal> (or <literal>\list</literal>)</term>
1153         <listitem>
1154         <para>
1155         List all the databases in the server as well as their owners.
1156         Append a <quote>+</quote> to the command name to see any
1157         descriptions for the databases as well. If your
1158         <productname>PostgreSQL</productname> installation was compiled
1159         with multibyte encoding support, the encoding scheme of each
1160         database is shown as well.
1161         </para>
1162         </listitem>
1163       </varlistentry>
1164
1165
1166       <varlistentry>
1167         <term><literal>\lo_export</literal> <replaceable class="parameter">loid</replaceable> <replaceable class="parameter">filename</replaceable></term>
1168
1169         <listitem>
1170         <para>
1171         Reads the large object with <acronym>OID</acronym> <replaceable
1172         class="parameter">loid</replaceable> from the database and
1173         writes it to <replaceable
1174         class="parameter">filename</replaceable>. Note that this is
1175         subtly different from the server function
1176         <function>lo_export</function>, which acts with the permissions
1177         of the user that the database server runs as and on the server's
1178         file system.
1179         </para>
1180         <tip>
1181         <para>
1182         Use <command>\lo_list</command> to find out the large object's
1183         <acronym>OID</acronym>.
1184         </para>
1185         </tip>
1186         <note>
1187         <para>
1188         See the description of the <varname>LO_TRANSACTION</varname>
1189         variable for important information concerning all large object
1190         operations.
1191         </para>
1192         </note>
1193         </listitem>
1194       </varlistentry>
1195
1196
1197       <varlistentry>
1198         <term><literal>\lo_import</literal> <replaceable class="parameter">filename</replaceable> [ <replaceable class="parameter">comment</replaceable> ]</term>
1199
1200         <listitem>
1201         <para>
1202         Stores the file into a <productname>PostgreSQL</productname>
1203         <quote>large object</quote>. Optionally, it associates the given
1204         comment with the object. Example:
1205 <programlisting>
1206 foo=> <userinput>\lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'</userinput>
1207 lo_import 152801
1208 </programlisting>
1209         The response indicates that the large object received object id
1210         152801 which one ought to remember if one wants to access the
1211         object ever again. For that reason it is recommended to always
1212         associate a human-readable comment with every object. Those can
1213         then be seen with the <command>\lo_list</command> command.
1214         </para>
1215
1216         <para>
1217         Note that this command is subtly different from the server-side
1218         <function>lo_import</function> because it acts as the local user
1219         on the local file system, rather than the server's user and file
1220         system.
1221         </para>
1222
1223         <note>
1224         <para>
1225         See the description of the <varname>LO_TRANSACTION</varname>
1226         variable for important information concerning all large object
1227         operations.
1228         </para>
1229         </note>
1230         </listitem>
1231       </varlistentry>
1232
1233       <varlistentry>
1234         <term><literal>\lo_list</literal></term>
1235         <listitem>
1236         <para>
1237         Shows a list of all <productname>PostgreSQL</productname>
1238         <quote>large objects</quote> currently stored in the database,
1239         along with any comments provided for them.
1240         </para>
1241         </listitem>
1242       </varlistentry>
1243
1244       <varlistentry>
1245         <term><literal>\lo_unlink</literal> <replaceable class="parameter">loid</replaceable></term>
1246
1247         <listitem>
1248         <para>
1249         Deletes the large object with <acronym>OID</acronym>
1250         <replaceable class="parameter">loid</replaceable> from the
1251         database.
1252         </para>
1253
1254         <tip>
1255         <para>
1256         Use <command>\lo_list</command> to find out the large object's
1257         <acronym>OID</acronym>.
1258         </para>
1259         </tip>
1260         <note>
1261         <para>
1262         See the description of the <varname>LO_TRANSACTION</varname>
1263         variable for important information concerning all large object
1264         operations.
1265         </para>
1266         </note>
1267         </listitem>
1268       </varlistentry>
1269
1270
1271       <varlistentry>
1272         <term><literal>\o</literal> [ {<replaceable class="parameter">filename</replaceable> | <literal>|</literal><replaceable class="parameter">command</replaceable>} ]</term>
1273
1274         <listitem>
1275         <para>
1276         Saves future query results to the file <replaceable
1277         class="parameter">filename</replaceable> or pipes future results
1278         into a separate Unix shell to execute <replaceable
1279         class="parameter">command</replaceable>. If no arguments are
1280         specified, the query output will be reset to
1281         <filename>stdout</filename>.
1282         </para>
1283
1284         <para>
1285         <quote>Query results</quote> includes all tables, command
1286         responses, and notices obtained from the database server, as
1287         well as output of various backslash commands that query the
1288         database (such as <command>\d</command>), but not error
1289         messages.
1290         </para>
1291
1292         <tip>
1293         <para>
1294         To intersperse text output in between query results, use
1295         <command>\qecho</command>.
1296         </para>
1297         </tip>
1298         </listitem>
1299       </varlistentry>
1300
1301
1302       <varlistentry>
1303         <term><literal>\p</literal></term>
1304         <listitem>
1305         <para>
1306         Print the current query buffer to the standard output.
1307         </para>
1308         </listitem>
1309       </varlistentry>
1310
1311
1312       <varlistentry>
1313         <term><literal>\pset</literal> <replaceable class="parameter">parameter</replaceable> [ <replaceable class="parameter">value</replaceable> ]</term>
1314
1315         <listitem>
1316         <para>
1317         This command sets options affecting the output of query result
1318         tables. <replaceable class="parameter">parameter</replaceable>
1319         describes which option is to be set. The semantics of
1320         <replaceable class="parameter">value</replaceable> depend
1321         thereon.
1322         </para>
1323
1324         <para>
1325         Adjustable printing options are:
1326         <variablelist>
1327           <varlistentry>
1328           <term><literal>format</literal></term>
1329           <listitem>
1330           <para>
1331           Sets the output format to one of <literal>unaligned</literal>,
1332           <literal>aligned</literal>, <literal>html</literal>, or
1333           <literal>latex</literal>. Unique abbreviations are allowed.
1334           (That would mean one letter is enough.)
1335           </para>
1336
1337           <para>
1338           <quote>Unaligned</quote> writes all fields of a tuple on a
1339           line, separated by the currently active field separator. This
1340           is intended to create output that might be intended to be read
1341           in by other programs (tab-separated, comma-separated).
1342           <quote>Aligned</quote> mode is the standard, human-readable,
1343           nicely formatted text output that is default. The
1344           <quote><acronym>HTML</acronym></quote> and
1345           <quote>LaTeX</quote> modes put out tables that are intended to
1346           be included in documents using the respective mark-up
1347           language. They are not complete documents! (This might not be
1348           so dramatic in <acronym>HTML</acronym>, but in LaTeX you must
1349           have a complete document wrapper.)
1350           </para>
1351           </listitem>
1352           </varlistentry>
1353
1354           <varlistentry>
1355           <term><literal>border</literal></term>
1356           <listitem>
1357           <para>
1358           The second argument must be a number. In general, the higher
1359           the number the more borders and lines the tables will have,
1360           but this depends on the particular format. In
1361           <acronym>HTML</acronym> mode, this will translate directly
1362           into the <literal>border=...</literal> attribute, in the
1363           others only values 0 (no border), 1 (internal dividing lines),
1364           and 2 (table frame) make sense.
1365           </para>
1366           </listitem>
1367           </varlistentry>
1368
1369           <varlistentry>
1370           <term><literal>expanded</literal> (or <literal>x</literal>)</term>
1371           <listitem>
1372           <para>
1373           Toggles between regular and expanded format. When expanded
1374           format is enabled, all output has two columns with the field
1375           name on the left and the data on the right. This mode is
1376           useful if the data wouldn't fit on the screen in the normal
1377           <quote>horizontal</quote> mode.
1378           </para>
1379
1380           <para>
1381           Expanded mode is supported by all four output modes.
1382           </para>
1383           </listitem>
1384           </varlistentry>
1385
1386           <varlistentry>
1387           <term><literal>null</literal></term>
1388           <listitem>
1389           <para>
1390           The second argument is a string that should be printed
1391           whenever a field is null. The default is not to print
1392           anything, which can easily be mistaken for, say, an empty
1393           string. Thus, one might choose to write <literal>\pset null
1394           '(null)'</literal>.
1395           </para>
1396           </listitem>
1397           </varlistentry>
1398
1399           <varlistentry>
1400           <term><literal>fieldsep</literal></term>
1401           <listitem>
1402           <para>
1403           Specifies the field separator to be used in unaligned output
1404           mode. That way one can create, for example, tab- or
1405           comma-separated output, which other programs might prefer. To
1406           set a tab as field separator, type <literal>\pset fieldsep
1407           '\t'</literal>. The default field separator is
1408           <literal>'|'</literal> (a <quote>pipe</quote> symbol).
1409           </para>
1410           </listitem>
1411           </varlistentry>
1412
1413           <varlistentry>
1414           <term><literal>footer</literal></term>
1415           <listitem>
1416           <para>
1417           Toggles the display of the default footer <literal>(x
1418           rows)</literal>.
1419           </para>
1420           </listitem>
1421           </varlistentry>
1422
1423           <varlistentry>
1424           <term><literal>recordsep</literal></term>
1425           <listitem>
1426           <para>
1427           Specifies the record (line) separator to use in unaligned
1428           output mode. The default is a newline character.
1429           </para>
1430           </listitem>
1431           </varlistentry>
1432
1433           <varlistentry>
1434           <term><literal>tuples_only</literal> (or <literal>t</literal>)</term>
1435           <listitem>
1436           <para>
1437           Toggles between tuples only and full display. Full display may
1438           show extra information such as column headers, titles, and
1439           various footers. In tuples only mode, only actual table data
1440           is shown.
1441           </para>
1442           </listitem>
1443           </varlistentry>
1444
1445           <varlistentry>
1446           <term><literal>title</literal> [ <replaceable class="parameter">text</replaceable> ]</term>
1447           <listitem>
1448           <para>
1449           Sets the table title for any subsequently printed tables. This
1450           can be used to give your output descriptive tags. If no
1451           argument is given, the title is unset.
1452           </para>
1453
1454           <note>
1455           <para>
1456           This formerly only affected <acronym>HTML</acronym> mode. You
1457           can now set titles in any output format.
1458           </para>
1459           </note>
1460           </listitem>
1461           </varlistentry>
1462
1463           <varlistentry>
1464           <term><literal>tableattr</literal> (or <literal>T</literal>) [ <replaceable class="parameter">text</replaceable> ]</term>
1465           <listitem>
1466           <para>
1467           Allows you to specify any attributes to be placed inside the
1468           <acronym>HTML</acronym> <sgmltag>table</sgmltag> tag. This
1469           could for example be <literal>cellpadding</literal> or
1470           <literal>bgcolor</literal>. Note that you probably don't want
1471           to specify <literal>border</literal> here, as that is already
1472           taken care of by <literal>\pset border</literal>.
1473           </para>
1474           </listitem>
1475           </varlistentry>
1476
1477
1478           <varlistentry>
1479           <term><literal>pager</literal></term>
1480           <listitem>
1481           <para>
1482           Controls use of a pager for query and <application>psql</>
1483           help output. If the environment variable <envar>PAGER</envar>
1484           is set, the output is piped to the specified program.
1485           Otherwise a platform-dependent default (such as
1486           <filename>more</filename>) is used.
1487           </para>
1488
1489           <para>
1490           When the pager is off, the pager is not used. When the pager
1491           is on, the pager is used only when appropriate, i.e. the
1492           output is to a terminal and will not fit on the screen.
1493           (<application>psql</> does not do a perfect job of estimating
1494           when to use the pager.) <literal>\pset pager</> turns the
1495           pager on and off. Pager can also be set to <literal>always</>,
1496           which causes the pager to be always used.
1497           </para>
1498           </listitem>
1499           </varlistentry>
1500         </variablelist>
1501         Illustrations on how these different formats look can be seen in
1502         the <xref linkend="APP-PSQL-examples"
1503         endterm="APP-PSQL-examples-title"> section.
1504         </para>
1505
1506         <tip>
1507         <para>
1508         There are various shortcut commands for <command>\pset</command>. See
1509         <command>\a</command>, <command>\C</command>, <command>\H</command>,
1510         <command>\t</command>, <command>\T</command>, and <command>\x</command>.
1511         </para>
1512         </tip>
1513
1514         <note>
1515         <para>
1516         It is an error to call <command>\pset</command> without
1517         arguments. In the future this call might show the current status
1518         of all printing options.
1519         </para>
1520         </note>
1521
1522         </listitem>
1523       </varlistentry>
1524
1525
1526       <varlistentry>
1527         <term><literal>\q</literal></term>
1528         <listitem>
1529         <para>
1530         Quit the <application>psql</application> program.
1531         </para>
1532         </listitem>
1533       </varlistentry>
1534
1535
1536       <varlistentry>
1537         <term><literal>\qecho</literal> <replaceable class="parameter">text</replaceable> [ ... ] </term>
1538         <listitem>
1539         <para>
1540         This command is identical to <command>\echo</command> except
1541         that all output will be written to the query output channel, as
1542         set by <command>\o</command>.
1543         </para>
1544         </listitem>
1545       </varlistentry>
1546
1547
1548       <varlistentry>
1549         <term><literal>\r</literal></term>
1550         <listitem>
1551         <para>
1552         Resets (clears) the query buffer.
1553         </para>
1554         </listitem>
1555       </varlistentry>
1556
1557
1558       <varlistentry>
1559         <term><literal>\s</literal> [ <replaceable class="parameter">filename</replaceable> ]</term>
1560         <listitem>
1561         <para>
1562         Print or save the command line history to <replaceable
1563         class="parameter">filename</replaceable>. If <replaceable
1564         class="parameter">filename</replaceable> is omitted, the history
1565         is written to the standard output. This option is only available
1566         if <application>psql</application> is configured to use the
1567         <acronym>GNU</acronym> history library.
1568         </para>
1569
1570         <note>
1571         <para>
1572         In the current version, it is no longer necessary to save the
1573         command history, since that will be done automatically on
1574         program termination. The history is also loaded automatically
1575         every time <application>psql</application> starts up.
1576         </para>
1577         </note>
1578         </listitem>
1579       </varlistentry>
1580
1581
1582       <varlistentry>
1583         <term><literal>\set</literal> [ <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> [ ... ]]]</term>
1584
1585         <listitem>
1586         <para>
1587         Sets the internal variable <replaceable
1588         class="parameter">name</replaceable> to <replaceable
1589         class="parameter">value</replaceable> or, if more than one value
1590         is given, to the concatenation of all of them. If no second
1591         argument is given, the variable is just set with no value. To
1592         unset a variable, use the <command>\unset</command> command.
1593         </para>
1594
1595         <para>
1596         Valid variable names can contain characters, digits, and
1597         underscores. See the section about
1598         <application>psql</application> variables for details.
1599         </para>
1600
1601         <para>
1602         Although you are welcome to set any variable to anything you
1603         want, <application>psql</application> treats several variables
1604         as special. They are documented in the section about variables.
1605         </para>
1606
1607         <note>
1608         <para>
1609         This command is totally separate from the <acronym>SQL</acronym>
1610         command <xref linkend="SQL-SET" endterm="SQL-SET-title">.
1611         </para>
1612         </note>
1613         </listitem>
1614       </varlistentry>
1615
1616
1617       <varlistentry>
1618         <term><literal>\t</literal></term>
1619         <listitem>
1620         <para>
1621         Toggles the display of output column name headings and row count
1622         footer. This command is equivalent to <literal>\pset
1623         tuples_only</literal> and is provided for convenience.
1624         </para>
1625         </listitem>
1626       </varlistentry>
1627
1628
1629       <varlistentry>
1630         <term><literal>\T</literal> <replaceable class="parameter">table_options</replaceable></term>
1631         <listitem>
1632         <para>
1633         Allows you to specify options to be placed within the
1634         <sgmltag>table</sgmltag> tag in <acronym>HTML</acronym> tabular
1635         output mode. This command is equivalent to <literal>\pset
1636         tableattr <replaceable
1637         class="parameter">table_options</replaceable></literal>.
1638         </para>
1639         </listitem>
1640       </varlistentry>
1641
1642
1643       <varlistentry>
1644        <term><literal>\timing</literal></term>
1645         <listitem>
1646         <para>
1647          Toggles a display of how long each SQL statement takes, in milliseconds.
1648         </para>
1649        </listitem>
1650       </varlistentry>
1651
1652
1653       <varlistentry>
1654         <term><literal>\w</literal> {<replaceable class="parameter">filename</replaceable> | <replaceable class="parameter">|command</replaceable>}</term>
1655         <listitem>
1656         <para>
1657         Outputs the current query buffer to the file <replaceable
1658         class="parameter">filename</replaceable> or pipes it to the Unix
1659         command <replaceable class="parameter">command</replaceable>.
1660         </para>
1661         </listitem>
1662       </varlistentry>
1663
1664
1665       <varlistentry>
1666         <term><literal>\x</literal></term>
1667         <listitem>
1668         <para>
1669         Toggles extended row format mode. As such it is equivalent to
1670         <literal>\pset expanded</literal>.
1671        </para>
1672        </listitem>
1673       </varlistentry>
1674
1675
1676       <varlistentry>
1677         <term><literal>\z</literal> [ <replaceable class="parameter">pattern</replaceable> ]</term>
1678         <listitem>
1679         <para>
1680         Produces a list of all available tables with their
1681         associated access permissions.
1682         If a <replaceable class="parameter">pattern</replaceable> is
1683         specified, only tables whose name matches the pattern are listed.
1684         </para>
1685
1686         <para>
1687         The commands <xref linkend="SQL-GRANT"> and
1688         <xref linkend="SQL-REVOKE">
1689         are used to set access permissions.  See <xref linkend="SQL-GRANT">
1690         for more information.
1691         </para>
1692
1693         <para>
1694         This is an alias for <command>\dp</command> (<quote>display
1695         permissions</quote>).
1696         </para>
1697         </listitem>
1698       </varlistentry>
1699
1700
1701       <varlistentry>
1702         <term><literal>\!</literal> [ <replaceable class="parameter">command</replaceable> ]</term>
1703         <listitem>
1704         <para>
1705         Escapes to a separate Unix shell or executes the Unix command
1706         <replaceable class="parameter">command</replaceable>. The
1707         arguments are not further interpreted, the shell will see them
1708         as is.
1709         </para>
1710         </listitem>
1711       </varlistentry>
1712
1713
1714       <varlistentry>
1715         <term><literal>\?</literal></term>
1716         <listitem>
1717         <para>
1718         Get help information about the backslash (<quote>\</quote>)
1719         commands.
1720         </para>
1721         </listitem>
1722       </varlistentry>
1723
1724     </variablelist>
1725   </para>
1726
1727   <para>
1728    The various <literal>\d</> commands accept a <replaceable
1729    class="parameter">pattern</replaceable> parameter to specify the
1730    object name(s) to be displayed.  Patterns are interpreted similarly
1731    to SQL identifiers, in that unquoted letters are forced to lowercase,
1732    while double quotes (<literal>"</>) protect letters from case conversion
1733    and allow incorporation of whitespace into the identifier.  Within
1734    double quotes, paired double quotes reduce to a single double quote in
1735    the resulting name.  For example, <literal>FOO"BAR"BAZ</> is interpreted
1736    as <literal>fooBARbaz</>, and <literal>"A weird"" name"</> becomes
1737    <literal>A weird" name</>.
1738   </para>
1739
1740   <para>
1741    More interestingly, <literal>\d</> patterns allow the use of
1742    <literal>*</> to mean <quote>any sequence of characters</>, and
1743    <literal>?</> to mean <quote>any single character</>.  (This notation
1744    is comparable to Unix shell filename patterns.)  Advanced users can
1745    also use regular-expression notations such as character classes, for
1746    example <literal>[0-9]</> to match <quote>any digit</>.  To make any of
1747    these pattern-matching characters be interpreted literally, surround it
1748    with double quotes.
1749   </para>
1750
1751   <para>
1752    A pattern that contains an (unquoted) dot is interpreted as a schema
1753    name pattern followed by an object name pattern.  For example,
1754    <literal> \dt foo*.bar*</> displays all tables in schemas whose name
1755    starts with <literal>foo</> and whose table name 
1756    starts with <literal>bar</>.  If no dot appears, then the pattern
1757    matches only objects that are visible in the current schema search path.
1758   </para>
1759
1760   <para>
1761    Whenever the <replaceable class="parameter">pattern</replaceable> parameter
1762    is omitted completely, the <literal>\d</> commands display all objects
1763    that are visible in the current schema search path.  To see all objects
1764    in the database, use the pattern <literal>*.*</>.
1765   </para>
1766  </refsect2>
1767
1768  <refsect2>
1769   <title>Advanced features</title>
1770
1771    <refsect3 id="APP-PSQL-variables">
1772     <title id="APP-PSQL-variables-title">Variables</title>
1773
1774     <para>
1775     <application>psql</application> provides variable substitution
1776     features similar to common Unix command shells. This feature is new
1777     and not very sophisticated, yet, but there are plans to expand it in
1778     the future. Variables are simply name/value pairs, where the value
1779     can be any string of any length. To set variables, use the
1780     <application>psql</application> meta-command
1781     <command>\set</command>:
1782 <programlisting>
1783 testdb=> <userinput>\set foo bar</userinput>
1784 </programlisting>
1785     sets the variable <quote>foo</quote> to the value
1786     <quote>bar</quote>. To retrieve the content of the variable, precede
1787     the name with a colon and use it as the argument of any slash
1788     command:
1789 <programlisting>
1790 testdb=> <userinput>\echo :foo</userinput>
1791 bar
1792 </programlisting>
1793     </para>
1794
1795     <note>
1796     <para>
1797     The arguments of <command>\set</command> are subject to the same
1798     substitution rules as with other commands. Thus you can construct
1799     interesting references such as <literal>\set :foo
1800     'something'</literal> and get <quote>soft links</quote> or
1801     <quote>variable variables</quote> of <productname>Perl</productname>
1802     or <productname><acronym>PHP</acronym></productname> fame,
1803     respectively. Unfortunately (or fortunately?), there is no way to do
1804     anything useful with these constructs. On the other hand,
1805     <literal>\set bar :foo</literal> is a perfectly valid way to copy a
1806     variable.
1807     </para>
1808     </note>
1809
1810     <para>
1811     If you call <command>\set</command> without a second argument, the
1812     variable is simply set, but has no value. To unset (or delete) a
1813     variable, use the command <command>\unset</command>.
1814     </para>
1815
1816     <para>
1817     <application>psql</application>'s internal variable names can
1818     consist of letters, numbers, and underscores in any order and any
1819     number of them. A number of regular variables are treated specially
1820     by <application>psql</application>. They indicate certain option
1821     settings that can be changed at run time by altering the value of
1822     the variable or represent some state of the application. Although
1823     you can use these variables for any other purpose, this is not
1824     recommended, as the program behavior might grow really strange
1825     really quickly. By convention, all specially treated variables
1826     consist of all upper-case letters (and possibly numbers and
1827     underscores). To ensure maximum compatibility in the future, avoid
1828     such variables. A list of all specially treated variables follows.
1829     <variablelist>
1830       <varlistentry>
1831         <term><varname>DBNAME</varname></term>
1832         <listitem>
1833         <para>
1834         The name of the database you are currently connected to. This is
1835         set every time you connect to a database (including program
1836         start-up), but can be unset.
1837         </para>
1838         </listitem>
1839       </varlistentry>
1840
1841       <varlistentry>
1842         <term><varname>ECHO</varname></term>
1843         <listitem>
1844         <para>
1845         If set to <quote><literal>all</literal></quote>, all lines
1846         entered or from a script are written to the standard output
1847         before they are parsed or executed. To specify this on program
1848         start-up, use the switch <option>-a</option>. If set to
1849         <quote><literal>queries</literal></quote>,
1850         <application>psql</application> merely prints all queries as
1851         they are sent to the backend. The option for this is
1852         <option>-e</option>.
1853         </para>
1854         </listitem>
1855       </varlistentry>
1856
1857       <varlistentry>
1858         <term><varname>ECHO_HIDDEN</varname></term>
1859         <listitem>
1860         <para>
1861         When this variable is set and a backslash command queries the
1862         database, the query is first shown. This way you can study the
1863         <productname>PostgreSQL</productname> internals and provide
1864         similar functionality in your own programs. If you set the
1865         variable to the value <literal>noexec</literal>, the queries are
1866         just shown but are not actually sent to the backend and
1867         executed.
1868         </para>
1869         </listitem>
1870       </varlistentry>
1871
1872       <varlistentry>
1873         <term><varname>ENCODING</varname></term>
1874         <listitem>
1875         <para>
1876         The current client multibyte encoding. If you are not set up to
1877         use multibyte characters, this variable will always contain
1878         <quote>SQL_ASCII</quote>.
1879         </para>
1880         </listitem>
1881       </varlistentry>
1882
1883       <varlistentry>
1884         <term><varname>HISTCONTROL</varname></term>
1885         <listitem>
1886         <para>
1887          If this variable is set to <literal>ignorespace</literal>,
1888          lines which begin with a space are not entered into the history
1889          list. If set to a value of <literal>ignoredups</literal>, lines
1890          matching the previous history line are not entered. A value of
1891          <literal>ignoreboth</literal> combines the two options. If
1892          unset, or if set to any other value than those above, all lines
1893          read in interactive mode are saved on the history list.
1894         </para>
1895         <note>
1896         <para>
1897         This feature was shamelessly plagiarized from
1898         <application>bash</application>.
1899         </para>
1900         </note>
1901         </listitem>
1902       </varlistentry>
1903
1904       <varlistentry>
1905         <term><varname>HISTSIZE</varname></term>
1906         <listitem>
1907         <para>
1908         The number of commands to store in the command history. The
1909         default value is 500.
1910         </para>
1911         <note>
1912         <para>
1913         This feature was shamelessly plagiarized from
1914         <application>bash</application>.
1915         </para>
1916         </note>
1917         </listitem>
1918       </varlistentry>
1919
1920       <varlistentry>
1921         <term><varname>HOST</varname></term>
1922         <listitem>
1923         <para>
1924         The database server host you are currently connected to. This is
1925         set every time you connect to a database (including program
1926         start-up), but can be unset.
1927         </para>
1928         </listitem>
1929       </varlistentry>
1930
1931       <varlistentry>
1932         <term><varname>IGNOREEOF</varname></term>
1933         <listitem>
1934         <para>
1935          If unset, sending an <acronym>EOF</> character (usually
1936          <keycombo action="simul"><keycap>Control</><keycap>D</></>)
1937          to an interactive session of <application>psql</application>
1938          will terminate the application. If set to a numeric value,
1939          that many <acronym>EOF</> characters are ignored before the
1940          application terminates.  If the variable is set but has no
1941          numeric value, the default is 10.
1942         </para>
1943         <note>
1944         <para>
1945         This feature was shamelessly plagiarized from
1946         <application>bash</application>.
1947         </para>
1948         </note>
1949         </listitem>
1950       </varlistentry>
1951
1952       <varlistentry>
1953         <term><varname>LASTOID</varname></term>
1954         <listitem>
1955         <para>
1956         The value of the last affected OID, as returned from an
1957         <command>INSERT</command> or <command>lo_insert</command>
1958         command. This variable is only guaranteed to be valid until
1959         after the result of the next <acronym>SQL</acronym> command has
1960         been displayed.
1961         </para>
1962         </listitem>
1963       </varlistentry>
1964
1965       <varlistentry>
1966         <term><varname>LO_TRANSACTION</varname></term>
1967         <listitem>
1968         <para>
1969         If you use the <productname>PostgreSQL</productname> large
1970         object interface to specially store data that does not fit into
1971         one tuple, all the operations must be contained in a transaction
1972         block. (See the documentation of the large object interface for
1973         more information.) Since <application>psql</application> has no
1974         way to tell if you already have a transaction in progress when
1975         you call one of its internal commands
1976         (<command>\lo_export</command>, <command>\lo_import</command>,
1977         <command>\lo_unlink</command>) it must take some arbitrary
1978         action. This action could either be to roll back any transaction
1979         that might already be in progress, or to commit any such
1980         transaction, or to do nothing at all. In the last case you must
1981         provide your own <command>BEGIN
1982         TRANSACTION</command>/<command>COMMIT</command> block or the
1983         results will be unpredictable (usually resulting in the desired
1984         action's not being performed in any case).
1985         </para>
1986
1987         <para>
1988         To choose what you want to do you set this variable to one of
1989         <quote>rollback</quote>, <quote>commit</quote>, or
1990         <quote>nothing</quote>. The default is to roll back the
1991         transaction. If you just want to load one or a few objects this
1992         is fine. However, if you intend to transfer many large objects,
1993         it might be advisable to provide one explicit transaction block
1994         around all commands.
1995         </para>
1996         </listitem>
1997       </varlistentry>
1998
1999       <varlistentry>
2000         <term><varname>ON_ERROR_STOP</varname></term>
2001         <listitem>
2002         <para>
2003         By default, if non-interactive scripts encounter an error, such
2004         as a malformed <acronym>SQL</acronym> query or internal
2005         meta-command, processing continues. This has been the
2006         traditional behavior of <application>psql</application> but it
2007         is sometimes not desirable. If this variable is set, script
2008         processing will immediately terminate. If the script was called
2009         from another script it will terminate in the same fashion. If
2010         the outermost script was not called from an interactive
2011         <application>psql</application> session but rather using the
2012         <option>-f</option> option, <application>psql</application> will
2013         return error code 3, to distinguish this case from fatal error
2014         conditions (error code 1).
2015         </para>
2016         </listitem>
2017       </varlistentry>
2018
2019       <varlistentry>
2020         <term><varname>PORT</varname></term>
2021         <listitem>
2022         <para>
2023         The database server port to which you are currently connected.
2024         This is set every time you connect to a database (including
2025         program start-up), but can be unset.
2026         </para>
2027         </listitem>
2028       </varlistentry>
2029
2030       <varlistentry>
2031         <term><varname>PROMPT1</varname></term>
2032         <term><varname>PROMPT2</varname></term>
2033         <term><varname>PROMPT3</varname></term>
2034         <listitem>
2035         <para>
2036         These specify what the prompt <application>psql</application>
2037         issues is supposed to look like. See <quote><xref
2038         linkend="APP-PSQL-prompting"
2039         endterm="APP-PSQL-prompting-title"></quote> below.
2040         </para>
2041         </listitem>
2042       </varlistentry>
2043
2044       <varlistentry>
2045         <term><varname>QUIET</varname></term>
2046         <listitem>
2047         <para>
2048         This variable is equivalent to the command line option
2049         <option>-q</option>. It is probably not too useful in
2050         interactive mode.
2051         </para>
2052         </listitem>
2053       </varlistentry>
2054
2055       <varlistentry>
2056         <term><varname>SINGLELINE</varname></term>
2057         <listitem>
2058         <para>
2059         This variable is set by the command line option
2060         <option>-S</option>. You can unset or reset it at run time.
2061         </para>
2062         </listitem>
2063       </varlistentry>
2064
2065       <varlistentry>
2066         <term><varname>SINGLESTEP</varname></term>
2067         <listitem>
2068         <para>
2069         This variable is equivalent to the command line option
2070         <option>-s</option>.
2071         </para>
2072         </listitem>
2073       </varlistentry>
2074
2075       <varlistentry>
2076         <term><varname>USER</varname></term>
2077         <listitem>
2078         <para>
2079         The database user you are currently connected as. This is set
2080         every time you connect to a database (including program
2081         start-up), but can be unset.
2082         </para>
2083         </listitem>
2084       </varlistentry>
2085
2086     </variablelist>
2087
2088     </para>
2089
2090    </refsect3>
2091
2092    <refsect3>
2093     <title><acronym>SQL</acronym> Interpolation</title>
2094
2095     <para>
2096     An additional useful feature of <application>psql</application>
2097     variables is that you can substitute (<quote>interpolate</quote>)
2098     them into regular <acronym>SQL</acronym> statements. The syntax for
2099     this is again to prepend the variable name with a colon
2100     (<literal>:</literal>).
2101 <programlisting>
2102 testdb=> <userinput>\set foo 'my_table'</userinput>
2103 testdb=> <userinput>SELECT * FROM :foo;</userinput>
2104 </programlisting>
2105     would then query the table <literal>my_table</literal>. The value of
2106     the variable is copied literally, so it can even contain unbalanced
2107     quotes or backslash commands. You must make sure that it makes sense
2108     where you put it. Variable interpolation will not be performed into
2109     quoted <acronym>SQL</acronym> entities.
2110     </para>
2111
2112     <para>
2113     A popular application of this facility is to refer to the last
2114     inserted <acronym>OID</acronym> in subsequent statements to build a
2115     foreign key scenario. Another possible use of this mechanism is to
2116     copy the contents of a file into a field. First load the file into a
2117     variable and then proceed as above.
2118 <programlisting>
2119 testdb=> <userinput>\set content '\'' `cat my_file.txt` '\''</userinput>
2120 testdb=> <userinput>INSERT INTO my_table VALUES (:content);</userinput>
2121 </programlisting>
2122     One possible problem with this approach is that <filename>my_file.txt</filename>
2123     might contain single quotes. These need to be escaped so that
2124     they don't cause a syntax error when the third line is processed. This
2125     could be done with the program <application>sed</application>:
2126 <programlisting>
2127 testdb=> <userinput>\set content '\'' `sed -e "s/'/\\\\\\'/g" < my_file.txt` '\''</userinput>
2128 </programlisting>
2129     Observe the correct number of backslashes (6)! You can resolve it
2130     this way: After <application>psql</application> has parsed this
2131     line, it passes <literal>sed -e "s/'/\\\'/g" < my_file.txt</literal>
2132     to the shell. The shell will do its own thing inside the double
2133     quotes and execute <filename>sed</filename> with the arguments
2134     <literal>-e</literal> and <literal>s/'/\\'/g</literal>. When
2135     <application>sed</application> parses this it will replace the two
2136     backslashes with a single one and then do the substitution. Perhaps
2137     at one point you thought it was great that all Unix commands use the
2138     same escape character. And this is ignoring the fact that you might
2139     have to escape all backslashes as well because
2140     <acronym>SQL</acronym> text constants are also subject to certain
2141     interpretations. In that case you might be better off preparing the
2142     file externally.
2143     </para>
2144
2145     <para>
2146     Since colons may legally appear in queries, the following rule
2147     applies: If the variable is not set, the character sequence
2148     <quote>colon+name</quote> is not changed. In any case you can escape
2149     a colon with a backslash to protect it from interpretation. (The
2150     colon syntax for variables is standard <acronym>SQL</acronym> for
2151     embedded query languages, such as <application>ecpg</application>.
2152     The colon syntax for array slices and type casts are
2153     <productname>PostgreSQL</productname> extensions, hence the
2154     conflict.)
2155     </para>
2156
2157    </refsect3>
2158
2159    <refsect3 id="APP-PSQL-prompting">
2160     <title id="APP-PSQL-prompting-title">Prompting</title>
2161
2162     <para>
2163     The prompts <application>psql</application> issues can be customized
2164     to your preference. The three variables <varname>PROMPT1</varname>,
2165     <varname>PROMPT2</varname>, and <varname>PROMPT3</varname> contain strings
2166     and special escape sequences that describe the appearance of the
2167     prompt. Prompt 1 is the normal prompt that is issued when
2168     <application>psql</application> requests a new query. Prompt 2 is
2169     issued when more input is expected during query input because the
2170     query was not terminated with a semicolon or a quote was not closed.
2171     Prompt 3 is issued when you run an <acronym>SQL</acronym>
2172     <command>COPY</command> command and you are expected to type in the
2173     tuples on the terminal.
2174     </para>
2175
2176     <para>
2177     The value of the respective prompt variable is printed literally,
2178     except where a percent sign (<quote>%</quote>) is encountered.
2179     Depending on the next character, certain other text is substituted
2180     instead. Defined substitutions are:
2181
2182     <variablelist>
2183       <varlistentry>
2184         <term><literal>%M</literal></term>
2185         <listitem>
2186          <para>
2187           The full host name (with domain name) of the database server,
2188           or <literal>[local]</literal> if the connection is over a Unix
2189           domain socket, or
2190           <literal>[local:<replaceable>/dir/name</replaceable>]</literal
2191           >, if the Unix domain socket is not at the compiled in default
2192           location.
2193         </para>
2194        </listitem>
2195       </varlistentry>
2196
2197       <varlistentry>
2198         <term><literal>%m</literal></term>
2199         <listitem>
2200          <para>
2201           The host name of the database server, truncated after the
2202           first dot, or <literal>[local]</literal> if the connection is
2203           over a Unix domain socket.
2204          </para>
2205         </listitem>
2206       </varlistentry>
2207
2208       <varlistentry>
2209         <term><literal>%&gt;</literal></term>
2210         <listitem><para>The port number at which the database server is listening.</para></listitem>
2211       </varlistentry>
2212
2213       <varlistentry>
2214         <term><literal>%n</literal></term>
2215         <listitem><para>The user name you are connected as (not your local system
2216          user name).</para></listitem>
2217       </varlistentry>
2218
2219       <varlistentry>
2220         <term><literal>%/</literal></term>
2221         <listitem><para>The name of the current database.</para></listitem>
2222       </varlistentry>
2223
2224       <varlistentry>
2225         <term><literal>%~</literal></term>
2226         <listitem><para>Like <literal>%/</literal>, but the output is <quote>~</quote>
2227          (tilde) if the database is your default database.</para></listitem>
2228       </varlistentry>
2229
2230       <varlistentry>
2231         <term><literal>%#</literal></term>
2232         <listitem><para>If the current user is a database superuser, then a
2233          <quote>#</quote>, otherwise a <quote>&gt;</quote>.</para></listitem>
2234       </varlistentry>
2235
2236       <varlistentry>
2237         <term><literal>%R</literal></term>
2238         <listitem>
2239         <para>
2240         In prompt 1 normally <quote>=</quote>, but <quote>^</quote> if
2241         in single-line mode, and <quote>!</quote> if the session is
2242         disconnected from the database (which can happen if
2243         <command>\connect</command> fails). In prompt 2 the sequence is
2244         replaced by <quote>-</quote>, <quote>*</quote>, a single quote,
2245         or a double quote, depending on whether
2246         <application>psql</application> expects more input because the
2247         query wasn't terminated yet, because you are inside a
2248         <literal>/* ... */</literal> comment, or because you are inside
2249         a quote. In prompt 3 the sequence doesn't resolve to anything.
2250         </para>
2251         </listitem>
2252       </varlistentry>
2253
2254       <varlistentry>
2255         <term><literal>%</literal><replaceable class="parameter">digits</replaceable></term>
2256         <listitem>
2257         <para>
2258         If <replaceable class="parameter">digits</replaceable> starts
2259         with <literal>0x</literal> the rest of the characters are
2260         interpreted as a hexadecimal digit and the character with the
2261         corresponding code is substituted. If the first digit is
2262         <literal>0</literal> the characters are interpreted as on octal
2263         number and the corresponding character is substituted. Otherwise
2264         a decimal number is assumed.
2265         </para>
2266         </listitem>
2267       </varlistentry>
2268
2269       <varlistentry>
2270         <term><literal>%:</literal><replaceable class="parameter">name</replaceable><literal>:</literal></term>
2271         <listitem>
2272         <para>
2273         The value of the <application>psql</application>, variable
2274         <replaceable class="parameter">name</replaceable>. See the
2275         section <quote><xref linkend="APP-PSQL-variables"
2276         endterm="APP-PSQL-variables-title"></quote> for details.
2277         </para>
2278         </listitem>
2279       </varlistentry>
2280
2281       <varlistentry>
2282         <term><literal>%`</literal><replaceable class="parameter">command</replaceable><literal>`</literal></term>
2283         <listitem>
2284         <para>
2285         The output of <replaceable
2286         class="parameter">command</replaceable>, similar to ordinary
2287         <quote>back-tick</quote> substitution.
2288         </para>
2289         </listitem>
2290       </varlistentry>
2291
2292     </variablelist>
2293
2294     To insert a percent sign into your prompt, write
2295     <literal>%%</literal>. The default prompts are equivalent to
2296     <literal>'%/%R%# '</literal> for prompts 1 and 2, and
2297     <literal>'&gt;&gt; '</literal> for prompt 3.
2298     </para>
2299
2300     <note>
2301     <para>
2302     This feature was shamelessly plagiarized from
2303     <application>tcsh</application>.
2304     </para>
2305     </note>
2306
2307    </refsect3>
2308
2309    <refsect3>
2310     <title>Command-Line Editing</title>
2311
2312     <para>
2313     <application>psql</application> supports the <application>Readline</application>
2314     library for convenient line editing and retrieval. The command
2315     history is stored in a file named <filename>.psql_history</filename>
2316     in your home directory and is reloaded when
2317     <application>psql</application> starts up. Tab-completion is also
2318     supported, although the completion logic makes no claim to be an
2319     <acronym>SQL</acronym> parser. When available,
2320     <application>psql</application> is automatically built to use these
2321     features. If for some reason you do not like the tab completion, you
2322     can turn if off by putting this in a file named
2323     <filename>.inputrc</filename> in your home directory:
2324 <programlisting>
2325 $if psql
2326 set disable-completion on
2327 $endif
2328 </programlisting>
2329     (This is not a <application>psql</application> but a
2330     <application>readline</application> feature. Read its documentation
2331     for further details.)
2332     </para>
2333    </refsect3>
2334   </refsect2>
2335  </refsect1>
2336
2337
2338  <refsect1>
2339   <title>Environment</title>
2340
2341   <variablelist>
2342    <varlistentry>
2343     <term><envar>HOME</envar></term>
2344
2345     <listitem>
2346      <para>
2347       Directory for initialization file (<filename>.psqlrc</filename>)
2348       and command history file (<filename>.psql_history</filename>).
2349      </para>
2350     </listitem>
2351    </varlistentry>
2352
2353    <varlistentry>
2354     <term><envar>PAGER</envar></term>
2355
2356     <listitem>
2357      <para>
2358       If the query results do not fit on the screen, they are piped
2359       through this command.  Typical values are
2360       <literal>more</literal> or <literal>less</literal>.  The default
2361       is platform-dependent.  The use of the pager can be disabled by
2362       using the <command>\pset</command> command.
2363      </para>
2364     </listitem>
2365    </varlistentry>
2366
2367    <varlistentry>
2368     <term><envar>PGDATABASE</envar></term>
2369
2370     <listitem>
2371      <para>
2372       Default database to connect to
2373      </para>
2374     </listitem>
2375    </varlistentry>
2376
2377    <varlistentry>
2378     <term><envar>PGHOST</envar></term>
2379     <term><envar>PGPORT</envar></term>
2380     <term><envar>PGUSER</envar></term>
2381
2382     <listitem>
2383      <para>
2384       Default connection parameters
2385      </para>
2386     </listitem>
2387    </varlistentry>
2388
2389    <varlistentry>
2390     <term><envar>PSQL_EDITOR</envar></term>
2391     <term><envar>EDITOR</envar></term>
2392     <term><envar>VISUAL</envar></term>
2393
2394     <listitem>
2395      <para>
2396       Editor used by the <command>\e</command> command.  The variables
2397       are examined in the order listed; the first that is set is used.
2398      </para>
2399     </listitem>
2400    </varlistentry>
2401
2402    <varlistentry>
2403     <term><envar>SHELL</envar></term>
2404
2405     <listitem>
2406      <para>
2407       Command executed by the <command>\!</command> command.
2408      </para>
2409     </listitem>
2410    </varlistentry>
2411
2412    <varlistentry>
2413     <term><envar>TMPDIR</envar></term>
2414
2415     <listitem>
2416      <para>
2417       Directory for storing temporary files.  The default is
2418       <filename>/tmp</filename>.
2419      </para>
2420     </listitem>
2421    </varlistentry>
2422   </variablelist>
2423  </refsect1>
2424
2425
2426  <refsect1>
2427   <title>Files</title>
2428
2429   <itemizedlist>
2430    <listitem>
2431     <para>
2432      Before starting up, <application>psql</application> attempts to
2433      read and execute commands from the file
2434      <filename>$HOME/.psqlrc</filename>. It could be used to set up
2435      the client or the server to taste (using the <command>\set
2436      </command> and <command>SET</command> commands).
2437     </para>
2438    </listitem>
2439
2440    <listitem>
2441     <para>
2442      The command-line history is stored in the file
2443      <filename>$HOME/.psql_history</filename>.
2444     </para>
2445    </listitem>
2446   </itemizedlist>
2447  </refsect1>
2448
2449
2450  <refsect1>
2451   <title>Notes</title>
2452
2453     <itemizedlist>
2454       <listitem>
2455       <para>
2456       In an earlier life <application>psql</application> allowed the
2457       first argument of a single-letter backslash command to start
2458       directly after the command, without intervening whitespace. For
2459       compatibility this is still supported to some extent,
2460       but I am not going to explain the details here as this use is
2461       discouraged.  If you get strange messages, keep this in mind.
2462       For example
2463 <programlisting>
2464 testdb=> <userinput>\foo</userinput>
2465 Field separator is "oo",
2466 </programlisting>
2467       which is perhaps not what one would expect.
2468       </para>
2469       </listitem>
2470
2471       <listitem>
2472       <para>
2473       <application>psql</application> only works smoothly with servers
2474       of the same version. That does not mean other combinations will
2475       fail outright, but subtle and not-so-subtle problems might come
2476       up.  Backslash commands are particularly likely to fail if the
2477       server is of a different version.
2478       </para>
2479       </listitem>
2480
2481       <listitem>
2482       <para>
2483       Pressing Control-C during a <quote>copy in</quote> (data sent to
2484       the server) doesn't show the most ideal of behaviors. If you get a
2485       message such as <quote>COPY state must be terminated
2486       first</quote>, simply reset the connection by entering <literal>\c
2487       - -</literal>.
2488       </para>
2489       </listitem>
2490
2491     </itemizedlist>
2492  </refsect1>
2493
2494
2495  <refsect1 id="APP-PSQL-examples">
2496   <title id="APP-PSQL-examples-title">Examples</title>
2497
2498   <note>
2499   <para>
2500   This section only shows a few examples specific to
2501   <application>psql</application>. If you want to learn
2502   <acronym>SQL</acronym> or get familiar with
2503   <productname>PostgreSQL</productname>, you might wish to read the
2504   Tutorial that is included in the distribution.
2505   </para>
2506   </note>
2507
2508   <para>
2509   The first example shows how to spread a query over several lines of
2510   input. Notice the changing prompt:
2511 <programlisting>
2512 testdb=> <userinput>CREATE TABLE my_table (</userinput>
2513 testdb(> <userinput> first integer not null default 0,</userinput>
2514 testdb(> <userinput> second text</userinput>
2515 testdb-> <userinput>);</userinput>
2516 CREATE
2517 </programlisting>
2518   Now look at the table definition again:
2519 <programlisting>
2520 testdb=> <userinput>\d my_table</userinput>
2521              Table "my_table"
2522  Attribute |  Type   |      Modifier
2523 -----------+---------+--------------------
2524  first     | integer | not null default 0
2525  second    | text    |
2526
2527 </programlisting>
2528   At this point you decide to change the prompt to something more
2529   interesting:
2530 <programlisting>
2531 testdb=> <userinput>\set PROMPT1 '%n@%m %~%R%# '</userinput>
2532 peter@localhost testdb=>
2533 </programlisting>
2534   Let's assume you have filled the table with data and want to take a
2535   look at it:
2536 <programlisting>
2537 peter@localhost testdb=> SELECT * FROM my_table;
2538  first | second
2539 -------+--------
2540      1 | one
2541      2 | two
2542      3 | three
2543      4 | four
2544 (4 rows)
2545
2546 </programlisting>
2547   You can make this table look differently by using the
2548   <command>\pset</command> command:
2549 <programlisting>
2550 peter@localhost testdb=> <userinput>\pset border 2</userinput>
2551 Border style is 2.
2552 peter@localhost testdb=> <userinput>SELECT * FROM my_table;</userinput>
2553 +-------+--------+
2554 | first | second |
2555 +-------+--------+
2556 |     1 | one    |
2557 |     2 | two    |
2558 |     3 | three  |
2559 |     4 | four   |
2560 +-------+--------+
2561 (4 rows)
2562
2563 peter@localhost testdb=> <userinput>\pset border 0</userinput>
2564 Border style is 0.
2565 peter@localhost testdb=> <userinput>SELECT * FROM my_table;</userinput>
2566 first second
2567 ----- ------
2568     1 one
2569     2 two
2570     3 three
2571     4 four
2572 (4 rows)
2573
2574 peter@localhost testdb=> <userinput>\pset border 1</userinput>
2575 Border style is 1.
2576 peter@localhost testdb=> <userinput>\pset format unaligned</userinput>
2577 Output format is unaligned.
2578 peter@localhost testdb=> <userinput>\pset fieldsep ","</userinput>
2579 Field separator is ",".
2580 peter@localhost testdb=> <userinput>\pset tuples_only</userinput>
2581 Showing only tuples.
2582 peter@localhost testdb=> <userinput>SELECT second, first FROM my_table;</userinput>
2583 one,1
2584 two,2
2585 three,3
2586 four,4
2587 </programlisting>
2588   Alternatively, use the short commands:
2589 <programlisting>
2590 peter@localhost testdb=> <userinput>\a \t \x</userinput>
2591 Output format is aligned.
2592 Tuples only is off.
2593 Expanded display is on.
2594 peter@localhost testdb=> <userinput>SELECT * FROM my_table;</userinput>
2595 -[ RECORD 1 ]-
2596 first  | 1
2597 second | one
2598 -[ RECORD 2 ]-
2599 first  | 2
2600 second | two
2601 -[ RECORD 3 ]-
2602 first  | 3
2603 second | three
2604 -[ RECORD 4 ]-
2605 first  | 4
2606 second | four
2607 </programlisting>
2608   </para>
2609
2610  </refsect1>
2611
2612 </refentry>
2613
2614 <!-- Keep this comment at the end of the file
2615 Local variables:
2616 mode: sgml
2617 sgml-omittag:nil
2618 sgml-shorttag:t
2619 sgml-minimize-attributes:nil
2620 sgml-always-quote-attributes:t
2621 sgml-indent-step:1
2622 sgml-indent-data:t
2623 sgml-parent-document:nil
2624 sgml-default-dtd-file:"../reference.ced"
2625 sgml-exposed-tags:nil
2626 sgml-local-catalogs:"/usr/lib/sgml/catalog"
2627 sgml-local-ecat-files:nil
2628 End:
2629 -->