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