Repair memory leaks in plpython.
[pgsql.git] / doc / src / sgml / ref / psql-ref.sgml
blob72f3347e53d6eccf207772742851a739575ec509
1 <!--
2 doc/src/sgml/ref/psql-ref.sgml
3 PostgreSQL documentation
4 -->
6 <refentry id="app-psql">
7 <indexterm zone="app-psql">
8 <primary>psql</primary>
9 </indexterm>
11 <refmeta>
12 <refentrytitle><application>psql</application></refentrytitle>
13 <manvolnum>1</manvolnum>
14 <refmiscinfo>Application</refmiscinfo>
15 </refmeta>
17 <refnamediv>
18 <refname><application>psql</application></refname>
19 <refpurpose>
20 <productname>PostgreSQL</productname> interactive terminal
21 </refpurpose>
22 </refnamediv>
24 <refsynopsisdiv>
25 <cmdsynopsis>
26 <command>psql</command>
27 <arg rep="repeat"><replaceable class="parameter">option</replaceable></arg>
28 <arg choice="opt"><replaceable class="parameter">dbname</replaceable>
29 <arg choice="opt"><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 or from command line
42 arguments. In addition, <application>psql</application> provides a
43 number of meta-commands and various shell-like features to
44 facilitate writing scripts and automating a wide variety of tasks.
45 </para>
46 </refsect1>
48 <refsect1 id="r1-app-psql-3">
49 <title>Options</title>
51 <variablelist>
52 <varlistentry id="app-psql-option-echo-all">
53 <term><option>-a</option></term>
54 <term><option>--echo-all</option></term>
55 <listitem>
56 <para>
57 Print all nonempty input lines to standard output as they are read.
58 (This does not apply to lines read interactively.) This is
59 equivalent to setting the variable <varname>ECHO</varname> to
60 <literal>all</literal>.
61 </para>
62 </listitem>
63 </varlistentry>
65 <varlistentry id="app-psql-option-no-align">
66 <term><option>-A</option></term>
67 <term><option>--no-align</option></term>
68 <listitem>
69 <para>
70 Switches to unaligned output mode. (The default output mode is
71 <literal>aligned</literal>.) This is equivalent to
72 <command>\pset format unaligned</command>.
73 </para>
74 </listitem>
75 </varlistentry>
77 <varlistentry id="app-psql-option-echo-errors">
78 <term><option>-b</option></term>
79 <term><option>--echo-errors</option></term>
80 <listitem>
81 <para>
82 Print failed SQL commands to standard error output. This is
83 equivalent to setting the variable <varname>ECHO</varname> to
84 <literal>errors</literal>.
85 </para>
86 </listitem>
87 </varlistentry>
89 <varlistentry id="app-psql-option-command">
90 <term><option>-c <replaceable class="parameter">command</replaceable></option></term>
91 <term><option>--command=<replaceable class="parameter">command</replaceable></option></term>
92 <listitem>
93 <para>
94 Specifies that <application>psql</application> is to execute the given
95 command string, <replaceable class="parameter">command</replaceable>.
96 This option can be repeated and combined in any order with
97 the <option>-f</option> option. When either <option>-c</option>
98 or <option>-f</option> is specified, <application>psql</application>
99 does not read commands from standard input; instead it terminates
100 after processing all the <option>-c</option> and <option>-f</option>
101 options in sequence.
102 </para>
103 <para>
104 <replaceable class="parameter">command</replaceable> must be either
105 a command string that is completely parsable by the server (i.e.,
106 it contains no <application>psql</application>-specific features),
107 or a single backslash command. Thus you cannot mix
108 <acronym>SQL</acronym> and <application>psql</application>
109 meta-commands within a <option>-c</option> option. To achieve that,
110 you could use repeated <option>-c</option> options or pipe the string
111 into <application>psql</application>, for example:
112 <programlisting>
113 psql -c '\x' -c 'SELECT * FROM foo;'
114 </programlisting>
116 <programlisting>
117 echo '\x \\ SELECT * FROM foo;' | psql
118 </programlisting>
119 (<literal>\\</literal> is the separator meta-command.)
120 </para>
121 <para>
122 Each <acronym>SQL</acronym> command string passed
123 to <option>-c</option> is sent to the server as a single request.
124 Because of this, the server executes it as a single transaction even
125 if the string contains multiple <acronym>SQL</acronym> commands,
126 unless there are explicit <command>BEGIN</command>/<command>COMMIT</command>
127 commands included in the string to divide it into multiple
128 transactions. (See <xref linkend="protocol-flow-multi-statement"/>
129 for more details about how the server handles multi-query strings.)
130 </para>
131 <para>
132 If having several commands executed in one transaction is not desired,
133 use repeated <option>-c</option> commands or feed multiple commands to
134 <application>psql</application>'s standard input,
135 either using <application>echo</application> as illustrated above, or
136 via a shell here-document, for example:
137 <programlisting>
138 psql &lt;&lt;EOF
140 SELECT * FROM foo;
142 </programlisting></para>
143 </listitem>
144 </varlistentry>
146 <varlistentry id="app-psql-option-csv">
147 <term><option>--csv</option></term>
148 <listitem>
149 <para>
150 Switches to <acronym>CSV</acronym> (Comma-Separated Values) output
151 mode. This is equivalent to <command>\pset format csv</command>.
152 </para>
153 </listitem>
154 </varlistentry>
156 <varlistentry id="app-psql-option-dbname">
157 <term><option>-d <replaceable class="parameter">dbname</replaceable></option></term>
158 <term><option>--dbname=<replaceable class="parameter">dbname</replaceable></option></term>
159 <listitem>
160 <para>
161 Specifies the name of the database to connect to. This is
162 equivalent to specifying <replaceable
163 class="parameter">dbname</replaceable> as the first non-option
164 argument on the command line. The <replaceable>dbname</replaceable>
165 can be a <link linkend="libpq-connstring">connection string</link>.
166 If so, connection string parameters will override any conflicting
167 command line options.
168 </para>
169 </listitem>
170 </varlistentry>
172 <varlistentry id="app-psql-option-echo-queries">
173 <term><option>-e</option></term>
174 <term><option>--echo-queries</option></term>
175 <listitem>
176 <para>
177 Copy all SQL commands sent to the server to standard output as well.
178 This is equivalent
179 to setting the variable <varname>ECHO</varname> to
180 <literal>queries</literal>.
181 </para>
182 </listitem>
183 </varlistentry>
185 <varlistentry id="app-psql-option-echo-hidden">
186 <term><option>-E</option></term>
187 <term><option>--echo-hidden</option></term>
188 <listitem>
189 <para>
190 Echo the actual queries generated by <command>\d</command> and other backslash
191 commands. You can use this to study <application>psql</application>'s
192 internal operations. This is equivalent to
193 setting the variable <varname>ECHO_HIDDEN</varname> to <literal>on</literal>.
194 </para>
195 </listitem>
196 </varlistentry>
198 <varlistentry id="app-psql-option-file">
199 <term><option>-f <replaceable class="parameter">filename</replaceable></option></term>
200 <term><option>--file=<replaceable class="parameter">filename</replaceable></option></term>
201 <listitem>
202 <para>
203 Read commands from the
204 file <replaceable class="parameter">filename</replaceable>,
205 rather than standard input.
206 This option can be repeated and combined in any order with
207 the <option>-c</option> option. When either <option>-c</option>
208 or <option>-f</option> is specified, <application>psql</application>
209 does not read commands from standard input; instead it terminates
210 after processing all the <option>-c</option> and <option>-f</option>
211 options in sequence.
212 Except for that, this option is largely equivalent to the
213 meta-command <command>\i</command>.
214 </para>
216 <para>
217 If <replaceable>filename</replaceable> is <literal>-</literal>
218 (hyphen), then standard input is read until an EOF indication
219 or <command>\q</command> meta-command. This can be used to intersperse
220 interactive input with input from files. Note however that Readline
221 is not used in this case (much as if <option>-n</option> had been
222 specified).
223 </para>
225 <para>
226 Using this option is subtly different from writing <literal>psql
227 &lt; <replaceable
228 class="parameter">filename</replaceable></literal>. In general,
229 both will do what you expect, but using <literal>-f</literal>
230 enables some nice features such as error messages with line
231 numbers. There is also a slight chance that using this option will
232 reduce the start-up overhead. On the other hand, the variant using
233 the shell's input redirection is (in theory) guaranteed to yield
234 exactly the same output you would have received had you entered
235 everything by hand.
236 </para>
237 </listitem>
238 </varlistentry>
240 <varlistentry id="app-psql-option-field-separator">
241 <term><option>-F <replaceable class="parameter">separator</replaceable></option></term>
242 <term><option>--field-separator=<replaceable class="parameter">separator</replaceable></option></term>
243 <listitem>
244 <para>
245 Use <replaceable class="parameter">separator</replaceable> as the
246 field separator for unaligned output. This is equivalent to
247 <command>\pset fieldsep</command> or <command>\f</command>.
248 </para>
249 </listitem>
250 </varlistentry>
252 <varlistentry id="app-psql-option-field-host">
253 <term><option>-h <replaceable class="parameter">hostname</replaceable></option></term>
254 <term><option>--host=<replaceable class="parameter">hostname</replaceable></option></term>
255 <listitem>
256 <para>
257 Specifies the host name of the machine on which the
258 server is running. If the value begins
259 with a slash, it is used as the directory for the Unix-domain
260 socket.
261 </para>
262 </listitem>
263 </varlistentry>
265 <varlistentry id="app-psql-option-html">
266 <term><option>-H</option></term>
267 <term><option>--html</option></term>
268 <listitem>
269 <para>
270 Switches to <acronym>HTML</acronym> output mode. This is
271 equivalent to <command>\pset format html</command> or the
272 <command>\H</command> command.
273 </para>
274 </listitem>
275 </varlistentry>
277 <varlistentry id="app-psql-option-list">
278 <term><option>-l</option></term>
279 <term><option>--list</option></term>
280 <listitem>
281 <para>
282 List all available databases, then exit. Other non-connection
283 options are ignored. This is similar to the meta-command
284 <command>\list</command>.
285 </para>
287 <para>
288 When this option is used, <application>psql</application> will connect
289 to the database <literal>postgres</literal>, unless a different database
290 is named on the command line (option <option>-d</option> or non-option
291 argument, possibly via a service entry, but not via an environment
292 variable).
293 </para>
294 </listitem>
295 </varlistentry>
297 <varlistentry id="app-psql-option-log-file">
298 <term><option>-L <replaceable class="parameter">filename</replaceable></option></term>
299 <term><option>--log-file=<replaceable class="parameter">filename</replaceable></option></term>
300 <listitem>
301 <para>
302 Write all query output into file <replaceable
303 class="parameter">filename</replaceable>, in addition to the
304 normal output destination.
305 </para>
306 </listitem>
307 </varlistentry>
309 <varlistentry id="app-psql-option-no-readline">
310 <term><option>-n</option></term>
311 <term><option>--no-readline</option></term>
312 <listitem>
313 <para>
314 Do not use <application>Readline</application> for line editing and
315 do not use the command history (see
316 <xref linkend="app-psql-readline"/> below).
317 </para>
318 </listitem>
319 </varlistentry>
321 <varlistentry id="app-psql-option-output">
322 <term><option>-o <replaceable class="parameter">filename</replaceable></option></term>
323 <term><option>--output=<replaceable class="parameter">filename</replaceable></option></term>
324 <listitem>
325 <para>
326 Put all query output into file <replaceable
327 class="parameter">filename</replaceable>. This is equivalent to
328 the command <command>\o</command>.
329 </para>
330 </listitem>
331 </varlistentry>
333 <varlistentry id="app-psql-option-port">
334 <term><option>-p <replaceable class="parameter">port</replaceable></option></term>
335 <term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
336 <listitem>
337 <para>
338 Specifies the TCP port or the local Unix-domain
339 socket file extension on which the server is listening for
340 connections. Defaults to the value of the <envar>PGPORT</envar>
341 environment variable or, if not set, to the port specified at
342 compile time, usually 5432.
343 </para>
344 </listitem>
345 </varlistentry>
347 <varlistentry id="app-psql-option-pset">
348 <term><option>-P <replaceable class="parameter">assignment</replaceable></option></term>
349 <term><option>--pset=<replaceable class="parameter">assignment</replaceable></option></term>
350 <listitem>
351 <para>
352 Specifies printing options, in the style of
353 <command>\pset</command>. Note that here you
354 have to separate name and value with an equal sign instead of a
355 space. For example, to set the output format to <application>LaTeX</application>, you could write
356 <literal>-P format=latex</literal>.
357 </para>
358 </listitem>
359 </varlistentry>
361 <varlistentry id="app-psql-option-quiet">
362 <term><option>-q</option></term>
363 <term><option>--quiet</option></term>
364 <listitem>
365 <para>
366 Specifies that <application>psql</application> should do its work
367 quietly. By default, it prints welcome messages and various
368 informational output. If this option is used, none of this
369 happens. This is useful with the <option>-c</option> option.
370 This is equivalent to setting the variable <varname>QUIET</varname>
371 to <literal>on</literal>.
372 </para>
373 </listitem>
374 </varlistentry>
376 <varlistentry id="app-psql-option-record-separator">
377 <term><option>-R <replaceable class="parameter">separator</replaceable></option></term>
378 <term><option>--record-separator=<replaceable class="parameter">separator</replaceable></option></term>
379 <listitem>
380 <para>
381 Use <replaceable class="parameter">separator</replaceable> as the
382 record separator for unaligned output. This is equivalent to
383 <command>\pset recordsep</command>.
384 </para>
385 </listitem>
386 </varlistentry>
388 <varlistentry id="app-psql-option-single-step">
389 <term><option>-s</option></term>
390 <term><option>--single-step</option></term>
391 <listitem>
392 <para>
393 Run in single-step mode. That means the user is prompted before
394 each command is sent to the server, with the option to cancel
395 execution as well. Use this to debug scripts.
396 </para>
397 </listitem>
398 </varlistentry>
400 <varlistentry id="app-psql-option-single-line">
401 <term><option>-S</option></term>
402 <term><option>--single-line</option></term>
403 <listitem>
404 <para>
405 Runs in single-line mode where a newline terminates an SQL command, as a
406 semicolon does.
407 </para>
409 <note>
410 <para>
411 This mode is provided for those who insist on it, but you are not
412 necessarily encouraged to use it. In particular, if you mix
413 <acronym>SQL</acronym> and meta-commands on a line the order of
414 execution might not always be clear to the inexperienced user.
415 </para>
416 </note>
417 </listitem>
418 </varlistentry>
420 <varlistentry id="app-psql-option-tuples-only">
421 <term><option>-t</option></term>
422 <term><option>--tuples-only</option></term>
423 <listitem>
424 <para>
425 Turn off printing of column names and result row count footers,
426 etc. This is equivalent to <command>\t</command> or
427 <command>\pset tuples_only</command>.
428 </para>
429 </listitem>
430 </varlistentry>
432 <varlistentry id="app-psql-option-table-attr">
433 <term><option>-T <replaceable class="parameter">table_options</replaceable></option></term>
434 <term><option>--table-attr=<replaceable class="parameter">table_options</replaceable></option></term>
435 <listitem>
436 <para>
437 Specifies options to be placed within the
438 <acronym>HTML</acronym> <sgmltag>table</sgmltag> tag. See
439 <command>\pset tableattr</command> for details.
440 </para>
441 </listitem>
442 </varlistentry>
444 <varlistentry id="app-psql-option-username">
445 <term><option>-U <replaceable class="parameter">username</replaceable></option></term>
446 <term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
447 <listitem>
448 <para>
449 Connect to the database as the user <replaceable
450 class="parameter">username</replaceable> instead of the default.
451 (You must have permission to do so, of course.)
452 </para>
453 </listitem>
454 </varlistentry>
456 <varlistentry id="app-psql-option-variable">
457 <term><option>-v <replaceable class="parameter">assignment</replaceable></option></term>
458 <term><option>--set=<replaceable class="parameter">assignment</replaceable></option></term>
459 <term><option>--variable=<replaceable class="parameter">assignment</replaceable></option></term>
460 <listitem>
461 <para>
462 Perform a variable assignment, like the <command>\set</command>
463 meta-command. Note that you must separate name and value, if
464 any, by an equal sign on the command line. To unset a variable,
465 leave off the equal sign. To set a variable with an empty value,
466 use the equal sign but leave off the value. These assignments are
467 done during command line processing, so variables that reflect
468 connection state will get overwritten later.
469 </para>
470 </listitem>
471 </varlistentry>
473 <varlistentry id="app-psql-option-version">
474 <term><option>-V</option></term>
475 <term><option>--version</option></term>
476 <listitem>
477 <para>
478 Print the <application>psql</application> version and exit.
479 </para>
480 </listitem>
481 </varlistentry>
483 <varlistentry id="app-psql-option-no-password">
484 <term><option>-w</option></term>
485 <term><option>--no-password</option></term>
486 <listitem>
487 <para>
488 Never issue a password prompt. If the server requires password
489 authentication and a password is not available from other sources
490 such as a <filename>.pgpass</filename> file, the connection
491 attempt will fail. This option can be useful in batch jobs and
492 scripts where no user is present to enter a password.
493 </para>
495 <para>
496 Note that this option will remain set for the entire session,
497 and so it affects uses of the meta-command
498 <command>\connect</command> as well as the initial connection attempt.
499 </para>
500 </listitem>
501 </varlistentry>
503 <varlistentry id="app-psql-option-password">
504 <term><option>-W</option></term>
505 <term><option>--password</option></term>
506 <listitem>
507 <para>
508 Force <application>psql</application> to prompt for a
509 password before connecting to a database, even if the password will
510 not be used.
511 </para>
513 <para>
514 If the server requires password authentication and a password is not
515 available from other sources such as a <filename>.pgpass</filename>
516 file, <application>psql</application> will prompt for a
517 password in any case. However, <application>psql</application>
518 will waste a connection attempt finding out that the server wants a
519 password. In some cases it is worth typing <option>-W</option> to avoid
520 the extra connection attempt.
521 </para>
523 <para>
524 Note that this option will remain set for the entire session,
525 and so it affects uses of the meta-command
526 <command>\connect</command> as well as the initial connection attempt.
527 </para>
528 </listitem>
529 </varlistentry>
531 <varlistentry id="app-psql-option-expanded">
532 <term><option>-x</option></term>
533 <term><option>--expanded</option></term>
534 <listitem>
535 <para>
536 Turn on the expanded table formatting mode. This is equivalent to
537 <command>\x</command> or <command>\pset expanded</command>.
538 </para>
539 </listitem>
540 </varlistentry>
542 <varlistentry id="app-psql-option-no-psqlrc">
543 <term><option>-X</option></term>
544 <term><option>--no-psqlrc</option></term>
545 <listitem>
546 <para>
547 Do not read the start-up file (neither the system-wide
548 <filename>psqlrc</filename> file nor the user's
549 <filename>~/.psqlrc</filename> file).
550 </para>
551 </listitem>
552 </varlistentry>
554 <varlistentry id="app-psql-option-field-separator-zero">
555 <term><option>-z</option></term>
556 <term><option>--field-separator-zero</option></term>
557 <listitem>
558 <para>
559 Set the field separator for unaligned output to a zero byte. This is
560 equivalent to <command>\pset fieldsep_zero</command>.
561 </para>
562 </listitem>
563 </varlistentry>
565 <varlistentry id="app-psql-option-record-separator-zero">
566 <term><option>-0</option></term>
567 <term><option>--record-separator-zero</option></term>
568 <listitem>
569 <para>
570 Set the record separator for unaligned output to a zero byte. This is
571 useful for interfacing, for example, with <literal>xargs -0</literal>.
572 This is equivalent to <command>\pset recordsep_zero</command>.
573 </para>
574 </listitem>
575 </varlistentry>
577 <varlistentry id="app-psql-option-single-transaction">
578 <term><option>-1</option></term>
579 <term><option>--single-transaction</option></term>
580 <listitem>
581 <para>
582 This option can only be used in combination with one or more
583 <option>-c</option> and/or <option>-f</option> options. It causes
584 <application>psql</application> to issue a <command>BEGIN</command> command
585 before the first such option and a <command>COMMIT</command> command after
586 the last one, thereby wrapping all the commands into a single
587 transaction. If any of the commands fails and the variable
588 <varname>ON_ERROR_STOP</varname> was set, a
589 <command>ROLLBACK</command> command is sent instead. This ensures that
590 either all the commands complete successfully, or no changes are
591 applied.
592 </para>
594 <para>
595 If the commands themselves
596 contain <command>BEGIN</command>, <command>COMMIT</command>,
597 or <command>ROLLBACK</command>, this option will not have the desired
598 effects. Also, if an individual command cannot be executed inside a
599 transaction block, specifying this option will cause the whole
600 transaction to fail.
601 </para>
602 </listitem>
603 </varlistentry>
605 <varlistentry id="app-psql-option-help">
606 <term><option>-?</option></term>
607 <term><option>--help[=<replaceable class="parameter">topic</replaceable>]</option></term>
608 <listitem>
609 <para>
610 Show help about <application>psql</application> and exit. The optional
611 <replaceable class="parameter">topic</replaceable> parameter (defaulting
612 to <literal>options</literal>) selects which part of <application>psql</application> is
613 explained: <literal>commands</literal> describes <application>psql</application>'s
614 backslash commands; <literal>options</literal> describes the command-line
615 options that can be passed to <application>psql</application>;
616 and <literal>variables</literal> shows help about <application>psql</application> configuration
617 variables.
618 </para>
619 </listitem>
620 </varlistentry>
622 </variablelist>
623 </refsect1>
626 <refsect1>
627 <title>Exit Status</title>
629 <para>
630 <application>psql</application> returns 0 to the shell if it
631 finished normally, 1 if a fatal error of its own occurs (e.g., out of memory,
632 file not found), 2 if the connection to the server went bad
633 and the session was not interactive, and 3 if an error occurred in a
634 script and the variable <varname>ON_ERROR_STOP</varname> was set.
635 </para>
636 </refsect1>
639 <refsect1>
640 <title>Usage</title>
642 <refsect2 id="r2-app-psql-connecting">
643 <title>Connecting to a Database</title>
645 <para>
646 <application>psql</application> is a regular
647 <productname>PostgreSQL</productname> client application. In order
648 to connect to a database you need to know the name of your target
649 database, the host name and port number of the server, and what
650 database user name you want to connect as. <application>psql</application>
651 can be told about those parameters via command line options, namely
652 <option>-d</option>, <option>-h</option>, <option>-p</option>, and
653 <option>-U</option> respectively. If an argument is found that does
654 not belong to any option it will be interpreted as the database name
655 (or the database user name, if the database name is already given). Not all
656 of these options are required; there are useful defaults. If you omit the host
657 name, <application>psql</application> will connect via a Unix-domain socket
658 to a server on the local host, or via TCP/IP to <literal>localhost</literal> on
659 Windows. The default port number is
660 determined at compile time.
661 Since the database server uses the same default, you will not have
662 to specify the port in most cases. The default database user name is your
663 operating-system user name. Once the database user name is determined, it
664 is used as the default database name.
665 Note that you cannot
666 just connect to any database under any database user name. Your database
667 administrator should have informed you about your access rights.
668 </para>
670 <para>
671 When the defaults aren't quite right, you can save yourself
672 some typing by setting the environment variables
673 <envar>PGDATABASE</envar>, <envar>PGHOST</envar>,
674 <envar>PGPORT</envar> and/or <envar>PGUSER</envar> to appropriate
675 values. (For additional environment variables, see <xref
676 linkend="libpq-envars"/>.) It is also convenient to have a
677 <filename>~/.pgpass</filename> file to avoid regularly having to type in
678 passwords. See <xref linkend="libpq-pgpass"/> for more information.
679 </para>
681 <para>
682 An alternative way to specify connection parameters is in a
683 <parameter>conninfo</parameter> string or
684 a <acronym>URI</acronym>, which is used instead of a database
685 name. This mechanism give you very wide control over the
686 connection. For example:
687 <programlisting>
688 $ <userinput>psql "service=myservice sslmode=require"</userinput>
689 $ <userinput>psql postgresql://dbmaster:5433/mydb?sslmode=require</userinput>
690 </programlisting>
691 This way you can also use <acronym>LDAP</acronym> for connection
692 parameter lookup as described in <xref linkend="libpq-ldap"/>.
693 See <xref linkend="libpq-paramkeywords"/> for more information on all the
694 available connection options.
695 </para>
697 <para>
698 If the connection could not be made for any reason (e.g., insufficient
699 privileges, server is not running on the targeted host, etc.),
700 <application>psql</application> will return an error and terminate.
701 </para>
703 <para>
704 If both standard input and standard output are a
705 terminal, then <application>psql</application> sets the client
706 encoding to <quote>auto</quote>, which will detect the
707 appropriate client encoding from the locale settings
708 (<envar>LC_CTYPE</envar> environment variable on Unix systems).
709 If this doesn't work out as expected, the client encoding can be
710 overridden using the environment
711 variable <envar>PGCLIENTENCODING</envar>.
712 </para>
713 </refsect2>
715 <refsect2 id="r2-app-psql-4">
716 <title>Entering SQL Commands</title>
718 <para>
719 In normal operation, <application>psql</application> provides a
720 prompt with the name of the database to which
721 <application>psql</application> is currently connected, followed by
722 the string <literal>=&gt;</literal>. For example:
723 <programlisting>
724 $ <userinput>psql testdb</userinput>
725 psql (&version;)
726 Type "help" for help.
728 testdb=&gt;
729 </programlisting>
730 </para>
732 <para>
733 At the prompt, the user can type in <acronym>SQL</acronym> commands.
734 Ordinarily, input lines are sent to the server when a
735 command-terminating semicolon is reached. An end of line does not
736 terminate a command. Thus commands can be spread over several lines for
737 clarity. If the command was sent and executed without error, the results
738 of the command are displayed on the screen.
739 </para>
741 <para>
742 If untrusted users have access to a database that has not adopted a
743 <link linkend="ddl-schemas-patterns">secure schema usage pattern</link>,
744 begin your session by removing publicly-writable schemas
745 from <varname>search_path</varname>. One can
746 add <literal>options=-csearch_path=</literal> to the connection string or
747 issue <literal>SELECT pg_catalog.set_config('search_path', '',
748 false)</literal> before other SQL commands. This consideration is not
749 specific to <application>psql</application>; it applies to every interface
750 for executing arbitrary SQL commands.
751 </para>
753 <para>
754 Whenever a command is executed, <application>psql</application> also polls
755 for asynchronous notification events generated by
756 <link linkend="sql-listen"><command>LISTEN</command></link> and
757 <link linkend="sql-notify"><command>NOTIFY</command></link>.
758 </para>
760 <para>
761 While C-style block comments are passed to the server for
762 processing and removal, SQL-standard comments are removed by
763 <application>psql</application>.
764 </para>
765 </refsect2>
767 <refsect2 id="app-psql-meta-commands">
768 <title>Meta-Commands</title>
770 <para>
771 Anything you enter in <application>psql</application> that begins
772 with an unquoted backslash is a <application>psql</application>
773 meta-command that is processed by <application>psql</application>
774 itself. These commands make
775 <application>psql</application> more useful for administration or
776 scripting. Meta-commands are often called slash or backslash commands.
777 </para>
779 <para>
780 The format of a <application>psql</application> command is the backslash,
781 followed immediately by a command verb, then any arguments. The arguments
782 are separated from the command verb and each other by any number of
783 whitespace characters.
784 </para>
786 <para>
787 To include whitespace in an argument you can quote it with
788 single quotes. To include a single quote in an argument,
789 write two single quotes within single-quoted text.
790 Anything contained in single quotes is
791 furthermore subject to C-like substitutions for
792 <literal>\n</literal> (new line), <literal>\t</literal> (tab),
793 <literal>\b</literal> (backspace), <literal>\r</literal> (carriage return),
794 <literal>\f</literal> (form feed),
795 <literal>\</literal><replaceable>digits</replaceable> (octal), and
796 <literal>\x</literal><replaceable>digits</replaceable> (hexadecimal).
797 A backslash preceding any other character within single-quoted text
798 quotes that single character, whatever it is.
799 </para>
801 <para>
802 If an unquoted colon (<literal>:</literal>) followed by a
803 <application>psql</application> variable name appears within an argument, it is
804 replaced by the variable's value, as described in <xref
805 linkend="app-psql-interpolation"/> below.
806 The forms <literal>:'<replaceable>variable_name</replaceable>'</literal> and
807 <literal>:"<replaceable>variable_name</replaceable>"</literal> described there
808 work as well.
809 The <literal>:{?<replaceable>variable_name</replaceable>}</literal> syntax allows
810 testing whether a variable is defined. It is substituted by
811 TRUE or FALSE.
812 Escaping the colon with a backslash protects it from substitution.
813 </para>
815 <para>
816 Within an argument, text that is enclosed in backquotes
817 (<literal>`</literal>) is taken as a command line that is passed to the
818 shell. The output of the command (with any trailing newline removed)
819 replaces the backquoted text. Within the text enclosed in backquotes,
820 no special quoting or other processing occurs, except that appearances
821 of <literal>:<replaceable>variable_name</replaceable></literal> where
822 <replaceable>variable_name</replaceable> is a <application>psql</application> variable name
823 are replaced by the variable's value. Also, appearances of
824 <literal>:'<replaceable>variable_name</replaceable>'</literal> are replaced by the
825 variable's value suitably quoted to become a single shell command
826 argument. (The latter form is almost always preferable, unless you are
827 very sure of what is in the variable.) Because carriage return and line
828 feed characters cannot be safely quoted on all platforms, the
829 <literal>:'<replaceable>variable_name</replaceable>'</literal> form prints an
830 error message and does not substitute the variable value when such
831 characters appear in the value.
832 </para>
834 <para>
835 Some commands take an <acronym>SQL</acronym> identifier (such as a
836 table name) as argument. These arguments follow the syntax rules
837 of <acronym>SQL</acronym>: Unquoted letters are forced to
838 lowercase, while double quotes (<literal>"</literal>) protect letters
839 from case conversion and allow incorporation of whitespace into
840 the identifier. Within double quotes, paired double quotes reduce
841 to a single double quote in the resulting name. For example,
842 <literal>FOO"BAR"BAZ</literal> is interpreted as <literal>fooBARbaz</literal>,
843 and <literal>"A weird"" name"</literal> becomes <literal>A weird"
844 name</literal>.
845 </para>
847 <para>
848 Parsing for arguments stops at the end of the line, or when another
849 unquoted backslash is found. An unquoted backslash
850 is taken as the beginning of a new meta-command. The special
851 sequence <literal>\\</literal> (two backslashes) marks the end of
852 arguments and continues parsing <acronym>SQL</acronym> commands, if
853 any. That way <acronym>SQL</acronym> and
854 <application>psql</application> commands can be freely mixed on a
855 line. But in any case, the arguments of a meta-command cannot
856 continue beyond the end of the line.
857 </para>
859 <para>
860 Many of the meta-commands act on the <firstterm>current query buffer</firstterm>.
861 This is simply a buffer holding whatever SQL command text has been typed
862 but not yet sent to the server for execution. This will include previous
863 input lines as well as any text appearing before the meta-command on the
864 same line.
865 </para>
867 <para>
868 The following meta-commands are defined:
870 <variablelist>
871 <varlistentry id="app-psql-meta-command-a">
872 <term><literal>\a</literal></term>
873 <listitem>
874 <para>
875 If the current table output format is unaligned, it is switched to aligned.
876 If it is not unaligned, it is set to unaligned. This command is
877 kept for backwards compatibility. See <command>\pset</command> for a
878 more general solution.
879 </para>
880 </listitem>
881 </varlistentry>
883 <varlistentry id="app-psql-meta-command-bind">
884 <term><literal>\bind</literal> [ <replaceable class="parameter">parameter</replaceable> ] ... </term>
886 <listitem>
887 <para>
888 Sets query parameters for the next query execution, with the
889 specified parameters passed for any parameter placeholders
890 (<literal>$1</literal> etc.).
891 </para>
893 <para>
894 Example:
895 <programlisting>
896 INSERT INTO tbl1 VALUES ($1, $2) \bind 'first value' 'second value' \g
897 </programlisting>
898 </para>
900 <para>
901 This also works for query-execution commands besides
902 <literal>\g</literal>, such as <literal>\gx</literal> and
903 <literal>\gset</literal>.
904 </para>
906 <para>
907 This command causes the extended query protocol (see <xref
908 linkend="protocol-query-concepts"/>) to be used, unlike normal
909 <application>psql</application> operation, which uses the simple
910 query protocol. So this command can be useful to test the extended
911 query protocol from <application>psql</application>. (The extended
912 query protocol is used even if the query has no parameters and this
913 command specifies zero parameters.) This command affects only the
914 next query executed; all subsequent queries will use the simple query
915 protocol by default.
916 </para>
917 </listitem>
918 </varlistentry>
920 <varlistentry id="app-psql-meta-command-bind-named">
921 <term><literal>\bind_named</literal> <replaceable class="parameter">statement_name</replaceable> [ <replaceable class="parameter">parameter</replaceable> ] ... </term>
923 <listitem>
924 <para>
925 <literal>\bind_named</literal> is equivalent to <literal>\bind</literal>,
926 except that it takes the name of an existing prepared statement as
927 first parameter. An empty string denotes the unnamed prepared
928 statement.
929 </para>
931 <para>
932 Example:
933 <programlisting>
934 INSERT INTO tbls1 VALUES ($1, $2) \parse stmt1
935 \bind_named stmt1 'first value' 'second value' \g
936 </programlisting>
937 </para>
939 <para>
940 This command causes the extended query protocol (see
941 <xref linkend="protocol-query-concepts"/>) to be used, unlike normal
942 <application>psql</application> operation, which uses the simple
943 query protocol. So this command can be useful to test the extended
944 query protocol from <application>psql</application>.
945 </para>
947 </listitem>
948 </varlistentry>
950 <varlistentry id="app-psql-meta-command-c-lc">
951 <term><literal>\c</literal> or <literal>\connect [ -reuse-previous=<replaceable class="parameter">on|off</replaceable> ] [ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] [ <replaceable class="parameter">host</replaceable> ] [ <replaceable class="parameter">port</replaceable> ] | <replaceable class="parameter">conninfo</replaceable> ]</literal></term>
952 <listitem>
953 <para>
954 Establishes a new connection to a <productname>PostgreSQL</productname>
955 server. The connection parameters to use can be specified either
956 using a positional syntax (one or more of database name, user,
957 host, and port), or using a <replaceable>conninfo</replaceable>
958 connection string as detailed in
959 <xref linkend="libpq-connstring"/>. If no arguments are given, a
960 new connection is made using the same parameters as before.
961 </para>
963 <para>
964 Specifying any
965 of <replaceable class="parameter">dbname</replaceable>,
966 <replaceable class="parameter">username</replaceable>,
967 <replaceable class="parameter">host</replaceable> or
968 <replaceable class="parameter">port</replaceable>
969 as <literal>-</literal> is equivalent to omitting that parameter.
970 </para>
972 <para>
973 The new connection can re-use connection parameters from the previous
974 connection; not only database name, user, host, and port, but other
975 settings such as <replaceable>sslmode</replaceable>. By default,
976 parameters are re-used in the positional syntax, but not when
977 a <replaceable>conninfo</replaceable> string is given. Passing a
978 first argument of <literal>-reuse-previous=on</literal>
979 or <literal>-reuse-previous=off</literal> overrides that default. If
980 parameters are re-used, then any parameter not explicitly specified as
981 a positional parameter or in the <replaceable>conninfo</replaceable>
982 string is taken from the existing connection's parameters. An
983 exception is that if the <replaceable>host</replaceable> setting
984 is changed from its previous value using the positional syntax,
985 any <replaceable>hostaddr</replaceable> setting present in the
986 existing connection's parameters is dropped.
987 Also, any password used for the existing connection will be re-used
988 only if the user, host, and port settings are not changed.
989 When the command neither specifies nor reuses a particular parameter,
990 the <application>libpq</application> default is used.
991 </para>
993 <para>
994 If the new connection is successfully made, the previous
995 connection is closed.
996 If the connection attempt fails (wrong user name, access
997 denied, etc.), the previous connection will be kept if
998 <application>psql</application> is in interactive mode. But when
999 executing a non-interactive script, the old connection is closed
1000 and an error is reported. That may or may not terminate the
1001 script; if it does not, all database-accessing commands will fail
1002 until another <literal>\connect</literal> command is successfully
1003 executed. This distinction was chosen as
1004 a user convenience against typos on the one hand, and a safety
1005 mechanism that scripts are not accidentally acting on the
1006 wrong database on the other hand.
1007 Note that whenever a <literal>\connect</literal> command attempts
1008 to re-use parameters, the values re-used are those of the last
1009 successful connection, not of any failed attempts made subsequently.
1010 However, in the case of a
1011 non-interactive <literal>\connect</literal> failure, no parameters
1012 are allowed to be re-used later, since the script would likely be
1013 expecting the values from the failed <literal>\connect</literal>
1014 to be re-used.
1015 </para>
1017 <para>
1018 Examples:
1019 </para>
1020 <programlisting>
1021 =&gt; \c mydb myuser host.dom 6432
1022 =&gt; \c service=foo
1023 =&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
1024 =&gt; \c -reuse-previous=on sslmode=require -- changes only sslmode
1025 =&gt; \c postgresql://tom@localhost/mydb?application_name=myapp
1026 </programlisting>
1027 </listitem>
1028 </varlistentry>
1030 <varlistentry id="app-psql-meta-command-c-uc">
1031 <term><literal>\C [ <replaceable class="parameter">title</replaceable> ]</literal></term>
1032 <listitem>
1033 <para>
1034 Sets the title of any tables being printed as the result of a
1035 query or unset any such title. This command is equivalent to
1036 <literal>\pset title <replaceable
1037 class="parameter">title</replaceable></literal>. (The name of
1038 this command derives from <quote>caption</quote>, as it was
1039 previously only used to set the caption in an
1040 <acronym>HTML</acronym> table.)
1041 </para>
1042 </listitem>
1043 </varlistentry>
1045 <varlistentry id="app-psql-meta-command-cd">
1046 <term><literal>\cd [ <replaceable>directory</replaceable> ]</literal></term>
1047 <listitem>
1048 <para>
1049 Changes the current working directory to
1050 <replaceable>directory</replaceable>. Without argument, changes
1051 to the current user's home directory. For details on how home
1052 directories are found, see <xref linkend="libpq-pgpass"/>.
1053 </para>
1055 <tip>
1056 <para>
1057 To print your current working directory, use <literal>\! pwd</literal>.
1058 </para>
1059 </tip>
1060 </listitem>
1061 </varlistentry>
1063 <varlistentry id="app-psql-meta-command-conninfo">
1064 <term><literal>\conninfo</literal></term>
1065 <listitem>
1066 <para>
1067 Outputs information about the current database connection.
1068 </para>
1069 </listitem>
1070 </varlistentry>
1072 <varlistentry id="app-psql-meta-command-close">
1073 <term><literal>\close</literal> <replaceable class="parameter">prepared_statement_name</replaceable></term>
1075 <listitem>
1076 <para>
1077 Closes the specified prepared statement. An empty string denotes the
1078 unnamed prepared statement. If no prepared statement exists with this
1079 name, the operation is a no-op.
1080 </para>
1082 <para>
1083 Example:
1084 <programlisting>
1085 SELECT $1 \parse stmt1
1086 \close stmt1
1087 </programlisting>
1088 </para>
1090 <para>
1091 This command causes the extended query protocol to be used,
1092 unlike normal <application>psql</application> operation, which
1093 uses the simple query protocol. So this command can be useful
1094 to test the extended query protocol from
1095 <application>psql</application>.
1096 </para>
1098 </listitem>
1099 </varlistentry>
1101 <varlistentry id="app-psql-meta-commands-copy">
1102 <term><literal>\copy { <replaceable class="parameter">table</replaceable> [ ( <replaceable class="parameter">column_list</replaceable> ) ] }
1103 <literal>from</literal>
1104 { <replaceable class="parameter">'filename'</replaceable> | program <replaceable class="parameter">'command'</replaceable> | stdin | pstdin }
1105 [ [ with ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
1106 [ where <replaceable class="parameter">condition</replaceable> ]</literal></term>
1108 <term><literal>\copy { <replaceable class="parameter">table</replaceable> [ ( <replaceable class="parameter">column_list</replaceable> ) ] | ( <replaceable class="parameter">query</replaceable> ) }
1109 <literal>to</literal>
1110 { <replaceable class="parameter">'filename'</replaceable> | program <replaceable class="parameter">'command'</replaceable> | stdout | pstdout }
1111 [ [ with ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]</literal></term>
1113 <listitem>
1114 <para>
1115 Performs a frontend (client) copy. This is an operation that
1116 runs an <acronym>SQL</acronym> <link linkend="sql-copy"><command>COPY</command></link>
1117 command, but instead of the server
1118 reading or writing the specified file,
1119 <application>psql</application> reads or writes the file and
1120 routes the data between the server and the local file system.
1121 This means that file accessibility and privileges are those of
1122 the local user, not the server, and no SQL superuser
1123 privileges are required.
1124 </para>
1126 <para>
1127 When <literal>program</literal> is specified,
1128 <replaceable class="parameter">command</replaceable> is
1129 executed by <application>psql</application> and the data passed from
1130 or to <replaceable class="parameter">command</replaceable> is
1131 routed between the server and the client.
1132 Again, the execution privileges are those of
1133 the local user, not the server, and no SQL superuser
1134 privileges are required.
1135 </para>
1137 <para>
1138 For <literal>\copy ... from stdin</literal>, data rows are read from the same
1139 source that issued the command, continuing until a line containing
1140 only <literal>\.</literal>
1141 is read or the stream reaches <acronym>EOF</acronym>. This option is useful
1142 for populating tables in-line within an SQL script file.
1143 For <literal>\copy ... to stdout</literal>, output is sent to the same place
1144 as <application>psql</application> command output, and
1145 the <literal>COPY <replaceable>count</replaceable></literal> command status is
1146 not printed (since it might be confused with a data row).
1147 To read/write <application>psql</application>'s standard input or
1148 output regardless of the current command source or <literal>\o</literal>
1149 option, write <literal>from pstdin</literal> or <literal>to pstdout</literal>.
1150 </para>
1152 <para>
1153 The syntax of this command is similar to that of the
1154 <acronym>SQL</acronym> <link linkend="sql-copy"><command>COPY</command></link>
1155 command. All options other than the data source/destination are
1156 as specified for <command>COPY</command>.
1157 Because of this, special parsing rules apply to the <command>\copy</command>
1158 meta-command. Unlike most other meta-commands, the entire remainder
1159 of the line is always taken to be the arguments of <command>\copy</command>,
1160 and neither variable interpolation nor backquote expansion are
1161 performed in the arguments.
1162 </para>
1164 <tip>
1165 <para>
1166 Another way to obtain the same result as <literal>\copy
1167 ... to</literal> is to use the <acronym>SQL</acronym> <literal>COPY
1168 ... TO STDOUT</literal> command and terminate it
1169 with <literal>\g <replaceable>filename</replaceable></literal>
1170 or <literal>\g |<replaceable>program</replaceable></literal>.
1171 Unlike <literal>\copy</literal>, this method allows the command to
1172 span multiple lines; also, variable interpolation and backquote
1173 expansion can be used.
1174 </para>
1175 </tip>
1177 <tip>
1178 <para>
1179 These operations are not as efficient as the <acronym>SQL</acronym>
1180 <command>COPY</command> command with a file or program data source or
1181 destination, because all data must pass through the client/server
1182 connection. For large amounts of data the <acronym>SQL</acronym>
1183 command might be preferable.
1184 </para>
1185 </tip>
1187 </listitem>
1188 </varlistentry>
1190 <varlistentry id="app-psql-meta-command-copyright">
1191 <term><literal>\copyright</literal></term>
1192 <listitem>
1193 <para>
1194 Shows the copyright and distribution terms of
1195 <productname>PostgreSQL</productname>.
1196 </para>
1197 </listitem>
1198 </varlistentry>
1201 <varlistentry id="app-psql-meta-commands-crosstabview">
1202 <term><literal>\crosstabview [
1203 <replaceable class="parameter">colV</replaceable>
1204 [ <replaceable class="parameter">colH</replaceable>
1205 [ <replaceable class="parameter">colD</replaceable>
1206 [ <replaceable class="parameter">sortcolH</replaceable>
1207 ] ] ] ] </literal></term>
1208 <listitem>
1209 <para>
1210 Executes the current query buffer (like <literal>\g</literal>) and
1211 shows the results in a crosstab grid.
1212 The query must return at least three columns.
1213 The output column identified by <replaceable class="parameter">colV</replaceable>
1214 becomes a vertical header and the output column identified by
1215 <replaceable class="parameter">colH</replaceable>
1216 becomes a horizontal header.
1217 <replaceable class="parameter">colD</replaceable> identifies
1218 the output column to display within the grid.
1219 <replaceable class="parameter">sortcolH</replaceable> identifies
1220 an optional sort column for the horizontal header.
1221 </para>
1223 <para>
1224 Each column specification can be a column number (starting at 1) or
1225 a column name. The usual SQL case folding and quoting rules apply to
1226 column names. If omitted,
1227 <replaceable class="parameter">colV</replaceable> is taken as column 1
1228 and <replaceable class="parameter">colH</replaceable> as column 2.
1229 <replaceable class="parameter">colH</replaceable> must differ from
1230 <replaceable class="parameter">colV</replaceable>.
1231 If <replaceable class="parameter">colD</replaceable> is not
1232 specified, then there must be exactly three columns in the query
1233 result, and the column that is neither
1234 <replaceable class="parameter">colV</replaceable> nor
1235 <replaceable class="parameter">colH</replaceable>
1236 is taken to be <replaceable class="parameter">colD</replaceable>.
1237 </para>
1239 <para>
1240 The vertical header, displayed as the leftmost column, contains the
1241 values found in column <replaceable class="parameter">colV</replaceable>, in the
1242 same order as in the query results, but with duplicates removed.
1243 </para>
1245 <para>
1246 The horizontal header, displayed as the first row, contains the values
1247 found in column <replaceable class="parameter">colH</replaceable>,
1248 with duplicates removed. By default, these appear in the same order
1249 as in the query results. But if the
1250 optional <replaceable class="parameter">sortcolH</replaceable> argument is given,
1251 it identifies a column whose values must be integer numbers, and the
1252 values from <replaceable class="parameter">colH</replaceable> will
1253 appear in the horizontal header sorted according to the
1254 corresponding <replaceable class="parameter">sortcolH</replaceable> values.
1255 </para>
1257 <para>
1258 Inside the crosstab grid, for each distinct value <literal>x</literal>
1259 of <replaceable class="parameter">colH</replaceable> and each distinct
1260 value <literal>y</literal>
1261 of <replaceable class="parameter">colV</replaceable>, the cell located
1262 at the intersection <literal>(x,y)</literal> contains the value of
1263 the <literal>colD</literal> column in the query result row for which
1264 the value of <replaceable class="parameter">colH</replaceable>
1265 is <literal>x</literal> and the value
1266 of <replaceable class="parameter">colV</replaceable>
1267 is <literal>y</literal>. If there is no such row, the cell is empty. If
1268 there are multiple such rows, an error is reported.
1269 </para>
1270 </listitem>
1271 </varlistentry>
1274 <varlistentry id="app-psql-meta-command-d">
1275 <term><literal>\d[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1277 <listitem>
1278 <para>
1279 For each relation (table, view, materialized view, index, sequence,
1280 or foreign table)
1281 or composite type matching the
1282 <replaceable class="parameter">pattern</replaceable>, show all
1283 columns, their types, the tablespace (if not the default) and any
1284 special attributes such as <literal>NOT NULL</literal> or defaults.
1285 Associated indexes, constraints, rules, and triggers are
1286 also shown. For foreign tables, the associated foreign
1287 server is shown as well.
1288 (<quote>Matching the pattern</quote> is defined in
1289 <xref linkend="app-psql-patterns"/> below.)
1290 </para>
1292 <para>
1293 For some types of relation, <literal>\d</literal> shows additional information
1294 for each column: column values for sequences, indexed expressions for
1295 indexes, and foreign data wrapper options for foreign tables.
1296 </para>
1298 <para>
1299 The command form <literal>\d+</literal> is identical, except that
1300 more information is displayed: any comments associated with the
1301 columns of the table are shown, as is the presence of OIDs in the
1302 table, the view definition if the relation is a view, a non-default
1303 <link linkend="sql-altertable-replica-identity">replica
1304 identity</link> setting and the
1305 <link linkend="sql-create-access-method">access method</link> name
1306 if the relation has an access method.
1307 </para>
1309 <para>
1310 By default, only user-created objects are shown; supply a
1311 pattern or the <literal>S</literal> modifier to include system
1312 objects.
1313 </para>
1315 <note>
1316 <para>
1317 If <command>\d</command> is used without a
1318 <replaceable class="parameter">pattern</replaceable> argument, it is
1319 equivalent to <command>\dtvmsE</command> which will show a list of
1320 all visible tables, views, materialized views, sequences and
1321 foreign tables.
1322 This is purely a convenience measure.
1323 </para>
1324 </note>
1325 </listitem>
1326 </varlistentry>
1328 <varlistentry id="app-psql-meta-command-da-lc">
1329 <term><literal>\da[S] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1331 <listitem>
1332 <para>
1333 Lists aggregate functions, together with their
1334 return type and the data types they operate on. If <replaceable
1335 class="parameter">pattern</replaceable>
1336 is specified, only aggregates whose names match the pattern are shown.
1337 By default, only user-created objects are shown; supply a
1338 pattern or the <literal>S</literal> modifier to include system
1339 objects.
1340 </para>
1341 </listitem>
1342 </varlistentry>
1344 <varlistentry id="app-psql-meta-command-da-uc">
1345 <term><literal>\dA[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1347 <listitem>
1348 <para>
1349 Lists access methods. If <replaceable
1350 class="parameter">pattern</replaceable> is specified, only access
1351 methods whose names match the pattern are shown. If
1352 <literal>+</literal> is appended to the command name, each access
1353 method is listed with its associated handler function and description.
1354 </para>
1355 </listitem>
1356 </varlistentry>
1358 <varlistentry id="app-psql-meta-command-dac">
1359 <term>
1360 <literal>\dAc[+]
1361 [<link linkend="app-psql-patterns"><replaceable class="parameter">access-method-pattern</replaceable></link>
1362 [<link linkend="app-psql-patterns"><replaceable class="parameter">input-type-pattern</replaceable></link>]]
1363 </literal>
1364 </term>
1365 <listitem>
1366 <para>
1367 Lists operator classes
1368 (see <xref linkend="xindex-opclass"/>).
1369 If <replaceable class="parameter">access-method-pattern</replaceable>
1370 is specified, only operator classes associated with access methods whose
1371 names match that pattern are listed.
1372 If <replaceable class="parameter">input-type-pattern</replaceable>
1373 is specified, only operator classes associated with input types whose
1374 names match that pattern are listed.
1375 If <literal>+</literal> is appended to the command name, each operator
1376 class is listed with its associated operator family and owner.
1377 </para>
1378 </listitem>
1379 </varlistentry>
1381 <varlistentry id="app-psql-meta-command-daf">
1382 <term>
1383 <literal>\dAf[+]
1384 [<link linkend="app-psql-patterns"><replaceable class="parameter">access-method-pattern</replaceable></link>
1385 [<link linkend="app-psql-patterns"><replaceable class="parameter">input-type-pattern</replaceable></link>]]
1386 </literal>
1387 </term>
1388 <listitem>
1389 <para>
1390 Lists operator families
1391 (see <xref linkend="xindex-opfamily"/>).
1392 If <replaceable class="parameter">access-method-pattern</replaceable>
1393 is specified, only operator families associated with access methods whose
1394 names match that pattern are listed.
1395 If <replaceable class="parameter">input-type-pattern</replaceable>
1396 is specified, only operator families associated with input types whose
1397 names match that pattern are listed.
1398 If <literal>+</literal> is appended to the command name, each operator
1399 family is listed with its owner.
1400 </para>
1401 </listitem>
1402 </varlistentry>
1404 <varlistentry id="app-psql-meta-command-dao">
1405 <term>
1406 <literal>\dAo[+]
1407 [<link linkend="app-psql-patterns"><replaceable class="parameter">access-method-pattern</replaceable></link>
1408 [<link linkend="app-psql-patterns"><replaceable class="parameter">operator-family-pattern</replaceable></link>]]
1409 </literal>
1410 </term>
1412 <listitem>
1413 <para>
1414 Lists operators associated with operator families
1415 (see <xref linkend="xindex-strategies"/>).
1416 If <replaceable class="parameter">access-method-pattern</replaceable>
1417 is specified, only members of operator families associated with access
1418 methods whose names match that pattern are listed.
1419 If <replaceable class="parameter">operator-family-pattern</replaceable>
1420 is specified, only members of operator families whose names match that
1421 pattern are listed.
1422 If <literal>+</literal> is appended to the command name, each operator
1423 is listed with its sort operator family (if it is an ordering operator).
1424 </para>
1425 </listitem>
1426 </varlistentry>
1428 <varlistentry id="app-psql-meta-command-dap">
1429 <term>
1430 <literal>\dAp[+]
1431 [<link linkend="app-psql-patterns"><replaceable class="parameter">access-method-pattern</replaceable></link>
1432 [<link linkend="app-psql-patterns"><replaceable class="parameter">operator-family-pattern</replaceable></link>]]
1433 </literal>
1434 </term>
1435 <listitem>
1436 <para>
1437 Lists support functions associated with operator families
1438 (see <xref linkend="xindex-support"/>).
1439 If <replaceable class="parameter">access-method-pattern</replaceable>
1440 is specified, only functions of operator families associated with
1441 access methods whose names match that pattern are listed.
1442 If <replaceable class="parameter">operator-family-pattern</replaceable>
1443 is specified, only functions of operator families whose names match
1444 that pattern are listed.
1445 If <literal>+</literal> is appended to the command name, functions are
1446 displayed verbosely, with their actual parameter lists.
1447 </para>
1448 </listitem>
1449 </varlistentry>
1451 <varlistentry id="app-psql-meta-command-db">
1452 <term><literal>\db[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1454 <listitem>
1455 <para>
1456 Lists tablespaces. If <replaceable
1457 class="parameter">pattern</replaceable>
1458 is specified, only tablespaces whose names match the pattern are shown.
1459 If <literal>+</literal> is appended to the command name, each tablespace
1460 is listed with its associated options, on-disk size, permissions and
1461 description.
1462 </para>
1463 </listitem>
1464 </varlistentry>
1467 <varlistentry id="app-psql-meta-command-dc-lc">
1468 <term><literal>\dc[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1469 <listitem>
1470 <para>
1471 Lists conversions between character-set encodings.
1472 If <replaceable class="parameter">pattern</replaceable>
1473 is specified, only conversions whose names match the pattern are
1474 listed.
1475 By default, only user-created objects are shown; supply a
1476 pattern or the <literal>S</literal> modifier to include system
1477 objects.
1478 If <literal>+</literal> is appended to the command name, each object
1479 is listed with its associated description.
1480 </para>
1481 </listitem>
1482 </varlistentry>
1485 <varlistentry id="app-psql-meta-command-dconfig">
1486 <term><literal>\dconfig[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1487 <listitem>
1488 <para>
1489 Lists server configuration parameters and their values.
1490 If <replaceable class="parameter">pattern</replaceable> is specified,
1491 only parameters whose names match the pattern are listed. Without
1492 a <replaceable class="parameter">pattern</replaceable>, only
1493 parameters that are set to non-default values are listed.
1494 (Use <literal>\dconfig *</literal> to see all parameters.)
1495 If <literal>+</literal> is appended to the command name, each
1496 parameter is listed with its data type, context in which the
1497 parameter can be set, and access privileges (if non-default access
1498 privileges have been granted).
1499 </para>
1500 </listitem>
1501 </varlistentry>
1504 <varlistentry id="app-psql-meta-command-dc-uc">
1505 <term><literal>\dC[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1506 <listitem>
1507 <para>
1508 Lists type casts.
1509 If <replaceable class="parameter">pattern</replaceable>
1510 is specified, only casts whose source or target types match the
1511 pattern are listed.
1512 If <literal>+</literal> is appended to the command name, each object
1513 is listed with its associated description.
1514 </para>
1515 </listitem>
1516 </varlistentry>
1519 <varlistentry id="app-psql-meta-command-dd-lc">
1520 <term><literal>\dd[S] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1521 <listitem>
1522 <para>
1523 Shows the descriptions of objects of type <literal>constraint</literal>,
1524 <literal>operator class</literal>, <literal>operator family</literal>,
1525 <literal>rule</literal>, and <literal>trigger</literal>. All
1526 other comments may be viewed by the respective backslash commands for
1527 those object types.
1528 </para>
1530 <para><literal>\dd</literal> displays descriptions for objects matching the
1531 <replaceable class="parameter">pattern</replaceable>, or of visible
1532 objects of the appropriate type if no argument is given. But in either
1533 case, only objects that have a description are listed.
1534 By default, only user-created objects are shown; supply a
1535 pattern or the <literal>S</literal> modifier to include system
1536 objects.
1537 </para>
1539 <para>
1540 Descriptions for objects can be created with the <link
1541 linkend="sql-comment"><command>COMMENT</command></link>
1542 <acronym>SQL</acronym> command.
1543 </para>
1544 </listitem>
1545 </varlistentry>
1548 <varlistentry id="app-psql-meta-command-dd-uc">
1549 <term><literal>\dD[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1550 <listitem>
1551 <para>
1552 Lists domains. If <replaceable
1553 class="parameter">pattern</replaceable>
1554 is specified, only domains whose names match the pattern are shown.
1555 By default, only user-created objects are shown; supply a
1556 pattern or the <literal>S</literal> modifier to include system
1557 objects.
1558 If <literal>+</literal> is appended to the command name, each object
1559 is listed with its associated permissions and description.
1560 </para>
1561 </listitem>
1562 </varlistentry>
1565 <varlistentry id="app-psql-meta-command-ddp">
1566 <term><literal>\ddp [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1567 <listitem>
1568 <para>
1569 Lists default access privilege settings. An entry is shown for
1570 each role (and schema, if applicable) for which the default
1571 privilege settings have been changed from the built-in defaults.
1572 If <replaceable class="parameter">pattern</replaceable> is
1573 specified, only entries whose role name or schema name matches
1574 the pattern are listed.
1575 </para>
1577 <para>
1578 The <link linkend="sql-alterdefaultprivileges"><command>ALTER DEFAULT
1579 PRIVILEGES</command></link> command is used to set default access
1580 privileges. The meaning of the privilege display is explained in
1581 <xref linkend="ddl-priv"/>.
1582 </para>
1583 </listitem>
1584 </varlistentry>
1587 <varlistentry id="app-psql-meta-command-de">
1588 <term><literal>\dE[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1589 <term><literal>\di[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1590 <term><literal>\dm[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1591 <term><literal>\ds[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1592 <term><literal>\dt[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1593 <term><literal>\dv[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1595 <listitem>
1596 <para>
1597 In this group of commands, the letters <literal>E</literal>,
1598 <literal>i</literal>, <literal>m</literal>, <literal>s</literal>,
1599 <literal>t</literal>, and <literal>v</literal>
1600 stand for foreign table, index, materialized view,
1601 sequence, table, and view,
1602 respectively.
1603 You can specify any or all of
1604 these letters, in any order, to obtain a listing of objects
1605 of these types. For example, <literal>\dti</literal> lists
1606 tables and indexes. If <literal>+</literal> is
1607 appended to the command name, each object is listed with its
1608 persistence status (permanent, temporary, or unlogged),
1609 physical size on disk, and associated description if any.
1610 If <replaceable class="parameter">pattern</replaceable> is
1611 specified, only objects whose names match the pattern are listed.
1612 By default, only user-created objects are shown; supply a
1613 pattern or the <literal>S</literal> modifier to include system
1614 objects.
1615 </para>
1616 </listitem>
1617 </varlistentry>
1620 <varlistentry id="app-psql-meta-command-des">
1621 <term><literal>\des[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1622 <listitem>
1623 <para>
1624 Lists foreign servers (mnemonic: <quote>external
1625 servers</quote>).
1626 If <replaceable class="parameter">pattern</replaceable> is
1627 specified, only those servers whose name matches the pattern
1628 are listed. If the form <literal>\des+</literal> is used, a
1629 full description of each server is shown, including the
1630 server's access privileges, type, version, options, and description.
1631 </para>
1632 </listitem>
1633 </varlistentry>
1636 <varlistentry id="app-psql-meta-command-det">
1637 <term><literal>\det[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1638 <listitem>
1639 <para>
1640 Lists foreign tables (mnemonic: <quote>external tables</quote>).
1641 If <replaceable class="parameter">pattern</replaceable> is
1642 specified, only entries whose table name or schema name matches
1643 the pattern are listed. If the form <literal>\det+</literal>
1644 is used, generic options and the foreign table description
1645 are also displayed.
1646 </para>
1647 </listitem>
1648 </varlistentry>
1651 <varlistentry id="app-psql-meta-command-deu">
1652 <term><literal>\deu[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1653 <listitem>
1654 <para>
1655 Lists user mappings (mnemonic: <quote>external
1656 users</quote>).
1657 If <replaceable class="parameter">pattern</replaceable> is
1658 specified, only those mappings whose user names match the
1659 pattern are listed. If the form <literal>\deu+</literal> is
1660 used, additional information about each mapping is shown.
1661 </para>
1663 <caution>
1664 <para>
1665 <literal>\deu+</literal> might also display the user name and
1666 password of the remote user, so care should be taken not to
1667 disclose them.
1668 </para>
1669 </caution>
1670 </listitem>
1671 </varlistentry>
1674 <varlistentry id="app-psql-meta-command-dew">
1675 <term><literal>\dew[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1676 <listitem>
1677 <para>
1678 Lists foreign-data wrappers (mnemonic: <quote>external
1679 wrappers</quote>).
1680 If <replaceable class="parameter">pattern</replaceable> is
1681 specified, only those foreign-data wrappers whose name matches
1682 the pattern are listed. If the form <literal>\dew+</literal>
1683 is used, the access privileges, options, and description of the
1684 foreign-data wrapper are also shown.
1685 </para>
1686 </listitem>
1687 </varlistentry>
1690 <varlistentry id="app-psql-meta-command-df-lc">
1691 <term><literal>\df[anptwS+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> [ <replaceable class="parameter">arg_pattern</replaceable> ... ] ]</literal></term>
1693 <listitem>
1694 <para>
1695 Lists functions, together with their result data types, argument data
1696 types, and function types, which are classified as <quote>agg</quote>
1697 (aggregate), <quote>normal</quote>, <quote>procedure</quote>, <quote>trigger</quote>, or <quote>window</quote>.
1698 To display only functions
1699 of specific type(s), add the corresponding letters <literal>a</literal>,
1700 <literal>n</literal>, <literal>p</literal>, <literal>t</literal>, or <literal>w</literal> to the command.
1701 If <replaceable
1702 class="parameter">pattern</replaceable> is specified, only
1703 functions whose names match the pattern are shown.
1704 Any additional arguments are type-name patterns, which are matched
1705 to the type names of the first, second, and so on arguments of the
1706 function. (Matching functions can have more arguments than what
1707 you specify. To prevent that, write a dash <literal>-</literal> as
1708 the last <replaceable class="parameter">arg_pattern</replaceable>.)
1709 By default, only user-created
1710 objects are shown; supply a pattern or the <literal>S</literal>
1711 modifier to include system objects.
1712 If the form <literal>\df+</literal> is used, additional information
1713 about each function is shown, including volatility,
1714 parallel safety, owner, security classification, access privileges,
1715 language, internal name (for C and internal functions only),
1716 and description.
1717 Source code for a specific function can be seen
1718 using <literal>\sf</literal>.
1719 </para>
1721 </listitem>
1722 </varlistentry>
1724 <varlistentry id="app-psql-meta-command-df-uc">
1725 <term><literal>\dF[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1726 <listitem>
1727 <para>
1728 Lists text search configurations.
1729 If <replaceable class="parameter">pattern</replaceable> is specified,
1730 only configurations whose names match the pattern are shown.
1731 If the form <literal>\dF+</literal> is used, a full description of
1732 each configuration is shown, including the underlying text search
1733 parser and the dictionary list for each parser token type.
1734 </para>
1735 </listitem>
1736 </varlistentry>
1738 <varlistentry id="app-psql-meta-command-dfd">
1739 <term><literal>\dFd[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1740 <listitem>
1741 <para>
1742 Lists text search dictionaries.
1743 If <replaceable class="parameter">pattern</replaceable> is specified,
1744 only dictionaries whose names match the pattern are shown.
1745 If the form <literal>\dFd+</literal> is used, additional information
1746 is shown about each selected dictionary, including the underlying
1747 text search template and the option values.
1748 </para>
1749 </listitem>
1750 </varlistentry>
1752 <varlistentry id="app-psql-meta-command-dfp">
1753 <term><literal>\dFp[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1754 <listitem>
1755 <para>
1756 Lists text search parsers.
1757 If <replaceable class="parameter">pattern</replaceable> is specified,
1758 only parsers whose names match the pattern are shown.
1759 If the form <literal>\dFp+</literal> is used, a full description of
1760 each parser is shown, including the underlying functions and the
1761 list of recognized token types.
1762 </para>
1763 </listitem>
1764 </varlistentry>
1766 <varlistentry id="app-psql-meta-command-dft">
1767 <term><literal>\dFt[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1768 <listitem>
1769 <para>
1770 Lists text search templates.
1771 If <replaceable class="parameter">pattern</replaceable> is specified,
1772 only templates whose names match the pattern are shown.
1773 If the form <literal>\dFt+</literal> is used, additional information
1774 is shown about each template, including the underlying function names.
1775 </para>
1776 </listitem>
1777 </varlistentry>
1780 <varlistentry id="app-psql-meta-command-dg">
1781 <term><literal>\dg[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1782 <listitem>
1783 <para>
1784 Lists database roles.
1785 (Since the concepts of <quote>users</quote> and <quote>groups</quote> have been
1786 unified into <quote>roles</quote>, this command is now equivalent to
1787 <literal>\du</literal>.)
1788 By default, only user-created roles are shown; supply the
1789 <literal>S</literal> modifier to include system roles.
1790 If <replaceable class="parameter">pattern</replaceable> is specified,
1791 only those roles whose names match the pattern are listed.
1792 If the form <literal>\dg+</literal> is used, additional information
1793 is shown about each role; currently this adds the comment for each
1794 role.
1795 </para>
1796 </listitem>
1797 </varlistentry>
1800 <varlistentry id="app-psql-meta-command-dl-lc">
1801 <term><literal>\dl[+]</literal></term>
1802 <listitem>
1803 <para>
1804 This is an alias for <command>\lo_list</command>, which shows a
1805 list of large objects.
1806 If <literal>+</literal> is appended to the command name,
1807 each large object is listed with its associated permissions,
1808 if any.
1809 </para>
1810 </listitem>
1811 </varlistentry>
1813 <varlistentry id="app-psql-meta-command-dl-uc">
1814 <term><literal>\dL[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1815 <listitem>
1816 <para>
1817 Lists procedural languages. If <replaceable
1818 class="parameter">pattern</replaceable>
1819 is specified, only languages whose names match the pattern are listed.
1820 By default, only user-created languages
1821 are shown; supply the <literal>S</literal> modifier to include system
1822 objects. If <literal>+</literal> is appended to the command name, each
1823 language is listed with its call handler, validator, access privileges,
1824 and whether it is a system object.
1825 </para>
1826 </listitem>
1827 </varlistentry>
1830 <varlistentry id="app-psql-meta-command-dn">
1831 <term><literal>\dn[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1833 <listitem>
1834 <para>
1835 Lists schemas (namespaces). If <replaceable
1836 class="parameter">pattern</replaceable>
1837 is specified, only schemas whose names match the pattern are listed.
1838 By default, only user-created objects are shown; supply a
1839 pattern or the <literal>S</literal> modifier to include system objects.
1840 If <literal>+</literal> is appended to the command name, each object
1841 is listed with its associated permissions and description, if any.
1842 </para>
1843 </listitem>
1844 </varlistentry>
1847 <varlistentry id="app-psql-meta-command-do-lc">
1848 <term><literal>\do[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> [ <replaceable class="parameter">arg_pattern</replaceable> [ <replaceable class="parameter">arg_pattern</replaceable> ] ] ]</literal></term>
1849 <listitem>
1850 <para>
1851 Lists operators with their operand and result types.
1852 If <replaceable class="parameter">pattern</replaceable> is
1853 specified, only operators whose names match the pattern are listed.
1854 If one <replaceable class="parameter">arg_pattern</replaceable> is
1855 specified, only prefix operators whose right argument's type name
1856 matches that pattern are listed.
1857 If two <replaceable class="parameter">arg_pattern</replaceable>s
1858 are specified, only binary operators whose argument type names match
1859 those patterns are listed. (Alternatively, write <literal>-</literal>
1860 for the unused argument of a unary operator.)
1861 By default, only user-created objects are shown; supply a
1862 pattern or the <literal>S</literal> modifier to include system
1863 objects.
1864 If <literal>+</literal> is appended to the command name,
1865 additional information about each operator is shown, currently just
1866 the name of the underlying function.
1867 </para>
1868 </listitem>
1869 </varlistentry>
1872 <varlistentry id="app-psql-meta-command-do-uc">
1873 <term><literal>\dO[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1874 <listitem>
1875 <para>
1876 Lists collations.
1877 If <replaceable class="parameter">pattern</replaceable> is
1878 specified, only collations whose names match the pattern are
1879 listed. By default, only user-created objects are shown;
1880 supply a pattern or the <literal>S</literal> modifier to
1881 include system objects. If <literal>+</literal> is appended
1882 to the command name, each collation is listed with its associated
1883 description, if any.
1884 Note that only collations usable with the current database's encoding
1885 are shown, so the results may vary in different databases of the
1886 same installation.
1887 </para>
1888 </listitem>
1889 </varlistentry>
1892 <varlistentry id="app-psql-meta-command-dp-lc">
1893 <term><literal>\dp[S] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1894 <listitem>
1895 <para>
1896 Lists tables, views and sequences with their
1897 associated access privileges.
1898 If <replaceable class="parameter">pattern</replaceable> is
1899 specified, only tables, views and sequences whose names match the
1900 pattern are listed. By default only user-created objects are shown;
1901 supply a pattern or the <literal>S</literal> modifier to include
1902 system objects.
1903 </para>
1905 <para>
1906 The <link linkend="sql-grant"><command>GRANT</command></link> and
1907 <link linkend="sql-revoke"><command>REVOKE</command></link>
1908 commands are used to set access privileges. The meaning of the
1909 privilege display is explained in
1910 <xref linkend="ddl-priv"/>.
1911 </para>
1912 </listitem>
1913 </varlistentry>
1916 <varlistentry id="app-psql-meta-command-dp-uc">
1917 <term><literal>\dP[itn+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1918 <listitem>
1919 <para>
1920 Lists partitioned relations.
1921 If <replaceable class="parameter">pattern</replaceable>
1922 is specified, only entries whose name matches the pattern are listed.
1923 The modifiers <literal>t</literal> (tables) and <literal>i</literal>
1924 (indexes) can be appended to the command, filtering the kind of
1925 relations to list. By default, partitioned tables and indexes are
1926 listed.
1927 </para>
1929 <para>
1930 If the modifier <literal>n</literal> (<quote>nested</quote>) is used,
1931 or a pattern is specified, then non-root partitioned relations are
1932 included, and a column is shown displaying the parent of each
1933 partitioned relation.
1934 </para>
1936 <para>
1937 If <literal>+</literal> is appended to the command name, the sum of the
1938 sizes of each relation's partitions is also displayed, along with the
1939 relation's description.
1940 If <literal>n</literal> is combined with <literal>+</literal>, two
1941 sizes are shown: one including the total size of directly-attached
1942 leaf partitions, and another showing the total size of all partitions,
1943 including indirectly attached sub-partitions.
1944 </para>
1945 </listitem>
1946 </varlistentry>
1949 <varlistentry id="app-psql-meta-command-drds">
1950 <term><literal>\drds [ <link linkend="app-psql-patterns"><replaceable class="parameter">role-pattern</replaceable></link> [ <link linkend="app-psql-patterns"><replaceable class="parameter">database-pattern</replaceable></link> ] ]</literal></term>
1951 <listitem>
1952 <para>
1953 Lists defined configuration settings. These settings can be
1954 role-specific, database-specific, or both.
1955 <replaceable>role-pattern</replaceable> and
1956 <replaceable>database-pattern</replaceable> are used to select
1957 specific roles and databases to list, respectively. If omitted, or if
1958 <literal>*</literal> is specified, all settings are listed, including those
1959 not role-specific or database-specific, respectively.
1960 </para>
1962 <para>
1963 The <link linkend="sql-alterrole"><command>ALTER ROLE</command></link> and
1964 <link linkend="sql-alterdatabase"><command>ALTER DATABASE</command></link>
1965 commands are used to define per-role and per-database configuration
1966 settings.
1967 </para>
1968 </listitem>
1969 </varlistentry>
1972 <varlistentry id="app-psql-meta-command-drg">
1973 <term><literal>\drg[S] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1974 <listitem>
1975 <para>
1976 Lists information about each granted role membership, including
1977 assigned options (<literal>ADMIN</literal>,
1978 <literal>INHERIT</literal> and/or <literal>SET</literal>) and grantor.
1979 See the <link linkend="sql-grant"><command>GRANT</command></link>
1980 command for information about role memberships.
1981 </para>
1982 <para>
1983 By default, only grants to user-created roles are shown; supply the
1984 <literal>S</literal> modifier to include system roles.
1985 If <replaceable class="parameter">pattern</replaceable> is specified,
1986 only grants to those roles whose names match the pattern are listed.
1987 </para>
1988 </listitem>
1989 </varlistentry>
1992 <varlistentry id="app-psql-meta-command-drp">
1993 <term><literal>\dRp[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1994 <listitem>
1995 <para>
1996 Lists replication publications.
1997 If <replaceable class="parameter">pattern</replaceable> is
1998 specified, only those publications whose names match the pattern are
1999 listed.
2000 If <literal>+</literal> is appended to the command name, the tables and
2001 schemas associated with each publication are shown as well.
2002 </para>
2003 </listitem>
2004 </varlistentry>
2006 <varlistentry id="app-psql-meta-command-drs">
2007 <term><literal>\dRs[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2008 <listitem>
2009 <para>
2010 Lists replication subscriptions.
2011 If <replaceable class="parameter">pattern</replaceable> is
2012 specified, only those subscriptions whose names match the pattern are
2013 listed.
2014 If <literal>+</literal> is appended to the command name, additional
2015 properties of the subscriptions are shown.
2016 </para>
2017 </listitem>
2018 </varlistentry>
2020 <varlistentry id="app-psql-meta-command-dt">
2021 <term><literal>\dT[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2022 <listitem>
2023 <para>
2024 Lists data types.
2025 If <replaceable class="parameter">pattern</replaceable> is
2026 specified, only types whose names match the pattern are listed.
2027 If <literal>+</literal> is appended to the command name, each type is
2028 listed with its internal name and size, its allowed values
2029 if it is an <type>enum</type> type, and its associated permissions.
2030 By default, only user-created objects are shown; supply a
2031 pattern or the <literal>S</literal> modifier to include system
2032 objects.
2033 </para>
2034 </listitem>
2035 </varlistentry>
2037 <varlistentry id="app-psql-meta-command-du">
2038 <term><literal>\du[S+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2039 <listitem>
2040 <para>
2041 Lists database roles.
2042 (Since the concepts of <quote>users</quote> and <quote>groups</quote> have been
2043 unified into <quote>roles</quote>, this command is now equivalent to
2044 <literal>\dg</literal>.)
2045 By default, only user-created roles are shown; supply the
2046 <literal>S</literal> modifier to include system roles.
2047 If <replaceable class="parameter">pattern</replaceable> is specified,
2048 only those roles whose names match the pattern are listed.
2049 If the form <literal>\du+</literal> is used, additional information
2050 is shown about each role; currently this adds the comment for each
2051 role.
2052 </para>
2053 </listitem>
2054 </varlistentry>
2056 <varlistentry id="app-psql-meta-command-dx-lc">
2057 <term><literal>\dx[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2058 <listitem>
2059 <para>
2060 Lists installed extensions.
2061 If <replaceable class="parameter">pattern</replaceable>
2062 is specified, only those extensions whose names match the pattern
2063 are listed.
2064 If the form <literal>\dx+</literal> is used, all the objects belonging
2065 to each matching extension are listed.
2066 </para>
2067 </listitem>
2068 </varlistentry>
2070 <varlistentry id="app-psql-meta-command-dx-uc">
2071 <term><literal>\dX [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2072 <listitem>
2073 <para>
2074 Lists extended statistics.
2075 If <replaceable class="parameter">pattern</replaceable>
2076 is specified, only those extended statistics whose names match the
2077 pattern are listed.
2078 </para>
2080 <para>
2081 The status of each kind of extended statistics is shown in a column
2082 named after its statistic kind (e.g. Ndistinct).
2083 <literal>defined</literal> means that it was requested when creating
2084 the statistics, and NULL means it wasn't requested.
2085 You can use <structname>pg_stats_ext</structname> if you'd like to
2086 know whether <link linkend="sql-analyze"><command>ANALYZE</command></link>
2087 was run and statistics are available to the planner.
2088 </para>
2089 </listitem>
2090 </varlistentry>
2092 <varlistentry id="app-psql-meta-command-dy">
2093 <term><literal>\dy[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2094 <listitem>
2095 <para>
2096 Lists event triggers.
2097 If <replaceable class="parameter">pattern</replaceable>
2098 is specified, only those event triggers whose names match the pattern
2099 are listed.
2100 If <literal>+</literal> is appended to the command name, each object
2101 is listed with its associated description.
2102 </para>
2103 </listitem>
2104 </varlistentry>
2106 <varlistentry id="app-psql-meta-command-edit">
2107 <term><literal>\e</literal> or <literal>\edit</literal> <literal> <optional> <replaceable class="parameter">filename</replaceable> </optional> <optional> <replaceable class="parameter">line_number</replaceable> </optional> </literal></term>
2109 <listitem>
2110 <para>
2111 If <replaceable class="parameter">filename</replaceable> is
2112 specified, the file is edited; after the editor exits, the file's
2113 content is copied into the current query buffer. If no <replaceable
2114 class="parameter">filename</replaceable> is given, the current query
2115 buffer is copied to a temporary file which is then edited in the same
2116 fashion. Or, if the current query buffer is empty, the most recently
2117 executed query is copied to a temporary file and edited in the same
2118 fashion.
2119 </para>
2121 <para>
2122 If you edit a file or the previous query, and you quit the editor without
2123 modifying the file, the query buffer is cleared.
2124 Otherwise, the new contents of the query buffer are re-parsed according to
2125 the normal rules of <application>psql</application>, treating the
2126 whole buffer as a single line. Any complete queries are immediately
2127 executed; that is, if the query buffer contains or ends with a
2128 semicolon, everything up to that point is executed and removed from
2129 the query buffer. Whatever remains in the query buffer is
2130 redisplayed. Type semicolon or <literal>\g</literal> to send it,
2131 or <literal>\r</literal> to cancel it by clearing the query buffer.
2132 </para>
2134 <para>
2135 Treating the buffer as a single line primarily affects meta-commands:
2136 whatever is in the buffer after a meta-command will be taken as
2137 argument(s) to the meta-command, even if it spans multiple lines.
2138 (Thus you cannot make meta-command-using scripts this way.
2139 Use <command>\i</command> for that.)
2140 </para>
2142 <para>
2143 If a line number is specified, <application>psql</application> will
2144 position the cursor on the specified line of the file or query buffer.
2145 Note that if a single all-digits argument is given,
2146 <application>psql</application> assumes it is a line number,
2147 not a file name.
2148 </para>
2150 <tip>
2151 <para>
2152 See <xref linkend="app-psql-environment"/>, below, for how to
2153 configure and customize your editor.
2154 </para>
2155 </tip>
2156 </listitem>
2157 </varlistentry>
2159 <varlistentry id="app-psql-meta-command-echo">
2160 <term><literal>\echo <replaceable class="parameter">text</replaceable> [ ... ]</literal></term>
2161 <listitem>
2162 <para>
2163 Prints the evaluated arguments to standard output, separated by
2164 spaces and followed by a newline. This can be useful to
2165 intersperse information in the output of scripts. For example:
2166 <programlisting>
2167 =&gt; <userinput>\echo `date`</userinput>
2168 Tue Oct 26 21:40:57 CEST 1999
2169 </programlisting>
2170 If the first argument is an unquoted <literal>-n</literal> the trailing
2171 newline is not written (nor is the first argument).
2172 </para>
2174 <tip>
2175 <para>
2176 If you use the <command>\o</command> command to redirect your
2177 query output you might wish to use <command>\qecho</command>
2178 instead of this command. See also <command>\warn</command>.
2179 </para>
2180 </tip>
2181 </listitem>
2182 </varlistentry>
2184 <varlistentry id="app-psql-meta-command-ef">
2185 <term><literal>\ef <optional> <replaceable class="parameter">function_description</replaceable> <optional> <replaceable class="parameter">line_number</replaceable> </optional> </optional> </literal></term>
2187 <listitem>
2188 <para>
2189 This command fetches and edits the definition of the named function or procedure,
2190 in the form of a <command>CREATE OR REPLACE FUNCTION</command> or
2191 <command>CREATE OR REPLACE PROCEDURE</command> command.
2192 Editing is done in the same way as for <literal>\edit</literal>.
2193 If you quit the editor without saving, the statement is discarded.
2194 If you save and exit the editor, the updated command is executed immediately
2195 if you added a semicolon to it. Otherwise it is redisplayed;
2196 type semicolon or <literal>\g</literal> to send it, or <literal>\r</literal>
2197 to cancel.
2198 </para>
2200 <para>
2201 The target function can be specified by name alone, or by name
2202 and arguments, for example <literal>foo(integer, text)</literal>.
2203 The argument types must be given if there is more
2204 than one function of the same name.
2205 </para>
2207 <para>
2208 If no function is specified, a blank <command>CREATE FUNCTION</command>
2209 template is presented for editing.
2210 </para>
2212 <para>
2213 If a line number is specified, <application>psql</application> will
2214 position the cursor on the specified line of the function body.
2215 (Note that the function body typically does not begin on the first
2216 line of the file.)
2217 </para>
2219 <para>
2220 Unlike most other meta-commands, the entire remainder of the line is
2221 always taken to be the argument(s) of <command>\ef</command>, and neither
2222 variable interpolation nor backquote expansion are performed in the
2223 arguments.
2224 </para>
2226 <tip>
2227 <para>
2228 See <xref linkend="app-psql-environment"/>, below, for how to
2229 configure and customize your editor.
2230 </para>
2231 </tip>
2232 </listitem>
2233 </varlistentry>
2236 <varlistentry id="app-psql-meta-command-encoding">
2237 <term><literal>\encoding [ <replaceable class="parameter">encoding</replaceable> ]</literal></term>
2239 <listitem>
2240 <para>
2241 Sets the client character set encoding. Without an argument, this command
2242 shows the current encoding.
2243 </para>
2244 </listitem>
2245 </varlistentry>
2248 <varlistentry id="app-psql-meta-command-errverbose">
2249 <term><literal>\errverbose</literal></term>
2251 <listitem>
2252 <para>
2253 Repeats the most recent server error message at maximum
2254 verbosity, as though <varname>VERBOSITY</varname> were set
2255 to <literal>verbose</literal> and <varname>SHOW_CONTEXT</varname> were
2256 set to <literal>always</literal>.
2257 </para>
2258 </listitem>
2259 </varlistentry>
2262 <varlistentry id="app-psql-meta-command-ev">
2263 <term><literal>\ev <optional> <replaceable class="parameter">view_name</replaceable> <optional> <replaceable class="parameter">line_number</replaceable> </optional> </optional> </literal></term>
2265 <listitem>
2266 <para>
2267 This command fetches and edits the definition of the named view,
2268 in the form of a <command>CREATE OR REPLACE VIEW</command> command.
2269 Editing is done in the same way as for <literal>\edit</literal>.
2270 If you quit the editor without saving, the statement is discarded.
2271 If you save and exit the editor, the updated command is executed immediately
2272 if you added a semicolon to it. Otherwise it is redisplayed;
2273 type semicolon or <literal>\g</literal> to send it, or <literal>\r</literal>
2274 to cancel.
2275 </para>
2277 <para>
2278 If no view is specified, a blank <command>CREATE VIEW</command>
2279 template is presented for editing.
2280 </para>
2282 <para>
2283 If a line number is specified, <application>psql</application> will
2284 position the cursor on the specified line of the view definition.
2285 </para>
2287 <para>
2288 Unlike most other meta-commands, the entire remainder of the line is
2289 always taken to be the argument(s) of <command>\ev</command>, and neither
2290 variable interpolation nor backquote expansion are performed in the
2291 arguments.
2292 </para>
2293 </listitem>
2294 </varlistentry>
2297 <varlistentry id="app-psql-meta-command-f">
2298 <term><literal>\f [ <replaceable class="parameter">string</replaceable> ]</literal></term>
2300 <listitem>
2301 <para>
2302 Sets the field separator for unaligned query output. The default
2303 is the vertical bar (<literal>|</literal>). It is equivalent to
2304 <command>\pset fieldsep</command>.
2305 </para>
2306 </listitem>
2307 </varlistentry>
2310 <varlistentry id="app-psql-meta-command-g">
2311 <term><literal>\g [ (<replaceable class="parameter">option</replaceable>=<replaceable class="parameter">value</replaceable> [...]) ] [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2312 <term><literal>\g [ (<replaceable class="parameter">option</replaceable>=<replaceable class="parameter">value</replaceable> [...]) ] [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
2313 <listitem>
2314 <para>
2315 Sends the current query buffer to the server for execution.
2316 </para>
2317 <para>
2318 If parentheses appear after <literal>\g</literal>, they surround a
2319 space-separated list
2320 of <replaceable class="parameter">option</replaceable><literal>=</literal><replaceable class="parameter">value</replaceable>
2321 formatting-option clauses, which are interpreted in the same way
2322 as <literal>\pset</literal>
2323 <replaceable class="parameter">option</replaceable>
2324 <replaceable class="parameter">value</replaceable> commands, but take
2325 effect only for the duration of this query. In this list, spaces are
2326 not allowed around <literal>=</literal> signs, but are required
2327 between option clauses.
2328 If <literal>=</literal><replaceable class="parameter">value</replaceable>
2329 is omitted, the
2330 named <replaceable class="parameter">option</replaceable> is changed
2331 in the same way as for
2332 <literal>\pset</literal> <replaceable class="parameter">option</replaceable>
2333 with no explicit <replaceable class="parameter">value</replaceable>.
2334 </para>
2335 <para>
2336 If a <replaceable class="parameter">filename</replaceable>
2337 or <literal>|</literal><replaceable class="parameter">command</replaceable>
2338 argument is given, the query's output is written to the named
2339 file or piped to the given shell command, instead of displaying it as
2340 usual. The file or command is written to only if the query
2341 successfully returns zero or more tuples, not if the query fails or
2342 is a non-data-returning SQL command.
2343 </para>
2344 <para>
2345 If the current query buffer is empty, the most recently sent query is
2346 re-executed instead. Except for that behavior, <literal>\g</literal>
2347 without any arguments is essentially equivalent to a semicolon.
2348 With arguments, <literal>\g</literal> provides
2349 a <quote>one-shot</quote> alternative to the <command>\o</command>
2350 command, and additionally allows one-shot adjustments of the
2351 output formatting options normally set by <literal>\pset</literal>.
2352 </para>
2353 <para>
2354 When the last argument begins with <literal>|</literal>, the entire
2355 remainder of the line is taken to be
2356 the <replaceable class="parameter">command</replaceable> to execute,
2357 and neither variable interpolation nor backquote expansion are
2358 performed in it. The rest of the line is simply passed literally to
2359 the shell.
2360 </para>
2361 </listitem>
2362 </varlistentry>
2365 <varlistentry id="app-psql-meta-command-gdesc">
2366 <term><literal>\gdesc</literal></term>
2368 <listitem>
2369 <para>
2370 Shows the description (that is, the column names and data types)
2371 of the result of the current query buffer. The query is not
2372 actually executed; however, if it contains some type of syntax
2373 error, that error will be reported in the normal way.
2374 </para>
2376 <para>
2377 If the current query buffer is empty, the most recently sent query
2378 is described instead.
2379 </para>
2380 </listitem>
2381 </varlistentry>
2384 <varlistentry id="app-psql-meta-command-getenv">
2385 <term><literal>\getenv <replaceable class="parameter">psql_var</replaceable> <replaceable class="parameter">env_var</replaceable></literal></term>
2387 <listitem>
2388 <para>
2389 Gets the value of the environment
2390 variable <replaceable class="parameter">env_var</replaceable>
2391 and assigns it to the <application>psql</application>
2392 variable <replaceable class="parameter">psql_var</replaceable>.
2393 If <replaceable class="parameter">env_var</replaceable> is
2394 not defined in the <application>psql</application> process's
2395 environment, <replaceable class="parameter">psql_var</replaceable>
2396 is not changed. Example:
2397 <programlisting>
2398 =&gt; <userinput>\getenv home HOME</userinput>
2399 =&gt; <userinput>\echo :home</userinput>
2400 /home/postgres
2401 </programlisting></para>
2402 </listitem>
2403 </varlistentry>
2406 <varlistentry id="app-psql-meta-command-gexec">
2407 <term><literal>\gexec</literal></term>
2409 <listitem>
2410 <para>
2411 Sends the current query buffer to the server, then treats
2412 each column of each row of the query's output (if any) as an SQL
2413 statement to be executed. For example, to create an index on each
2414 column of <structname>my_table</structname>:
2415 <programlisting>
2416 =&gt; <userinput>SELECT format('create index on my_table(%I)', attname)</userinput>
2417 -&gt; <userinput>FROM pg_attribute</userinput>
2418 -&gt; <userinput>WHERE attrelid = 'my_table'::regclass AND attnum &gt; 0</userinput>
2419 -&gt; <userinput>ORDER BY attnum</userinput>
2420 -&gt; <userinput>\gexec</userinput>
2421 CREATE INDEX
2422 CREATE INDEX
2423 CREATE INDEX
2424 CREATE INDEX
2425 </programlisting>
2426 </para>
2428 <para>
2429 The generated queries are executed in the order in which the rows
2430 are returned, and left-to-right within each row if there is more
2431 than one column. NULL fields are ignored. The generated queries
2432 are sent literally to the server for processing, so they cannot be
2433 <application>psql</application> meta-commands nor contain <application>psql</application>
2434 variable references. If any individual query fails, execution of
2435 the remaining queries continues
2436 unless <varname>ON_ERROR_STOP</varname> is set. Execution of each
2437 query is subject to <varname>ECHO</varname> processing.
2438 (Setting <varname>ECHO</varname> to <literal>all</literal>
2439 or <literal>queries</literal> is often advisable when
2440 using <command>\gexec</command>.) Query logging, single-step mode,
2441 timing, and other query execution features apply to each generated
2442 query as well.
2443 </para>
2444 <para>
2445 If the current query buffer is empty, the most recently sent query
2446 is re-executed instead.
2447 </para>
2448 </listitem>
2449 </varlistentry>
2452 <varlistentry id="app-psql-meta-command-gset">
2453 <term><literal>\gset [ <replaceable class="parameter">prefix</replaceable> ]</literal></term>
2455 <listitem>
2456 <para>
2457 Sends the current query buffer to the server and stores the
2458 query's output into <application>psql</application> variables
2459 (see <xref linkend="app-psql-variables"/> below).
2460 The query to be executed must return exactly one row. Each column of
2461 the row is stored into a separate variable, named the same as the
2462 column. For example:
2463 <programlisting>
2464 =&gt; <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput>
2465 -&gt; <userinput>\gset</userinput>
2466 =&gt; <userinput>\echo :var1 :var2</userinput>
2467 hello 10
2468 </programlisting>
2469 </para>
2470 <para>
2471 If you specify a <replaceable class="parameter">prefix</replaceable>,
2472 that string is prepended to the query's column names to create the
2473 variable names to use:
2474 <programlisting>
2475 =&gt; <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput>
2476 -&gt; <userinput>\gset result_</userinput>
2477 =&gt; <userinput>\echo :result_var1 :result_var2</userinput>
2478 hello 10
2479 </programlisting>
2480 </para>
2481 <para>
2482 If a column result is NULL, the corresponding variable is unset
2483 rather than being set.
2484 </para>
2485 <para>
2486 If the query fails or does not return one row,
2487 no variables are changed.
2488 </para>
2489 <para>
2490 If the current query buffer is empty, the most recently sent query
2491 is re-executed instead.
2492 </para>
2493 </listitem>
2494 </varlistentry>
2497 <varlistentry id="app-psql-meta-command-gx">
2498 <term><literal>\gx [ (<replaceable class="parameter">option</replaceable>=<replaceable class="parameter">value</replaceable> [...]) ] [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2499 <term><literal>\gx [ (<replaceable class="parameter">option</replaceable>=<replaceable class="parameter">value</replaceable> [...]) ] [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
2500 <listitem>
2501 <para>
2502 <literal>\gx</literal> is equivalent to <literal>\g</literal>, except
2503 that it forces expanded output mode for this query, as
2504 if <literal>expanded=on</literal> were included in the list of
2505 <literal>\pset</literal> options. See also <literal>\x</literal>.
2506 </para>
2507 </listitem>
2508 </varlistentry>
2511 <varlistentry id="app-psql-meta-command-help">
2512 <term><literal>\h</literal> or <literal>\help</literal> <literal>[ <replaceable class="parameter">command</replaceable> ]</literal></term>
2513 <listitem>
2514 <para>
2515 Gives syntax help on the specified <acronym>SQL</acronym>
2516 command. If <replaceable class="parameter">command</replaceable>
2517 is not specified, then <application>psql</application> will list
2518 all the commands for which syntax help is available. If
2519 <replaceable class="parameter">command</replaceable> is an
2520 asterisk (<literal>*</literal>), then syntax help on all
2521 <acronym>SQL</acronym> commands is shown.
2522 </para>
2524 <para>
2525 Unlike most other meta-commands, the entire remainder of the line is
2526 always taken to be the argument(s) of <command>\help</command>, and neither
2527 variable interpolation nor backquote expansion are performed in the
2528 arguments.
2529 </para>
2531 <note>
2532 <para>
2533 To simplify typing, commands that consists of several words do
2534 not have to be quoted. Thus it is fine to type <userinput>\help
2535 alter table</userinput>.
2536 </para>
2537 </note>
2538 </listitem>
2539 </varlistentry>
2542 <varlistentry id="app-psql-meta-command-html">
2543 <term><literal>\H</literal> or <literal>\html</literal></term>
2544 <listitem>
2545 <para>
2546 Turns on <acronym>HTML</acronym> query output format. If the
2547 <acronym>HTML</acronym> format is already on, it is switched
2548 back to the default aligned text format. This command is for
2549 compatibility and convenience, but see <command>\pset</command>
2550 about setting other output options.
2551 </para>
2552 </listitem>
2553 </varlistentry>
2556 <varlistentry id="app-psql-meta-command-include">
2557 <term><literal>\i</literal> or <literal>\include</literal> <replaceable class="parameter">filename</replaceable></term>
2558 <listitem>
2559 <para>
2560 Reads input from the file <replaceable
2561 class="parameter">filename</replaceable> and executes it as
2562 though it had been typed on the keyboard.
2563 </para>
2564 <para>
2565 If <replaceable>filename</replaceable> is <literal>-</literal>
2566 (hyphen), then standard input is read until an EOF indication
2567 or <command>\q</command> meta-command. This can be used to intersperse
2568 interactive input with input from files. Note that Readline behavior
2569 will be used only if it is active at the outermost level.
2570 </para>
2571 <note>
2572 <para>
2573 If you want to see the lines on the screen as they are read you
2574 must set the variable <varname>ECHO</varname> to
2575 <literal>all</literal>.
2576 </para>
2577 </note>
2578 </listitem>
2579 </varlistentry>
2582 <varlistentry id="psql-metacommand-if">
2583 <term><literal>\if</literal> <replaceable class="parameter">expression</replaceable></term>
2584 <term><literal>\elif</literal> <replaceable class="parameter">expression</replaceable></term>
2585 <term><literal>\else</literal></term>
2586 <term><literal>\endif</literal></term>
2587 <listitem>
2588 <para>
2589 This group of commands implements nestable conditional blocks.
2590 A conditional block must begin with an <command>\if</command> and end
2591 with an <command>\endif</command>. In between there may be any number
2592 of <command>\elif</command> clauses, which may optionally be followed
2593 by a single <command>\else</command> clause. Ordinary queries and
2594 other types of backslash commands may (and usually do) appear between
2595 the commands forming a conditional block.
2596 </para>
2597 <para>
2598 The <command>\if</command> and <command>\elif</command> commands read
2599 their argument(s) and evaluate them as a Boolean expression. If the
2600 expression yields <literal>true</literal> then processing continues
2601 normally; otherwise, lines are skipped until a
2602 matching <command>\elif</command>, <command>\else</command>,
2603 or <command>\endif</command> is reached. Once
2604 an <command>\if</command> or <command>\elif</command> test has
2605 succeeded, the arguments of later <command>\elif</command> commands in
2606 the same block are not evaluated but are treated as false. Lines
2607 following an <command>\else</command> are processed only if no earlier
2608 matching <command>\if</command> or <command>\elif</command> succeeded.
2609 </para>
2610 <para>
2611 The <replaceable class="parameter">expression</replaceable> argument
2612 of an <command>\if</command> or <command>\elif</command> command
2613 is subject to variable interpolation and backquote expansion, just
2614 like any other backslash command argument. After that it is evaluated
2615 like the value of an on/off option variable. So a valid value
2616 is any unambiguous case-insensitive match for one of:
2617 <literal>true</literal>, <literal>false</literal>, <literal>1</literal>,
2618 <literal>0</literal>, <literal>on</literal>, <literal>off</literal>,
2619 <literal>yes</literal>, <literal>no</literal>. For example,
2620 <literal>t</literal>, <literal>T</literal>, and <literal>tR</literal>
2621 will all be considered to be <literal>true</literal>.
2622 </para>
2623 <para>
2624 Expressions that do not properly evaluate to true or false will
2625 generate a warning and be treated as false.
2626 </para>
2627 <para>
2628 Lines being skipped are parsed normally to identify queries and
2629 backslash commands, but queries are not sent to the server, and
2630 backslash commands other than conditionals
2631 (<command>\if</command>, <command>\elif</command>,
2632 <command>\else</command>, <command>\endif</command>) are
2633 ignored. Conditional commands are checked only for valid nesting.
2634 Variable references in skipped lines are not expanded, and backquote
2635 expansion is not performed either.
2636 </para>
2637 <para>
2638 All the backslash commands of a given conditional block must appear in
2639 the same source file. If EOF is reached on the main input file or an
2640 <command>\include</command>-ed file before all local
2641 <command>\if</command>-blocks have been closed,
2642 then <application>psql</application> will raise an error.
2643 </para>
2644 <para>
2645 Here is an example:
2646 </para>
2647 <programlisting>
2648 -- check for the existence of two separate records in the database and store
2649 -- the results in separate psql variables
2650 SELECT
2651 EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer,
2652 EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee
2653 \gset
2654 \if :is_customer
2655 SELECT * FROM customer WHERE customer_id = 123;
2656 \elif :is_employee
2657 \echo 'is not a customer but is an employee'
2658 SELECT * FROM employee WHERE employee_id = 456;
2659 \else
2660 \if yes
2661 \echo 'not a customer or employee'
2662 \else
2663 \echo 'this will never print'
2664 \endif
2665 \endif
2666 </programlisting>
2667 </listitem>
2668 </varlistentry>
2671 <varlistentry id="app-psql-meta-command-include-relative">
2672 <term><literal>\ir</literal> or <literal>\include_relative</literal> <replaceable class="parameter">filename</replaceable></term>
2673 <listitem>
2674 <para>
2675 The <literal>\ir</literal> command is similar to <literal>\i</literal>, but resolves
2676 relative file names differently. When executing in interactive mode,
2677 the two commands behave identically. However, when invoked from a
2678 script, <literal>\ir</literal> interprets file names relative to the
2679 directory in which the script is located, rather than the current
2680 working directory.
2681 </para>
2682 </listitem>
2683 </varlistentry>
2686 <varlistentry id="app-psql-meta-command-list">
2687 <term><literal>\l[+]</literal> or <literal>\list[+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2688 <listitem>
2689 <para>
2690 List the databases in the server and show their names, owners,
2691 character set encodings, and access privileges.
2692 If <replaceable class="parameter">pattern</replaceable> is specified,
2693 only databases whose names match the pattern are listed.
2694 If <literal>+</literal> is appended to the command name, database
2695 sizes, default tablespaces, and descriptions are also displayed.
2696 (Size information is only available for databases that the current
2697 user can connect to.)
2698 </para>
2699 </listitem>
2700 </varlistentry>
2703 <varlistentry id="app-psql-meta-command-lo-export">
2704 <term><literal>\lo_export <replaceable class="parameter">loid</replaceable> <replaceable class="parameter">filename</replaceable></literal></term>
2706 <listitem>
2707 <para>
2708 Reads the large object with <acronym>OID</acronym> <replaceable
2709 class="parameter">loid</replaceable> from the database and
2710 writes it to <replaceable
2711 class="parameter">filename</replaceable>. Note that this is
2712 subtly different from the server function
2713 <function>lo_export</function>, which acts with the permissions
2714 of the user that the database server runs as and on the server's
2715 file system.
2716 </para>
2717 <tip>
2718 <para>
2719 Use <command>\lo_list</command> to find out the large object's
2720 <acronym>OID</acronym>.
2721 </para>
2722 </tip>
2723 </listitem>
2724 </varlistentry>
2727 <varlistentry id="app-psql-meta-command-lo-import">
2728 <term><literal>\lo_import <replaceable class="parameter">filename</replaceable> [ <replaceable class="parameter">comment</replaceable> ]</literal></term>
2730 <listitem>
2731 <para>
2732 Stores the file into a <productname>PostgreSQL</productname>
2733 large object. Optionally, it associates the given
2734 comment with the object. Example:
2735 <programlisting>
2736 foo=&gt; <userinput>\lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'</userinput>
2737 lo_import 152801
2738 </programlisting>
2739 The response indicates that the large object received object
2740 ID 152801, which can be used to access the newly-created large
2741 object in the future. For the sake of readability, it is
2742 recommended to always associate a human-readable comment with
2743 every object. Both OIDs and comments can be viewed with the
2744 <command>\lo_list</command> command.
2745 </para>
2747 <para>
2748 Note that this command is subtly different from the server-side
2749 <function>lo_import</function> because it acts as the local user
2750 on the local file system, rather than the server's user and file
2751 system.
2752 </para>
2753 </listitem>
2754 </varlistentry>
2756 <varlistentry id="app-psql-meta-command-lo-list">
2757 <term><literal>\lo_list[+]</literal></term>
2758 <listitem>
2759 <para>
2760 Shows a list of all <productname>PostgreSQL</productname>
2761 large objects currently stored in the database,
2762 along with any comments provided for them.
2763 If <literal>+</literal> is appended to the command name,
2764 each large object is listed with its associated permissions,
2765 if any.
2766 </para>
2767 </listitem>
2768 </varlistentry>
2770 <varlistentry id="app-psql-meta-command-lo-unlink">
2771 <term><literal>\lo_unlink <replaceable class="parameter">loid</replaceable></literal></term>
2773 <listitem>
2774 <para>
2775 Deletes the large object with <acronym>OID</acronym>
2776 <replaceable class="parameter">loid</replaceable> from the
2777 database.
2778 </para>
2780 <tip>
2781 <para>
2782 Use <command>\lo_list</command> to find out the large object's
2783 <acronym>OID</acronym>.
2784 </para>
2785 </tip>
2786 </listitem>
2787 </varlistentry>
2790 <varlistentry id="app-psql-meta-command-out">
2791 <term><literal>\o</literal> or <literal>\out [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2792 <term><literal>\o</literal> or <literal>\out [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
2793 <listitem>
2794 <para>
2795 Arranges to save future query results to the file <replaceable
2796 class="parameter">filename</replaceable> or pipe future results
2797 to the shell command <replaceable
2798 class="parameter">command</replaceable>. If no argument is
2799 specified, the query output is reset to the standard output.
2800 </para>
2802 <para>
2803 If the argument begins with <literal>|</literal>, then the entire remainder
2804 of the line is taken to be
2805 the <replaceable class="parameter">command</replaceable> to execute,
2806 and neither variable interpolation nor backquote expansion are
2807 performed in it. The rest of the line is simply passed literally to
2808 the shell.
2809 </para>
2811 <para>
2812 <quote>Query results</quote> includes all tables, command
2813 responses, and notices obtained from the database server, as
2814 well as output of various backslash commands that query the
2815 database (such as <command>\d</command>); but not error
2816 messages.
2817 </para>
2819 <tip>
2820 <para>
2821 To intersperse text output in between query results, use
2822 <command>\qecho</command>.
2823 </para>
2824 </tip>
2825 </listitem>
2826 </varlistentry>
2829 <varlistentry id="app-psql-meta-command-print">
2830 <term><literal>\p</literal> or <literal>\print</literal></term>
2831 <listitem>
2832 <para>
2833 Print the current query buffer to the standard output.
2834 If the current query buffer is empty, the most recently executed query
2835 is printed instead.
2836 </para>
2837 </listitem>
2838 </varlistentry>
2840 <varlistentry id="app-psql-meta-command-parse">
2841 <term><literal>\parse <replaceable class="parameter">statement_name</replaceable></literal></term>
2842 <listitem>
2843 <para>
2844 Creates a prepared statement from the current query buffer, based on
2845 the name of a destination prepared-statement object. An empty string
2846 denotes the unnamed prepared statement.
2847 </para>
2849 <para>
2850 Example:
2851 <programlisting>
2852 SELECT $1 \parse stmt1
2853 </programlisting>
2854 </para>
2856 <para>
2857 This command causes the extended query protocol to be used, unlike
2858 normal <application>psql</application> operation, which uses the
2859 simple query protocol. A
2860 <xref linkend="protocol-message-formats-Parse"/>
2861 message will be issued by this command so it can be useful to
2862 test the extended query protocol from
2863 <application>psql</application>. This command affects only the next
2864 query executed; all subsequent queries will use the simple query
2865 protocol by default.
2866 </para>
2868 </listitem>
2869 </varlistentry>
2871 <varlistentry id="app-psql-meta-command-password">
2872 <term><literal>\password [ <replaceable class="parameter">username</replaceable> ]</literal></term>
2873 <listitem>
2874 <para>
2875 Changes the password of the specified user (by default, the current
2876 user). This command prompts for the new password, encrypts it, and
2877 sends it to the server as an <command>ALTER ROLE</command> command. This
2878 makes sure that the new password does not appear in cleartext in the
2879 command history, the server log, or elsewhere.
2880 </para>
2881 </listitem>
2882 </varlistentry>
2884 <varlistentry id="app-psql-meta-command-prompt">
2885 <term><literal>\prompt [ <replaceable class="parameter">text</replaceable> ] <replaceable class="parameter">name</replaceable></literal></term>
2886 <listitem>
2887 <para>
2888 Prompts the user to supply text, which is assigned to the variable
2889 <replaceable class="parameter">name</replaceable>.
2890 An optional prompt string, <replaceable
2891 class="parameter">text</replaceable>, can be specified. (For multiword
2892 prompts, surround the text with single quotes.)
2893 </para>
2895 <para>
2896 By default, <literal>\prompt</literal> uses the terminal for input and
2897 output. However, if the <option>-f</option> command line switch was
2898 used, <literal>\prompt</literal> uses standard input and standard output.
2899 </para>
2900 </listitem>
2901 </varlistentry>
2903 <varlistentry id="app-psql-meta-command-pset">
2904 <term><literal>\pset [ <replaceable class="parameter">option</replaceable> [ <replaceable class="parameter">value</replaceable> ] ]</literal></term>
2906 <listitem>
2907 <para>
2908 This command sets options affecting the output of query result tables.
2909 <replaceable class="parameter">option</replaceable>
2910 indicates which option is to be set. The semantics of
2911 <replaceable class="parameter">value</replaceable> vary depending
2912 on the selected option. For some options, omitting <replaceable
2913 class="parameter">value</replaceable> causes the option to be toggled
2914 or unset, as described under the particular option. If no such
2915 behavior is mentioned, then omitting
2916 <replaceable class="parameter">value</replaceable> just results in
2917 the current setting being displayed.
2918 </para>
2920 <para>
2921 <command>\pset</command> without any arguments displays the current status
2922 of all printing options.
2923 </para>
2925 <para>
2926 Adjustable printing options are:
2927 <variablelist>
2928 <varlistentry id="app-psql-meta-command-pset-border">
2929 <term><literal>border</literal></term>
2930 <listitem>
2931 <para>
2932 The <replaceable class="parameter">value</replaceable> must be a
2933 number. In general, the higher
2934 the number the more borders and lines the tables will have,
2935 but details depend on the particular format.
2936 In <acronym>HTML</acronym> format, this will translate directly
2937 into the <literal>border=...</literal> attribute.
2938 In most other formats only values 0 (no border), 1 (internal
2939 dividing lines), and 2 (table frame) make sense, and values above 2
2940 will be treated the same as <literal>border = 2</literal>.
2941 The <literal>latex</literal> and <literal>latex-longtable</literal>
2942 formats additionally allow a value of 3 to add dividing lines
2943 between data rows.
2944 </para>
2945 </listitem>
2946 </varlistentry>
2948 <varlistentry id="app-psql-meta-command-pset-columns">
2949 <term><literal>columns</literal></term>
2950 <listitem>
2951 <para>
2952 Sets the target width for the <literal>wrapped</literal> format, and also
2953 the width limit for determining whether output is wide enough to
2954 require the pager or switch to the vertical display in expanded auto
2955 mode.
2956 Zero (the default) causes the target width to be controlled by the
2957 environment variable <envar>COLUMNS</envar>, or the detected screen width
2958 if <envar>COLUMNS</envar> is not set.
2959 In addition, if <literal>columns</literal> is zero then the
2960 <literal>wrapped</literal> format only affects screen output.
2961 If <literal>columns</literal> is nonzero then file and pipe output is
2962 wrapped to that width as well.
2963 </para>
2964 </listitem>
2965 </varlistentry>
2967 <varlistentry id="app-psql-meta-command-pset-csv-fieldsep">
2968 <term><literal>csv_fieldsep</literal></term>
2969 <listitem>
2970 <para>
2971 Specifies the field separator to be used in
2972 <acronym>CSV</acronym> output format. If the separator character
2973 appears in a field's value, that field is output within double
2974 quotes, following standard <acronym>CSV</acronym> rules.
2975 The default is a comma.
2976 </para>
2977 </listitem>
2978 </varlistentry>
2980 <varlistentry id="app-psql-meta-command-pset-expanded">
2981 <term><literal>expanded</literal> (or <literal>x</literal>)</term>
2982 <listitem>
2983 <para>
2984 If <replaceable class="parameter">value</replaceable> is specified it
2985 must be either <literal>on</literal> or <literal>off</literal>, which
2986 will enable or disable expanded mode, or <literal>auto</literal>.
2987 If <replaceable class="parameter">value</replaceable> is omitted the
2988 command toggles between the on and off settings. When expanded mode
2989 is enabled, query results are displayed in two columns, with the
2990 column name on the left and the data on the right. This mode is
2991 useful if the data wouldn't fit on the screen in the
2992 normal <quote>horizontal</quote> mode. In the auto setting, the
2993 expanded mode is used whenever the query output has more than one
2994 column and is wider than the screen; otherwise, the regular mode is
2995 used. The auto setting is only
2996 effective in the aligned and wrapped formats. In other formats, it
2997 always behaves as if the expanded mode is off.
2998 </para>
2999 </listitem>
3000 </varlistentry>
3002 <varlistentry id="app-psql-meta-command-pset-fieldsep">
3003 <term><literal>fieldsep</literal></term>
3004 <listitem>
3005 <para>
3006 Specifies the field separator to be used in unaligned output
3007 format. That way one can create, for example, tab-separated
3008 output, which other programs might prefer. To
3009 set a tab as field separator, type <literal>\pset fieldsep
3010 '\t'</literal>. The default field separator is
3011 <literal>'|'</literal> (a vertical bar).
3012 </para>
3013 </listitem>
3014 </varlistentry>
3016 <varlistentry id="app-psql-meta-command-pset-fieldsep-zero">
3017 <term><literal>fieldsep_zero</literal></term>
3018 <listitem>
3019 <para>
3020 Sets the field separator to use in unaligned output format to a zero
3021 byte.
3022 </para>
3023 </listitem>
3024 </varlistentry>
3026 <varlistentry id="app-psql-meta-command-pset-footer">
3027 <term><literal>footer</literal></term>
3028 <listitem>
3029 <para>
3030 If <replaceable class="parameter">value</replaceable> is specified
3031 it must be either <literal>on</literal> or <literal>off</literal>
3032 which will enable or disable display of the table footer
3033 (the <literal>(<replaceable>n</replaceable> rows)</literal> count).
3034 If <replaceable class="parameter">value</replaceable> is omitted the
3035 command toggles footer display on or off.
3036 </para>
3037 </listitem>
3038 </varlistentry>
3040 <varlistentry id="app-psql-meta-command-pset-format">
3041 <term><literal>format</literal></term>
3042 <listitem>
3043 <para>
3044 Sets the output format to one of <literal>aligned</literal>,
3045 <literal>asciidoc</literal>,
3046 <literal>csv</literal>,
3047 <literal>html</literal>,
3048 <literal>latex</literal>,
3049 <literal>latex-longtable</literal>, <literal>troff-ms</literal>,
3050 <literal>unaligned</literal>, or <literal>wrapped</literal>.
3051 Unique abbreviations are allowed.
3052 </para>
3054 <para><literal>aligned</literal> format is the standard,
3055 human-readable, nicely formatted text output; this is the default.
3056 </para>
3058 <para><literal>unaligned</literal> format writes all columns of a row on one
3059 line, separated by the currently active field separator. This
3060 is useful for creating output that might be intended to be read
3061 in by other programs, for example, tab-separated or comma-separated
3062 format. However, the field separator character is not treated
3063 specially if it appears in a column's value;
3064 so <acronym>CSV</acronym> format may be better suited for such
3065 purposes.
3066 </para>
3068 <para><literal>csv</literal> format
3069 <indexterm>
3070 <primary>CSV (Comma-Separated Values) format</primary>
3071 <secondary>in psql</secondary>
3072 </indexterm>
3073 writes column values separated by commas, applying the quoting
3074 rules described in
3075 <ulink url="https://datatracker.ietf.org/doc/html/rfc4180">RFC 4180</ulink>.
3076 This output is compatible with the CSV format of the server's
3077 <command>COPY</command> command.
3078 A header line with column names is generated unless
3079 the <literal>tuples_only</literal> parameter is
3080 <literal>on</literal>. Titles and footers are not printed.
3081 Each row is terminated by the system-dependent end-of-line character,
3082 which is typically a single newline (<literal>\n</literal>) for
3083 Unix-like systems or a carriage return and newline sequence
3084 (<literal>\r\n</literal>) for Microsoft Windows.
3085 Field separator characters other than comma can be selected with
3086 <command>\pset csv_fieldsep</command>.
3087 </para>
3089 <para><literal>wrapped</literal> format is like <literal>aligned</literal> but wraps
3090 wide data values across lines to make the output fit in the target
3091 column width. The target width is determined as described under
3092 the <literal>columns</literal> option. Note that <application>psql</application> will
3093 not attempt to wrap column header titles; therefore,
3094 <literal>wrapped</literal> format behaves the same as <literal>aligned</literal>
3095 if the total width needed for column headers exceeds the target.
3096 </para>
3098 <para>
3099 The <literal>asciidoc</literal>, <literal>html</literal>,
3100 <literal>latex</literal>, <literal>latex-longtable</literal>, and
3101 <literal>troff-ms</literal> formats put out tables that are intended
3102 to be included in documents using the respective mark-up
3103 language. They are not complete documents! This might not be
3104 necessary in <acronym>HTML</acronym>, but in
3105 <application>LaTeX</application> you must have a complete
3106 document wrapper.
3107 The <literal>latex</literal> format
3108 uses <application>LaTeX</application>'s <literal>tabular</literal>
3109 environment.
3110 The <literal>latex-longtable</literal> format
3111 requires the <application>LaTeX</application>
3112 <literal>longtable</literal> and <literal>booktabs</literal> packages.
3113 </para>
3114 </listitem>
3115 </varlistentry>
3117 <varlistentry id="app-psql-meta-command-pset-linestyle">
3118 <term><literal>linestyle</literal></term>
3119 <listitem>
3120 <para>
3121 Sets the border line drawing style to one
3122 of <literal>ascii</literal>, <literal>old-ascii</literal>,
3123 or <literal>unicode</literal>.
3124 Unique abbreviations are allowed. (That would mean one
3125 letter is enough.)
3126 The default setting is <literal>ascii</literal>.
3127 This option only affects the <literal>aligned</literal> and
3128 <literal>wrapped</literal> output formats.
3129 </para>
3131 <para><literal>ascii</literal> style uses plain <acronym>ASCII</acronym>
3132 characters. Newlines in data are shown using
3133 a <literal>+</literal> symbol in the right-hand margin.
3134 When the <literal>wrapped</literal> format wraps data from
3135 one line to the next without a newline character, a dot
3136 (<literal>.</literal>) is shown in the right-hand margin of the first line,
3137 and again in the left-hand margin of the following line.
3138 </para>
3140 <para><literal>old-ascii</literal> style uses plain <acronym>ASCII</acronym>
3141 characters, using the formatting style used
3142 in <productname>PostgreSQL</productname> 8.4 and earlier.
3143 Newlines in data are shown using a <literal>:</literal>
3144 symbol in place of the left-hand column separator.
3145 When the data is wrapped from one line
3146 to the next without a newline character, a <literal>;</literal>
3147 symbol is used in place of the left-hand column separator.
3148 </para>
3150 <para><literal>unicode</literal> style uses Unicode box-drawing characters.
3151 Newlines in data are shown using a carriage return symbol
3152 in the right-hand margin. When the data is wrapped from one line
3153 to the next without a newline character, an ellipsis symbol
3154 is shown in the right-hand margin of the first line, and
3155 again in the left-hand margin of the following line.
3156 </para>
3158 <para>
3159 When the <literal>border</literal> setting is greater than zero,
3160 the <literal>linestyle</literal> option also determines the
3161 characters with which the border lines are drawn.
3162 Plain <acronym>ASCII</acronym> characters work everywhere, but
3163 Unicode characters look nicer on displays that recognize them.
3164 </para>
3165 </listitem>
3166 </varlistentry>
3168 <varlistentry id="app-psql-meta-command-pset-null">
3169 <term><literal>null</literal></term>
3170 <listitem>
3171 <para>
3172 Sets the string to be printed in place of a null value.
3173 The default is to print nothing, which can easily be mistaken for
3174 an empty string. For example, one might prefer <literal>\pset null
3175 '(null)'</literal>.
3176 </para>
3177 </listitem>
3178 </varlistentry>
3180 <varlistentry id="app-psql-meta-command-pset-numericlocale">
3181 <term><literal>numericlocale</literal></term>
3182 <listitem>
3183 <para>
3184 If <replaceable class="parameter">value</replaceable> is specified
3185 it must be either <literal>on</literal> or <literal>off</literal>
3186 which will enable or disable display of a locale-specific character
3187 to separate groups of digits to the left of the decimal marker.
3188 If <replaceable class="parameter">value</replaceable> is omitted the
3189 command toggles between regular and locale-specific numeric output.
3190 </para>
3191 </listitem>
3192 </varlistentry>
3194 <varlistentry id="app-psql-meta-command-pset-pager">
3195 <term><literal>pager</literal></term>
3196 <listitem>
3197 <para>
3198 Controls use of a pager program for query and <application>psql</application>
3199 help output.
3200 When the <literal>pager</literal> option is <literal>off</literal>, the pager
3201 program is not used. When the <literal>pager</literal> option is
3202 <literal>on</literal>, the pager is used when appropriate, i.e., when the
3203 output is to a terminal and will not fit on the screen.
3204 The <literal>pager</literal> option can also be set to <literal>always</literal>,
3205 which causes the pager to be used for all terminal output regardless
3206 of whether it fits on the screen. <literal>\pset pager</literal>
3207 without a <replaceable class="parameter">value</replaceable>
3208 toggles pager use on and off.
3209 </para>
3211 <para>
3212 If the environment variable <envar>PSQL_PAGER</envar>
3213 or <envar>PAGER</envar> is set, output to be paged is piped to the
3214 specified program. Otherwise a platform-dependent default program
3215 (such as <filename>more</filename>) is used.
3216 </para>
3218 <para>
3219 When using the <literal>\watch</literal> command to execute a query
3220 repeatedly, the environment variable <envar>PSQL_WATCH_PAGER</envar>
3221 is used to find the pager program instead, on Unix systems. This is
3222 configured separately because it may confuse traditional pagers, but
3223 can be used to send output to tools that understand
3224 <application>psql</application>'s output format (such as
3225 <filename>pspg --stream</filename>).
3226 </para>
3227 </listitem>
3228 </varlistentry>
3230 <varlistentry id="app-psql-meta-command-pset-pager-min-lines">
3231 <term><literal>pager_min_lines</literal></term>
3232 <listitem>
3233 <para>
3234 If <literal>pager_min_lines</literal> is set to a number greater than the
3235 page height, the pager program will not be called unless there are
3236 at least this many lines of output to show. The default setting
3237 is 0.
3238 </para>
3239 </listitem>
3240 </varlistentry>
3242 <varlistentry id="app-psql-meta-command-pset-recordsep">
3243 <term><literal>recordsep</literal></term>
3244 <listitem>
3245 <para>
3246 Specifies the record (line) separator to use in unaligned
3247 output format. The default is a newline character.
3248 </para>
3249 </listitem>
3250 </varlistentry>
3252 <varlistentry id="app-psql-meta-command-pset-recordsep-zero">
3253 <term><literal>recordsep_zero</literal></term>
3254 <listitem>
3255 <para>
3256 Sets the record separator to use in unaligned output format to a zero
3257 byte.
3258 </para>
3259 </listitem>
3260 </varlistentry>
3262 <varlistentry id="app-psql-meta-command-pset-tableattr">
3263 <term><literal>tableattr</literal> (or <literal>T</literal>)</term>
3264 <listitem>
3265 <para>
3266 In <acronym>HTML</acronym> format, this specifies attributes
3267 to be placed inside the <sgmltag>table</sgmltag> tag. This
3268 could for example be <literal>cellpadding</literal> or
3269 <literal>bgcolor</literal>. Note that you probably don't want
3270 to specify <literal>border</literal> here, as that is already
3271 taken care of by <literal>\pset border</literal>.
3272 If no
3273 <replaceable class="parameter">value</replaceable> is given,
3274 the table attributes are unset.
3275 </para>
3276 <para>
3277 In <literal>latex-longtable</literal> format, this controls
3278 the proportional width of each column containing a left-aligned
3279 data type. It is specified as a whitespace-separated list of values,
3280 e.g., <literal>'0.2 0.2 0.6'</literal>. Unspecified output columns
3281 use the last specified value.
3282 </para>
3283 </listitem>
3284 </varlistentry>
3286 <varlistentry id="app-psql-meta-command-pset-title">
3287 <term><literal>title</literal> (or <literal>C</literal>)</term>
3288 <listitem>
3289 <para>
3290 Sets the table title for any subsequently printed tables. This
3291 can be used to give your output descriptive tags. If no
3292 <replaceable class="parameter">value</replaceable> is given,
3293 the title is unset.
3294 </para>
3295 </listitem>
3296 </varlistentry>
3298 <varlistentry id="app-psql-meta-command-pset-tuples-only">
3299 <term><literal>tuples_only</literal> (or <literal>t</literal>)</term>
3300 <listitem>
3301 <para>
3302 If <replaceable class="parameter">value</replaceable> is specified
3303 it must be either <literal>on</literal> or <literal>off</literal>
3304 which will enable or disable tuples-only mode.
3305 If <replaceable class="parameter">value</replaceable> is omitted the
3306 command toggles between regular and tuples-only output.
3307 Regular output includes extra information such
3308 as column headers, titles, and various footers. In tuples-only
3309 mode, only actual table data is shown.
3310 </para>
3311 </listitem>
3312 </varlistentry>
3314 <varlistentry id="app-psql-meta-command-pset-unicode-border-linestyle">
3315 <term><literal>unicode_border_linestyle</literal></term>
3316 <listitem>
3317 <para>
3318 Sets the border drawing style for the <literal>unicode</literal>
3319 line style to one of <literal>single</literal>
3320 or <literal>double</literal>.
3321 </para>
3322 </listitem>
3323 </varlistentry>
3325 <varlistentry id="app-psql-meta-command-pset-unicode-column-linestyle">
3326 <term><literal>unicode_column_linestyle</literal></term>
3327 <listitem>
3328 <para>
3329 Sets the column drawing style for the <literal>unicode</literal>
3330 line style to one of <literal>single</literal>
3331 or <literal>double</literal>.
3332 </para>
3333 </listitem>
3334 </varlistentry>
3336 <varlistentry id="app-psql-meta-command-pset-unicode-header-linestyle">
3337 <term><literal>unicode_header_linestyle</literal></term>
3338 <listitem>
3339 <para>
3340 Sets the header drawing style for the <literal>unicode</literal>
3341 line style to one of <literal>single</literal>
3342 or <literal>double</literal>.
3343 </para>
3344 </listitem>
3345 </varlistentry>
3347 <varlistentry id="app-psql-meta-command-pset-xheader-width">
3348 <term><literal>xheader_width</literal></term>
3349 <listitem>
3350 <para>
3351 Sets the maximum width of the header for expanded output to one of
3352 <literal>full</literal> (the default value),
3353 <literal>column</literal>, <literal>page</literal>, or an
3354 <replaceable class="parameter">integer value</replaceable>.
3355 </para>
3357 <para>
3358 <literal>full</literal>: the expanded header is not truncated,
3359 and will be as wide as the widest output line.
3360 </para>
3362 <para>
3363 <literal>column</literal>: truncate the header line to the
3364 width of the first column.
3365 </para>
3367 <para>
3368 <literal>page</literal>: truncate the header line to the terminal
3369 width.
3370 </para>
3372 <para>
3373 <replaceable class="parameter">integer value</replaceable>: specify
3374 the exact maximum width of the header line.
3375 </para>
3376 </listitem>
3377 </varlistentry>
3378 </variablelist>
3379 </para>
3381 <para>
3382 Illustrations of how these different formats look can be seen in
3383 <xref linkend="app-psql-examples"/>, below.
3384 </para>
3386 <tip>
3387 <para>
3388 There are various shortcut commands for <command>\pset</command>. See
3389 <command>\a</command>, <command>\C</command>, <command>\f</command>,
3390 <command>\H</command>, <command>\t</command>, <command>\T</command>,
3391 and <command>\x</command>.
3392 </para>
3393 </tip>
3395 </listitem>
3396 </varlistentry>
3399 <varlistentry id="app-psql-meta-command-quit">
3400 <term><literal>\q</literal> or <literal>\quit</literal></term>
3401 <listitem>
3402 <para>
3403 Quits the <application>psql</application> program.
3404 In a script file, only execution of that script is terminated.
3405 </para>
3406 </listitem>
3407 </varlistentry>
3410 <varlistentry id="app-psql-meta-command-qecho">
3411 <term><literal>\qecho <replaceable class="parameter">text</replaceable> [ ... ] </literal></term>
3412 <listitem>
3413 <para>
3414 This command is identical to <command>\echo</command> except
3415 that the output will be written to the query output channel, as
3416 set by <command>\o</command>.
3417 </para>
3418 </listitem>
3419 </varlistentry>
3422 <varlistentry id="app-psql-meta-command-reset">
3423 <term><literal>\r</literal> or <literal>\reset</literal></term>
3424 <listitem>
3425 <para>
3426 Resets (clears) the query buffer.
3427 </para>
3428 </listitem>
3429 </varlistentry>
3432 <varlistentry id="app-psql-meta-command-s">
3433 <term><literal>\s [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
3434 <listitem>
3435 <para>
3436 Print <application>psql</application>'s command line history
3437 to <replaceable class="parameter">filename</replaceable>.
3438 If <replaceable class="parameter">filename</replaceable> is omitted,
3439 the history is written to the standard output (using the pager if
3440 appropriate). This command is not available
3441 if <application>psql</application> was built
3442 without <application>Readline</application> support.
3443 </para>
3444 </listitem>
3445 </varlistentry>
3448 <varlistentry id="app-psql-meta-command-set">
3449 <term><literal>\set [ <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> [ ... ] ] ]</literal></term>
3451 <listitem>
3452 <para>
3453 Sets the <application>psql</application> variable <replaceable
3454 class="parameter">name</replaceable> to <replaceable
3455 class="parameter">value</replaceable>, or if more than one value
3456 is given, to the concatenation of all of them. If only one
3457 argument is given, the variable is set to an empty-string value. To
3458 unset a variable, use the <command>\unset</command> command.
3459 </para>
3461 <para><command>\set</command> without any arguments displays the names and values
3462 of all currently-set <application>psql</application> variables.
3463 </para>
3465 <para>
3466 Valid variable names can contain letters, digits, and
3467 underscores. See <xref linkend="app-psql-variables"/> below for details.
3468 Variable names are case-sensitive.
3469 </para>
3471 <para>
3472 Certain variables are special, in that they
3473 control <application>psql</application>'s behavior or are
3474 automatically set to reflect connection state. These variables are
3475 documented in <xref linkend="app-psql-variables"/>, below.
3476 </para>
3478 <note>
3479 <para>
3480 This command is unrelated to the <acronym>SQL</acronym>
3481 command <link linkend="sql-set"><command>SET</command></link>.
3482 </para>
3483 </note>
3484 </listitem>
3485 </varlistentry>
3488 <varlistentry id="app-psql-meta-command-setenv">
3489 <term><literal>\setenv <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> ]</literal></term>
3491 <listitem>
3492 <para>
3493 Sets the environment variable <replaceable
3494 class="parameter">name</replaceable> to <replaceable
3495 class="parameter">value</replaceable>, or if the
3496 <replaceable class="parameter">value</replaceable> is
3497 not supplied, unsets the environment variable. Example:
3498 <programlisting>
3499 testdb=&gt; <userinput>\setenv PAGER less</userinput>
3500 testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
3501 </programlisting></para>
3502 </listitem>
3503 </varlistentry>
3505 <varlistentry id="app-psql-meta-command-sf">
3506 <term><literal>\sf[+] <replaceable class="parameter">function_description</replaceable> </literal></term>
3508 <listitem>
3509 <para>
3510 This command fetches and shows the definition of the named function or procedure,
3511 in the form of a <command>CREATE OR REPLACE FUNCTION</command> or
3512 <command>CREATE OR REPLACE PROCEDURE</command> command.
3513 The definition is printed to the current query output channel,
3514 as set by <command>\o</command>.
3515 </para>
3517 <para>
3518 The target function can be specified by name alone, or by name
3519 and arguments, for example <literal>foo(integer, text)</literal>.
3520 The argument types must be given if there is more
3521 than one function of the same name.
3522 </para>
3524 <para>
3525 If <literal>+</literal> is appended to the command name, then the
3526 output lines are numbered, with the first line of the function body
3527 being line 1.
3528 </para>
3530 <para>
3531 Unlike most other meta-commands, the entire remainder of the line is
3532 always taken to be the argument(s) of <command>\sf</command>, and neither
3533 variable interpolation nor backquote expansion are performed in the
3534 arguments.
3535 </para>
3536 </listitem>
3537 </varlistentry>
3540 <varlistentry id="app-psql-meta-command-sv">
3541 <term><literal>\sv[+] <replaceable class="parameter">view_name</replaceable> </literal></term>
3543 <listitem>
3544 <para>
3545 This command fetches and shows the definition of the named view,
3546 in the form of a <command>CREATE OR REPLACE VIEW</command> command.
3547 The definition is printed to the current query output channel,
3548 as set by <command>\o</command>.
3549 </para>
3551 <para>
3552 If <literal>+</literal> is appended to the command name, then the
3553 output lines are numbered from 1.
3554 </para>
3556 <para>
3557 Unlike most other meta-commands, the entire remainder of the line is
3558 always taken to be the argument(s) of <command>\sv</command>, and neither
3559 variable interpolation nor backquote expansion are performed in the
3560 arguments.
3561 </para>
3562 </listitem>
3563 </varlistentry>
3566 <varlistentry id="app-psql-meta-command-t-lc">
3567 <term><literal>\t</literal></term>
3568 <listitem>
3569 <para>
3570 Toggles the display of output column name headings and row count
3571 footer. This command is equivalent to <literal>\pset
3572 tuples_only</literal> and is provided for convenience.
3573 </para>
3574 </listitem>
3575 </varlistentry>
3578 <varlistentry id="app-psql-meta-command-t-uc">
3579 <term><literal>\T <replaceable class="parameter">table_options</replaceable></literal></term>
3580 <listitem>
3581 <para>
3582 Specifies attributes to be placed within the
3583 <sgmltag>table</sgmltag> tag in <acronym>HTML</acronym>
3584 output format. This command is equivalent to <literal>\pset
3585 tableattr <replaceable
3586 class="parameter">table_options</replaceable></literal>.
3587 </para>
3588 </listitem>
3589 </varlistentry>
3592 <varlistentry id="app-psql-meta-command-timing">
3593 <term><literal>\timing [ <replaceable class="parameter">on</replaceable> | <replaceable class="parameter">off</replaceable> ]</literal></term>
3594 <listitem>
3595 <para>
3596 With a parameter, turns displaying of how long each SQL statement
3597 takes on or off. Without a parameter, toggles the display between
3598 on and off. The display is in milliseconds; intervals longer than
3599 1 second are also shown in minutes:seconds format, with hours and
3600 days fields added if needed.
3601 </para>
3602 </listitem>
3603 </varlistentry>
3606 <varlistentry id="app-psql-meta-command-unset">
3607 <term><literal>\unset <replaceable class="parameter">name</replaceable></literal></term>
3609 <listitem>
3610 <para>
3611 Unsets (deletes) the <application>psql</application> variable <replaceable
3612 class="parameter">name</replaceable>.
3613 </para>
3615 <para>
3616 Most variables that control <application>psql</application>'s behavior
3617 cannot be unset; instead, an <literal>\unset</literal> command is interpreted
3618 as setting them to their default values.
3619 See <xref linkend="app-psql-variables"/> below.
3620 </para>
3621 </listitem>
3622 </varlistentry>
3625 <varlistentry id="app-psql-meta-command-write">
3626 <term><literal>\w</literal> or <literal>\write</literal> <replaceable class="parameter">filename</replaceable></term>
3627 <term><literal>\w</literal> or <literal>\write</literal> <literal>|</literal><replaceable class="parameter">command</replaceable></term>
3628 <listitem>
3629 <para>
3630 Writes the current query buffer to the file <replaceable
3631 class="parameter">filename</replaceable> or pipes it to the shell
3632 command <replaceable class="parameter">command</replaceable>.
3633 If the current query buffer is empty, the most recently executed query
3634 is written instead.
3635 </para>
3637 <para>
3638 If the argument begins with <literal>|</literal>, then the entire remainder
3639 of the line is taken to be
3640 the <replaceable class="parameter">command</replaceable> to execute,
3641 and neither variable interpolation nor backquote expansion are
3642 performed in it. The rest of the line is simply passed literally to
3643 the shell.
3644 </para>
3645 </listitem>
3646 </varlistentry>
3649 <varlistentry id="app-psql-meta-command-warn">
3650 <term><literal>\warn <replaceable class="parameter">text</replaceable> [ ... ]</literal></term>
3651 <listitem>
3652 <para>
3653 This command is identical to <command>\echo</command> except
3654 that the output will be written to <application>psql</application>'s
3655 standard error channel, rather than standard output.
3656 </para>
3657 </listitem>
3658 </varlistentry>
3661 <varlistentry id="app-psql-meta-command-watch">
3662 <term><literal>\watch [ i[nterval]=<replaceable class="parameter">seconds</replaceable> ] [ c[ount]=<replaceable class="parameter">times</replaceable> ] [ m[in_rows]=<replaceable class="parameter">rows</replaceable> ] [ <replaceable class="parameter">seconds</replaceable> ]</literal></term>
3663 <listitem>
3664 <para>
3665 Repeatedly execute the current query buffer (as <literal>\g</literal> does)
3666 until interrupted, or the query fails, or the execution count limit
3667 (if given) is reached, or the query no longer returns the minimum number
3668 of rows. Wait the specified number of seconds (default 2) between executions.
3669 For backwards compatibility,
3670 <replaceable class="parameter">seconds</replaceable> can be specified
3671 with or without an <literal>interval=</literal> prefix.
3672 Each query result is
3673 displayed with a header that includes the <literal>\pset title</literal>
3674 string (if any), the time as of query start, and the delay interval.
3675 </para>
3676 <para>
3677 If the current query buffer is empty, the most recently sent query
3678 is re-executed instead.
3679 </para>
3680 </listitem>
3681 </varlistentry>
3684 <varlistentry id="app-psql-meta-command-x">
3685 <term><literal>\x [ <replaceable class="parameter">on</replaceable> | <replaceable class="parameter">off</replaceable> | <replaceable class="parameter">auto</replaceable> ]</literal></term>
3686 <listitem>
3687 <para>
3688 Sets or toggles expanded table formatting mode. As such it is equivalent to
3689 <literal>\pset expanded</literal>.
3690 </para>
3691 </listitem>
3692 </varlistentry>
3695 <varlistentry id="app-psql-meta-command-z">
3696 <term><literal>\z[S] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
3697 <listitem>
3698 <para>
3699 Lists tables, views and sequences with their
3700 associated access privileges.
3701 If a <replaceable class="parameter">pattern</replaceable> is
3702 specified, only tables, views and sequences whose names match the
3703 pattern are listed. By default only user-created objects are shown;
3704 supply a pattern or the <literal>S</literal> modifier to include
3705 system objects.
3706 </para>
3708 <para>
3709 This is an alias for <command>\dp</command> (<quote>display
3710 privileges</quote>).
3711 </para>
3712 </listitem>
3713 </varlistentry>
3716 <varlistentry id="app-psql-meta-command-exclamation-mark">
3717 <term><literal>\! [ <replaceable class="parameter">command</replaceable> ]</literal></term>
3718 <listitem>
3719 <para>
3720 With no argument, escapes to a sub-shell; <application>psql</application>
3721 resumes when the sub-shell exits. With an argument, executes the
3722 shell command <replaceable class="parameter">command</replaceable>.
3723 </para>
3725 <para>
3726 Unlike most other meta-commands, the entire remainder of the line is
3727 always taken to be the argument(s) of <command>\!</command>, and neither
3728 variable interpolation nor backquote expansion are performed in the
3729 arguments. The rest of the line is simply passed literally to the
3730 shell.
3731 </para>
3732 </listitem>
3733 </varlistentry>
3736 <varlistentry id="app-psql-meta-command-question-mark">
3737 <term><literal>\? [ <replaceable class="parameter">topic</replaceable> ]</literal></term>
3738 <listitem>
3739 <para>
3740 Shows help information. The optional
3741 <replaceable class="parameter">topic</replaceable> parameter
3742 (defaulting to <literal>commands</literal>) selects which part of <application>psql</application> is
3743 explained: <literal>commands</literal> describes <application>psql</application>'s
3744 backslash commands; <literal>options</literal> describes the command-line
3745 options that can be passed to <application>psql</application>;
3746 and <literal>variables</literal> shows help about <application>psql</application> configuration
3747 variables.
3748 </para>
3749 </listitem>
3750 </varlistentry>
3753 <varlistentry id="app-psql-meta-command-semicolon">
3754 <term><literal>\;</literal></term>
3755 <listitem>
3756 <para>
3757 Backslash-semicolon is not a meta-command in the same way as the
3758 preceding commands; rather, it simply causes a semicolon to be
3759 added to the query buffer without any further processing.
3760 </para>
3762 <para>
3763 Normally, <application>psql</application> will dispatch an SQL command to the
3764 server as soon as it reaches the command-ending semicolon, even if
3765 more input remains on the current line. Thus for example entering
3766 <programlisting>
3767 select 1; select 2; select 3;
3768 </programlisting>
3769 will result in the three SQL commands being individually sent to
3770 the server, with each one's results being displayed before
3771 continuing to the next command. However, a semicolon entered
3772 as <literal>\;</literal> will not trigger command processing, so that the
3773 command before it and the one after are effectively combined and
3774 sent to the server in one request. So for example
3775 <programlisting>
3776 select 1\; select 2\; select 3;
3777 </programlisting>
3778 results in sending the three SQL commands to the server in a single
3779 request, when the non-backslashed semicolon is reached.
3780 The server executes such a request as a single transaction,
3781 unless there are explicit <command>BEGIN</command>/<command>COMMIT</command>
3782 commands included in the string to divide it into multiple
3783 transactions. (See <xref linkend="protocol-flow-multi-statement"/>
3784 for more details about how the server handles multi-query strings.)
3785 </para>
3786 </listitem>
3787 </varlistentry>
3789 </variablelist>
3790 </para>
3792 <refsect3 id="app-psql-patterns" xreflabel="Patterns">
3793 <title>Patterns</title>
3795 <indexterm>
3796 <primary>patterns</primary>
3797 <secondary>in psql and pg_dump</secondary>
3798 </indexterm>
3800 <para>
3801 The various <literal>\d</literal> commands accept a <replaceable
3802 class="parameter">pattern</replaceable> parameter to specify the
3803 object name(s) to be displayed. In the simplest case, a pattern
3804 is just the exact name of the object. The characters within a
3805 pattern are normally folded to lower case, just as in SQL names;
3806 for example, <literal>\dt FOO</literal> will display the table named
3807 <literal>foo</literal>. As in SQL names, placing double quotes around
3808 a pattern stops folding to lower case. Should you need to include
3809 an actual double quote character in a pattern, write it as a pair
3810 of double quotes within a double-quote sequence; again this is in
3811 accord with the rules for SQL quoted identifiers. For example,
3812 <literal>\dt "FOO""BAR"</literal> will display the table named
3813 <literal>FOO"BAR</literal> (not <literal>foo"bar</literal>). Unlike the normal
3814 rules for SQL names, you can put double quotes around just part
3815 of a pattern, for instance <literal>\dt FOO"FOO"BAR</literal> will display
3816 the table named <literal>fooFOObar</literal>.
3817 </para>
3819 <para>
3820 Whenever the <replaceable class="parameter">pattern</replaceable> parameter
3821 is omitted completely, the <literal>\d</literal> commands display all objects
3822 that are visible in the current schema search path &mdash; this is
3823 equivalent to using <literal>*</literal> as the pattern.
3824 (An object is said to be <firstterm>visible</firstterm> if its
3825 containing schema is in the search path and no object of the same
3826 kind and name appears earlier in the search path. This is equivalent to the
3827 statement that the object can be referenced by name without explicit
3828 schema qualification.)
3829 To see all objects in the database regardless of visibility,
3830 use <literal>*.*</literal> as the pattern.
3831 </para>
3833 <para>
3834 Within a pattern, <literal>*</literal> matches any sequence of characters
3835 (including no characters) and <literal>?</literal> matches any single character.
3836 (This notation is comparable to Unix shell file name patterns.)
3837 For example, <literal>\dt int*</literal> displays tables whose names
3838 begin with <literal>int</literal>. But within double quotes, <literal>*</literal>
3839 and <literal>?</literal> lose these special meanings and are just matched
3840 literally.
3841 </para>
3843 <para>
3844 A relation pattern that contains a dot (<literal>.</literal>) is interpreted as a schema
3845 name pattern followed by an object name pattern. For example,
3846 <literal>\dt foo*.*bar*</literal> displays all tables whose table name
3847 includes <literal>bar</literal> that are in schemas whose schema name
3848 starts with <literal>foo</literal>. When no dot appears, then the pattern
3849 matches only objects that are visible in the current schema search path.
3850 Again, a dot within double quotes loses its special meaning and is matched
3851 literally. A relation pattern that contains two dots (<literal>.</literal>)
3852 is interpreted as a database name followed by a schema name pattern followed
3853 by an object name pattern. The database name portion will not be treated as
3854 a pattern and must match the name of the currently connected database, else
3855 an error will be raised.
3856 </para>
3858 <para>
3859 A schema pattern that contains a dot (<literal>.</literal>) is interpreted
3860 as a database name followed by a schema name pattern. For example,
3861 <literal>\dn mydb.*foo*</literal> displays all schemas whose schema name
3862 includes <literal>foo</literal>. The database name portion will not be
3863 treated as a pattern and must match the name of the currently connected
3864 database, else an error will be raised.
3865 </para>
3867 <para>
3868 Advanced users can use regular-expression notations such as character
3869 classes, for example <literal>[0-9]</literal> to match any digit. All regular
3870 expression special characters work as specified in
3871 <xref linkend="functions-posix-regexp"/>, except for <literal>.</literal> which
3872 is taken as a separator as mentioned above, <literal>*</literal> which is
3873 translated to the regular-expression notation <literal>.*</literal>,
3874 <literal>?</literal> which is translated to <literal>.</literal>, and
3875 <literal>$</literal> which is matched literally. You can emulate
3876 these pattern characters at need by writing
3877 <literal>?</literal> for <literal>.</literal>,
3878 <literal>(<replaceable class="parameter">R</replaceable>+|)</literal> for
3879 <literal><replaceable class="parameter">R</replaceable>*</literal>, or
3880 <literal>(<replaceable class="parameter">R</replaceable>|)</literal> for
3881 <literal><replaceable class="parameter">R</replaceable>?</literal>.
3882 <literal>$</literal> is not needed as a regular-expression character since
3883 the pattern must match the whole name, unlike the usual
3884 interpretation of regular expressions (in other words, <literal>$</literal>
3885 is automatically appended to your pattern). Write <literal>*</literal> at the
3886 beginning and/or end if you don't wish the pattern to be anchored.
3887 Note that within double quotes, all regular expression special characters
3888 lose their special meanings and are matched literally. Also, the regular
3889 expression special characters are matched literally in operator name
3890 patterns (i.e., the argument of <literal>\do</literal>).
3891 </para>
3892 </refsect3>
3893 </refsect2>
3895 <refsect2>
3896 <title>Advanced Features</title>
3898 <refsect3 id="app-psql-variables" xreflabel="Variables">
3899 <title>Variables</title>
3901 <para>
3902 <application>psql</application> provides variable substitution
3903 features similar to common Unix command shells.
3904 Variables are simply name/value pairs, where the value
3905 can be any string of any length. The name must consist of letters
3906 (including non-Latin letters), digits, and underscores.
3907 </para>
3909 <para>
3910 To set a variable, use the <application>psql</application> meta-command
3911 <command>\set</command>. For example,
3912 <programlisting>
3913 testdb=&gt; <userinput>\set foo bar</userinput>
3914 </programlisting>
3915 sets the variable <literal>foo</literal> to the value
3916 <literal>bar</literal>. To retrieve the content of the variable, precede
3917 the name with a colon, for example:
3918 <programlisting>
3919 testdb=&gt; <userinput>\echo :foo</userinput>
3921 </programlisting>
3922 This works in both regular SQL commands and meta-commands; there is
3923 more detail in <xref linkend="app-psql-interpolation"/>, below.
3924 </para>
3926 <para>
3927 If you call <command>\set</command> without a second argument, the
3928 variable is set to an empty-string value. To unset (i.e., delete)
3929 a variable, use the command <command>\unset</command>. To show the
3930 values of all variables, call <command>\set</command> without any argument.
3931 </para>
3933 <note>
3934 <para>
3935 The arguments of <command>\set</command> are subject to the same
3936 substitution rules as with other commands. Thus you can construct
3937 interesting references such as <literal>\set :foo
3938 'something'</literal> and get <quote>soft links</quote> or
3939 <quote>variable variables</quote> of <productname>Perl</productname>
3940 or <productname><acronym>PHP</acronym></productname> fame,
3941 respectively. Unfortunately (or fortunately?), there is no way to do
3942 anything useful with these constructs. On the other hand,
3943 <literal>\set bar :foo</literal> is a perfectly valid way to copy a
3944 variable.
3945 </para>
3946 </note>
3948 <para>
3949 A number of these variables are treated specially
3950 by <application>psql</application>. They represent certain option
3951 settings that can be changed at run time by altering the value of
3952 the variable, or in some cases represent changeable state of
3953 <application>psql</application>.
3954 By convention, all specially treated variables' names
3955 consist of all upper-case ASCII letters (and possibly digits and
3956 underscores). To ensure maximum compatibility in the future, avoid
3957 using such variable names for your own purposes.
3958 </para>
3960 <para>
3961 Variables that control <application>psql</application>'s behavior
3962 generally cannot be unset or set to invalid values. An <literal>\unset</literal>
3963 command is allowed but is interpreted as setting the variable to its
3964 default value. A <literal>\set</literal> command without a second argument is
3965 interpreted as setting the variable to <literal>on</literal>, for control
3966 variables that accept that value, and is rejected for others. Also,
3967 control variables that accept the values <literal>on</literal>
3968 and <literal>off</literal> will also accept other common spellings of Boolean
3969 values, such as <literal>true</literal> and <literal>false</literal>.
3970 </para>
3972 <para>
3973 The specially treated variables are:
3974 </para>
3976 <variablelist>
3977 <varlistentry id="app-psql-variables-autocommit">
3978 <term>
3979 <varname>AUTOCOMMIT</varname>
3980 <indexterm>
3981 <primary>autocommit</primary>
3982 <secondary>psql</secondary>
3983 </indexterm>
3984 </term>
3985 <listitem>
3986 <para>
3987 When <literal>on</literal> (the default), each SQL command is automatically
3988 committed upon successful completion. To postpone commit in this
3989 mode, you must enter a <command>BEGIN</command> or <command>START
3990 TRANSACTION</command> SQL command. When <literal>off</literal> or unset, SQL
3991 commands are not committed until you explicitly issue
3992 <command>COMMIT</command> or <command>END</command>. The autocommit-off
3993 mode works by issuing an implicit <command>BEGIN</command> for you, just
3994 before any command that is not already in a transaction block and
3995 is not itself a <command>BEGIN</command> or other transaction-control
3996 command, nor a command that cannot be executed inside a transaction
3997 block (such as <command>VACUUM</command>).
3998 </para>
4000 <note>
4001 <para>
4002 In autocommit-off mode, you must explicitly abandon any failed
4003 transaction by entering <command>ABORT</command> or <command>ROLLBACK</command>.
4004 Also keep in mind that if you exit the session
4005 without committing, your work will be lost.
4006 </para>
4007 </note>
4009 <note>
4010 <para>
4011 The autocommit-on mode is <productname>PostgreSQL</productname>'s traditional
4012 behavior, but autocommit-off is closer to the SQL spec. If you
4013 prefer autocommit-off, you might wish to set it in the system-wide
4014 <filename>psqlrc</filename> file or your
4015 <filename>~/.psqlrc</filename> file.
4016 </para>
4017 </note>
4018 </listitem>
4019 </varlistentry>
4021 <varlistentry id="app-psql-variables-comp-keyword-case">
4022 <term><varname>COMP_KEYWORD_CASE</varname></term>
4023 <listitem>
4024 <para>
4025 Determines which letter case to use when completing an SQL key word.
4026 If set to <literal>lower</literal> or <literal>upper</literal>, the
4027 completed word will be in lower or upper case, respectively. If set
4028 to <literal>preserve-lower</literal>
4029 or <literal>preserve-upper</literal> (the default), the completed word
4030 will be in the case of the word already entered, but words being
4031 completed without anything entered will be in lower or upper case,
4032 respectively.
4033 </para>
4034 </listitem>
4035 </varlistentry>
4037 <varlistentry id="app-psql-variables-dbname">
4038 <term><varname>DBNAME</varname></term>
4039 <listitem>
4040 <para>
4041 The name of the database you are currently connected to. This is
4042 set every time you connect to a database (including program
4043 start-up), but can be changed or unset.
4044 </para>
4045 </listitem>
4046 </varlistentry>
4048 <varlistentry id="app-psql-variables-echo">
4049 <term><varname>ECHO</varname></term>
4050 <listitem>
4051 <para>
4052 If set to <literal>all</literal>, all nonempty input lines are printed
4053 to standard output as they are read. (This does not apply to lines
4054 read interactively.) To select this behavior on program
4055 start-up, use the switch <option>-a</option>. If set to
4056 <literal>queries</literal>,
4057 <application>psql</application> prints each query to standard output
4058 as it is sent to the server. The switch to select this behavior is
4059 <option>-e</option>. If set to <literal>errors</literal>, then only
4060 failed queries are displayed on standard error output. The switch
4061 for this behavior is <option>-b</option>. If set to
4062 <literal>none</literal> (the default), then no queries are displayed.
4063 </para>
4064 </listitem>
4065 </varlistentry>
4067 <varlistentry id="app-psql-variables-echo-hidden">
4068 <term><varname>ECHO_HIDDEN</varname></term>
4069 <listitem>
4070 <para>
4071 When this variable is set to <literal>on</literal> and a backslash command
4072 queries the database, the query is first shown.
4073 This feature helps you to study
4074 <productname>PostgreSQL</productname> internals and provide
4075 similar functionality in your own programs. (To select this behavior
4076 on program start-up, use the switch <option>-E</option>.) If you set
4077 this variable to the value <literal>noexec</literal>, the queries are
4078 just shown but are not actually sent to the server and executed.
4079 The default value is <literal>off</literal>.
4080 </para>
4081 </listitem>
4082 </varlistentry>
4084 <varlistentry id="app-psql-variables-encoding">
4085 <term><varname>ENCODING</varname></term>
4086 <listitem>
4087 <para>
4088 The current client character set encoding.
4089 This is set every time you connect to a database (including
4090 program start-up), and when you change the encoding
4091 with <literal>\encoding</literal>, but it can be changed or unset.
4092 </para>
4093 </listitem>
4094 </varlistentry>
4096 <varlistentry id="app-psql-variables-error">
4097 <term><varname>ERROR</varname></term>
4098 <listitem>
4099 <para>
4100 <literal>true</literal> if the last SQL query failed, <literal>false</literal> if
4101 it succeeded. See also <varname>SQLSTATE</varname>.
4102 </para>
4103 </listitem>
4104 </varlistentry>
4106 <varlistentry id="app-psql-variables-fetch-count">
4107 <term><varname>FETCH_COUNT</varname></term>
4108 <listitem>
4109 <para>
4110 If this variable is set to an integer value greater than zero,
4111 the results of <command>SELECT</command> queries are fetched
4112 and displayed in groups of that many rows, rather than the
4113 default behavior of collecting the entire result set before
4114 display. Therefore only a
4115 limited amount of memory is used, regardless of the size of
4116 the result set. Settings of 100 to 1000 are commonly used
4117 when enabling this feature.
4118 Keep in mind that when using this feature, a query might
4119 fail after having already displayed some rows.
4120 </para>
4122 <tip>
4123 <para>
4124 Although you can use any output format with this feature,
4125 the default <literal>aligned</literal> format tends to look bad
4126 because each group of <varname>FETCH_COUNT</varname> rows
4127 will be formatted separately, leading to varying column
4128 widths across the row groups. The other output formats work better.
4129 </para>
4130 </tip>
4131 </listitem>
4132 </varlistentry>
4134 <varlistentry id="app-psql-variables-hide-tableam">
4135 <term><varname>HIDE_TABLEAM</varname></term>
4136 <listitem>
4137 <para>
4138 If this variable is set to <literal>true</literal>, a table's access
4139 method details are not displayed. This is mainly useful for
4140 regression tests.
4141 </para>
4142 </listitem>
4143 </varlistentry>
4145 <varlistentry id="app-psql-variables-hide-toast-compression">
4146 <term><varname>HIDE_TOAST_COMPRESSION</varname></term>
4147 <listitem>
4148 <para>
4149 If this variable is set to <literal>true</literal>, column
4150 compression method details are not displayed. This is mainly
4151 useful for regression tests.
4152 </para>
4153 </listitem>
4154 </varlistentry>
4156 <varlistentry id="app-psql-variables-histcontrol">
4157 <term><varname>HISTCONTROL</varname></term>
4158 <listitem>
4159 <para>
4160 If this variable is set to <literal>ignorespace</literal>,
4161 lines which begin with a space are not entered into the history
4162 list. If set to a value of <literal>ignoredups</literal>, lines
4163 matching the previous history line are not entered. A value of
4164 <literal>ignoreboth</literal> combines the two options. If
4165 set to <literal>none</literal> (the default), all lines
4166 read in interactive mode are saved on the history list.
4167 </para>
4168 <note>
4169 <para>
4170 This feature was shamelessly plagiarized from
4171 <application>Bash</application>.
4172 </para>
4173 </note>
4174 </listitem>
4175 </varlistentry>
4177 <varlistentry id="app-psql-variables-histfile">
4178 <term><varname>HISTFILE</varname></term>
4179 <listitem>
4180 <para>
4181 The file name that will be used to store the history list. If unset,
4182 the file name is taken from the <envar>PSQL_HISTORY</envar>
4183 environment variable. If that is not set either, the default
4184 is <filename>~/.psql_history</filename>,
4185 or <filename>%APPDATA%\postgresql\psql_history</filename> on Windows.
4186 For example, putting:
4187 <programlisting>
4188 \set HISTFILE ~/.psql_history-:DBNAME
4189 </programlisting>
4190 in <filename>~/.psqlrc</filename> will cause
4191 <application>psql</application> to maintain a separate history for
4192 each database.
4193 </para>
4194 <note>
4195 <para>
4196 This feature was shamelessly plagiarized from
4197 <application>Bash</application>.
4198 </para>
4199 </note>
4200 </listitem>
4201 </varlistentry>
4203 <varlistentry id="app-psql-variables-histsize">
4204 <term><varname>HISTSIZE</varname></term>
4205 <listitem>
4206 <para>
4207 The maximum number of commands to store in the command history
4208 (default 500). If set to a negative value, no limit is applied.
4209 </para>
4210 <note>
4211 <para>
4212 This feature was shamelessly plagiarized from
4213 <application>Bash</application>.
4214 </para>
4215 </note>
4216 </listitem>
4217 </varlistentry>
4219 <varlistentry id="app-psql-variables-host">
4220 <term><varname>HOST</varname></term>
4221 <listitem>
4222 <para>
4223 The database server host you are currently connected to. This is
4224 set every time you connect to a database (including program
4225 start-up), but can be changed or unset.
4226 </para>
4227 </listitem>
4228 </varlistentry>
4230 <varlistentry id="app-psql-variables-ignoreeof">
4231 <term><varname>IGNOREEOF</varname></term>
4232 <listitem>
4233 <para>
4234 If set to 1 or less, sending an <acronym>EOF</acronym> character (usually
4235 <keycombo action="simul"><keycap>Control</keycap><keycap>D</keycap></keycombo>)
4236 to an interactive session of <application>psql</application>
4237 will terminate the application. If set to a larger numeric value,
4238 that many consecutive <acronym>EOF</acronym> characters must be typed to
4239 make an interactive session terminate. If the variable is set to a
4240 non-numeric value, it is interpreted as 10. The default is 0.
4241 </para>
4242 <note>
4243 <para>
4244 This feature was shamelessly plagiarized from
4245 <application>Bash</application>.
4246 </para>
4247 </note>
4248 </listitem>
4249 </varlistentry>
4251 <varlistentry id="app-psql-variables-lastoid">
4252 <term><varname>LASTOID</varname></term>
4253 <listitem>
4254 <para>
4255 The value of the last affected OID, as returned from an
4256 <command>INSERT</command> or <command>\lo_import</command>
4257 command. This variable is only guaranteed to be valid until
4258 after the result of the next <acronym>SQL</acronym> command has
4259 been displayed.
4260 <productname>PostgreSQL</productname> servers since version 12 do not
4261 support OID system columns anymore, thus LASTOID will always be 0
4262 following <command>INSERT</command> when targeting such servers.
4263 </para>
4264 </listitem>
4265 </varlistentry>
4267 <varlistentry id="app-psql-variables-last-error-message">
4268 <term><varname>LAST_ERROR_MESSAGE</varname></term>
4269 <term><varname>LAST_ERROR_SQLSTATE</varname></term>
4270 <listitem>
4271 <para>
4272 The primary error message and associated SQLSTATE code for the most
4273 recent failed query in the current <application>psql</application> session, or
4274 an empty string and <literal>00000</literal> if no error has occurred in
4275 the current session.
4276 </para>
4277 </listitem>
4278 </varlistentry>
4280 <varlistentry id="app-psql-variables-on-error-rollback">
4281 <term>
4282 <varname>ON_ERROR_ROLLBACK</varname>
4283 <indexterm>
4284 <primary>rollback</primary>
4285 <secondary>psql</secondary>
4286 </indexterm>
4287 </term>
4288 <listitem>
4289 <para>
4290 When set to <literal>on</literal>, if a statement in a transaction block
4291 generates an error, the error is ignored and the transaction
4292 continues. When set to <literal>interactive</literal>, such errors are only
4293 ignored in interactive sessions, and not when reading script
4294 files. When set to <literal>off</literal> (the default), a statement in a
4295 transaction block that generates an error aborts the entire
4296 transaction. The error rollback mode works by issuing an
4297 implicit <command>SAVEPOINT</command> for you, just before each command
4298 that is in a transaction block, and then rolling back to the
4299 savepoint if the command fails.
4300 </para>
4301 </listitem>
4302 </varlistentry>
4304 <varlistentry id="app-psql-variables-on-error-stop">
4305 <term><varname>ON_ERROR_STOP</varname></term>
4306 <listitem>
4307 <para>
4308 By default, command processing continues after an error. When this
4309 variable is set to <literal>on</literal>, processing will instead stop
4310 immediately. In interactive mode,
4311 <application>psql</application> will return to the command prompt;
4312 otherwise, <application>psql</application> will exit, returning
4313 error code 3 to distinguish this case from fatal error
4314 conditions, which are reported using error code 1. In either case,
4315 any currently running scripts (the top-level script, if any, and any
4316 other scripts which it may have in invoked) will be terminated
4317 immediately. If the top-level command string contained multiple SQL
4318 commands, processing will stop with the current command.
4319 </para>
4320 </listitem>
4321 </varlistentry>
4323 <varlistentry id="app-psql-variables-port">
4324 <term><varname>PORT</varname></term>
4325 <listitem>
4326 <para>
4327 The database server port to which you are currently connected.
4328 This is set every time you connect to a database (including
4329 program start-up), but can be changed or unset.
4330 </para>
4331 </listitem>
4332 </varlistentry>
4334 <varlistentry id="app-psql-variables-prompt">
4335 <term><varname>PROMPT1</varname></term>
4336 <term><varname>PROMPT2</varname></term>
4337 <term><varname>PROMPT3</varname></term>
4338 <listitem>
4339 <para>
4340 These specify what the prompts <application>psql</application>
4341 issues should look like. See <xref
4342 linkend="app-psql-prompting"/> below.
4343 </para>
4344 </listitem>
4345 </varlistentry>
4347 <varlistentry id="app-psql-variables-quiet">
4348 <term><varname>QUIET</varname></term>
4349 <listitem>
4350 <para>
4351 Setting this variable to <literal>on</literal> is equivalent to the command
4352 line option <option>-q</option>. It is probably not too useful in
4353 interactive mode.
4354 </para>
4355 </listitem>
4356 </varlistentry>
4358 <varlistentry id="app-psql-variables-row-count">
4359 <term><varname>ROW_COUNT</varname></term>
4360 <listitem>
4361 <para>
4362 The number of rows returned or affected by the last SQL query, or 0
4363 if the query failed or did not report a row count.
4364 </para>
4365 </listitem>
4366 </varlistentry>
4368 <varlistentry id="app-psql-variables-server-version-name">
4369 <term><varname>SERVER_VERSION_NAME</varname></term>
4370 <term><varname>SERVER_VERSION_NUM</varname></term>
4371 <listitem>
4372 <para>
4373 The server's version number as a string, for
4374 example <literal>9.6.2</literal>, <literal>10.1</literal> or <literal>11beta1</literal>,
4375 and in numeric form, for
4376 example <literal>90602</literal> or <literal>100001</literal>.
4377 These are set every time you connect to a database
4378 (including program start-up), but can be changed or unset.
4379 </para>
4380 </listitem>
4381 </varlistentry>
4383 <varlistentry id="app-psql-variables-service">
4384 <term><varname>SERVICE</varname></term>
4385 <listitem>
4386 <para>
4387 The service name, if applicable.
4388 </para>
4389 </listitem>
4390 </varlistentry>
4392 <varlistentry id="app-psql-variables-shell-error">
4393 <term><varname>SHELL_ERROR</varname></term>
4394 <listitem>
4395 <para>
4396 <literal>true</literal> if the last shell command
4397 failed, <literal>false</literal> if it succeeded.
4398 This applies to shell commands invoked via the <literal>\!</literal>,
4399 <literal>\g</literal>, <literal>\o</literal>, <literal>\w</literal>,
4400 and <literal>\copy</literal> meta-commands, as well as backquote
4401 (<literal>`</literal>) expansion. Note that
4402 for <literal>\o</literal>, this variable is updated when the output
4403 pipe is closed by the next <literal>\o</literal> command.
4404 See also <varname>SHELL_EXIT_CODE</varname>.
4405 </para>
4406 </listitem>
4407 </varlistentry>
4409 <varlistentry id="app-psql-variables-shell-exit-code">
4410 <term><varname>SHELL_EXIT_CODE</varname></term>
4411 <listitem>
4412 <para>
4413 The exit status returned by the last shell command.
4414 0&ndash;127 represent program exit codes, 128&ndash;255
4415 indicate termination by a signal, and -1 indicates failure
4416 to launch a program or to collect its exit status.
4417 This applies to shell commands invoked via the <literal>\!</literal>,
4418 <literal>\g</literal>, <literal>\o</literal>, <literal>\w</literal>,
4419 and <literal>\copy</literal> meta-commands, as well as backquote
4420 (<literal>`</literal>) expansion. Note that
4421 for <literal>\o</literal>, this variable is updated when the output
4422 pipe is closed by the next <literal>\o</literal> command.
4423 See also <varname>SHELL_ERROR</varname>.
4424 </para>
4425 </listitem>
4426 </varlistentry>
4428 <varlistentry id="app-psql-variables-show-all-results">
4429 <term><varname>SHOW_ALL_RESULTS</varname></term>
4430 <listitem>
4431 <para>
4432 When this variable is set to <literal>off</literal>, only the last
4433 result of a combined query (<literal>\;</literal>) is shown instead of
4434 all of them. The default is <literal>on</literal>. The off behavior
4435 is for compatibility with older versions of
4436 <application>psql</application>.
4437 </para>
4438 </listitem>
4439 </varlistentry>
4441 <varlistentry id="app-psql-variables-show-context">
4442 <term><varname>SHOW_CONTEXT</varname></term>
4443 <listitem>
4444 <para>
4445 This variable can be set to the
4446 values <literal>never</literal>, <literal>errors</literal>, or <literal>always</literal>
4447 to control whether <literal>CONTEXT</literal> fields are displayed in
4448 messages from the server. The default is <literal>errors</literal> (meaning
4449 that context will be shown in error messages, but not in notice or
4450 warning messages). This setting has no effect
4451 when <varname>VERBOSITY</varname> is set to <literal>terse</literal>
4452 or <literal>sqlstate</literal>.
4453 (See also <command>\errverbose</command>, for use when you want a verbose
4454 version of the error you just got.)
4455 </para>
4456 </listitem>
4457 </varlistentry>
4459 <varlistentry id="app-psql-variables-singleline">
4460 <term><varname>SINGLELINE</varname></term>
4461 <listitem>
4462 <para>
4463 Setting this variable to <literal>on</literal> is equivalent to the command
4464 line option <option>-S</option>.
4465 </para>
4466 </listitem>
4467 </varlistentry>
4469 <varlistentry id="app-psql-variables-singlestep">
4470 <term><varname>SINGLESTEP</varname></term>
4471 <listitem>
4472 <para>
4473 Setting this variable to <literal>on</literal> is equivalent to the command
4474 line option <option>-s</option>.
4475 </para>
4476 </listitem>
4477 </varlistentry>
4479 <varlistentry id="app-psql-variables-sqlstate">
4480 <term><varname>SQLSTATE</varname></term>
4481 <listitem>
4482 <para>
4483 The error code (see <xref linkend="errcodes-appendix"/>) associated
4484 with the last SQL query's failure, or <literal>00000</literal> if it
4485 succeeded.
4486 </para>
4487 </listitem>
4488 </varlistentry>
4490 <varlistentry id="app-psql-variables-user">
4491 <term><varname>USER</varname></term>
4492 <listitem>
4493 <para>
4494 The database user you are currently connected as. This is set
4495 every time you connect to a database (including program
4496 start-up), but can be changed or unset.
4497 </para>
4498 </listitem>
4499 </varlistentry>
4501 <varlistentry id="app-psql-variables-verbosity">
4502 <term><varname>VERBOSITY</varname></term>
4503 <listitem>
4504 <para>
4505 This variable can be set to the values <literal>default</literal>,
4506 <literal>verbose</literal>, <literal>terse</literal>,
4507 or <literal>sqlstate</literal> to control the verbosity of error
4508 reports.
4509 (See also <command>\errverbose</command>, for use when you want a verbose
4510 version of the error you just got.)
4511 </para>
4512 </listitem>
4513 </varlistentry>
4515 <varlistentry id="app-psql-variables-version">
4516 <term><varname>VERSION</varname></term>
4517 <term><varname>VERSION_NAME</varname></term>
4518 <term><varname>VERSION_NUM</varname></term>
4519 <listitem>
4520 <para>
4521 These variables are set at program start-up to reflect
4522 <application>psql</application>'s version, respectively as a verbose string,
4523 a short string (e.g., <literal>9.6.2</literal>, <literal>10.1</literal>,
4524 or <literal>11beta1</literal>), and a number (e.g., <literal>90602</literal>
4525 or <literal>100001</literal>). They can be changed or unset.
4526 </para>
4527 </listitem>
4528 </varlistentry>
4530 </variablelist>
4532 </refsect3>
4534 <refsect3 id="app-psql-interpolation" xreflabel="SQL Interpolation">
4535 <title><acronym>SQL</acronym> Interpolation</title>
4537 <para>
4538 A key feature of <application>psql</application>
4539 variables is that you can substitute (<quote>interpolate</quote>)
4540 them into regular <acronym>SQL</acronym> statements, as well as the
4541 arguments of meta-commands. Furthermore,
4542 <application>psql</application> provides facilities for
4543 ensuring that variable values used as SQL literals and identifiers are
4544 properly quoted. The syntax for interpolating a value without
4545 any quoting is to prepend the variable name with a colon
4546 (<literal>:</literal>). For example,
4547 <programlisting>
4548 testdb=&gt; <userinput>\set foo 'my_table'</userinput>
4549 testdb=&gt; <userinput>SELECT * FROM :foo;</userinput>
4550 </programlisting>
4551 would query the table <literal>my_table</literal>. Note that this
4552 may be unsafe: the value of the variable is copied literally, so it can
4553 contain unbalanced quotes, or even backslash commands. You must make sure
4554 that it makes sense where you put it.
4555 </para>
4557 <para>
4558 When a value is to be used as an SQL literal or identifier, it is
4559 safest to arrange for it to be quoted. To quote the value of
4560 a variable as an SQL literal, write a colon followed by the variable
4561 name in single quotes. To quote the value as an SQL identifier, write
4562 a colon followed by the variable name in double quotes.
4563 These constructs deal correctly with quotes and other special
4564 characters embedded within the variable value.
4565 The previous example would be more safely written this way:
4566 <programlisting>
4567 testdb=&gt; <userinput>\set foo 'my_table'</userinput>
4568 testdb=&gt; <userinput>SELECT * FROM :"foo";</userinput>
4569 </programlisting>
4570 </para>
4572 <para>
4573 Variable interpolation will not be performed within quoted
4574 <acronym>SQL</acronym> literals and identifiers. Therefore, a
4575 construction such as <literal>':foo'</literal> doesn't work to produce a quoted
4576 literal from a variable's value (and it would be unsafe if it did work,
4577 since it wouldn't correctly handle quotes embedded in the value).
4578 </para>
4580 <para>
4581 One example use of this mechanism is to
4582 copy the contents of a file into a table column.
4583 First load the file into a variable and then interpolate the variable's
4584 value as a quoted string:
4585 <programlisting>
4586 testdb=&gt; <userinput>\set content `cat my_file.txt`</userinput>
4587 testdb=&gt; <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
4588 </programlisting>
4589 (Note that this still won't work if <filename>my_file.txt</filename> contains NUL bytes.
4590 <application>psql</application> does not support embedded NUL bytes in variable values.)
4591 </para>
4593 <para>
4594 Since colons can legally appear in SQL commands, an apparent attempt
4595 at interpolation (that is, <literal>:name</literal>,
4596 <literal>:'name'</literal>, or <literal>:"name"</literal>) is not
4597 replaced unless the named variable is currently set. In any case, you
4598 can escape a colon with a backslash to protect it from substitution.
4599 </para>
4601 <para>
4602 The <literal>:{?<replaceable>name</replaceable>}</literal> special syntax returns TRUE
4603 or FALSE depending on whether the variable exists or not, and is thus
4604 always substituted, unless the colon is backslash-escaped.
4605 </para>
4607 <para>
4608 The colon syntax for variables is standard <acronym>SQL</acronym> for
4609 embedded query languages, such as <application>ECPG</application>.
4610 The colon syntaxes for array slices and type casts are
4611 <productname>PostgreSQL</productname> extensions, which can sometimes
4612 conflict with the standard usage. The colon-quote syntax for escaping a
4613 variable's value as an SQL literal or identifier is a
4614 <application>psql</application> extension.
4615 </para>
4617 </refsect3>
4619 <refsect3 id="app-psql-prompting" xreflabel="Prompting">
4620 <title>Prompting</title>
4622 <para>
4623 The prompts <application>psql</application> issues can be customized
4624 to your preference. The three variables <varname>PROMPT1</varname>,
4625 <varname>PROMPT2</varname>, and <varname>PROMPT3</varname> contain strings
4626 and special escape sequences that describe the appearance of the
4627 prompt. Prompt 1 is the normal prompt that is issued when
4628 <application>psql</application> requests a new command. Prompt 2 is
4629 issued when more input is expected during command entry, for example
4630 because the command was not terminated with a semicolon or a quote
4631 was not closed.
4632 Prompt 3 is issued when you are running an <acronym>SQL</acronym>
4633 <command>COPY FROM STDIN</command> command and you need to type in
4634 a row value on the terminal.
4635 </para>
4637 <para>
4638 The value of the selected prompt variable is printed literally,
4639 except where a percent sign (<literal>%</literal>) is encountered.
4640 Depending on the next character, certain other text is substituted
4641 instead. Defined substitutions are:
4643 <variablelist>
4644 <varlistentry id="app-psql-prompting-m-uc">
4645 <term><literal>%M</literal></term>
4646 <listitem>
4647 <para>
4648 The full host name (with domain name) of the database server,
4649 or <literal>[local]</literal> if the connection is over a Unix
4650 domain socket, or
4651 <literal>[local:<replaceable>/dir/name</replaceable>]</literal>,
4652 if the Unix domain socket is not at the compiled in default
4653 location.
4654 </para>
4655 </listitem>
4656 </varlistentry>
4658 <varlistentry id="app-psql-prompting-m-lc">
4659 <term><literal>%m</literal></term>
4660 <listitem>
4661 <para>
4662 The host name of the database server, truncated at the
4663 first dot, or <literal>[local]</literal> if the connection is
4664 over a Unix domain socket.
4665 </para>
4666 </listitem>
4667 </varlistentry>
4669 <varlistentry id="app-psql-prompting-gt">
4670 <term><literal>%&gt;</literal></term>
4671 <listitem><para>The port number at which the database server is listening.</para></listitem>
4672 </varlistentry>
4674 <varlistentry id="app-psql-prompting-n">
4675 <term><literal>%n</literal></term>
4676 <listitem>
4677 <para>
4678 The database session user name. (The expansion of this
4679 value might change during a database session as the result
4680 of the command <command>SET SESSION
4681 AUTHORIZATION</command>.)
4682 </para>
4683 </listitem>
4684 </varlistentry>
4686 <varlistentry id="app-psql-prompting-s">
4687 <term><literal>%s</literal></term>
4688 <listitem><para>The name of the service.</para></listitem>
4689 </varlistentry>
4691 <varlistentry id="app-psql-prompting-slash">
4692 <term><literal>%/</literal></term>
4693 <listitem><para>The name of the current database.</para></listitem>
4694 </varlistentry>
4696 <varlistentry id="app-psql-prompting-tilde">
4697 <term><literal>%~</literal></term>
4698 <listitem><para>Like <literal>%/</literal>, but the output is <literal>~</literal>
4699 (tilde) if the database is your default database.</para></listitem>
4700 </varlistentry>
4702 <varlistentry id="app-psql-prompting-numbersign">
4703 <term><literal>%#</literal></term>
4704 <listitem>
4705 <para>
4706 If the session user is a database superuser, then a
4707 <literal>#</literal>, otherwise a <literal>&gt;</literal>.
4708 (The expansion of this value might change during a database
4709 session as the result of the command <command>SET SESSION
4710 AUTHORIZATION</command>.)
4711 </para>
4712 </listitem>
4713 </varlistentry>
4715 <varlistentry id="app-psql-prompting-p">
4716 <term><literal>%p</literal></term>
4717 <listitem>
4718 <para>The process ID of the backend currently connected to.</para>
4719 </listitem>
4720 </varlistentry>
4722 <varlistentry id="app-psql-prompting-r">
4723 <term><literal>%R</literal></term>
4724 <listitem>
4725 <para>
4726 In prompt 1 normally <literal>=</literal>,
4727 but <literal>@</literal> if the session is in an inactive branch of a
4728 conditional block, or <literal>^</literal> if in single-line mode,
4729 or <literal>!</literal> if the session is disconnected from the
4730 database (which can happen if <command>\connect</command> fails).
4731 In prompt 2 <literal>%R</literal> is replaced by a character that
4732 depends on why <application>psql</application> expects more input:
4733 <literal>-</literal> if the command simply wasn't terminated yet,
4734 but <literal>*</literal> if there is an unfinished
4735 <literal>/* ... */</literal> comment,
4736 a single quote if there is an unfinished quoted string,
4737 a double quote if there is an unfinished quoted identifier,
4738 a dollar sign if there is an unfinished dollar-quoted string,
4739 or <literal>(</literal> if there is an unmatched left parenthesis.
4740 In prompt 3 <literal>%R</literal> doesn't produce anything.
4741 </para>
4742 </listitem>
4743 </varlistentry>
4745 <varlistentry id="app-psql-prompting-x">
4746 <term><literal>%x</literal></term>
4747 <listitem>
4748 <para>
4749 Transaction status: an empty string when not in a transaction
4750 block, or <literal>*</literal> when in a transaction block, or
4751 <literal>!</literal> when in a failed transaction block, or <literal>?</literal>
4752 when the transaction state is indeterminate (for example, because
4753 there is no connection).
4754 </para>
4755 </listitem>
4756 </varlistentry>
4758 <varlistentry id="app-psql-prompting-l">
4759 <term><literal>%l</literal></term>
4760 <listitem>
4761 <para>
4762 The line number inside the current statement, starting from <literal>1</literal>.
4763 </para>
4764 </listitem>
4765 </varlistentry>
4767 <varlistentry id="app-psql-prompting-digits">
4768 <term><literal>%</literal><replaceable class="parameter">digits</replaceable></term>
4769 <listitem>
4770 <para>
4771 The character with the indicated octal code is substituted.
4772 </para>
4773 </listitem>
4774 </varlistentry>
4776 <varlistentry id="app-psql-prompting-name">
4777 <term><literal>%:</literal><replaceable class="parameter">name</replaceable><literal>:</literal></term>
4778 <listitem>
4779 <para>
4780 The value of the <application>psql</application> variable
4781 <replaceable class="parameter">name</replaceable>. See
4782 <xref linkend="app-psql-variables"/>, above, for details.
4783 </para>
4784 </listitem>
4785 </varlistentry>
4787 <varlistentry id="app-psql-prompting-command">
4788 <term><literal>%`</literal><replaceable class="parameter">command</replaceable><literal>`</literal></term>
4789 <listitem>
4790 <para>
4791 The output of <replaceable
4792 class="parameter">command</replaceable>, similar to ordinary
4793 <quote>back-tick</quote> substitution.
4794 </para>
4795 </listitem>
4796 </varlistentry>
4798 <varlistentry id="app-psql-prompting-square-brackets">
4799 <term><literal>%[</literal> ... <literal>%]</literal></term>
4800 <listitem>
4801 <para>
4802 Prompts can contain terminal control characters which, for
4803 example, change the color, background, or style of the prompt
4804 text, or change the title of the terminal window. In order for
4805 the line editing features of <application>Readline</application> to work properly, these
4806 non-printing control characters must be designated as invisible
4807 by surrounding them with <literal>%[</literal> and
4808 <literal>%]</literal>. Multiple pairs of these can occur within
4809 the prompt. For example:
4810 <programlisting>
4811 testdb=&gt; \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '
4812 </programlisting>
4813 results in a boldfaced (<literal>1;</literal>) yellow-on-black
4814 (<literal>33;40</literal>) prompt on VT100-compatible, color-capable
4815 terminals.
4816 </para>
4817 </listitem>
4818 </varlistentry>
4820 <varlistentry id="app-psql-prompting-w">
4821 <term><literal>%w</literal></term>
4822 <listitem>
4823 <para>
4824 Whitespace of the same width as the most recent output of
4825 <varname>PROMPT1</varname>. This can be used as a
4826 <varname>PROMPT2</varname> setting, so that multi-line statements are
4827 aligned with the first line, but there is no visible secondary prompt.
4828 </para>
4829 </listitem>
4830 </varlistentry>
4832 </variablelist>
4834 To insert a percent sign into your prompt, write
4835 <literal>%%</literal>. The default prompts are
4836 <literal>'%/%R%x%# '</literal> for prompts 1 and 2, and
4837 <literal>'&gt;&gt; '</literal> for prompt 3.
4838 </para>
4840 <note>
4841 <para>
4842 This feature was shamelessly plagiarized from
4843 <application>tcsh</application>.
4844 </para>
4845 </note>
4847 </refsect3>
4849 <refsect3 id="app-psql-readline">
4850 <title>Command-Line Editing</title>
4852 <indexterm>
4853 <primary>Readline</primary>
4854 <secondary>in psql</secondary>
4855 </indexterm>
4856 <indexterm>
4857 <primary>libedit</primary>
4858 <secondary>in psql</secondary>
4859 </indexterm>
4861 <para>
4862 <application>psql</application> uses
4863 the <application>Readline</application>
4864 or <application>libedit</application> library, if available, for
4865 convenient line editing and retrieval. The command history is
4866 automatically saved when <application>psql</application> exits and is
4867 reloaded when <application>psql</application> starts up. Type
4868 up-arrow or control-P to retrieve previous lines.
4869 </para>
4871 <para>
4872 You can also use tab completion to fill in partially-typed keywords
4873 and SQL object names in many (by no means all) contexts. For example,
4874 at the start of a command, typing <literal>ins</literal> and pressing
4875 TAB will fill in <literal>insert into </literal>. Then, typing a few
4876 characters of a table or schema name and pressing <literal>TAB</literal>
4877 will fill in the unfinished name, or offer a menu of possible completions
4878 when there's more than one. (Depending on the library in use, you may need to
4879 press <literal>TAB</literal> more than once to get a menu.)
4880 </para>
4882 <para>
4883 Tab completion for SQL object names requires sending queries to the
4884 server to find possible matches. In some contexts this can interfere
4885 with other operations. For example, after <command>BEGIN</command>
4886 it will be too late to issue <command>SET TRANSACTION ISOLATION
4887 LEVEL</command> if a tab-completion query is issued in between.
4888 If you do not want tab completion at all, you
4889 can turn it off permanently by putting this in a file named
4890 <filename>.inputrc</filename> in your home directory:
4891 <programlisting>
4892 $if psql
4893 set disable-completion on
4894 $endif
4895 </programlisting>
4896 (This is not a <application>psql</application> but a
4897 <application>Readline</application> feature. Read its documentation
4898 for further details.)
4899 </para>
4901 <para>
4902 The <option>-n</option> (<option>--no-readline</option>) command line
4903 option can also be useful to disable use
4904 of <application>Readline</application> for a single run
4905 of <application>psql</application>. This prevents tab completion,
4906 use or recording of command line history, and editing of multi-line
4907 commands. It is particularly useful when you need to copy-and-paste
4908 text that contains <literal>TAB</literal> characters.
4909 </para>
4910 </refsect3>
4911 </refsect2>
4912 </refsect1>
4915 <refsect1 id="app-psql-environment" xreflabel="Environment">
4916 <title>Environment</title>
4918 <variablelist>
4920 <varlistentry id="app-psql-environment-columns">
4921 <term><envar>COLUMNS</envar></term>
4923 <listitem>
4924 <para>
4925 If <literal>\pset columns</literal> is zero, controls the
4926 width for the <literal>wrapped</literal> format and width for determining
4927 if wide output requires the pager or should be switched to the
4928 vertical format in expanded auto mode.
4929 </para>
4930 </listitem>
4931 </varlistentry>
4933 <varlistentry id="app-psql-environment-pgdatabase">
4934 <term><envar>PGDATABASE</envar></term>
4935 <term><envar>PGHOST</envar></term>
4936 <term><envar>PGPORT</envar></term>
4937 <term><envar>PGUSER</envar></term>
4939 <listitem>
4940 <para>
4941 Default connection parameters (see <xref linkend="libpq-envars"/>).
4942 </para>
4943 </listitem>
4944 </varlistentry>
4946 <varlistentry id="app-psql-environment-pg-color">
4947 <term><envar>PG_COLOR</envar></term>
4948 <listitem>
4949 <para>
4950 Specifies whether to use color in diagnostic messages. Possible values
4951 are <literal>always</literal>, <literal>auto</literal> and
4952 <literal>never</literal>.
4953 </para>
4954 </listitem>
4955 </varlistentry>
4957 <varlistentry id="app-psql-environment-psql-editor">
4958 <term><envar>PSQL_EDITOR</envar></term>
4959 <term><envar>EDITOR</envar></term>
4960 <term><envar>VISUAL</envar></term>
4962 <listitem>
4963 <para>
4964 Editor used by the <command>\e</command>, <command>\ef</command>,
4965 and <command>\ev</command> commands.
4966 These variables are examined in the order listed;
4967 the first that is set is used.
4968 If none of them is set, the default is to use <filename>vi</filename>
4969 on Unix systems or <filename>notepad.exe</filename> on Windows systems.
4970 </para>
4971 </listitem>
4972 </varlistentry>
4974 <varlistentry id="app-psql-environment-psql-editor-linenumber-arg">
4975 <term><envar>PSQL_EDITOR_LINENUMBER_ARG</envar></term>
4977 <listitem>
4978 <para>
4979 When <command>\e</command>, <command>\ef</command>, or
4980 <command>\ev</command> is used
4981 with a line number argument, this variable specifies the
4982 command-line argument used to pass the starting line number to
4983 the user's editor. For editors such as <productname>Emacs</productname> or
4984 <productname>vi</productname>, this is a plus sign. Include a trailing
4985 space in the value of the variable if there needs to be space
4986 between the option name and the line number. Examples:
4987 <programlisting>
4988 PSQL_EDITOR_LINENUMBER_ARG='+'
4989 PSQL_EDITOR_LINENUMBER_ARG='--line '
4990 </programlisting>
4991 </para>
4993 <para>
4994 The default is <literal>+</literal> on Unix systems
4995 (corresponding to the default editor <filename>vi</filename>,
4996 and useful for many other common editors); but there is no
4997 default on Windows systems.
4998 </para>
4999 </listitem>
5000 </varlistentry>
5002 <varlistentry id="app-psql-environment-psql-history">
5003 <term><envar>PSQL_HISTORY</envar></term>
5005 <listitem>
5006 <para>
5007 Alternative location for the command history file. Tilde (<literal>~</literal>) expansion is performed.
5008 </para>
5009 </listitem>
5010 </varlistentry>
5012 <varlistentry id="app-psql-environment-pager">
5013 <term><envar>PSQL_PAGER</envar></term>
5014 <term><envar>PAGER</envar></term>
5016 <listitem>
5017 <para>
5018 If a query's results do not fit on the screen, they are piped
5019 through this command. Typical values are <literal>more</literal>
5020 or <literal>less</literal>.
5021 Use of the pager can be disabled by setting <envar>PSQL_PAGER</envar>
5022 or <envar>PAGER</envar> to an empty string, or by adjusting the
5023 pager-related options of the <command>\pset</command> command.
5024 These variables are examined in the order listed;
5025 the first that is set is used.
5026 If neither of them is set, the default is to use <literal>more</literal> on most
5027 platforms, but <literal>less</literal> on Cygwin.
5028 </para>
5030 </listitem>
5031 </varlistentry>
5033 <varlistentry id="app-psql-environment-psql-watch-pager">
5034 <term><envar>PSQL_WATCH_PAGER</envar></term>
5036 <listitem>
5037 <para>
5038 When a query is executed repeatedly with the <command>\watch</command>
5039 command, a pager is not used by default. This behavior can be changed
5040 by setting <envar>PSQL_WATCH_PAGER</envar> to a pager command, on Unix
5041 systems. The <literal>pspg</literal> pager (not part of
5042 <productname>PostgreSQL</productname> but available in many open source
5043 software distributions) can display the output of
5044 <command>\watch</command> if started with the option
5045 <literal>--stream</literal>.
5046 </para>
5048 </listitem>
5049 </varlistentry>
5051 <varlistentry id="app-psql-environment-psqlrc">
5052 <term><envar>PSQLRC</envar></term>
5054 <listitem>
5055 <para>
5056 Alternative location of the user's <filename>.psqlrc</filename> file. Tilde (<literal>~</literal>) expansion is performed.
5057 </para>
5058 </listitem>
5059 </varlistentry>
5061 <varlistentry id="app-psql-environment-shell">
5062 <term><envar>SHELL</envar></term>
5064 <listitem>
5065 <para>
5066 Command executed by the <command>\!</command> command.
5067 </para>
5068 </listitem>
5069 </varlistentry>
5071 <varlistentry id="app-psql-environment-tmpdir">
5072 <term><envar>TMPDIR</envar></term>
5074 <listitem>
5075 <para>
5076 Directory for storing temporary files. The default is
5077 <filename>/tmp</filename>.
5078 </para>
5079 </listitem>
5080 </varlistentry>
5081 </variablelist>
5083 <para>
5084 This utility, like most other <productname>PostgreSQL</productname> utilities,
5085 also uses the environment variables supported by <application>libpq</application>
5086 (see <xref linkend="libpq-envars"/>).
5087 </para>
5089 </refsect1>
5092 <refsect1>
5093 <title>Files</title>
5095 <variablelist>
5096 <varlistentry id="app-psql-files-psqlrc">
5097 <term><filename>psqlrc</filename> and <filename>~/.psqlrc</filename></term>
5098 <listitem>
5099 <para>
5100 Unless it is passed an <option>-X</option> option,
5101 <application>psql</application> attempts to read and execute commands
5102 from the system-wide startup file (<filename>psqlrc</filename>) and then
5103 the user's personal startup file (<filename>~/.psqlrc</filename>), after
5104 connecting to the database but before accepting normal commands.
5105 These files can be used to set up the client and/or the server to taste,
5106 typically with <command>\set</command> and <command>SET</command>
5107 commands.
5108 </para>
5109 <para>
5110 The system-wide startup file is named <filename>psqlrc</filename>.
5111 By default it is
5112 sought in the installation's <quote>system configuration</quote> directory,
5113 which is most reliably identified by running <literal>pg_config
5114 --sysconfdir</literal>.
5115 Typically this directory will be <filename>../etc/</filename>
5116 relative to the directory containing
5117 the <productname>PostgreSQL</productname> executables.
5118 The directory to look in can be set explicitly via
5119 the <envar>PGSYSCONFDIR</envar> environment variable.
5120 </para>
5121 <para>
5122 The user's personal startup file is named <filename>.psqlrc</filename>
5123 and is sought in the invoking user's home directory.
5124 On Windows the personal startup file is instead named
5125 <filename>%APPDATA%\postgresql\psqlrc.conf</filename>.
5126 In either case, this default file path can be overridden by setting
5127 the <envar>PSQLRC</envar> environment variable.
5128 </para>
5129 <para>
5130 Both the system-wide startup file and the user's personal startup file
5131 can be made <application>psql</application>-version-specific
5132 by appending a dash and the <productname>PostgreSQL</productname>
5133 major or minor release identifier to the file name,
5134 for example <filename>~/.psqlrc-&majorversion;</filename> or
5135 <filename>~/.psqlrc-&version;</filename>.
5136 The most specific version-matching file will be read in preference
5137 to a non-version-specific file.
5138 These version suffixes are added after determining the file path
5139 as explained above.
5140 </para>
5141 </listitem>
5142 </varlistentry>
5144 <varlistentry id="app-psql-files-psql-history">
5145 <term><filename>.psql_history</filename></term>
5146 <listitem>
5147 <para>
5148 The command-line history is stored in the file
5149 <filename>~/.psql_history</filename>, or
5150 <filename>%APPDATA%\postgresql\psql_history</filename> on Windows.
5151 </para>
5152 <para>
5153 The location of the history file can be set explicitly via
5154 the <varname>HISTFILE</varname> <application>psql</application> variable or
5155 the <envar>PSQL_HISTORY</envar> environment variable.
5156 </para>
5157 </listitem>
5158 </varlistentry>
5159 </variablelist>
5160 </refsect1>
5163 <refsect1>
5164 <title>Notes</title>
5166 <itemizedlist>
5167 <listitem>
5168 <para><application>psql</application> works best with servers of the same
5169 or an older major version. Backslash commands are particularly likely
5170 to fail if the server is of a newer version than <application>psql</application>
5171 itself. However, backslash commands of the <literal>\d</literal> family should
5172 work with servers of versions back to 9.2, though not necessarily with
5173 servers newer than <application>psql</application> itself. The general
5174 functionality of running SQL commands and displaying query results
5175 should also work with servers of a newer major version, but this cannot
5176 be guaranteed in all cases.
5177 </para>
5178 <para>
5179 If you want to use <application>psql</application> to connect to several
5180 servers of different major versions, it is recommended that you use the
5181 newest version of <application>psql</application>. Alternatively, you
5182 can keep around a copy of <application>psql</application> from each
5183 major version and be sure to use the version that matches the
5184 respective server. But in practice, this additional complication should
5185 not be necessary.
5186 </para>
5187 </listitem>
5189 <listitem>
5190 <para>
5191 Before <productname>PostgreSQL</productname> 9.6,
5192 the <option>-c</option> option implied <option>-X</option>
5193 (<option>--no-psqlrc</option>); this is no longer the case.
5194 </para>
5195 </listitem>
5197 <listitem>
5198 <para>
5199 Before <productname>PostgreSQL</productname> 8.4,
5200 <application>psql</application> allowed the
5201 first argument of a single-letter backslash command to start
5202 directly after the command, without intervening whitespace.
5203 Now, some whitespace is required.
5204 </para>
5205 </listitem>
5206 </itemizedlist>
5207 </refsect1>
5210 <refsect1>
5211 <title>Notes for Windows Users</title>
5213 <para>
5214 <application>psql</application> is built as a <quote>console
5215 application</quote>. Since the Windows console windows use a different
5216 encoding than the rest of the system, you must take special care
5217 when using 8-bit characters within <application>psql</application>.
5218 If <application>psql</application> detects a problematic
5219 console code page, it will warn you at startup. To change the
5220 console code page, two things are necessary:
5222 <itemizedlist>
5223 <listitem>
5224 <para>
5225 Set the code page by entering <userinput>cmd.exe /c chcp
5226 1252</userinput>. (1252 is a code page that is appropriate for
5227 German; replace it with your value.) If you are using Cygwin,
5228 you can put this command in <filename>/etc/profile</filename>.
5229 </para>
5230 </listitem>
5232 <listitem>
5233 <para>
5234 Set the console font to <literal>Lucida Console</literal>, because the
5235 raster font does not work with the ANSI code page.
5236 </para>
5237 </listitem>
5238 </itemizedlist></para>
5240 </refsect1>
5243 <refsect1 id="app-psql-examples" xreflabel="Examples">
5244 <title>Examples</title>
5246 <para>
5247 The first example shows how to spread a command over several lines of
5248 input. Notice the changing prompt:
5249 <programlisting>
5250 testdb=&gt; <userinput>CREATE TABLE my_table (</userinput>
5251 testdb(&gt; <userinput> first integer not null default 0,</userinput>
5252 testdb(&gt; <userinput> second text)</userinput>
5253 testdb-&gt; <userinput>;</userinput>
5254 CREATE TABLE
5255 </programlisting>
5256 Now look at the table definition again:
5257 <programlisting>
5258 testdb=&gt; <userinput>\d my_table</userinput>
5259 Table "public.my_table"
5260 Column | Type | Collation | Nullable | Default
5261 --------+---------+-----------+----------+---------
5262 first | integer | | not null | 0
5263 second | text | | |
5264 </programlisting>
5265 Now we change the prompt to something more interesting:
5266 <programlisting>
5267 testdb=&gt; <userinput>\set PROMPT1 '%n@%m %~%R%# '</userinput>
5268 peter@localhost testdb=&gt;
5269 </programlisting>
5270 Let's assume you have filled the table with data and want to take a
5271 look at it:
5272 <programlisting>
5273 peter@localhost testdb=&gt; SELECT * FROM my_table;
5274 first | second
5275 -------+--------
5276 1 | one
5277 2 | two
5278 3 | three
5279 4 | four
5280 (4 rows)
5281 </programlisting>
5282 You can display tables in different ways by using the
5283 <command>\pset</command> command:
5284 <programlisting>
5285 peter@localhost testdb=&gt; <userinput>\pset border 2</userinput>
5286 Border style is 2.
5287 peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table;</userinput>
5288 +-------+--------+
5289 | first | second |
5290 +-------+--------+
5291 | 1 | one |
5292 | 2 | two |
5293 | 3 | three |
5294 | 4 | four |
5295 +-------+--------+
5296 (4 rows)
5298 peter@localhost testdb=&gt; <userinput>\pset border 0</userinput>
5299 Border style is 0.
5300 peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table;</userinput>
5301 first second
5302 ----- ------
5303 1 one
5304 2 two
5305 3 three
5306 4 four
5307 (4 rows)
5309 peter@localhost testdb=&gt; <userinput>\pset border 1</userinput>
5310 Border style is 1.
5311 peter@localhost testdb=&gt; <userinput>\pset format csv</userinput>
5312 Output format is csv.
5313 peter@localhost testdb=&gt; <userinput>\pset tuples_only</userinput>
5314 Tuples only is on.
5315 peter@localhost testdb=&gt; <userinput>SELECT second, first FROM my_table;</userinput>
5316 one,1
5317 two,2
5318 three,3
5319 four,4
5320 peter@localhost testdb=&gt; <userinput>\pset format unaligned</userinput>
5321 Output format is unaligned.
5322 peter@localhost testdb=&gt; <userinput>\pset fieldsep '\t'</userinput>
5323 Field separator is " ".
5324 peter@localhost testdb=&gt; <userinput>SELECT second, first FROM my_table;</userinput>
5325 one 1
5326 two 2
5327 three 3
5328 four 4
5329 </programlisting>
5330 Alternatively, use the short commands:
5331 <programlisting>
5332 peter@localhost testdb=&gt; <userinput>\a \t \x</userinput>
5333 Output format is aligned.
5334 Tuples only is off.
5335 Expanded display is on.
5336 peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table;</userinput>
5337 -[ RECORD 1 ]-
5338 first | 1
5339 second | one
5340 -[ RECORD 2 ]-
5341 first | 2
5342 second | two
5343 -[ RECORD 3 ]-
5344 first | 3
5345 second | three
5346 -[ RECORD 4 ]-
5347 first | 4
5348 second | four
5349 </programlisting>
5350 </para>
5352 <para>
5353 Also, these output format options can be set for just one query by using
5354 <literal>\g</literal>:
5355 <programlisting>
5356 peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table</userinput>
5357 peter@localhost testdb-&gt; <userinput>\g (format=aligned tuples_only=off expanded=on)</userinput>
5358 -[ RECORD 1 ]-
5359 first | 1
5360 second | one
5361 -[ RECORD 2 ]-
5362 first | 2
5363 second | two
5364 -[ RECORD 3 ]-
5365 first | 3
5366 second | three
5367 -[ RECORD 4 ]-
5368 first | 4
5369 second | four
5370 </programlisting>
5371 </para>
5373 <para>
5374 Here is an example of using the <command>\df</command> command to
5375 find only functions with names matching <literal>int*pl</literal>
5376 and whose second argument is of type <type>bigint</type>:
5377 <programlisting>
5378 testdb=&gt; <userinput>\df int*pl * bigint</userinput>
5379 List of functions
5380 Schema | Name | Result data type | Argument data types | Type
5381 ------------+---------+------------------+---------------------+------
5382 pg_catalog | int28pl | bigint | smallint, bigint | func
5383 pg_catalog | int48pl | bigint | integer, bigint | func
5384 pg_catalog | int8pl | bigint | bigint, bigint | func
5385 (3 rows)
5386 </programlisting>
5387 </para>
5389 <para>
5390 When suitable, query results can be shown in a crosstab representation
5391 with the <command>\crosstabview</command> command:
5392 <programlisting>
5393 testdb=&gt; <userinput>SELECT first, second, first &gt; 2 AS gt2 FROM my_table;</userinput>
5394 first | second | gt2
5395 -------+--------+-----
5396 1 | one | f
5397 2 | two | f
5398 3 | three | t
5399 4 | four | t
5400 (4 rows)
5402 testdb=&gt; <userinput>\crosstabview first second</userinput>
5403 first | one | two | three | four
5404 -------+-----+-----+-------+------
5405 1 | f | | |
5406 2 | | f | |
5407 3 | | | t |
5408 4 | | | | t
5409 (4 rows)
5410 </programlisting>
5412 This second example shows a multiplication table with rows sorted in reverse
5413 numerical order and columns with an independent, ascending numerical order.
5414 <programlisting>
5415 testdb=&gt; <userinput>SELECT t1.first as "A", t2.first+100 AS "B", t1.first*(t2.first+100) as "AxB",</userinput>
5416 testdb-&gt; <userinput>row_number() over(order by t2.first) AS ord</userinput>
5417 testdb-&gt; <userinput>FROM my_table t1 CROSS JOIN my_table t2 ORDER BY 1 DESC</userinput>
5418 testdb-&gt; <userinput>\crosstabview "A" "B" "AxB" ord</userinput>
5419 A | 101 | 102 | 103 | 104
5420 ---+-----+-----+-----+-----
5421 4 | 404 | 408 | 412 | 416
5422 3 | 303 | 306 | 309 | 312
5423 2 | 202 | 204 | 206 | 208
5424 1 | 101 | 102 | 103 | 104
5425 (4 rows)
5426 </programlisting></para>
5428 </refsect1>
5430 </refentry>