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