Debian Codegraph @

doc/src/sgml/ref/psql-ref.sgml

raw
1 <!--
2 doc/src/sgml/ref/psql-ref.sgml
3 PostgreSQL documentation
4 -->
5
6 <refentry id="app-psql">
7 <indexterm zone="app-psql">
8 <primary>psql</primary>
9 </indexterm>
10
11 <refmeta>
12 <refentrytitle><application>psql</application></refentrytitle>
13 <manvolnum>1</manvolnum>
14 <refmiscinfo>Application</refmiscinfo>
15 </refmeta>
16
17 <refnamediv>
18 <refname><application>psql</application></refname>
19 <refpurpose>
20 <productname>PostgreSQL</productname> interactive terminal
21 </refpurpose>
22 </refnamediv>
23
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>
32
33 <refsect1>
34 <title>Description</title>
35
36 <para>
37 <application>psql</application> is a terminal-based front-end to
38 <productname>PostgreSQL</productname>. It enables you to type in
39 queries interactively, issue them to
40 <productname>PostgreSQL</productname>, and see the query results.
41 Alternatively, input can be from a file 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>
47
48 <refsect1 id="r1-app-psql-3">
49 <title>Options</title>
50
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>
64
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>
76
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>
88
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>
115 or
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
139 \x
140 SELECT * FROM foo;
141 EOF
142 </programlisting></para>
143 </listitem>
144 </varlistentry>
145
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>
155
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>
171
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>
184
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>
197
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>
215
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>
224
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>
239
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>
251
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>
264
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>
276
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>
286
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>
296
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>
308
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>
320
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>
332
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>
346
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>
360
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>
375
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>
387
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>
399
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>
408
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>
419
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>
431
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>
443
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>
455
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>
472
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>
482
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>
494
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>
502
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>
512
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>
522
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>
530
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>
541
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>
553
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>
564
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>
576
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>
593
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>
604
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>
621
622 </variablelist>
623 </refsect1>
624
625
626 <refsect1>
627 <title>Exit Status</title>
628
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>
637
638
639 <refsect1>
640 <title>Usage</title>
641
642 <refsect2 id="r2-app-psql-connecting">
643 <title>Connecting to a Database</title>
644
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>
669
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>
680
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>
696
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>
702
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>
714
715 <refsect2 id="r2-app-psql-4">
716 <title>Entering SQL Commands</title>
717
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.
727
728 testdb=&gt;
729 </programlisting>
730 </para>
731
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>
740
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>
752
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>
759
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>
766
767 <refsect2 id="app-psql-meta-commands">
768 <title>Meta-Commands</title>
769
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>
778
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>
785
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>
800
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>
814
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>
833
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>
846
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>
858
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>
866
867 <para>
868 Many of the meta-commands also allow <literal>x</literal> to be appended
869 as an option. This will cause the results to be displayed in expanded
870 mode, as if <command>\x</command> or <command>\pset expanded</command>
871 had been used.
872 </para>
873
874 <para>
875 The following meta-commands are defined:
876
877 <variablelist>
878 <varlistentry id="app-psql-meta-command-a">
879 <term><literal>\a</literal></term>
880 <listitem>
881 <para>
882 If the current table output format is unaligned, it is switched to aligned.
883 If it is not unaligned, it is set to unaligned. This command is
884 kept for backwards compatibility. See <command>\pset</command> for a
885 more general solution.
886 </para>
887 </listitem>
888 </varlistentry>
889
890 <varlistentry id="app-psql-meta-command-bind">
891 <term><literal>\bind</literal> [ <replaceable class="parameter">parameter</replaceable> ] ... </term>
892
893 <listitem>
894 <para>
895 Sets query parameters for the next query execution, with the
896 specified parameters passed for any parameter placeholders
897 (<literal>$1</literal> etc.).
898 </para>
899
900 <para>
901 Example:
902 <programlisting>
903 INSERT INTO tbl1 VALUES ($1, $2) \bind 'first value' 'second value' \g
904 </programlisting>
905 </para>
906
907 <para>
908 This also works for query-execution commands besides
909 <literal>\g</literal>, such as <literal>\gx</literal> and
910 <literal>\gset</literal>.
911 </para>
912
913 <para>
914 This command causes the extended query protocol (see <xref
915 linkend="protocol-query-concepts"/>) to be used, unlike normal
916 <application>psql</application> operation, which uses the simple
917 query protocol. So this command can be useful to test the extended
918 query protocol from <application>psql</application>. (The extended
919 query protocol is used even if the query has no parameters and this
920 command specifies zero parameters.) This command affects only the
921 next query executed; all subsequent queries will use the simple query
922 protocol by default.
923 </para>
924 </listitem>
925 </varlistentry>
926
927 <varlistentry id="app-psql-meta-command-bind-named">
928 <term><literal>\bind_named</literal> <replaceable class="parameter">statement_name</replaceable> [ <replaceable class="parameter">parameter</replaceable> ] ... </term>
929
930 <listitem>
931 <para>
932 <literal>\bind_named</literal> is equivalent to <literal>\bind</literal>,
933 except that it takes the name of an existing prepared statement as
934 first parameter. An empty string denotes the unnamed prepared
935 statement.
936 </para>
937
938 <para>
939 Example:
940 <programlisting>
941 INSERT INTO tbls1 VALUES ($1, $2) \parse stmt1
942 \bind_named stmt1 'first value' 'second value' \g
943 </programlisting>
944 </para>
945
946 <para>
947 This command causes the extended query protocol (see
948 <xref linkend="protocol-query-concepts"/>) to be used, unlike normal
949 <application>psql</application> operation, which uses the simple
950 query protocol. So this command can be useful to test the extended
951 query protocol from <application>psql</application>.
952 </para>
953
954 </listitem>
955 </varlistentry>
956
957 <varlistentry id="app-psql-meta-command-c-lc">
958 <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>
959 <listitem>
960 <para>
961 Establishes a new connection to a <productname>PostgreSQL</productname>
962 server. The connection parameters to use can be specified either
963 using a positional syntax (one or more of database name, user,
964 host, and port), or using a <replaceable>conninfo</replaceable>
965 connection string as detailed in
966 <xref linkend="libpq-connstring"/>. If no arguments are given, a
967 new connection is made using the same parameters as before.
968 </para>
969
970 <para>
971 Specifying any
972 of <replaceable class="parameter">dbname</replaceable>,
973 <replaceable class="parameter">username</replaceable>,
974 <replaceable class="parameter">host</replaceable> or
975 <replaceable class="parameter">port</replaceable>
976 as <literal>-</literal> is equivalent to omitting that parameter.
977 </para>
978
979 <para>
980 The new connection can re-use connection parameters from the previous
981 connection; not only database name, user, host, and port, but other
982 settings such as <replaceable>sslmode</replaceable>. By default,
983 parameters are re-used in the positional syntax, but not when
984 a <replaceable>conninfo</replaceable> string is given. Passing a
985 first argument of <literal>-reuse-previous=on</literal>
986 or <literal>-reuse-previous=off</literal> overrides that default. If
987 parameters are re-used, then any parameter not explicitly specified as
988 a positional parameter or in the <replaceable>conninfo</replaceable>
989 string is taken from the existing connection's parameters. An
990 exception is that if the <replaceable>host</replaceable> setting
991 is changed from its previous value using the positional syntax,
992 any <replaceable>hostaddr</replaceable> setting present in the
993 existing connection's parameters is dropped.
994 Also, any password used for the existing connection will be re-used
995 only if the user, host, and port settings are not changed.
996 When the command neither specifies nor reuses a particular parameter,
997 the <application>libpq</application> default is used.
998 </para>
999
1000 <para>
1001 If the new connection is successfully made, the previous
1002 connection is closed.
1003 If the connection attempt fails (wrong user name, access
1004 denied, etc.), the previous connection will be kept if
1005 <application>psql</application> is in interactive mode. But when
1006 executing a non-interactive script, the old connection is closed
1007 and an error is reported. That may or may not terminate the
1008 script; if it does not, all database-accessing commands will fail
1009 until another <literal>\connect</literal> command is successfully
1010 executed. This distinction was chosen as
1011 a user convenience against typos on the one hand, and a safety
1012 mechanism that scripts are not accidentally acting on the
1013 wrong database on the other hand.
1014 Note that whenever a <literal>\connect</literal> command attempts
1015 to re-use parameters, the values re-used are those of the last
1016 successful connection, not of any failed attempts made subsequently.
1017 However, in the case of a
1018 non-interactive <literal>\connect</literal> failure, no parameters
1019 are allowed to be re-used later, since the script would likely be
1020 expecting the values from the failed <literal>\connect</literal>
1021 to be re-used.
1022 </para>
1023
1024 <para>
1025 Examples:
1026 </para>
1027 <programlisting>
1028 =&gt; \c mydb myuser host.dom 6432
1029 =&gt; \c service=foo
1030 =&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
1031 =&gt; \c -reuse-previous=on sslmode=require -- changes only sslmode
1032 =&gt; \c postgresql://tom@localhost/mydb?application_name=myapp
1033 </programlisting>
1034 </listitem>
1035 </varlistentry>
1036
1037 <varlistentry id="app-psql-meta-command-c-uc">
1038 <term><literal>\C [ <replaceable class="parameter">title</replaceable> ]</literal></term>
1039 <listitem>
1040 <para>
1041 Sets the title of any tables being printed as the result of a
1042 query or unset any such title. This command is equivalent to
1043 <literal>\pset title <replaceable
1044 class="parameter">title</replaceable></literal>. (The name of
1045 this command derives from <quote>caption</quote>, as it was
1046 previously only used to set the caption in an
1047 <acronym>HTML</acronym> table.)
1048 </para>
1049 </listitem>
1050 </varlistentry>
1051
1052 <varlistentry id="app-psql-meta-command-cd">
1053 <term><literal>\cd [ <replaceable>directory</replaceable> ]</literal></term>
1054 <listitem>
1055 <para>
1056 Changes the current working directory to
1057 <replaceable>directory</replaceable>. Without argument, changes
1058 to the current user's home directory. For details on how home
1059 directories are found, see <xref linkend="libpq-pgpass"/>.
1060 </para>
1061
1062 <tip>
1063 <para>
1064 To print your current working directory, use <literal>\! pwd</literal>.
1065 </para>
1066 </tip>
1067 </listitem>
1068 </varlistentry>
1069
1070 <varlistentry id="app-psql-meta-command-close-prepared">
1071 <term><literal>\close_prepared</literal> <replaceable class="parameter">prepared_statement_name</replaceable></term>
1072
1073 <listitem>
1074 <para>
1075 Closes the specified prepared statement. An empty string denotes the
1076 unnamed prepared statement. If no prepared statement exists with this
1077 name, the operation is a no-op.
1078 </para>
1079
1080 <para>
1081 Example:
1082 <programlisting>
1083 SELECT $1 \parse stmt1
1084 \close_prepared stmt1
1085 </programlisting>
1086 </para>
1087
1088 <para>
1089 This command causes the extended query protocol to be used,
1090 unlike normal <application>psql</application> operation, which
1091 uses the simple query protocol. So this command can be useful
1092 to test the extended query protocol from
1093 <application>psql</application>.
1094 </para>
1095
1096 </listitem>
1097 </varlistentry>
1098
1099 <varlistentry id="app-psql-meta-command-conninfo">
1100 <term><literal>\conninfo</literal></term>
1101 <listitem>
1102 <para>
1103 Outputs information about the current database connection,
1104 including SSL-related information if SSL is in use.
1105 </para>
1106 <para>
1107 Note that the <structfield>Client User</structfield> field shows
1108 the user at the time of connection, while the
1109 <structfield>Superuser</structfield> field indicates whether
1110 the current user (in the current execution context) has
1111 superuser privileges. These users are usually the same, but they can
1112 differ, for example, if the current user was changed with the
1113 <command>SET ROLE</command> command.
1114 </para>
1115 </listitem>
1116 </varlistentry>
1117
1118 <varlistentry id="app-psql-meta-commands-copy">
1119 <term><literal>\copy { <replaceable class="parameter">table</replaceable> [ ( <replaceable class="parameter">column_list</replaceable> ) ] }
1120 <literal>from</literal>
1121 { <replaceable class="parameter">'filename'</replaceable> | program <replaceable class="parameter">'command'</replaceable> | stdin | pstdin }
1122 [ [ with ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
1123 [ where <replaceable class="parameter">condition</replaceable> ]</literal></term>
1124
1125 <term><literal>\copy { <replaceable class="parameter">table</replaceable> [ ( <replaceable class="parameter">column_list</replaceable> ) ] | ( <replaceable class="parameter">query</replaceable> ) }
1126 <literal>to</literal>
1127 { <replaceable class="parameter">'filename'</replaceable> | program <replaceable class="parameter">'command'</replaceable> | stdout | pstdout }
1128 [ [ with ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]</literal></term>
1129
1130 <listitem>
1131 <para>
1132 Performs a frontend (client) copy. This is an operation that
1133 runs an <acronym>SQL</acronym> <link linkend="sql-copy"><command>COPY</command></link>
1134 command, but instead of the server
1135 reading or writing the specified file,
1136 <application>psql</application> reads or writes the file and
1137 routes the data between the server and the local file system.
1138 This means that file accessibility and privileges are those of
1139 the local user, not the server, and no SQL superuser
1140 privileges are required.
1141 </para>
1142
1143 <para>
1144 When <literal>program</literal> is specified,
1145 <replaceable class="parameter">command</replaceable> is
1146 executed by <application>psql</application> and the data passed from
1147 or to <replaceable class="parameter">command</replaceable> is
1148 routed between the server and the client.
1149 Again, the execution privileges are those of
1150 the local user, not the server, and no SQL superuser
1151 privileges are required.
1152 </para>
1153
1154 <para>
1155 For <literal>\copy ... from stdin</literal>, data rows are read from the same
1156 source that issued the command, continuing until a line containing
1157 only <literal>\.</literal>
1158 is read or the stream reaches <acronym>EOF</acronym>. This option is useful
1159 for populating tables in-line within an SQL script file.
1160 For <literal>\copy ... to stdout</literal>, output is sent to the same place
1161 as <application>psql</application> command output, and
1162 the <literal>COPY <replaceable>count</replaceable></literal> command status is
1163 not printed (since it might be confused with a data row).
1164 To read/write <application>psql</application>'s standard input or
1165 output regardless of the current command source or <literal>\o</literal>
1166 option, write <literal>from pstdin</literal> or <literal>to pstdout</literal>.
1167 </para>
1168
1169 <para>
1170 The syntax of this command is similar to that of the
1171 <acronym>SQL</acronym> <link linkend="sql-copy"><command>COPY</command></link>
1172 command. All options other than the data source/destination are
1173 as specified for <command>COPY</command>.
1174 Because of this, special parsing rules apply to the <command>\copy</command>
1175 meta-command. Unlike most other meta-commands, the entire remainder
1176 of the line is always taken to be the arguments of <command>\copy</command>,
1177 and neither variable interpolation nor backquote expansion are
1178 performed in the arguments.
1179 </para>
1180
1181 <tip>
1182 <para>
1183 Another way to obtain the same result as <literal>\copy
1184 ... to</literal> is to use the <acronym>SQL</acronym> <literal>COPY
1185 ... TO STDOUT</literal> command and terminate it
1186 with <literal>\g <replaceable>filename</replaceable></literal>
1187 or <literal>\g |<replaceable>program</replaceable></literal>.
1188 Unlike <literal>\copy</literal>, this method allows the command to
1189 span multiple lines; also, variable interpolation and backquote
1190 expansion can be used.
1191 </para>
1192 </tip>
1193
1194 <tip>
1195 <para>
1196 These operations are not as efficient as the <acronym>SQL</acronym>
1197 <command>COPY</command> command with a file or program data source or
1198 destination, because all data must pass through the client/server
1199 connection. For large amounts of data the <acronym>SQL</acronym>
1200 command might be preferable.
1201 </para>
1202 </tip>
1203
1204 </listitem>
1205 </varlistentry>
1206
1207 <varlistentry id="app-psql-meta-command-copyright">
1208 <term><literal>\copyright</literal></term>
1209 <listitem>
1210 <para>
1211 Shows the copyright and distribution terms of
1212 <productname>PostgreSQL</productname>.
1213 </para>
1214 </listitem>
1215 </varlistentry>
1216
1217
1218 <varlistentry id="app-psql-meta-commands-crosstabview">
1219 <term><literal>\crosstabview [
1220 <replaceable class="parameter">colV</replaceable>
1221 [ <replaceable class="parameter">colH</replaceable>
1222 [ <replaceable class="parameter">colD</replaceable>
1223 [ <replaceable class="parameter">sortcolH</replaceable>
1224 ] ] ] ] </literal></term>
1225 <listitem>
1226 <para>
1227 Executes the current query buffer (like <literal>\g</literal>) and
1228 shows the results in a crosstab grid.
1229 The query must return at least three columns.
1230 The output column identified by <replaceable class="parameter">colV</replaceable>
1231 becomes a vertical header and the output column identified by
1232 <replaceable class="parameter">colH</replaceable>
1233 becomes a horizontal header.
1234 <replaceable class="parameter">colD</replaceable> identifies
1235 the output column to display within the grid.
1236 <replaceable class="parameter">sortcolH</replaceable> identifies
1237 an optional sort column for the horizontal header.
1238 </para>
1239
1240 <para>
1241 Each column specification can be a column number (starting at 1) or
1242 a column name. The usual SQL case folding and quoting rules apply to
1243 column names. If omitted,
1244 <replaceable class="parameter">colV</replaceable> is taken as column 1
1245 and <replaceable class="parameter">colH</replaceable> as column 2.
1246 <replaceable class="parameter">colH</replaceable> must differ from
1247 <replaceable class="parameter">colV</replaceable>.
1248 If <replaceable class="parameter">colD</replaceable> is not
1249 specified, then there must be exactly three columns in the query
1250 result, and the column that is neither
1251 <replaceable class="parameter">colV</replaceable> nor
1252 <replaceable class="parameter">colH</replaceable>
1253 is taken to be <replaceable class="parameter">colD</replaceable>.
1254 </para>
1255
1256 <para>
1257 The vertical header, displayed as the leftmost column, contains the
1258 values found in column <replaceable class="parameter">colV</replaceable>, in the
1259 same order as in the query results, but with duplicates removed.
1260 </para>
1261
1262 <para>
1263 The horizontal header, displayed as the first row, contains the values
1264 found in column <replaceable class="parameter">colH</replaceable>,
1265 with duplicates removed. By default, these appear in the same order
1266 as in the query results. But if the
1267 optional <replaceable class="parameter">sortcolH</replaceable> argument is given,
1268 it identifies a column whose values must be integer numbers, and the
1269 values from <replaceable class="parameter">colH</replaceable> will
1270 appear in the horizontal header sorted according to the
1271 corresponding <replaceable class="parameter">sortcolH</replaceable> values.
1272 </para>
1273
1274 <para>
1275 Inside the crosstab grid, for each distinct value <literal>x</literal>
1276 of <replaceable class="parameter">colH</replaceable> and each distinct
1277 value <literal>y</literal>
1278 of <replaceable class="parameter">colV</replaceable>, the cell located
1279 at the intersection <literal>(x,y)</literal> contains the value of
1280 the <literal>colD</literal> column in the query result row for which
1281 the value of <replaceable class="parameter">colH</replaceable>
1282 is <literal>x</literal> and the value
1283 of <replaceable class="parameter">colV</replaceable>
1284 is <literal>y</literal>. If there is no such row, the cell is empty. If
1285 there are multiple such rows, an error is reported.
1286 </para>
1287 </listitem>
1288 </varlistentry>
1289
1290
1291 <varlistentry id="app-psql-meta-command-d">
1292 <term><literal>\d[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1293
1294 <listitem>
1295 <para>
1296 For each relation (table, view, materialized view, index, sequence,
1297 or foreign table)
1298 or composite type matching the
1299 <replaceable class="parameter">pattern</replaceable>, show all
1300 columns, their types, the tablespace (if not the default) and any
1301 special attributes such as <literal>NOT NULL</literal> or defaults.
1302 Associated indexes, constraints, rules, and triggers are
1303 also shown. For foreign tables, the associated foreign
1304 server is shown as well.
1305 (<quote>Matching the pattern</quote> is defined in
1306 <xref linkend="app-psql-patterns"/> below.)
1307 </para>
1308
1309 <para>
1310 For some types of relation, <literal>\d</literal> shows additional information
1311 for each column: column values for sequences, indexed expressions for
1312 indexes, and foreign data wrapper options for foreign tables.
1313 </para>
1314
1315 <para>
1316 The command form <literal>\d+</literal> is identical, except that
1317 more information is displayed: any comments associated with the
1318 columns of the table are shown, as is the presence of OIDs in the
1319 table, the view definition if the relation is a view, a non-default
1320 <link linkend="sql-altertable-replica-identity">replica
1321 identity</link> setting and the
1322 <link linkend="sql-create-access-method">access method</link> name
1323 if the relation has an access method.
1324 </para>
1325
1326 <para>
1327 By default, only user-created objects are shown; supply a
1328 pattern or the <literal>S</literal> modifier to include system
1329 objects.
1330 </para>
1331
1332 <note>
1333 <para>
1334 If <command>\d</command> is used without a
1335 <replaceable class="parameter">pattern</replaceable> argument, it is
1336 equivalent to <command>\dtvmsE</command> which will show a list of
1337 all visible tables, views, materialized views, sequences and
1338 foreign tables.
1339 This is purely a convenience measure.
1340 </para>
1341 <para>
1342 As with many other commands, if <literal>x</literal> is appended to
1343 the command name, the results are displayed in expanded mode, but note
1344 that this only applies when <command>\d</command> is used without a
1345 <replaceable class="parameter">pattern</replaceable> argument, and
1346 the <literal>x</literal> modifier cannot appear immediately after the
1347 <command>\d</command> (because <command>\dx</command> is a different
1348 command); the <literal>x</literal> modifier may only appear after an
1349 <literal>S</literal> or <literal>+</literal> modifier. For example,
1350 <command>\d+x</command> is equivalent to <command>\dtvmsE+x</command>
1351 and will show a list of all relations in expanded mode.
1352 </para>
1353 </note>
1354 </listitem>
1355 </varlistentry>
1356
1357 <varlistentry id="app-psql-meta-command-da-lc">
1358 <term><literal>\da[Sx] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1359
1360 <listitem>
1361 <para>
1362 Lists aggregate functions, together with their
1363 return type and the data types they operate on. If <replaceable
1364 class="parameter">pattern</replaceable>
1365 is specified, only aggregates whose names match the pattern are shown.
1366 By default, only user-created objects are shown; supply a
1367 pattern or the <literal>S</literal> modifier to include system
1368 objects.
1369 If <literal>x</literal> is appended to the command name, the results
1370 are displayed in expanded mode.
1371 </para>
1372 </listitem>
1373 </varlistentry>
1374
1375 <varlistentry id="app-psql-meta-command-da-uc">
1376 <term><literal>\dA[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1377
1378 <listitem>
1379 <para>
1380 Lists access methods. If <replaceable
1381 class="parameter">pattern</replaceable> is specified, only access
1382 methods whose names match the pattern are shown.
1383 If <literal>x</literal> is appended to the command name, the results
1384 are displayed in expanded mode.
1385 If <literal>+</literal> is appended to the command name, each access
1386 method is listed with its associated handler function and description.
1387 </para>
1388 </listitem>
1389 </varlistentry>
1390
1391 <varlistentry id="app-psql-meta-command-dac">
1392 <term>
1393 <literal>\dAc[x+]
1394 [<link linkend="app-psql-patterns"><replaceable class="parameter">access-method-pattern</replaceable></link>
1395 [<link linkend="app-psql-patterns"><replaceable class="parameter">input-type-pattern</replaceable></link>]]
1396 </literal>
1397 </term>
1398 <listitem>
1399 <para>
1400 Lists operator classes
1401 (see <xref linkend="xindex-opclass"/>).
1402 If <replaceable class="parameter">access-method-pattern</replaceable>
1403 is specified, only operator classes associated with access methods whose
1404 names match that pattern are listed.
1405 If <replaceable class="parameter">input-type-pattern</replaceable>
1406 is specified, only operator classes associated with input types whose
1407 names match that pattern are listed.
1408 If <literal>x</literal> is appended to the command name, the results
1409 are displayed in expanded mode.
1410 If <literal>+</literal> is appended to the command name, each operator
1411 class is listed with its associated operator family and owner.
1412 </para>
1413 </listitem>
1414 </varlistentry>
1415
1416 <varlistentry id="app-psql-meta-command-daf">
1417 <term>
1418 <literal>\dAf[x+]
1419 [<link linkend="app-psql-patterns"><replaceable class="parameter">access-method-pattern</replaceable></link>
1420 [<link linkend="app-psql-patterns"><replaceable class="parameter">input-type-pattern</replaceable></link>]]
1421 </literal>
1422 </term>
1423 <listitem>
1424 <para>
1425 Lists operator families
1426 (see <xref linkend="xindex-opfamily"/>).
1427 If <replaceable class="parameter">access-method-pattern</replaceable>
1428 is specified, only operator families associated with access methods whose
1429 names match that pattern are listed.
1430 If <replaceable class="parameter">input-type-pattern</replaceable>
1431 is specified, only operator families associated with input types whose
1432 names match that pattern are listed.
1433 If <literal>x</literal> is appended to the command name, the results
1434 are displayed in expanded mode.
1435 If <literal>+</literal> is appended to the command name, each operator
1436 family is listed with its owner.
1437 </para>
1438 </listitem>
1439 </varlistentry>
1440
1441 <varlistentry id="app-psql-meta-command-dao">
1442 <term>
1443 <literal>\dAo[x+]
1444 [<link linkend="app-psql-patterns"><replaceable class="parameter">access-method-pattern</replaceable></link>
1445 [<link linkend="app-psql-patterns"><replaceable class="parameter">operator-family-pattern</replaceable></link>]]
1446 </literal>
1447 </term>
1448
1449 <listitem>
1450 <para>
1451 Lists operators associated with operator families
1452 (see <xref linkend="xindex-strategies"/>).
1453 If <replaceable class="parameter">access-method-pattern</replaceable>
1454 is specified, only members of operator families associated with access
1455 methods whose names match that pattern are listed.
1456 If <replaceable class="parameter">operator-family-pattern</replaceable>
1457 is specified, only members of operator families whose names match that
1458 pattern are listed.
1459 If <literal>x</literal> is appended to the command name, the results
1460 are displayed in expanded mode.
1461 If <literal>+</literal> is appended to the command name, each operator
1462 is listed with its sort operator family (if it is an ordering operator),
1463 and whether its underlying function is leakproof.
1464 </para>
1465 </listitem>
1466 </varlistentry>
1467
1468 <varlistentry id="app-psql-meta-command-dap">
1469 <term>
1470 <literal>\dAp[x+]
1471 [<link linkend="app-psql-patterns"><replaceable class="parameter">access-method-pattern</replaceable></link>
1472 [<link linkend="app-psql-patterns"><replaceable class="parameter">operator-family-pattern</replaceable></link>]]
1473 </literal>
1474 </term>
1475 <listitem>
1476 <para>
1477 Lists support functions associated with operator families
1478 (see <xref linkend="xindex-support"/>).
1479 If <replaceable class="parameter">access-method-pattern</replaceable>
1480 is specified, only functions of operator families associated with
1481 access methods whose names match that pattern are listed.
1482 If <replaceable class="parameter">operator-family-pattern</replaceable>
1483 is specified, only functions of operator families whose names match
1484 that pattern are listed.
1485 If <literal>x</literal> is appended to the command name, the results
1486 are displayed in expanded mode.
1487 If <literal>+</literal> is appended to the command name, functions are
1488 displayed verbosely, with their actual parameter lists.
1489 </para>
1490 </listitem>
1491 </varlistentry>
1492
1493 <varlistentry id="app-psql-meta-command-db">
1494 <term><literal>\db[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1495
1496 <listitem>
1497 <para>
1498 Lists tablespaces. If <replaceable
1499 class="parameter">pattern</replaceable>
1500 is specified, only tablespaces whose names match the pattern are shown.
1501 If <literal>x</literal> is appended to the command name, the results
1502 are displayed in expanded mode.
1503 If <literal>+</literal> is appended to the command name, each tablespace
1504 is listed with its associated options, on-disk size, permissions and
1505 description.
1506 </para>
1507 </listitem>
1508 </varlistentry>
1509
1510
1511 <varlistentry id="app-psql-meta-command-dc-lc">
1512 <term><literal>\dc[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1513 <listitem>
1514 <para>
1515 Lists conversions between character-set encodings.
1516 If <replaceable class="parameter">pattern</replaceable>
1517 is specified, only conversions whose names match the pattern are
1518 listed.
1519 By default, only user-created objects are shown; supply a
1520 pattern or the <literal>S</literal> modifier to include system
1521 objects.
1522 If <literal>x</literal> is appended to the command name, the results
1523 are displayed in expanded mode.
1524 If <literal>+</literal> is appended to the command name, each object
1525 is listed with its associated description.
1526 </para>
1527 </listitem>
1528 </varlistentry>
1529
1530
1531 <varlistentry id="app-psql-meta-command-dconfig">
1532 <term><literal>\dconfig[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1533 <listitem>
1534 <para>
1535 Lists server configuration parameters and their values.
1536 If <replaceable class="parameter">pattern</replaceable> is specified,
1537 only parameters whose names match the pattern are listed. Without
1538 a <replaceable class="parameter">pattern</replaceable>, only
1539 parameters that are set to non-default values are listed.
1540 (Use <literal>\dconfig *</literal> to see all parameters.)
1541 If <literal>x</literal> is appended to the command name, the results
1542 are displayed in expanded mode.
1543 If <literal>+</literal> is appended to the command name, each
1544 parameter is listed with its data type, context in which the
1545 parameter can be set, and access privileges (if non-default access
1546 privileges have been granted).
1547 </para>
1548 </listitem>
1549 </varlistentry>
1550
1551
1552 <varlistentry id="app-psql-meta-command-dc-uc">
1553 <term><literal>\dC[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1554 <listitem>
1555 <para>
1556 Lists type casts.
1557 If <replaceable class="parameter">pattern</replaceable>
1558 is specified, only casts whose source or target types match the
1559 pattern are listed.
1560 If <literal>x</literal> is appended to the command name, the results
1561 are displayed in expanded mode.
1562 If <literal>+</literal> is appended to the command name, additional
1563 information about each cast is shown, including whether its underlying
1564 function is leakproof, and the cast's description.
1565 </para>
1566 </listitem>
1567 </varlistentry>
1568
1569
1570 <varlistentry id="app-psql-meta-command-dd-lc">
1571 <term><literal>\dd[Sx] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1572 <listitem>
1573 <para>
1574 Shows the descriptions of objects of type <literal>constraint</literal>,
1575 <literal>operator class</literal>, <literal>operator family</literal>,
1576 <literal>rule</literal>, and <literal>trigger</literal>. All
1577 other comments may be viewed by the respective backslash commands for
1578 those object types.
1579 </para>
1580
1581 <para><literal>\dd</literal> displays descriptions for objects matching the
1582 <replaceable class="parameter">pattern</replaceable>, or of visible
1583 objects of the appropriate type if no argument is given. But in either
1584 case, only objects that have a description are listed.
1585 By default, only user-created objects are shown; supply a
1586 pattern or the <literal>S</literal> modifier to include system
1587 objects.
1588 If <literal>x</literal> is appended to the command name, the results
1589 are displayed in expanded mode.
1590 </para>
1591
1592 <para>
1593 Descriptions for objects can be created with the <link
1594 linkend="sql-comment"><command>COMMENT</command></link>
1595 <acronym>SQL</acronym> command.
1596 </para>
1597 </listitem>
1598 </varlistentry>
1599
1600
1601 <varlistentry id="app-psql-meta-command-dd-uc">
1602 <term><literal>\dD[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1603 <listitem>
1604 <para>
1605 Lists domains. If <replaceable
1606 class="parameter">pattern</replaceable>
1607 is specified, only domains whose names match the pattern are shown.
1608 By default, only user-created objects are shown; supply a
1609 pattern or the <literal>S</literal> modifier to include system
1610 objects.
1611 If <literal>x</literal> is appended to the command name, the results
1612 are displayed in expanded mode.
1613 If <literal>+</literal> is appended to the command name, each object
1614 is listed with its associated permissions and description.
1615 </para>
1616 </listitem>
1617 </varlistentry>
1618
1619
1620 <varlistentry id="app-psql-meta-command-ddp">
1621 <term><literal>\ddp[x] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1622 <listitem>
1623 <para>
1624 Lists default access privilege settings. An entry is shown for
1625 each role (and schema, if applicable) for which the default
1626 privilege settings have been changed from the built-in defaults.
1627 If <replaceable class="parameter">pattern</replaceable> is
1628 specified, only entries whose role name or schema name matches
1629 the pattern are listed.
1630 If <literal>x</literal> is appended to the command name, the results
1631 are displayed in expanded mode.
1632 </para>
1633
1634 <para>
1635 The <link linkend="sql-alterdefaultprivileges"><command>ALTER DEFAULT
1636 PRIVILEGES</command></link> command is used to set default access
1637 privileges. The meaning of the privilege display is explained in
1638 <xref linkend="ddl-priv"/>.
1639 </para>
1640 </listitem>
1641 </varlistentry>
1642
1643
1644 <varlistentry id="app-psql-meta-command-de">
1645 <term><literal>\dE[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1646 <term><literal>\di[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1647 <term><literal>\dm[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1648 <term><literal>\ds[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1649 <term><literal>\dt[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1650 <term><literal>\dv[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1651
1652 <listitem>
1653 <para>
1654 In this group of commands, the letters <literal>E</literal>,
1655 <literal>i</literal>, <literal>m</literal>, <literal>s</literal>,
1656 <literal>t</literal>, and <literal>v</literal>
1657 stand for foreign table, index, materialized view,
1658 sequence, table, and view,
1659 respectively.
1660 You can specify any or all of
1661 these letters, in any order, to obtain a listing of objects
1662 of these types. For example, <literal>\dti</literal> lists
1663 tables and indexes.
1664 If <literal>x</literal> is appended to the command name, the results
1665 are displayed in expanded mode.
1666 If <literal>+</literal> is
1667 appended to the command name, each object is listed with its
1668 persistence status (permanent, temporary, or unlogged),
1669 physical size on disk, and associated description if any.
1670 If <replaceable class="parameter">pattern</replaceable> is
1671 specified, only objects whose names match the pattern are listed.
1672 By default, only user-created objects are shown; supply a
1673 pattern or the <literal>S</literal> modifier to include system
1674 objects.
1675 </para>
1676 </listitem>
1677 </varlistentry>
1678
1679
1680 <varlistentry id="app-psql-meta-command-des">
1681 <term><literal>\des[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1682 <listitem>
1683 <para>
1684 Lists foreign servers (mnemonic: <quote>external
1685 servers</quote>).
1686 If <replaceable class="parameter">pattern</replaceable> is
1687 specified, only those servers whose name matches the pattern
1688 are listed.
1689 If <literal>x</literal> is appended to the command name, the results
1690 are displayed in expanded mode.
1691 If <literal>+</literal> is appended to the command name, a
1692 full description of each server is shown, including the
1693 server's access privileges, type, version, options, and description.
1694 </para>
1695 </listitem>
1696 </varlistentry>
1697
1698
1699 <varlistentry id="app-psql-meta-command-det">
1700 <term><literal>\det[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1701 <listitem>
1702 <para>
1703 Lists foreign tables (mnemonic: <quote>external tables</quote>).
1704 If <replaceable class="parameter">pattern</replaceable> is
1705 specified, only entries whose table name or schema name matches
1706 the pattern are listed.
1707 If <literal>x</literal> is appended to the command name, the results
1708 are displayed in expanded mode.
1709 If <literal>+</literal> is appended to the command name,
1710 generic options and the foreign table description
1711 are also displayed.
1712 </para>
1713 </listitem>
1714 </varlistentry>
1715
1716
1717 <varlistentry id="app-psql-meta-command-deu">
1718 <term><literal>\deu[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1719 <listitem>
1720 <para>
1721 Lists user mappings (mnemonic: <quote>external
1722 users</quote>).
1723 If <replaceable class="parameter">pattern</replaceable> is
1724 specified, only those mappings whose user names match the
1725 pattern are listed.
1726 If <literal>x</literal> is appended to the command name, the results
1727 are displayed in expanded mode.
1728 If <literal>+</literal> is appended to the command name,
1729 additional information about each mapping is shown.
1730 </para>
1731
1732 <caution>
1733 <para>
1734 <literal>\deu+</literal> might also display the user name and
1735 password of the remote user, so care should be taken not to
1736 disclose them.
1737 </para>
1738 </caution>
1739 </listitem>
1740 </varlistentry>
1741
1742
1743 <varlistentry id="app-psql-meta-command-dew">
1744 <term><literal>\dew[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1745 <listitem>
1746 <para>
1747 Lists foreign-data wrappers (mnemonic: <quote>external
1748 wrappers</quote>).
1749 If <replaceable class="parameter">pattern</replaceable> is
1750 specified, only those foreign-data wrappers whose name matches
1751 the pattern are listed.
1752 If <literal>x</literal> is appended to the command name, the results
1753 are displayed in expanded mode.
1754 If <literal>+</literal> is appended to the command name,
1755 the access privileges, options, and description of the
1756 foreign-data wrapper are also shown.
1757 </para>
1758 </listitem>
1759 </varlistentry>
1760
1761
1762 <varlistentry id="app-psql-meta-command-df-lc">
1763 <term><literal>\df[anptwSx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> [ <replaceable class="parameter">arg_pattern</replaceable> ... ] ]</literal></term>
1764
1765 <listitem>
1766 <para>
1767 Lists functions, together with their result data types, argument data
1768 types, and function types, which are classified as <quote>agg</quote>
1769 (aggregate), <quote>normal</quote>, <quote>procedure</quote>, <quote>trigger</quote>, or <quote>window</quote>.
1770 To display only functions
1771 of specific type(s), add the corresponding letters <literal>a</literal>,
1772 <literal>n</literal>, <literal>p</literal>, <literal>t</literal>, or <literal>w</literal> to the command.
1773 If <replaceable
1774 class="parameter">pattern</replaceable> is specified, only
1775 functions whose names match the pattern are shown.
1776 Any additional arguments are type-name patterns, which are matched
1777 to the type names of the first, second, and so on arguments of the
1778 function. (Matching functions can have more arguments than what
1779 you specify. To prevent that, write a dash <literal>-</literal> as
1780 the last <replaceable class="parameter">arg_pattern</replaceable>.)
1781 By default, only user-created
1782 objects are shown; supply a pattern or the <literal>S</literal>
1783 modifier to include system objects.
1784 If <literal>x</literal> is appended to the command name, the results
1785 are displayed in expanded mode.
1786 If <literal>+</literal> is appended to the command name, additional information
1787 about each function is shown, including volatility,
1788 parallel safety, owner, security classification, whether it is
1789 leakproof, access privileges, language, internal name (for C and
1790 internal functions only), and description.
1791 Source code for a specific function can be seen
1792 using <literal>\sf</literal>.
1793 </para>
1794
1795 </listitem>
1796 </varlistentry>
1797
1798 <varlistentry id="app-psql-meta-command-df-uc">
1799 <term><literal>\dF[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1800 <listitem>
1801 <para>
1802 Lists text search configurations.
1803 If <replaceable class="parameter">pattern</replaceable> is specified,
1804 only configurations whose names match the pattern are shown.
1805 If <literal>x</literal> is appended to the command name, the results
1806 are displayed in expanded mode.
1807 If <literal>+</literal> is appended to the command name, a full description of
1808 each configuration is shown, including the underlying text search
1809 parser and the dictionary list for each parser token type.
1810 </para>
1811 </listitem>
1812 </varlistentry>
1813
1814 <varlistentry id="app-psql-meta-command-dfd">
1815 <term><literal>\dFd[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1816 <listitem>
1817 <para>
1818 Lists text search dictionaries.
1819 If <replaceable class="parameter">pattern</replaceable> is specified,
1820 only dictionaries whose names match the pattern are shown.
1821 If <literal>x</literal> is appended to the command name, the results
1822 are displayed in expanded mode.
1823 If <literal>+</literal> is appended to the command name, additional information
1824 is shown about each selected dictionary, including the underlying
1825 text search template and the option values.
1826 </para>
1827 </listitem>
1828 </varlistentry>
1829
1830 <varlistentry id="app-psql-meta-command-dfp">
1831 <term><literal>\dFp[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1832 <listitem>
1833 <para>
1834 Lists text search parsers.
1835 If <replaceable class="parameter">pattern</replaceable> is specified,
1836 only parsers whose names match the pattern are shown.
1837 If <literal>x</literal> is appended to the command name, the results
1838 are displayed in expanded mode.
1839 If <literal>+</literal> is appended to the command name, a full description of
1840 each parser is shown, including the underlying functions and the
1841 list of recognized token types.
1842 </para>
1843 </listitem>
1844 </varlistentry>
1845
1846 <varlistentry id="app-psql-meta-command-dft">
1847 <term><literal>\dFt[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1848 <listitem>
1849 <para>
1850 Lists text search templates.
1851 If <replaceable class="parameter">pattern</replaceable> is specified,
1852 only templates whose names match the pattern are shown.
1853 If <literal>x</literal> is appended to the command name, the results
1854 are displayed in expanded mode.
1855 If <literal>+</literal> is appended to the command name, additional information
1856 is shown about each template, including the underlying function names.
1857 </para>
1858 </listitem>
1859 </varlistentry>
1860
1861
1862 <varlistentry id="app-psql-meta-command-dg">
1863 <term><literal>\dg[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1864 <listitem>
1865 <para>
1866 Lists database roles.
1867 (Since the concepts of <quote>users</quote> and <quote>groups</quote> have been
1868 unified into <quote>roles</quote>, this command is now equivalent to
1869 <literal>\du</literal>.)
1870 By default, only user-created roles are shown; supply the
1871 <literal>S</literal> modifier to include system roles.
1872 If <replaceable class="parameter">pattern</replaceable> is specified,
1873 only those roles whose names match the pattern are listed.
1874 If <literal>x</literal> is appended to the command name, the results
1875 are displayed in expanded mode.
1876 If <literal>+</literal> is appended to the command name, additional information
1877 is shown about each role; currently this adds the comment for each
1878 role.
1879 </para>
1880 </listitem>
1881 </varlistentry>
1882
1883
1884 <varlistentry id="app-psql-meta-command-dl-lc">
1885 <term><literal>\dl[x+]</literal></term>
1886 <listitem>
1887 <para>
1888 This is an alias for <command>\lo_list</command>, which shows a
1889 list of large objects.
1890 If <literal>x</literal> is appended to the command name, the results
1891 are displayed in expanded mode.
1892 If <literal>+</literal> is appended to the command name,
1893 each large object is listed with its associated permissions,
1894 if any.
1895 </para>
1896 </listitem>
1897 </varlistentry>
1898
1899 <varlistentry id="app-psql-meta-command-dl-uc">
1900 <term><literal>\dL[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1901 <listitem>
1902 <para>
1903 Lists procedural languages. If <replaceable
1904 class="parameter">pattern</replaceable>
1905 is specified, only languages whose names match the pattern are listed.
1906 By default, only user-created languages
1907 are shown; supply the <literal>S</literal> modifier to include system
1908 objects.
1909 If <literal>x</literal> is appended to the command name, the results
1910 are displayed in expanded mode.
1911 If <literal>+</literal> is appended to the command name, each
1912 language is listed with its call handler, validator, access privileges,
1913 and whether it is a system object.
1914 </para>
1915 </listitem>
1916 </varlistentry>
1917
1918
1919 <varlistentry id="app-psql-meta-command-dn">
1920 <term><literal>\dn[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1921
1922 <listitem>
1923 <para>
1924 Lists schemas (namespaces). If <replaceable
1925 class="parameter">pattern</replaceable>
1926 is specified, only schemas whose names match the pattern are listed.
1927 By default, only user-created objects are shown; supply a
1928 pattern or the <literal>S</literal> modifier to include system objects.
1929 If <literal>x</literal> is appended to the command name, the results
1930 are displayed in expanded mode.
1931 If <literal>+</literal> is appended to the command name, each object
1932 is listed with its associated permissions and description, if any.
1933 </para>
1934 </listitem>
1935 </varlistentry>
1936
1937
1938 <varlistentry id="app-psql-meta-command-do-lc">
1939 <term><literal>\do[Sx+] [ <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>
1940 <listitem>
1941 <para>
1942 Lists operators with their operand and result types.
1943 If <replaceable class="parameter">pattern</replaceable> is
1944 specified, only operators whose names match the pattern are listed.
1945 If one <replaceable class="parameter">arg_pattern</replaceable> is
1946 specified, only prefix operators whose right argument's type name
1947 matches that pattern are listed.
1948 If two <replaceable class="parameter">arg_pattern</replaceable>s
1949 are specified, only binary operators whose argument type names match
1950 those patterns are listed. (Alternatively, write <literal>-</literal>
1951 for the unused argument of a unary operator.)
1952 By default, only user-created objects are shown; supply a
1953 pattern or the <literal>S</literal> modifier to include system
1954 objects.
1955 If <literal>x</literal> is appended to the command name, the results
1956 are displayed in expanded mode.
1957 If <literal>+</literal> is appended to the command name,
1958 additional information about each operator is shown, including
1959 the name of the underlying function, and whether it is leakproof.
1960 </para>
1961 </listitem>
1962 </varlistentry>
1963
1964
1965 <varlistentry id="app-psql-meta-command-do-uc">
1966 <term><literal>\dO[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1967 <listitem>
1968 <para>
1969 Lists collations.
1970 If <replaceable class="parameter">pattern</replaceable> is
1971 specified, only collations whose names match the pattern are
1972 listed. By default, only user-created objects are shown;
1973 supply a pattern or the <literal>S</literal> modifier to
1974 include system objects.
1975 If <literal>x</literal> is appended to the command name, the results
1976 are displayed in expanded mode.
1977 If <literal>+</literal> is appended
1978 to the command name, each collation is listed with its associated
1979 description, if any.
1980 Note that only collations usable with the current database's encoding
1981 are shown, so the results may vary in different databases of the
1982 same installation.
1983 </para>
1984 </listitem>
1985 </varlistentry>
1986
1987
1988 <varlistentry id="app-psql-meta-command-dp-lc">
1989 <term><literal>\dp[Sx] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1990 <listitem>
1991 <para>
1992 Lists tables, views and sequences with their
1993 associated access privileges.
1994 If <replaceable class="parameter">pattern</replaceable> is
1995 specified, only tables, views and sequences whose names match the
1996 pattern are listed. By default only user-created objects are shown;
1997 supply a pattern or the <literal>S</literal> modifier to include
1998 system objects.
1999 If <literal>x</literal> is appended to the command name, the results
2000 are displayed in expanded mode.
2001 </para>
2002
2003 <para>
2004 The <link linkend="sql-grant"><command>GRANT</command></link> and
2005 <link linkend="sql-revoke"><command>REVOKE</command></link>
2006 commands are used to set access privileges. The meaning of the
2007 privilege display is explained in
2008 <xref linkend="ddl-priv"/>.
2009 </para>
2010 </listitem>
2011 </varlistentry>
2012
2013
2014 <varlistentry id="app-psql-meta-command-dp-uc">
2015 <term><literal>\dP[itnx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2016 <listitem>
2017 <para>
2018 Lists partitioned relations.
2019 If <replaceable class="parameter">pattern</replaceable>
2020 is specified, only entries whose name matches the pattern are listed.
2021 The modifiers <literal>t</literal> (tables) and <literal>i</literal>
2022 (indexes) can be appended to the command, filtering the kind of
2023 relations to list. By default, partitioned tables and indexes are
2024 listed.
2025 </para>
2026
2027 <para>
2028 If the modifier <literal>n</literal> (<quote>nested</quote>) is used,
2029 or a pattern is specified, then non-root partitioned relations are
2030 included, and a column is shown displaying the parent of each
2031 partitioned relation.
2032 </para>
2033
2034 <para>
2035 If <literal>x</literal> is appended to the command name, the results
2036 are displayed in expanded mode.
2037 If <literal>+</literal> is appended to the command name, the sum of the
2038 sizes of each relation's partitions is also displayed, along with the
2039 relation's description.
2040 If <literal>n</literal> is combined with <literal>+</literal>, two
2041 sizes are shown: one including the total size of directly-attached
2042 leaf partitions, and another showing the total size of all partitions,
2043 including indirectly attached sub-partitions.
2044 </para>
2045 </listitem>
2046 </varlistentry>
2047
2048
2049 <varlistentry id="app-psql-meta-command-drds">
2050 <term><literal>\drds[x] [ <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>
2051 <listitem>
2052 <para>
2053 Lists defined configuration settings. These settings can be
2054 role-specific, database-specific, or both.
2055 <replaceable>role-pattern</replaceable> and
2056 <replaceable>database-pattern</replaceable> are used to select
2057 specific roles and databases to list, respectively. If omitted, or if
2058 <literal>*</literal> is specified, all settings are listed, including those
2059 not role-specific or database-specific, respectively.
2060 If <literal>x</literal> is appended to the command name, the results
2061 are displayed in expanded mode.
2062 </para>
2063
2064 <para>
2065 The <link linkend="sql-alterrole"><command>ALTER ROLE</command></link> and
2066 <link linkend="sql-alterdatabase"><command>ALTER DATABASE</command></link>
2067 commands are used to define per-role and per-database configuration
2068 settings.
2069 </para>
2070 </listitem>
2071 </varlistentry>
2072
2073
2074 <varlistentry id="app-psql-meta-command-drg">
2075 <term><literal>\drg[Sx] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2076 <listitem>
2077 <para>
2078 Lists information about each granted role membership, including
2079 assigned options (<literal>ADMIN</literal>,
2080 <literal>INHERIT</literal> and/or <literal>SET</literal>) and grantor.
2081 See the <link linkend="sql-grant"><command>GRANT</command></link>
2082 command for information about role memberships.
2083 </para>
2084 <para>
2085 By default, only grants to user-created roles are shown; supply the
2086 <literal>S</literal> modifier to include system roles.
2087 If <replaceable class="parameter">pattern</replaceable> is specified,
2088 only grants to those roles whose names match the pattern are listed.
2089 If <literal>x</literal> is appended to the command name, the results
2090 are displayed in expanded mode.
2091 </para>
2092 </listitem>
2093 </varlistentry>
2094
2095
2096 <varlistentry id="app-psql-meta-command-drp">
2097 <term><literal>\dRp[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2098 <listitem>
2099 <para>
2100 Lists replication publications.
2101 If <replaceable class="parameter">pattern</replaceable> is
2102 specified, only those publications whose names match the pattern are
2103 listed.
2104 If <literal>x</literal> is appended to the command name, the results
2105 are displayed in expanded mode.
2106 If <literal>+</literal> is appended to the command name, the tables and
2107 schemas associated with each publication are shown as well.
2108 </para>
2109 </listitem>
2110 </varlistentry>
2111
2112 <varlistentry id="app-psql-meta-command-drs">
2113 <term><literal>\dRs[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2114 <listitem>
2115 <para>
2116 Lists replication subscriptions.
2117 If <replaceable class="parameter">pattern</replaceable> is
2118 specified, only those subscriptions whose names match the pattern are
2119 listed.
2120 If <literal>x</literal> is appended to the command name, the results
2121 are displayed in expanded mode.
2122 If <literal>+</literal> is appended to the command name, additional
2123 properties of the subscriptions are shown.
2124 </para>
2125 </listitem>
2126 </varlistentry>
2127
2128 <varlistentry id="app-psql-meta-command-dt">
2129 <term><literal>\dT[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2130 <listitem>
2131 <para>
2132 Lists data types.
2133 If <replaceable class="parameter">pattern</replaceable> is
2134 specified, only types whose names match the pattern are listed.
2135 If <literal>x</literal> is appended to the command name, the results
2136 are displayed in expanded mode.
2137 If <literal>+</literal> is appended to the command name, each type is
2138 listed with its internal name and size, its allowed values
2139 if it is an <type>enum</type> type, and its associated permissions.
2140 By default, only user-created objects are shown; supply a
2141 pattern or the <literal>S</literal> modifier to include system
2142 objects.
2143 </para>
2144 </listitem>
2145 </varlistentry>
2146
2147 <varlistentry id="app-psql-meta-command-du">
2148 <term><literal>\du[Sx+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2149 <listitem>
2150 <para>
2151 Lists database roles.
2152 (Since the concepts of <quote>users</quote> and <quote>groups</quote> have been
2153 unified into <quote>roles</quote>, this command is now equivalent to
2154 <literal>\dg</literal>.)
2155 By default, only user-created roles are shown; supply the
2156 <literal>S</literal> modifier to include system roles.
2157 If <replaceable class="parameter">pattern</replaceable> is specified,
2158 only those roles whose names match the pattern are listed.
2159 If <literal>x</literal> is appended to the command name, the results
2160 are displayed in expanded mode.
2161 If <literal>+</literal> is appended to the command name, additional information
2162 is shown about each role; currently this adds the comment for each
2163 role.
2164 </para>
2165 </listitem>
2166 </varlistentry>
2167
2168 <varlistentry id="app-psql-meta-command-dx-lc">
2169 <term><literal>\dx[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2170 <listitem>
2171 <para>
2172 Lists installed extensions.
2173 If <replaceable class="parameter">pattern</replaceable>
2174 is specified, only those extensions whose names match the pattern
2175 are listed.
2176 If <literal>x</literal> is appended to the command name, the results
2177 are displayed in expanded mode.
2178 If <literal>+</literal> is appended to the command name, all the objects belonging
2179 to each matching extension are listed.
2180 </para>
2181 </listitem>
2182 </varlistentry>
2183
2184 <varlistentry id="app-psql-meta-command-dx-uc">
2185 <term><literal>\dX[x] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2186 <listitem>
2187 <para>
2188 Lists extended statistics.
2189 If <replaceable class="parameter">pattern</replaceable>
2190 is specified, only those extended statistics whose names match the
2191 pattern are listed.
2192 If <literal>x</literal> is appended to the command name, the results
2193 are displayed in expanded mode.
2194 </para>
2195
2196 <para>
2197 The status of each kind of extended statistics is shown in a column
2198 named after its statistic kind (e.g. Ndistinct).
2199 <literal>defined</literal> means that it was requested when creating
2200 the statistics, and NULL means it wasn't requested.
2201 You can use <structname>pg_stats_ext</structname> if you'd like to
2202 know whether <link linkend="sql-analyze"><command>ANALYZE</command></link>
2203 was run and statistics are available to the planner.
2204 </para>
2205 </listitem>
2206 </varlistentry>
2207
2208 <varlistentry id="app-psql-meta-command-dy">
2209 <term><literal>\dy[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2210 <listitem>
2211 <para>
2212 Lists event triggers.
2213 If <replaceable class="parameter">pattern</replaceable>
2214 is specified, only those event triggers whose names match the pattern
2215 are listed.
2216 If <literal>x</literal> is appended to the command name, the results
2217 are displayed in expanded mode.
2218 If <literal>+</literal> is appended to the command name, each object
2219 is listed with its associated description.
2220 </para>
2221 </listitem>
2222 </varlistentry>
2223
2224 <varlistentry id="app-psql-meta-command-edit">
2225 <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>
2226
2227 <listitem>
2228 <para>
2229 If <replaceable class="parameter">filename</replaceable> is
2230 specified, the file is edited; after the editor exits, the file's
2231 content is copied into the current query buffer. If no <replaceable
2232 class="parameter">filename</replaceable> is given, the current query
2233 buffer is copied to a temporary file which is then edited in the same
2234 fashion. Or, if the current query buffer is empty, the most recently
2235 executed query is copied to a temporary file and edited in the same
2236 fashion.
2237 </para>
2238
2239 <para>
2240 If you edit a file or the previous query, and you quit the editor without
2241 modifying the file, the query buffer is cleared.
2242 Otherwise, the new contents of the query buffer are re-parsed according to
2243 the normal rules of <application>psql</application>, treating the
2244 whole buffer as a single line. Any complete queries are immediately
2245 executed; that is, if the query buffer contains or ends with a
2246 semicolon, everything up to that point is executed and removed from
2247 the query buffer. Whatever remains in the query buffer is
2248 redisplayed. Type semicolon or <literal>\g</literal> to send it,
2249 or <literal>\r</literal> to cancel it by clearing the query buffer.
2250 </para>
2251
2252 <para>
2253 Treating the buffer as a single line primarily affects meta-commands:
2254 whatever is in the buffer after a meta-command will be taken as
2255 argument(s) to the meta-command, even if it spans multiple lines.
2256 (Thus you cannot make meta-command-using scripts this way.
2257 Use <command>\i</command> for that.)
2258 </para>
2259
2260 <para>
2261 If a line number is specified, <application>psql</application> will
2262 position the cursor on the specified line of the file or query buffer.
2263 Note that if a single all-digits argument is given,
2264 <application>psql</application> assumes it is a line number,
2265 not a file name.
2266 </para>
2267
2268 <tip>
2269 <para>
2270 See <xref linkend="app-psql-environment"/>, below, for how to
2271 configure and customize your editor.
2272 </para>
2273 </tip>
2274 </listitem>
2275 </varlistentry>
2276
2277 <varlistentry id="app-psql-meta-command-echo">
2278 <term><literal>\echo <replaceable class="parameter">text</replaceable> [ ... ]</literal></term>
2279 <listitem>
2280 <para>
2281 Prints the evaluated arguments to standard output, separated by
2282 spaces and followed by a newline. This can be useful to
2283 intersperse information in the output of scripts. For example:
2284 <programlisting>
2285 =&gt; <userinput>\echo `date`</userinput>
2286 Tue Oct 26 21:40:57 CEST 1999
2287 </programlisting>
2288 If the first argument is an unquoted <literal>-n</literal> the trailing
2289 newline is not written (nor is the first argument).
2290 </para>
2291
2292 <tip>
2293 <para>
2294 If you use the <command>\o</command> command to redirect your
2295 query output you might wish to use <command>\qecho</command>
2296 instead of this command. See also <command>\warn</command>.
2297 </para>
2298 </tip>
2299 </listitem>
2300 </varlistentry>
2301
2302 <varlistentry id="app-psql-meta-command-ef">
2303 <term><literal>\ef <optional> <replaceable class="parameter">function_description</replaceable> <optional> <replaceable class="parameter">line_number</replaceable> </optional> </optional> </literal></term>
2304
2305 <listitem>
2306 <para>
2307 This command fetches and edits the definition of the named function or procedure,
2308 in the form of a <command>CREATE OR REPLACE FUNCTION</command> or
2309 <command>CREATE OR REPLACE PROCEDURE</command> command.
2310 Editing is done in the same way as for <literal>\edit</literal>.
2311 If you quit the editor without saving, the statement is discarded.
2312 If you save and exit the editor, the updated command is executed immediately
2313 if you added a semicolon to it. Otherwise it is redisplayed;
2314 type semicolon or <literal>\g</literal> to send it, or <literal>\r</literal>
2315 to cancel.
2316 </para>
2317
2318 <para>
2319 The target function can be specified by name alone, or by name
2320 and arguments, for example <literal>foo(integer, text)</literal>.
2321 The argument types must be given if there is more
2322 than one function of the same name.
2323 </para>
2324
2325 <para>
2326 If no function is specified, a blank <command>CREATE FUNCTION</command>
2327 template is presented for editing.
2328 </para>
2329
2330 <para>
2331 If a line number is specified, <application>psql</application> will
2332 position the cursor on the specified line of the function body.
2333 (Note that the function body typically does not begin on the first
2334 line of the file.)
2335 </para>
2336
2337 <para>
2338 Unlike most other meta-commands, the entire remainder of the line is
2339 always taken to be the argument(s) of <command>\ef</command>, and neither
2340 variable interpolation nor backquote expansion are performed in the
2341 arguments.
2342 </para>
2343
2344 <tip>
2345 <para>
2346 See <xref linkend="app-psql-environment"/>, below, for how to
2347 configure and customize your editor.
2348 </para>
2349 </tip>
2350 </listitem>
2351 </varlistentry>
2352
2353
2354 <varlistentry id="app-psql-meta-command-encoding">
2355 <term><literal>\encoding [ <replaceable class="parameter">encoding</replaceable> ]</literal></term>
2356
2357 <listitem>
2358 <para>
2359 Sets the client character set encoding. Without an argument, this command
2360 shows the current encoding.
2361 </para>
2362 </listitem>
2363 </varlistentry>
2364
2365
2366 <varlistentry id="app-psql-meta-command-errverbose">
2367 <term><literal>\errverbose</literal></term>
2368
2369 <listitem>
2370 <para>
2371 Repeats the most recent server error message at maximum
2372 verbosity, as though <varname>VERBOSITY</varname> were set
2373 to <literal>verbose</literal> and <varname>SHOW_CONTEXT</varname> were
2374 set to <literal>always</literal>.
2375 </para>
2376 </listitem>
2377 </varlistentry>
2378
2379
2380 <varlistentry id="app-psql-meta-command-ev">
2381 <term><literal>\ev <optional> <replaceable class="parameter">view_name</replaceable> <optional> <replaceable class="parameter">line_number</replaceable> </optional> </optional> </literal></term>
2382
2383 <listitem>
2384 <para>
2385 This command fetches and edits the definition of the named view,
2386 in the form of a <command>CREATE OR REPLACE VIEW</command> command.
2387 Editing is done in the same way as for <literal>\edit</literal>.
2388 If you quit the editor without saving, the statement is discarded.
2389 If you save and exit the editor, the updated command is executed immediately
2390 if you added a semicolon to it. Otherwise it is redisplayed;
2391 type semicolon or <literal>\g</literal> to send it, or <literal>\r</literal>
2392 to cancel.
2393 </para>
2394
2395 <para>
2396 If no view is specified, a blank <command>CREATE VIEW</command>
2397 template is presented for editing.
2398 </para>
2399
2400 <para>
2401 If a line number is specified, <application>psql</application> will
2402 position the cursor on the specified line of the view definition.
2403 </para>
2404
2405 <para>
2406 Unlike most other meta-commands, the entire remainder of the line is
2407 always taken to be the argument(s) of <command>\ev</command>, and neither
2408 variable interpolation nor backquote expansion are performed in the
2409 arguments.
2410 </para>
2411 </listitem>
2412 </varlistentry>
2413
2414
2415 <varlistentry id="app-psql-meta-command-f">
2416 <term><literal>\f [ <replaceable class="parameter">string</replaceable> ]</literal></term>
2417
2418 <listitem>
2419 <para>
2420 Sets the field separator for unaligned query output. The default
2421 is the vertical bar (<literal>|</literal>). It is equivalent to
2422 <command>\pset fieldsep</command>.
2423 </para>
2424 </listitem>
2425 </varlistentry>
2426
2427
2428 <varlistentry id="app-psql-meta-command-g">
2429 <term><literal>\g [ (<replaceable class="parameter">option</replaceable>=<replaceable class="parameter">value</replaceable> [...]) ] [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2430 <term><literal>\g [ (<replaceable class="parameter">option</replaceable>=<replaceable class="parameter">value</replaceable> [...]) ] [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
2431 <listitem>
2432 <para>
2433 Sends the current query buffer to the server for execution.
2434 </para>
2435 <para>
2436 If parentheses appear after <literal>\g</literal>, they surround a
2437 space-separated list
2438 of <replaceable class="parameter">option</replaceable><literal>=</literal><replaceable class="parameter">value</replaceable>
2439 formatting-option clauses, which are interpreted in the same way
2440 as <literal>\pset</literal>
2441 <replaceable class="parameter">option</replaceable>
2442 <replaceable class="parameter">value</replaceable> commands, but take
2443 effect only for the duration of this query. In this list, spaces are
2444 not allowed around <literal>=</literal> signs, but are required
2445 between option clauses.
2446 If <literal>=</literal><replaceable class="parameter">value</replaceable>
2447 is omitted, the
2448 named <replaceable class="parameter">option</replaceable> is changed
2449 in the same way as for
2450 <literal>\pset</literal> <replaceable class="parameter">option</replaceable>
2451 with no explicit <replaceable class="parameter">value</replaceable>.
2452 </para>
2453 <para>
2454 If a <replaceable class="parameter">filename</replaceable>
2455 or <literal>|</literal><replaceable class="parameter">command</replaceable>
2456 argument is given, the query's output is written to the named
2457 file or piped to the given shell command, instead of displaying it as
2458 usual. The file or command is written to only if the query
2459 successfully returns zero or more tuples, not if the query fails or
2460 is a non-data-returning SQL command.
2461 </para>
2462 <para>
2463 If the current query buffer is empty, the most recently sent query is
2464 re-executed instead. Except for that behavior, <literal>\g</literal>
2465 without any arguments is essentially equivalent to a semicolon.
2466 With arguments, <literal>\g</literal> provides
2467 a <quote>one-shot</quote> alternative to the <command>\o</command>
2468 command, and additionally allows one-shot adjustments of the
2469 output formatting options normally set by <literal>\pset</literal>.
2470 </para>
2471 <para>
2472 When the last argument begins with <literal>|</literal>, the entire
2473 remainder of the line is taken to be
2474 the <replaceable class="parameter">command</replaceable> to execute,
2475 and neither variable interpolation nor backquote expansion are
2476 performed in it. The rest of the line is simply passed literally to
2477 the shell.
2478 </para>
2479 </listitem>
2480 </varlistentry>
2481
2482
2483 <varlistentry id="app-psql-meta-command-gdesc">
2484 <term><literal>\gdesc</literal></term>
2485
2486 <listitem>
2487 <para>
2488 Shows the description (that is, the column names and data types)
2489 of the result of the current query buffer. The query is not
2490 actually executed; however, if it contains some type of syntax
2491 error, that error will be reported in the normal way.
2492 </para>
2493
2494 <para>
2495 If the current query buffer is empty, the most recently sent query
2496 is described instead.
2497 </para>
2498 </listitem>
2499 </varlistentry>
2500
2501
2502 <varlistentry id="app-psql-meta-command-getenv">
2503 <term><literal>\getenv <replaceable class="parameter">psql_var</replaceable> <replaceable class="parameter">env_var</replaceable></literal></term>
2504
2505 <listitem>
2506 <para>
2507 Gets the value of the environment
2508 variable <replaceable class="parameter">env_var</replaceable>
2509 and assigns it to the <application>psql</application>
2510 variable <replaceable class="parameter">psql_var</replaceable>.
2511 If <replaceable class="parameter">env_var</replaceable> is
2512 not defined in the <application>psql</application> process's
2513 environment, <replaceable class="parameter">psql_var</replaceable>
2514 is not changed. Example:
2515 <programlisting>
2516 =&gt; <userinput>\getenv home HOME</userinput>
2517 =&gt; <userinput>\echo :home</userinput>
2518 /home/postgres
2519 </programlisting></para>
2520 </listitem>
2521 </varlistentry>
2522
2523
2524 <varlistentry id="app-psql-meta-command-gexec">
2525 <term><literal>\gexec</literal></term>
2526
2527 <listitem>
2528 <para>
2529 Sends the current query buffer to the server, then treats
2530 each column of each row of the query's output (if any) as an SQL
2531 statement to be executed. For example, to create an index on each
2532 column of <structname>my_table</structname>:
2533 <programlisting>
2534 =&gt; <userinput>SELECT format('create index on my_table(%I)', attname)</userinput>
2535 -&gt; <userinput>FROM pg_attribute</userinput>
2536 -&gt; <userinput>WHERE attrelid = 'my_table'::regclass AND attnum &gt; 0</userinput>
2537 -&gt; <userinput>ORDER BY attnum</userinput>
2538 -&gt; <userinput>\gexec</userinput>
2539 CREATE INDEX
2540 CREATE INDEX
2541 CREATE INDEX
2542 CREATE INDEX
2543 </programlisting>
2544 </para>
2545
2546 <para>
2547 The generated queries are executed in the order in which the rows
2548 are returned, and left-to-right within each row if there is more
2549 than one column. NULL fields are ignored. The generated queries
2550 are sent literally to the server for processing, so they cannot be
2551 <application>psql</application> meta-commands nor contain <application>psql</application>
2552 variable references. If any individual query fails, execution of
2553 the remaining queries continues
2554 unless <varname>ON_ERROR_STOP</varname> is set. Execution of each
2555 query is subject to <varname>ECHO</varname> processing.
2556 (Setting <varname>ECHO</varname> to <literal>all</literal>
2557 or <literal>queries</literal> is often advisable when
2558 using <command>\gexec</command>.) Query logging, single-step mode,
2559 timing, and other query execution features apply to each generated
2560 query as well.
2561 </para>
2562 <para>
2563 If the current query buffer is empty, the most recently sent query
2564 is re-executed instead.
2565 </para>
2566 </listitem>
2567 </varlistentry>
2568
2569
2570 <varlistentry id="app-psql-meta-command-gset">
2571 <term><literal>\gset [ <replaceable class="parameter">prefix</replaceable> ]</literal></term>
2572
2573 <listitem>
2574 <para>
2575 Sends the current query buffer to the server and stores the
2576 query's output into <application>psql</application> variables
2577 (see <xref linkend="app-psql-variables"/> below).
2578 The query to be executed must return exactly one row. Each column of
2579 the row is stored into a separate variable, named the same as the
2580 column. For example:
2581 <programlisting>
2582 =&gt; <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput>
2583 -&gt; <userinput>\gset</userinput>
2584 =&gt; <userinput>\echo :var1 :var2</userinput>
2585 hello 10
2586 </programlisting>
2587 </para>
2588 <para>
2589 If you specify a <replaceable class="parameter">prefix</replaceable>,
2590 that string is prepended to the query's column names to create the
2591 variable names to use:
2592 <programlisting>
2593 =&gt; <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput>
2594 -&gt; <userinput>\gset result_</userinput>
2595 =&gt; <userinput>\echo :result_var1 :result_var2</userinput>
2596 hello 10
2597 </programlisting>
2598 </para>
2599 <para>
2600 If a column result is NULL, the corresponding variable is unset
2601 rather than being set.
2602 </para>
2603 <para>
2604 If the query fails or does not return one row,
2605 no variables are changed.
2606 </para>
2607 <para>
2608 If the current query buffer is empty, the most recently sent query
2609 is re-executed instead.
2610 </para>
2611 </listitem>
2612 </varlistentry>
2613
2614
2615 <varlistentry id="app-psql-meta-command-gx">
2616 <term><literal>\gx [ (<replaceable class="parameter">option</replaceable>=<replaceable class="parameter">value</replaceable> [...]) ] [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2617 <term><literal>\gx [ (<replaceable class="parameter">option</replaceable>=<replaceable class="parameter">value</replaceable> [...]) ] [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
2618 <listitem>
2619 <para>
2620 <literal>\gx</literal> is equivalent to <literal>\g</literal>, except
2621 that it forces expanded output mode for this query, as
2622 if <literal>expanded=on</literal> were included in the list of
2623 <literal>\pset</literal> options. See also <literal>\x</literal>.
2624 </para>
2625 </listitem>
2626 </varlistentry>
2627
2628
2629 <varlistentry id="app-psql-meta-command-help">
2630 <term><literal>\h</literal> or <literal>\help</literal> <literal>[ <replaceable class="parameter">command</replaceable> ]</literal></term>
2631 <listitem>
2632 <para>
2633 Gives syntax help on the specified <acronym>SQL</acronym>
2634 command. If <replaceable class="parameter">command</replaceable>
2635 is not specified, then <application>psql</application> will list
2636 all the commands for which syntax help is available. If
2637 <replaceable class="parameter">command</replaceable> is an
2638 asterisk (<literal>*</literal>), then syntax help on all
2639 <acronym>SQL</acronym> commands is shown.
2640 </para>
2641
2642 <para>
2643 Unlike most other meta-commands, the entire remainder of the line is
2644 always taken to be the argument(s) of <command>\help</command>, and neither
2645 variable interpolation nor backquote expansion are performed in the
2646 arguments.
2647 </para>
2648
2649 <note>
2650 <para>
2651 To simplify typing, commands that consists of several words do
2652 not have to be quoted. Thus it is fine to type <userinput>\help
2653 alter table</userinput>.
2654 </para>
2655 </note>
2656 </listitem>
2657 </varlistentry>
2658
2659
2660 <varlistentry id="app-psql-meta-command-html">
2661 <term><literal>\H</literal> or <literal>\html</literal></term>
2662 <listitem>
2663 <para>
2664 Turns on <acronym>HTML</acronym> query output format. If the
2665 <acronym>HTML</acronym> format is already on, it is switched
2666 back to the default aligned text format. This command is for
2667 compatibility and convenience, but see <command>\pset</command>
2668 about setting other output options.
2669 </para>
2670 </listitem>
2671 </varlistentry>
2672
2673
2674 <varlistentry id="app-psql-meta-command-include">
2675 <term><literal>\i</literal> or <literal>\include</literal> <replaceable class="parameter">filename</replaceable></term>
2676 <listitem>
2677 <para>
2678 Reads input from the file <replaceable
2679 class="parameter">filename</replaceable> and executes it as
2680 though it had been typed on the keyboard.
2681 </para>
2682 <para>
2683 If <replaceable>filename</replaceable> is <literal>-</literal>
2684 (hyphen), then standard input is read until an EOF indication
2685 or <command>\q</command> meta-command. This can be used to intersperse
2686 interactive input with input from files. Note that Readline behavior
2687 will be used only if it is active at the outermost level.
2688 </para>
2689 <note>
2690 <para>
2691 If you want to see the lines on the screen as they are read you
2692 must set the variable <varname>ECHO</varname> to
2693 <literal>all</literal>.
2694 </para>
2695 </note>
2696 </listitem>
2697 </varlistentry>
2698
2699
2700 <varlistentry id="psql-metacommand-if">
2701 <term><literal>\if</literal> <replaceable class="parameter">expression</replaceable></term>
2702 <term><literal>\elif</literal> <replaceable class="parameter">expression</replaceable></term>
2703 <term><literal>\else</literal></term>
2704 <term><literal>\endif</literal></term>
2705 <listitem>
2706 <para>
2707 This group of commands implements nestable conditional blocks.
2708 A conditional block must begin with an <command>\if</command> and end
2709 with an <command>\endif</command>. In between there may be any number
2710 of <command>\elif</command> clauses, which may optionally be followed
2711 by a single <command>\else</command> clause. Ordinary queries and
2712 other types of backslash commands may (and usually do) appear between
2713 the commands forming a conditional block.
2714 </para>
2715 <para>
2716 The <command>\if</command> and <command>\elif</command> commands read
2717 their argument(s) and evaluate them as a Boolean expression. If the
2718 expression yields <literal>true</literal> then processing continues
2719 normally; otherwise, lines are skipped until a
2720 matching <command>\elif</command>, <command>\else</command>,
2721 or <command>\endif</command> is reached. Once
2722 an <command>\if</command> or <command>\elif</command> test has
2723 succeeded, the arguments of later <command>\elif</command> commands in
2724 the same block are not evaluated but are treated as false. Lines
2725 following an <command>\else</command> are processed only if no earlier
2726 matching <command>\if</command> or <command>\elif</command> succeeded.
2727 </para>
2728 <para>
2729 The <replaceable class="parameter">expression</replaceable> argument
2730 of an <command>\if</command> or <command>\elif</command> command
2731 is subject to variable interpolation and backquote expansion, just
2732 like any other backslash command argument. After that it is evaluated
2733 like the value of an on/off option variable. So a valid value
2734 is any unambiguous case-insensitive match for one of:
2735 <literal>true</literal>, <literal>false</literal>, <literal>1</literal>,
2736 <literal>0</literal>, <literal>on</literal>, <literal>off</literal>,
2737 <literal>yes</literal>, <literal>no</literal>. For example,
2738 <literal>t</literal>, <literal>T</literal>, and <literal>tR</literal>
2739 will all be considered to be <literal>true</literal>.
2740 </para>
2741 <para>
2742 Expressions that do not properly evaluate to true or false will
2743 generate a warning and be treated as false.
2744 </para>
2745 <para>
2746 Lines being skipped are parsed normally to identify queries and
2747 backslash commands, but queries are not sent to the server, and
2748 backslash commands other than conditionals
2749 (<command>\if</command>, <command>\elif</command>,
2750 <command>\else</command>, <command>\endif</command>) are
2751 ignored. Conditional commands are checked only for valid nesting.
2752 Variable references in skipped lines are not expanded, and backquote
2753 expansion is not performed either.
2754 </para>
2755 <para>
2756 All the backslash commands of a given conditional block must appear in
2757 the same source file. If EOF is reached on the main input file or an
2758 <command>\include</command>-ed file before all local
2759 <command>\if</command>-blocks have been closed,
2760 then <application>psql</application> will raise an error.
2761 </para>
2762 <para>
2763 Here is an example:
2764 </para>
2765 <programlisting>
2766 -- check for the existence of two separate records in the database and store
2767 -- the results in separate psql variables
2768 SELECT
2769 EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer,
2770 EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee
2771 \gset
2772 \if :is_customer
2773 SELECT * FROM customer WHERE customer_id = 123;
2774 \elif :is_employee
2775 \echo 'is not a customer but is an employee'
2776 SELECT * FROM employee WHERE employee_id = 456;
2777 \else
2778 \if yes
2779 \echo 'not a customer or employee'
2780 \else
2781 \echo 'this will never print'
2782 \endif
2783 \endif
2784 </programlisting>
2785 </listitem>
2786 </varlistentry>
2787
2788
2789 <varlistentry id="app-psql-meta-command-include-relative">
2790 <term><literal>\ir</literal> or <literal>\include_relative</literal> <replaceable class="parameter">filename</replaceable></term>
2791 <listitem>
2792 <para>
2793 The <literal>\ir</literal> command is similar to <literal>\i</literal>, but resolves
2794 relative file names differently. When executing in interactive mode,
2795 the two commands behave identically. However, when invoked from a
2796 script, <literal>\ir</literal> interprets file names relative to the
2797 directory in which the script is located, rather than the current
2798 working directory.
2799 </para>
2800 </listitem>
2801 </varlistentry>
2802
2803
2804 <varlistentry id="app-psql-meta-command-list">
2805 <term><literal>\l[x+]</literal> or <literal>\list[x+] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2806 <listitem>
2807 <para>
2808 List the databases in the server and show their names, owners,
2809 character set encodings, and access privileges.
2810 If <replaceable class="parameter">pattern</replaceable> is specified,
2811 only databases whose names match the pattern are listed.
2812 If <literal>x</literal> is appended to the command name, the results
2813 are displayed in expanded mode.
2814 If <literal>+</literal> is appended to the command name, database
2815 sizes, default tablespaces, and descriptions are also displayed.
2816 Size information is available for databases on which the current user has
2817 <literal>CONNECT</literal> privilege, or if the current user is a superuser
2818 or has privileges of the <literal>pg_read_all_stats</literal> role.
2819 </para>
2820 </listitem>
2821 </varlistentry>
2822
2823
2824 <varlistentry id="app-psql-meta-command-lo-export">
2825 <term><literal>\lo_export <replaceable class="parameter">loid</replaceable> <replaceable class="parameter">filename</replaceable></literal></term>
2826
2827 <listitem>
2828 <para>
2829 Reads the large object with <acronym>OID</acronym> <replaceable
2830 class="parameter">loid</replaceable> from the database and
2831 writes it to <replaceable
2832 class="parameter">filename</replaceable>. Note that this is
2833 subtly different from the server function
2834 <function>lo_export</function>, which acts with the permissions
2835 of the user that the database server runs as and on the server's
2836 file system.
2837 </para>
2838 <tip>
2839 <para>
2840 Use <command>\lo_list</command> to find out the large object's
2841 <acronym>OID</acronym>.
2842 </para>
2843 </tip>
2844 </listitem>
2845 </varlistentry>
2846
2847
2848 <varlistentry id="app-psql-meta-command-lo-import">
2849 <term><literal>\lo_import <replaceable class="parameter">filename</replaceable> [ <replaceable class="parameter">comment</replaceable> ]</literal></term>
2850
2851 <listitem>
2852 <para>
2853 Stores the file into a <productname>PostgreSQL</productname>
2854 large object. Optionally, it associates the given
2855 comment with the object. Example:
2856 <programlisting>
2857 foo=&gt; <userinput>\lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'</userinput>
2858 lo_import 152801
2859 </programlisting>
2860 The response indicates that the large object received object
2861 ID 152801, which can be used to access the newly-created large
2862 object in the future. For the sake of readability, it is
2863 recommended to always associate a human-readable comment with
2864 every object. Both OIDs and comments can be viewed with the
2865 <command>\lo_list</command> command.
2866 </para>
2867
2868 <para>
2869 Note that this command is subtly different from the server-side
2870 <function>lo_import</function> because it acts as the local user
2871 on the local file system, rather than the server's user and file
2872 system.
2873 </para>
2874 </listitem>
2875 </varlistentry>
2876
2877 <varlistentry id="app-psql-meta-command-lo-list">
2878 <term><literal>\lo_list[x+]</literal></term>
2879 <listitem>
2880 <para>
2881 Shows a list of all <productname>PostgreSQL</productname>
2882 large objects currently stored in the database,
2883 along with any comments provided for them.
2884 If <literal>x</literal> is appended to the command name, the results
2885 are displayed in expanded mode.
2886 If <literal>+</literal> is appended to the command name,
2887 each large object is listed with its associated permissions,
2888 if any.
2889 </para>
2890 </listitem>
2891 </varlistentry>
2892
2893 <varlistentry id="app-psql-meta-command-lo-unlink">
2894 <term><literal>\lo_unlink <replaceable class="parameter">loid</replaceable></literal></term>
2895
2896 <listitem>
2897 <para>
2898 Deletes the large object with <acronym>OID</acronym>
2899 <replaceable class="parameter">loid</replaceable> from the
2900 database.
2901 </para>
2902
2903 <tip>
2904 <para>
2905 Use <command>\lo_list</command> to find out the large object's
2906 <acronym>OID</acronym>.
2907 </para>
2908 </tip>
2909 </listitem>
2910 </varlistentry>
2911
2912
2913 <varlistentry id="app-psql-meta-command-out">
2914 <term><literal>\o</literal> or <literal>\out [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2915 <term><literal>\o</literal> or <literal>\out [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
2916 <listitem>
2917 <para>
2918 Arranges to save future query results to the file <replaceable
2919 class="parameter">filename</replaceable> or pipe future results
2920 to the shell command <replaceable
2921 class="parameter">command</replaceable>. If no argument is
2922 specified, the query output is reset to the standard output.
2923 </para>
2924
2925 <para>
2926 If the argument begins with <literal>|</literal>, then the entire remainder
2927 of the line is taken to be
2928 the <replaceable class="parameter">command</replaceable> to execute,
2929 and neither variable interpolation nor backquote expansion are
2930 performed in it. The rest of the line is simply passed literally to
2931 the shell.
2932 </para>
2933
2934 <para>
2935 <quote>Query results</quote> includes all tables, command
2936 responses, and notices obtained from the database server, as
2937 well as output of various backslash commands that query the
2938 database (such as <command>\d</command>); but not error
2939 messages.
2940 </para>
2941
2942 <tip>
2943 <para>
2944 To intersperse text output in between query results, use
2945 <command>\qecho</command>.
2946 </para>
2947 </tip>
2948 </listitem>
2949 </varlistentry>
2950
2951
2952 <varlistentry id="app-psql-meta-command-print">
2953 <term><literal>\p</literal> or <literal>\print</literal></term>
2954 <listitem>
2955 <para>
2956 Print the current query buffer to the standard output.
2957 If the current query buffer is empty, the most recently executed query
2958 is printed instead.
2959 </para>
2960 </listitem>
2961 </varlistentry>
2962
2963 <varlistentry id="app-psql-meta-command-parse">
2964 <term><literal>\parse <replaceable class="parameter">statement_name</replaceable></literal></term>
2965 <listitem>
2966 <para>
2967 Creates a prepared statement from the current query buffer, based on
2968 the name of a destination prepared-statement object. An empty string
2969 denotes the unnamed prepared statement.
2970 </para>
2971
2972 <para>
2973 Example:
2974 <programlisting>
2975 SELECT $1 \parse stmt1
2976 </programlisting>
2977 </para>
2978
2979 <para>
2980 This command causes the extended query protocol to be used, unlike
2981 normal <application>psql</application> operation, which uses the
2982 simple query protocol. A
2983 <xref linkend="protocol-message-formats-Parse"/>
2984 message will be issued by this command so it can be useful to
2985 test the extended query protocol from
2986 <application>psql</application>. This command affects only the next
2987 query executed; all subsequent queries will use the simple query
2988 protocol by default.
2989 </para>
2990
2991 </listitem>
2992 </varlistentry>
2993
2994 <varlistentry id="app-psql-meta-command-password">
2995 <term><literal>\password [ <replaceable class="parameter">username</replaceable> ]</literal></term>
2996 <listitem>
2997 <para>
2998 Changes the password of the specified user (by default, the current
2999 user). This command prompts for the new password, encrypts it, and
3000 sends it to the server as an <command>ALTER ROLE</command> command. This
3001 makes sure that the new password does not appear in cleartext in the
3002 command history, the server log, or elsewhere.
3003 </para>
3004 </listitem>
3005 </varlistentry>
3006
3007 <varlistentry id="app-psql-meta-command-prompt">
3008 <term><literal>\prompt [ <replaceable class="parameter">text</replaceable> ] <replaceable class="parameter">name</replaceable></literal></term>
3009 <listitem>
3010 <para>
3011 Prompts the user to supply text, which is assigned to the variable
3012 <replaceable class="parameter">name</replaceable>.
3013 An optional prompt string, <replaceable
3014 class="parameter">text</replaceable>, can be specified. (For multiword
3015 prompts, surround the text with single quotes.)
3016 </para>
3017
3018 <para>
3019 By default, <literal>\prompt</literal> uses the terminal for input and
3020 output. However, if the <option>-f</option> command line switch was
3021 used, <literal>\prompt</literal> uses standard input and standard output.
3022 </para>
3023 </listitem>
3024 </varlistentry>
3025
3026 <varlistentry id="app-psql-meta-command-pset">
3027 <term><literal>\pset [ <replaceable class="parameter">option</replaceable> [ <replaceable class="parameter">value</replaceable> ] ]</literal></term>
3028
3029 <listitem>
3030 <para>
3031 This command sets options affecting the output of query result tables.
3032 <replaceable class="parameter">option</replaceable>
3033 indicates which option is to be set. The semantics of
3034 <replaceable class="parameter">value</replaceable> vary depending
3035 on the selected option. For some options, omitting <replaceable
3036 class="parameter">value</replaceable> causes the option to be toggled
3037 or unset, as described under the particular option. If no such
3038 behavior is mentioned, then omitting
3039 <replaceable class="parameter">value</replaceable> just results in
3040 the current setting being displayed.
3041 </para>
3042
3043 <para>
3044 <command>\pset</command> without any arguments displays the current status
3045 of all printing options.
3046 </para>
3047
3048 <para>
3049 Adjustable printing options are:
3050 <variablelist>
3051 <varlistentry id="app-psql-meta-command-pset-border">
3052 <term><literal>border</literal></term>
3053 <listitem>
3054 <para>
3055 The <replaceable class="parameter">value</replaceable> must be a
3056 number. In general, the higher
3057 the number the more borders and lines the tables will have,
3058 but details depend on the particular format.
3059 In <acronym>HTML</acronym> format, this will translate directly
3060 into the <literal>border=...</literal> attribute.
3061 In most other formats only values 0 (no border), 1 (internal
3062 dividing lines), and 2 (table frame) make sense, and values above 2
3063 will be treated the same as <literal>border = 2</literal>.
3064 The <literal>latex</literal> and <literal>latex-longtable</literal>
3065 formats additionally allow a value of 3 to add dividing lines
3066 between data rows.
3067 </para>
3068 </listitem>
3069 </varlistentry>
3070
3071 <varlistentry id="app-psql-meta-command-pset-columns">
3072 <term><literal>columns</literal></term>
3073 <listitem>
3074 <para>
3075 Sets the target width for the <literal>wrapped</literal> format, and also
3076 the width limit for determining whether output is wide enough to
3077 require the pager or switch to the vertical display in expanded auto
3078 mode.
3079 Zero (the default) causes the target width to be controlled by the
3080 environment variable <envar>COLUMNS</envar>, or the detected screen width
3081 if <envar>COLUMNS</envar> is not set.
3082 In addition, if <literal>columns</literal> is zero then the
3083 <literal>wrapped</literal> format only affects screen output.
3084 If <literal>columns</literal> is nonzero then file and pipe output is
3085 wrapped to that width as well.
3086 </para>
3087 </listitem>
3088 </varlistentry>
3089
3090 <varlistentry id="app-psql-meta-command-pset-csv-fieldsep">
3091 <term><literal>csv_fieldsep</literal></term>
3092 <listitem>
3093 <para>
3094 Specifies the field separator to be used in
3095 <acronym>CSV</acronym> output format. If the separator character
3096 appears in a field's value, that field is output within double
3097 quotes, following standard <acronym>CSV</acronym> rules.
3098 The default is a comma.
3099 </para>
3100 </listitem>
3101 </varlistentry>
3102
3103 <varlistentry id="app-psql-meta-command-pset-expanded">
3104 <term><literal>expanded</literal> (or <literal>x</literal>)</term>
3105 <listitem>
3106 <para>
3107 If <replaceable class="parameter">value</replaceable> is specified it
3108 must be either <literal>on</literal> or <literal>off</literal>, which
3109 will enable or disable expanded mode, or <literal>auto</literal>.
3110 If <replaceable class="parameter">value</replaceable> is omitted the
3111 command toggles between the on and off settings. When expanded mode
3112 is enabled, query results are displayed in two columns, with the
3113 column name on the left and the data on the right. This mode is
3114 useful if the data wouldn't fit on the screen in the
3115 normal <quote>horizontal</quote> mode. In the auto setting, the
3116 expanded mode is used whenever the query output has more than one
3117 column and is wider than the screen; otherwise, the regular mode is
3118 used. The auto setting is only
3119 effective in the aligned and wrapped formats. In other formats, it
3120 always behaves as if the expanded mode is off.
3121 </para>
3122 </listitem>
3123 </varlistentry>
3124
3125 <varlistentry id="app-psql-meta-command-pset-fieldsep">
3126 <term><literal>fieldsep</literal></term>
3127 <listitem>
3128 <para>
3129 Specifies the field separator to be used in unaligned output
3130 format. That way one can create, for example, tab-separated
3131 output, which other programs might prefer. To
3132 set a tab as field separator, type <literal>\pset fieldsep
3133 '\t'</literal>. The default field separator is
3134 <literal>'|'</literal> (a vertical bar).
3135 </para>
3136 </listitem>
3137 </varlistentry>
3138
3139 <varlistentry id="app-psql-meta-command-pset-fieldsep-zero">
3140 <term><literal>fieldsep_zero</literal></term>
3141 <listitem>
3142 <para>
3143 Sets the field separator to use in unaligned output format to a zero
3144 byte.
3145 </para>
3146 </listitem>
3147 </varlistentry>
3148
3149 <varlistentry id="app-psql-meta-command-pset-footer">
3150 <term><literal>footer</literal></term>
3151 <listitem>
3152 <para>
3153 If <replaceable class="parameter">value</replaceable> is specified
3154 it must be either <literal>on</literal> or <literal>off</literal>
3155 which will enable or disable display of the table footer
3156 (the <literal>(<replaceable>n</replaceable> rows)</literal> count).
3157 If <replaceable class="parameter">value</replaceable> is omitted the
3158 command toggles footer display on or off.
3159 </para>
3160 </listitem>
3161 </varlistentry>
3162
3163 <varlistentry id="app-psql-meta-command-pset-format">
3164 <term><literal>format</literal></term>
3165 <listitem>
3166 <para>
3167 Sets the output format to one of <literal>aligned</literal>,
3168 <literal>asciidoc</literal>,
3169 <literal>csv</literal>,
3170 <literal>html</literal>,
3171 <literal>latex</literal>,
3172 <literal>latex-longtable</literal>, <literal>troff-ms</literal>,
3173 <literal>unaligned</literal>, or <literal>wrapped</literal>.
3174 Unique abbreviations are allowed.
3175 </para>
3176
3177 <para><literal>aligned</literal> format is the standard,
3178 human-readable, nicely formatted text output; this is the default.
3179 </para>
3180
3181 <para><literal>unaligned</literal> format writes all columns of a row on one
3182 line, separated by the currently active field separator. This
3183 is useful for creating output that might be intended to be read
3184 in by other programs, for example, tab-separated or comma-separated
3185 format. However, the field separator character is not treated
3186 specially if it appears in a column's value;
3187 so <acronym>CSV</acronym> format may be better suited for such
3188 purposes.
3189 </para>
3190
3191 <para><literal>csv</literal> format
3192 <indexterm>
3193 <primary>CSV (Comma-Separated Values) format</primary>
3194 <secondary>in psql</secondary>
3195 </indexterm>
3196 writes column values separated by commas, applying the quoting
3197 rules described in
3198 <ulink url="https://datatracker.ietf.org/doc/html/rfc4180">RFC 4180</ulink>.
3199 This output is compatible with the CSV format of the server's
3200 <command>COPY</command> command.
3201 A header line with column names is generated unless
3202 the <literal>tuples_only</literal> parameter is
3203 <literal>on</literal>. Titles and footers are not printed.
3204 Each row is terminated by the system-dependent end-of-line character,
3205 which is typically a single newline (<literal>\n</literal>) for
3206 Unix-like systems or a carriage return and newline sequence
3207 (<literal>\r\n</literal>) for Microsoft Windows.
3208 Field separator characters other than comma can be selected with
3209 <command>\pset csv_fieldsep</command>.
3210 </para>
3211
3212 <para><literal>wrapped</literal> format is like <literal>aligned</literal> but wraps
3213 wide data values across lines to make the output fit in the target
3214 column width. The target width is determined as described under
3215 the <literal>columns</literal> option. Note that <application>psql</application> will
3216 not attempt to wrap column header titles; therefore,
3217 <literal>wrapped</literal> format behaves the same as <literal>aligned</literal>
3218 if the total width needed for column headers exceeds the target.
3219 </para>
3220
3221 <para>
3222 The <literal>asciidoc</literal>, <literal>html</literal>,
3223 <literal>latex</literal>, <literal>latex-longtable</literal>, and
3224 <literal>troff-ms</literal> formats put out tables that are intended
3225 to be included in documents using the respective mark-up
3226 language. They are not complete documents! This might not be
3227 necessary in <acronym>HTML</acronym>, but in
3228 <application>LaTeX</application> you must have a complete
3229 document wrapper.
3230 The <literal>latex</literal> format
3231 uses <application>LaTeX</application>'s <literal>tabular</literal>
3232 environment.
3233 The <literal>latex-longtable</literal> format
3234 requires the <application>LaTeX</application>
3235 <literal>longtable</literal> and <literal>booktabs</literal> packages.
3236 </para>
3237 </listitem>
3238 </varlistentry>
3239
3240 <varlistentry id="app-psql-meta-command-pset-linestyle">
3241 <term><literal>linestyle</literal></term>
3242 <listitem>
3243 <para>
3244 Sets the border line drawing style to one
3245 of <literal>ascii</literal>, <literal>old-ascii</literal>,
3246 or <literal>unicode</literal>.
3247 Unique abbreviations are allowed. (That would mean one
3248 letter is enough.)
3249 The default setting is <literal>ascii</literal>.
3250 This option only affects the <literal>aligned</literal> and
3251 <literal>wrapped</literal> output formats.
3252 </para>
3253
3254 <para><literal>ascii</literal> style uses plain <acronym>ASCII</acronym>
3255 characters. Newlines in data are shown using
3256 a <literal>+</literal> symbol in the right-hand margin.
3257 When the <literal>wrapped</literal> format wraps data from
3258 one line to the next without a newline character, a dot
3259 (<literal>.</literal>) is shown in the right-hand margin of the first line,
3260 and again in the left-hand margin of the following line.
3261 </para>
3262
3263 <para><literal>old-ascii</literal> style uses plain <acronym>ASCII</acronym>
3264 characters, using the formatting style used
3265 in <productname>PostgreSQL</productname> 8.4 and earlier.
3266 Newlines in data are shown using a <literal>:</literal>
3267 symbol in place of the left-hand column separator.
3268 When the data is wrapped from one line
3269 to the next without a newline character, a <literal>;</literal>
3270 symbol is used in place of the left-hand column separator.
3271 </para>
3272
3273 <para><literal>unicode</literal> style uses Unicode box-drawing characters.
3274 Newlines in data are shown using a carriage return symbol
3275 in the right-hand margin. When the data is wrapped from one line
3276 to the next without a newline character, an ellipsis symbol
3277 is shown in the right-hand margin of the first line, and
3278 again in the left-hand margin of the following line.
3279 </para>
3280
3281 <para>
3282 When the <literal>border</literal> setting is greater than zero,
3283 the <literal>linestyle</literal> option also determines the
3284 characters with which the border lines are drawn.
3285 Plain <acronym>ASCII</acronym> characters work everywhere, but
3286 Unicode characters look nicer on displays that recognize them.
3287 </para>
3288 </listitem>
3289 </varlistentry>
3290
3291 <varlistentry id="app-psql-meta-command-pset-null">
3292 <term><literal>null</literal></term>
3293 <listitem>
3294 <para>
3295 Sets the string to be printed in place of a null value.
3296 The default is to print nothing, which can easily be mistaken for
3297 an empty string. For example, one might prefer <literal>\pset null
3298 '(null)'</literal>.
3299 </para>
3300 </listitem>
3301 </varlistentry>
3302
3303 <varlistentry id="app-psql-meta-command-pset-numericlocale">
3304 <term><literal>numericlocale</literal></term>
3305 <listitem>
3306 <para>
3307 If <replaceable class="parameter">value</replaceable> is specified
3308 it must be either <literal>on</literal> or <literal>off</literal>
3309 which will enable or disable display of a locale-specific character
3310 to separate groups of digits to the left of the decimal marker.
3311 If <replaceable class="parameter">value</replaceable> is omitted the
3312 command toggles between regular and locale-specific numeric output.
3313 </para>
3314 </listitem>
3315 </varlistentry>
3316
3317 <varlistentry id="app-psql-meta-command-pset-pager">
3318 <term><literal>pager</literal></term>
3319 <listitem>
3320 <para>
3321 Controls use of a pager program for query and <application>psql</application>
3322 help output.
3323 When the <literal>pager</literal> option is <literal>off</literal>, the pager
3324 program is not used. When the <literal>pager</literal> option is
3325 <literal>on</literal>, the pager is used when appropriate, i.e., when the
3326 output is to a terminal and will not fit on the screen.
3327 The <literal>pager</literal> option can also be set to <literal>always</literal>,
3328 which causes the pager to be used for all terminal output regardless
3329 of whether it fits on the screen. <literal>\pset pager</literal>
3330 without a <replaceable class="parameter">value</replaceable>
3331 toggles pager use on and off.
3332 </para>
3333
3334 <para>
3335 If the environment variable <envar>PSQL_PAGER</envar>
3336 or <envar>PAGER</envar> is set, output to be paged is piped to the
3337 specified program. Otherwise a platform-dependent default program
3338 (such as <filename>more</filename>) is used.
3339 </para>
3340
3341 <para>
3342 When using the <literal>\watch</literal> command to execute a query
3343 repeatedly, the environment variable <envar>PSQL_WATCH_PAGER</envar>
3344 is used to find the pager program instead, on Unix systems. This is
3345 configured separately because it may confuse traditional pagers, but
3346 can be used to send output to tools that understand
3347 <application>psql</application>'s output format (such as
3348 <filename>pspg --stream</filename>).
3349 </para>
3350 </listitem>
3351 </varlistentry>
3352
3353 <varlistentry id="app-psql-meta-command-pset-pager-min-lines">
3354 <term><literal>pager_min_lines</literal></term>
3355 <listitem>
3356 <para>
3357 If <literal>pager_min_lines</literal> is set to a number greater than the
3358 page height, the pager program will not be called unless there are
3359 at least this many lines of output to show. The default setting
3360 is 0.
3361 </para>
3362 </listitem>
3363 </varlistentry>
3364
3365 <varlistentry id="app-psql-meta-command-pset-recordsep">
3366 <term><literal>recordsep</literal></term>
3367 <listitem>
3368 <para>
3369 Specifies the record (line) separator to use in unaligned
3370 output format. The default is a newline character.
3371 </para>
3372 </listitem>
3373 </varlistentry>
3374
3375 <varlistentry id="app-psql-meta-command-pset-recordsep-zero">
3376 <term><literal>recordsep_zero</literal></term>
3377 <listitem>
3378 <para>
3379 Sets the record separator to use in unaligned output format to a zero
3380 byte.
3381 </para>
3382 </listitem>
3383 </varlistentry>
3384
3385 <varlistentry id="app-psql-meta-command-pset-tableattr">
3386 <term><literal>tableattr</literal> (or <literal>T</literal>)</term>
3387 <listitem>
3388 <para>
3389 In <acronym>HTML</acronym> format, this specifies attributes
3390 to be placed inside the <sgmltag>table</sgmltag> tag. This
3391 could for example be <literal>cellpadding</literal> or
3392 <literal>bgcolor</literal>. Note that you probably don't want
3393 to specify <literal>border</literal> here, as that is already
3394 taken care of by <literal>\pset border</literal>.
3395 If no
3396 <replaceable class="parameter">value</replaceable> is given,
3397 the table attributes are unset.
3398 </para>
3399 <para>
3400 In <literal>latex-longtable</literal> format, this controls
3401 the proportional width of each column containing a left-aligned
3402 data type. It is specified as a whitespace-separated list of values,
3403 e.g., <literal>'0.2 0.2 0.6'</literal>. Unspecified output columns
3404 use the last specified value.
3405 </para>
3406 </listitem>
3407 </varlistentry>
3408
3409 <varlistentry id="app-psql-meta-command-pset-title">
3410 <term><literal>title</literal> (or <literal>C</literal>)</term>
3411 <listitem>
3412 <para>
3413 Sets the table title for any subsequently printed tables. This
3414 can be used to give your output descriptive tags. If no
3415 <replaceable class="parameter">value</replaceable> is given,
3416 the title is unset.
3417 </para>
3418 </listitem>
3419 </varlistentry>
3420
3421 <varlistentry id="app-psql-meta-command-pset-tuples-only">
3422 <term><literal>tuples_only</literal> (or <literal>t</literal>)</term>
3423 <listitem>
3424 <para>
3425 If <replaceable class="parameter">value</replaceable> is specified
3426 it must be either <literal>on</literal> or <literal>off</literal>
3427 which will enable or disable tuples-only mode.
3428 If <replaceable class="parameter">value</replaceable> is omitted the
3429 command toggles between regular and tuples-only output.
3430 Regular output includes extra information such
3431 as column headers, titles, and various footers. In tuples-only
3432 mode, only actual table data is shown.
3433 </para>
3434 </listitem>
3435 </varlistentry>
3436
3437 <varlistentry id="app-psql-meta-command-pset-unicode-border-linestyle">
3438 <term><literal>unicode_border_linestyle</literal></term>
3439 <listitem>
3440 <para>
3441 Sets the border drawing style for the <literal>unicode</literal>
3442 line style to one of <literal>single</literal>
3443 or <literal>double</literal>.
3444 </para>
3445 </listitem>
3446 </varlistentry>
3447
3448 <varlistentry id="app-psql-meta-command-pset-unicode-column-linestyle">
3449 <term><literal>unicode_column_linestyle</literal></term>
3450 <listitem>
3451 <para>
3452 Sets the column drawing style for the <literal>unicode</literal>
3453 line style to one of <literal>single</literal>
3454 or <literal>double</literal>.
3455 </para>
3456 </listitem>
3457 </varlistentry>
3458
3459 <varlistentry id="app-psql-meta-command-pset-unicode-header-linestyle">
3460 <term><literal>unicode_header_linestyle</literal></term>
3461 <listitem>
3462 <para>
3463 Sets the header drawing style for the <literal>unicode</literal>
3464 line style to one of <literal>single</literal>
3465 or <literal>double</literal>.
3466 </para>
3467 </listitem>
3468 </varlistentry>
3469
3470 <varlistentry id="app-psql-meta-command-pset-xheader-width">
3471 <term><literal>xheader_width</literal></term>
3472 <listitem>
3473 <para>
3474 Sets the maximum width of the header for expanded output to one of
3475 <literal>full</literal> (the default value),
3476 <literal>column</literal>, <literal>page</literal>, or an
3477 <replaceable class="parameter">integer value</replaceable>.
3478 </para>
3479
3480 <para>
3481 <literal>full</literal>: the expanded header is not truncated,
3482 and will be as wide as the widest output line.
3483 </para>
3484
3485 <para>
3486 <literal>column</literal>: truncate the header line to the
3487 width of the first column.
3488 </para>
3489
3490 <para>
3491 <literal>page</literal>: truncate the header line to the terminal
3492 width.
3493 </para>
3494
3495 <para>
3496 <replaceable class="parameter">integer value</replaceable>: specify
3497 the exact maximum width of the header line.
3498 </para>
3499 </listitem>
3500 </varlistentry>
3501 </variablelist>
3502 </para>
3503
3504 <para>
3505 Illustrations of how these different formats look can be seen in
3506 <xref linkend="app-psql-examples"/>, below.
3507 </para>
3508
3509 <tip>
3510 <para>
3511 There are various shortcut commands for <command>\pset</command>. See
3512 <command>\a</command>, <command>\C</command>, <command>\f</command>,
3513 <command>\H</command>, <command>\t</command>, <command>\T</command>,
3514 and <command>\x</command>.
3515 </para>
3516 </tip>
3517
3518 </listitem>
3519 </varlistentry>
3520
3521
3522 <varlistentry id="app-psql-meta-command-quit">
3523 <term><literal>\q</literal> or <literal>\quit</literal></term>
3524 <listitem>
3525 <para>
3526 Quits the <application>psql</application> program.
3527 In a script file, only execution of that script is terminated.
3528 </para>
3529 </listitem>
3530 </varlistentry>
3531
3532
3533 <varlistentry id="app-psql-meta-command-qecho">
3534 <term><literal>\qecho <replaceable class="parameter">text</replaceable> [ ... ] </literal></term>
3535 <listitem>
3536 <para>
3537 This command is identical to <command>\echo</command> except
3538 that the output will be written to the query output channel, as
3539 set by <command>\o</command>.
3540 </para>
3541 </listitem>
3542 </varlistentry>
3543
3544
3545 <varlistentry id="app-psql-meta-command-reset">
3546 <term><literal>\r</literal> or <literal>\reset</literal></term>
3547 <listitem>
3548 <para>
3549 Resets (clears) the query buffer.
3550 </para>
3551 </listitem>
3552 </varlistentry>
3553
3554
3555 <varlistentry id="app-psql-meta-command-restrict">
3556 <term><literal>\restrict <replaceable class="parameter">restrict_key</replaceable></literal></term>
3557 <listitem>
3558 <para>
3559 Enter "restricted" mode with the provided key. In this mode, the only
3560 allowed meta-command is <command>\unrestrict</command>, to exit
3561 restricted mode. The key may contain only alphanumeric characters.
3562 </para>
3563 <para>
3564 This command is primarily intended for use in plain-text dumps
3565 generated by <application>pg_dump</application>,
3566 <application>pg_dumpall</application>, and
3567 <application>pg_restore</application>, but it may be useful elsewhere.
3568 </para>
3569 </listitem>
3570 </varlistentry>
3571
3572
3573 <varlistentry id="app-psql-meta-command-s">
3574 <term><literal>\s [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
3575 <listitem>
3576 <para>
3577 Print <application>psql</application>'s command line history
3578 to <replaceable class="parameter">filename</replaceable>.
3579 If <replaceable class="parameter">filename</replaceable> is omitted,
3580 the history is written to the standard output (using the pager if
3581 appropriate). This command is not available
3582 if <application>psql</application> was built
3583 without <application>Readline</application> support.
3584 </para>
3585 </listitem>
3586 </varlistentry>
3587
3588
3589 <varlistentry id="app-psql-meta-command-set">
3590 <term><literal>\set [ <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> [ ... ] ] ]</literal></term>
3591
3592 <listitem>
3593 <para>
3594 Sets the <application>psql</application> variable <replaceable
3595 class="parameter">name</replaceable> to <replaceable
3596 class="parameter">value</replaceable>, or if more than one value
3597 is given, to the concatenation of all of them. If only one
3598 argument is given, the variable is set to an empty-string value. To
3599 unset a variable, use the <command>\unset</command> command.
3600 </para>
3601
3602 <para><command>\set</command> without any arguments displays the names and values
3603 of all currently-set <application>psql</application> variables.
3604 </para>
3605
3606 <para>
3607 Valid variable names can contain letters, digits, and
3608 underscores. See <xref linkend="app-psql-variables"/> below for details.
3609 Variable names are case-sensitive.
3610 </para>
3611
3612 <para>
3613 Certain variables are special, in that they
3614 control <application>psql</application>'s behavior or are
3615 automatically set to reflect connection state. These variables are
3616 documented in <xref linkend="app-psql-variables"/>, below.
3617 </para>
3618
3619 <note>
3620 <para>
3621 This command is unrelated to the <acronym>SQL</acronym>
3622 command <link linkend="sql-set"><command>SET</command></link>.
3623 </para>
3624 </note>
3625 </listitem>
3626 </varlistentry>
3627
3628
3629 <varlistentry id="app-psql-meta-command-setenv">
3630 <term><literal>\setenv <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> ]</literal></term>
3631
3632 <listitem>
3633 <para>
3634 Sets the environment variable <replaceable
3635 class="parameter">name</replaceable> to <replaceable
3636 class="parameter">value</replaceable>, or if the
3637 <replaceable class="parameter">value</replaceable> is
3638 not supplied, unsets the environment variable. Example:
3639 <programlisting>
3640 testdb=&gt; <userinput>\setenv PAGER less</userinput>
3641 testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
3642 </programlisting></para>
3643 </listitem>
3644 </varlistentry>
3645
3646 <varlistentry id="app-psql-meta-command-sf">
3647 <term><literal>\sf[+] <replaceable class="parameter">function_description</replaceable> </literal></term>
3648
3649 <listitem>
3650 <para>
3651 This command fetches and shows the definition of the named function or procedure,
3652 in the form of a <command>CREATE OR REPLACE FUNCTION</command> or
3653 <command>CREATE OR REPLACE PROCEDURE</command> command.
3654 The definition is printed to the current query output channel,
3655 as set by <command>\o</command>.
3656 </para>
3657
3658 <para>
3659 The target function can be specified by name alone, or by name
3660 and arguments, for example <literal>foo(integer, text)</literal>.
3661 The argument types must be given if there is more
3662 than one function of the same name.
3663 </para>
3664
3665 <para>
3666 If <literal>+</literal> is appended to the command name, then the
3667 output lines are numbered, with the first line of the function body
3668 being line 1.
3669 </para>
3670
3671 <para>
3672 Unlike most other meta-commands, the entire remainder of the line is
3673 always taken to be the argument(s) of <command>\sf</command>, and neither
3674 variable interpolation nor backquote expansion are performed in the
3675 arguments.
3676 </para>
3677 </listitem>
3678 </varlistentry>
3679
3680
3681 <varlistentry id="app-psql-meta-command-sv">
3682 <term><literal>\sv[+] <replaceable class="parameter">view_name</replaceable> </literal></term>
3683
3684 <listitem>
3685 <para>
3686 This command fetches and shows the definition of the named view,
3687 in the form of a <command>CREATE OR REPLACE VIEW</command> command.
3688 The definition is printed to the current query output channel,
3689 as set by <command>\o</command>.
3690 </para>
3691
3692 <para>
3693 If <literal>+</literal> is appended to the command name, then the
3694 output lines are numbered from 1.
3695 </para>
3696
3697 <para>
3698 Unlike most other meta-commands, the entire remainder of the line is
3699 always taken to be the argument(s) of <command>\sv</command>, and neither
3700 variable interpolation nor backquote expansion are performed in the
3701 arguments.
3702 </para>
3703 </listitem>
3704 </varlistentry>
3705
3706 <varlistentry id="app-psql-meta-command-pipeline">
3707 <term><literal>\startpipeline</literal></term>
3708 <term><literal>\sendpipeline</literal></term>
3709 <term><literal>\syncpipeline</literal></term>
3710 <term><literal>\endpipeline</literal></term>
3711 <term><literal>\flushrequest</literal></term>
3712 <term><literal>\flush</literal></term>
3713 <term><literal>\getresults [ <replaceable class="parameter">number_results</replaceable> ]</literal></term>
3714
3715 <listitem>
3716 <para>
3717 This group of commands implements pipelining of SQL statements.
3718 A pipeline must begin with a <command>\startpipeline</command>
3719 and end with an <command>\endpipeline</command>. In between there
3720 may be any number of <command>\syncpipeline</command> commands,
3721 which sends a <link linkend="protocol-flow-ext-query">sync message</link>
3722 without ending the ongoing pipeline and flushing the send buffer.
3723 In pipeline mode, statements are sent to the server without waiting
3724 for the results of previous statements.
3725 See <xref linkend="libpq-pipeline-mode"/> for more details.
3726 </para>
3727
3728 <para>
3729 All queries executed while a pipeline is ongoing use the extended
3730 query protocol. Queries are appended to the pipeline when ending with
3731 a semicolon. The meta-commands <literal>\bind</literal>,
3732 <literal>\bind_named</literal>, <literal>\close_prepared</literal> or
3733 <literal>\parse</literal> can be used in an ongoing pipeline. While
3734 a pipeline is ongoing, <literal>\sendpipeline</literal> will append
3735 the current query buffer to the pipeline. Other meta-commands like
3736 <literal>\g</literal>, <literal>\gx</literal> or
3737 <literal>\gdesc</literal> are not allowed in pipeline mode.
3738 </para>
3739
3740 <para>
3741 <command>\flushrequest</command> appends a flush command to the
3742 pipeline, allowing to read results with
3743 <command>\getresults</command> without issuing a sync or ending the
3744 pipeline. <command>\getresults</command> will automatically push
3745 unsent data to the server. <command>\flush</command> can be used to
3746 manually push unsent data.
3747 </para>
3748
3749 <para>
3750 <command>\getresults</command> accepts an optional
3751 <replaceable class="parameter">number_results</replaceable> parameter.
3752 If provided, only the first
3753 <replaceable class="parameter">number_results</replaceable> pending
3754 results will be read. If not provided or <literal>0</literal>, all
3755 pending results are read.
3756 </para>
3757
3758 <para>
3759 When pipeline mode is active, a dedicated prompt variable is available
3760 to report the pipeline status.
3761 See <xref linkend="app-psql-prompting-p-uc"/> for more details
3762 </para>
3763
3764 <para>
3765 <command>COPY</command> is not supported while in pipeline mode.
3766 </para>
3767
3768 <para>
3769 Example:
3770 <programlisting>
3771 \startpipeline
3772 SELECT * FROM pg_class;
3773 SELECT 1 \bind \sendpipeline
3774 \flushrequest
3775 \getresults
3776 \endpipeline
3777 </programlisting>
3778 </para>
3779
3780 </listitem>
3781 </varlistentry>
3782
3783
3784 <varlistentry id="app-psql-meta-command-t-lc">
3785 <term><literal>\t</literal></term>
3786 <listitem>
3787 <para>
3788 Toggles the display of output column name headings and row count
3789 footer. This command is equivalent to <literal>\pset
3790 tuples_only</literal> and is provided for convenience.
3791 </para>
3792 </listitem>
3793 </varlistentry>
3794
3795
3796 <varlistentry id="app-psql-meta-command-t-uc">
3797 <term><literal>\T <replaceable class="parameter">table_options</replaceable></literal></term>
3798 <listitem>
3799 <para>
3800 Specifies attributes to be placed within the
3801 <sgmltag>table</sgmltag> tag in <acronym>HTML</acronym>
3802 output format. This command is equivalent to <literal>\pset
3803 tableattr <replaceable
3804 class="parameter">table_options</replaceable></literal>.
3805 </para>
3806 </listitem>
3807 </varlistentry>
3808
3809
3810 <varlistentry id="app-psql-meta-command-timing">
3811 <term><literal>\timing [ <replaceable class="parameter">on</replaceable> | <replaceable class="parameter">off</replaceable> ]</literal></term>
3812 <listitem>
3813 <para>
3814 With a parameter, turns displaying of how long each SQL statement
3815 takes on or off. Without a parameter, toggles the display between
3816 on and off. The display is in milliseconds; intervals longer than
3817 1 second are also shown in minutes:seconds format, with hours and
3818 days fields added if needed.
3819 </para>
3820 </listitem>
3821 </varlistentry>
3822
3823
3824 <varlistentry id="app-psql-meta-command-unrestrict">
3825 <term><literal>\unrestrict <replaceable class="parameter">restrict_key</replaceable></literal></term>
3826 <listitem>
3827 <para>
3828 Exit "restricted" mode (i.e., where all other meta-commands are
3829 blocked), provided the specified key matches the one given to
3830 <command>\restrict</command> when restricted mode was entered.
3831 </para>
3832 <para>
3833 This command is primarily intended for use in plain-text dumps
3834 generated by <application>pg_dump</application>,
3835 <application>pg_dumpall</application>, and
3836 <application>pg_restore</application>, but it may be useful elsewhere.
3837 </para>
3838 <para>
3839 Unlike most other meta-commands, the entire remainder of the line is
3840 always taken to be the argument of <command>\unrestrict</command>, and
3841 neither variable interpolation nor backquote expansion are performed.
3842 </para>
3843 </listitem>
3844 </varlistentry>
3845
3846
3847 <varlistentry id="app-psql-meta-command-unset">
3848 <term><literal>\unset <replaceable class="parameter">name</replaceable></literal></term>
3849
3850 <listitem>
3851 <para>
3852 Unsets (deletes) the <application>psql</application> variable <replaceable
3853 class="parameter">name</replaceable>.
3854 </para>
3855
3856 <para>
3857 Most variables that control <application>psql</application>'s behavior
3858 cannot be unset; instead, an <literal>\unset</literal> command is interpreted
3859 as setting them to their default values.
3860 See <xref linkend="app-psql-variables"/> below.
3861 </para>
3862 </listitem>
3863 </varlistentry>
3864
3865
3866 <varlistentry id="app-psql-meta-command-write">
3867 <term><literal>\w</literal> or <literal>\write</literal> <replaceable class="parameter">filename</replaceable></term>
3868 <term><literal>\w</literal> or <literal>\write</literal> <literal>|</literal><replaceable class="parameter">command</replaceable></term>
3869 <listitem>
3870 <para>
3871 Writes the current query buffer to the file <replaceable
3872 class="parameter">filename</replaceable> or pipes it to the shell
3873 command <replaceable class="parameter">command</replaceable>.
3874 If the current query buffer is empty, the most recently executed query
3875 is written instead.
3876 </para>
3877
3878 <para>
3879 If the argument begins with <literal>|</literal>, then the entire remainder
3880 of the line is taken to be
3881 the <replaceable class="parameter">command</replaceable> to execute,
3882 and neither variable interpolation nor backquote expansion are
3883 performed in it. The rest of the line is simply passed literally to
3884 the shell.
3885 </para>
3886 </listitem>
3887 </varlistentry>
3888
3889
3890 <varlistentry id="app-psql-meta-command-warn">
3891 <term><literal>\warn <replaceable class="parameter">text</replaceable> [ ... ]</literal></term>
3892 <listitem>
3893 <para>
3894 This command is identical to <command>\echo</command> except
3895 that the output will be written to <application>psql</application>'s
3896 standard error channel, rather than standard output.
3897 </para>
3898 </listitem>
3899 </varlistentry>
3900
3901
3902 <varlistentry id="app-psql-meta-command-watch">
3903 <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>
3904 <listitem>
3905 <para>
3906 Repeatedly execute the current query buffer (as <literal>\g</literal> does)
3907 until interrupted, or the query fails, or the execution count limit
3908 (if given) is reached, or the query no longer returns the minimum number
3909 of rows. Wait the specified number of seconds (default 2) between executions.
3910 The default wait can be changed with the variable
3911 <xref linkend="app-psql-variables-watch-interval"/>.
3912 For backwards compatibility,
3913 <replaceable class="parameter">seconds</replaceable> can be specified
3914 with or without an <literal>interval=</literal> prefix.
3915 Each query result is
3916 displayed with a header that includes the <literal>\pset title</literal>
3917 string (if any), the time as of query start, and the delay interval.
3918 </para>
3919 <para>
3920 If the current query buffer is empty, the most recently sent query
3921 is re-executed instead.
3922 </para>
3923 </listitem>
3924 </varlistentry>
3925
3926
3927 <varlistentry id="app-psql-meta-command-x">
3928 <term><literal>\x [ <replaceable class="parameter">on</replaceable> | <replaceable class="parameter">off</replaceable> | <replaceable class="parameter">auto</replaceable> ]</literal></term>
3929 <listitem>
3930 <para>
3931 Sets or toggles expanded table formatting mode. As such it is equivalent to
3932 <literal>\pset expanded</literal>.
3933 </para>
3934 </listitem>
3935 </varlistentry>
3936
3937
3938 <varlistentry id="app-psql-meta-command-z">
3939 <term><literal>\z[Sx] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
3940 <listitem>
3941 <para>
3942 Lists tables, views and sequences with their
3943 associated access privileges.
3944 If a <replaceable class="parameter">pattern</replaceable> is
3945 specified, only tables, views and sequences whose names match the
3946 pattern are listed. By default only user-created objects are shown;
3947 supply a pattern or the <literal>S</literal> modifier to include
3948 system objects.
3949 If <literal>x</literal> is appended to the command name, the results
3950 are displayed in expanded mode.
3951 </para>
3952
3953 <para>
3954 This is an alias for <command>\dp</command> (<quote>display
3955 privileges</quote>).
3956 </para>
3957 </listitem>
3958 </varlistentry>
3959
3960
3961 <varlistentry id="app-psql-meta-command-exclamation-mark">
3962 <term><literal>\! [ <replaceable class="parameter">command</replaceable> ]</literal></term>
3963 <listitem>
3964 <para>
3965 With no argument, escapes to a sub-shell; <application>psql</application>
3966 resumes when the sub-shell exits. With an argument, executes the
3967 shell command <replaceable class="parameter">command</replaceable>.
3968 </para>
3969
3970 <para>
3971 Unlike most other meta-commands, the entire remainder of the line is
3972 always taken to be the argument(s) of <command>\!</command>, and neither
3973 variable interpolation nor backquote expansion are performed in the
3974 arguments. The rest of the line is simply passed literally to the
3975 shell.
3976 </para>
3977 </listitem>
3978 </varlistentry>
3979
3980
3981 <varlistentry id="app-psql-meta-command-question-mark">
3982 <term><literal>\? [ <replaceable class="parameter">topic</replaceable> ]</literal></term>
3983 <listitem>
3984 <para>
3985 Shows help information. The optional
3986 <replaceable class="parameter">topic</replaceable> parameter
3987 (defaulting to <literal>commands</literal>) selects which part of <application>psql</application> is
3988 explained: <literal>commands</literal> describes <application>psql</application>'s
3989 backslash commands; <literal>options</literal> describes the command-line
3990 options that can be passed to <application>psql</application>;
3991 and <literal>variables</literal> shows help about <application>psql</application> configuration
3992 variables.
3993 </para>
3994 </listitem>
3995 </varlistentry>
3996
3997
3998 <varlistentry id="app-psql-meta-command-semicolon">
3999 <term><literal>\;</literal></term>
4000 <listitem>
4001 <para>
4002 Backslash-semicolon is not a meta-command in the same way as the
4003 preceding commands; rather, it simply causes a semicolon to be
4004 added to the query buffer without any further processing.
4005 </para>
4006
4007 <para>
4008 Normally, <application>psql</application> will dispatch an SQL command to the
4009 server as soon as it reaches the command-ending semicolon, even if
4010 more input remains on the current line. Thus for example entering
4011 <programlisting>
4012 select 1; select 2; select 3;
4013 </programlisting>
4014 will result in the three SQL commands being individually sent to
4015 the server, with each one's results being displayed before
4016 continuing to the next command. However, a semicolon entered
4017 as <literal>\;</literal> will not trigger command processing, so that the
4018 command before it and the one after are effectively combined and
4019 sent to the server in one request. So for example
4020 <programlisting>
4021 select 1\; select 2\; select 3;
4022 </programlisting>
4023 results in sending the three SQL commands to the server in a single
4024 request, when the non-backslashed semicolon is reached.
4025 The server executes such a request as a single transaction,
4026 unless there are explicit <command>BEGIN</command>/<command>COMMIT</command>
4027 commands included in the string to divide it into multiple
4028 transactions. (See <xref linkend="protocol-flow-multi-statement"/>
4029 for more details about how the server handles multi-query strings.)
4030 </para>
4031 </listitem>
4032 </varlistentry>
4033
4034 </variablelist>
4035 </para>
4036
4037 <refsect3 id="app-psql-patterns" xreflabel="Patterns">
4038 <title>Patterns</title>
4039
4040 <indexterm>
4041 <primary>patterns</primary>
4042 <secondary>in psql and pg_dump</secondary>
4043 </indexterm>
4044
4045 <para>
4046 The various <literal>\d</literal> commands accept a <replaceable
4047 class="parameter">pattern</replaceable> parameter to specify the
4048 object name(s) to be displayed. In the simplest case, a pattern
4049 is just the exact name of the object. The characters within a
4050 pattern are normally folded to lower case, just as in SQL names;
4051 for example, <literal>\dt FOO</literal> will display the table named
4052 <literal>foo</literal>. As in SQL names, placing double quotes around
4053 a pattern stops folding to lower case. Should you need to include
4054 an actual double quote character in a pattern, write it as a pair
4055 of double quotes within a double-quote sequence; again this is in
4056 accord with the rules for SQL quoted identifiers. For example,
4057 <literal>\dt "FOO""BAR"</literal> will display the table named
4058 <literal>FOO"BAR</literal> (not <literal>foo"bar</literal>). Unlike the normal
4059 rules for SQL names, you can put double quotes around just part
4060 of a pattern, for instance <literal>\dt FOO"FOO"BAR</literal> will display
4061 the table named <literal>fooFOObar</literal>.
4062 </para>
4063
4064 <para>
4065 Whenever the <replaceable class="parameter">pattern</replaceable> parameter
4066 is omitted completely, the <literal>\d</literal> commands display all objects
4067 that are visible in the current schema search path &mdash; this is
4068 equivalent to using <literal>*</literal> as the pattern.
4069 (An object is said to be <firstterm>visible</firstterm> if its
4070 containing schema is in the search path and no object of the same
4071 kind and name appears earlier in the search path. This is equivalent to the
4072 statement that the object can be referenced by name without explicit
4073 schema qualification.)
4074 To see all objects in the database regardless of visibility,
4075 use <literal>*.*</literal> as the pattern.
4076 </para>
4077
4078 <para>
4079 Within a pattern, <literal>*</literal> matches any sequence of characters
4080 (including no characters) and <literal>?</literal> matches any single character.
4081 (This notation is comparable to Unix shell file name patterns.)
4082 For example, <literal>\dt int*</literal> displays tables whose names
4083 begin with <literal>int</literal>. But within double quotes, <literal>*</literal>
4084 and <literal>?</literal> lose these special meanings and are just matched
4085 literally.
4086 </para>
4087
4088 <para>
4089 A relation pattern that contains a dot (<literal>.</literal>) is interpreted as a schema
4090 name pattern followed by an object name pattern. For example,
4091 <literal>\dt foo*.*bar*</literal> displays all tables whose table name
4092 includes <literal>bar</literal> that are in schemas whose schema name
4093 starts with <literal>foo</literal>. When no dot appears, then the pattern
4094 matches only objects that are visible in the current schema search path.
4095 Again, a dot within double quotes loses its special meaning and is matched
4096 literally. A relation pattern that contains two dots (<literal>.</literal>)
4097 is interpreted as a database name followed by a schema name pattern followed
4098 by an object name pattern. The database name portion will not be treated as
4099 a pattern and must match the name of the currently connected database, else
4100 an error will be raised.
4101 </para>
4102
4103 <para>
4104 A schema pattern that contains a dot (<literal>.</literal>) is interpreted
4105 as a database name followed by a schema name pattern. For example,
4106 <literal>\dn mydb.*foo*</literal> displays all schemas whose schema name
4107 includes <literal>foo</literal>. The database name portion will not be
4108 treated as a pattern and must match the name of the currently connected
4109 database, else an error will be raised.
4110 </para>
4111
4112 <para>
4113 Advanced users can use regular-expression notations such as character
4114 classes, for example <literal>[0-9]</literal> to match any digit. All regular
4115 expression special characters work as specified in
4116 <xref linkend="functions-posix-regexp"/>, except for <literal>.</literal> which
4117 is taken as a separator as mentioned above, <literal>*</literal> which is
4118 translated to the regular-expression notation <literal>.*</literal>,
4119 <literal>?</literal> which is translated to <literal>.</literal>, and
4120 <literal>$</literal> which is matched literally. You can emulate
4121 these pattern characters at need by writing
4122 <literal>?</literal> for <literal>.</literal>,
4123 <literal>(<replaceable class="parameter">R</replaceable>+|)</literal> for
4124 <literal><replaceable class="parameter">R</replaceable>*</literal>, or
4125 <literal>(<replaceable class="parameter">R</replaceable>|)</literal> for
4126 <literal><replaceable class="parameter">R</replaceable>?</literal>.
4127 <literal>$</literal> is not needed as a regular-expression character since
4128 the pattern must match the whole name, unlike the usual
4129 interpretation of regular expressions (in other words, <literal>$</literal>
4130 is automatically appended to your pattern). Write <literal>*</literal> at the
4131 beginning and/or end if you don't wish the pattern to be anchored.
4132 Note that within double quotes, all regular expression special characters
4133 lose their special meanings and are matched literally. Also, the regular
4134 expression special characters are matched literally in operator name
4135 patterns (i.e., the argument of <literal>\do</literal>).
4136 </para>
4137 </refsect3>
4138 </refsect2>
4139
4140 <refsect2>
4141 <title>Advanced Features</title>
4142
4143 <refsect3 id="app-psql-variables" xreflabel="Variables">
4144 <title>Variables</title>
4145
4146 <para>
4147 <application>psql</application> provides variable substitution
4148 features similar to common Unix command shells.
4149 Variables are simply name/value pairs, where the value
4150 can be any string of any length. The name must consist of letters
4151 (including non-Latin letters), digits, and underscores.
4152 </para>
4153
4154 <para>
4155 To set a variable, use the <application>psql</application> meta-command
4156 <command>\set</command>. For example,
4157 <programlisting>
4158 testdb=&gt; <userinput>\set foo bar</userinput>
4159 </programlisting>
4160 sets the variable <literal>foo</literal> to the value
4161 <literal>bar</literal>. To retrieve the content of the variable, precede
4162 the name with a colon, for example:
4163 <programlisting>
4164 testdb=&gt; <userinput>\echo :foo</userinput>
4165 bar
4166 </programlisting>
4167 This works in both regular SQL commands and meta-commands; there is
4168 more detail in <xref linkend="app-psql-interpolation"/>, below.
4169 </para>
4170
4171 <para>
4172 If you call <command>\set</command> without a second argument, the
4173 variable is set to an empty-string value. To unset (i.e., delete)
4174 a variable, use the command <command>\unset</command>. To show the
4175 values of all variables, call <command>\set</command> without any argument.
4176 </para>
4177
4178 <note>
4179 <para>
4180 The arguments of <command>\set</command> are subject to the same
4181 substitution rules as with other commands. Thus you can construct
4182 interesting references such as <literal>\set :foo
4183 'something'</literal> and get <quote>soft links</quote> or
4184 <quote>variable variables</quote> of <productname>Perl</productname>
4185 or <productname><acronym>PHP</acronym></productname> fame,
4186 respectively. Unfortunately (or fortunately?), there is no way to do
4187 anything useful with these constructs. On the other hand,
4188 <literal>\set bar :foo</literal> is a perfectly valid way to copy a
4189 variable.
4190 </para>
4191 </note>
4192
4193 <para>
4194 A number of these variables are treated specially
4195 by <application>psql</application>. They represent certain option
4196 settings that can be changed at run time by altering the value of
4197 the variable, or in some cases represent changeable state of
4198 <application>psql</application>.
4199 By convention, all specially treated variables' names
4200 consist of all upper-case ASCII letters (and possibly digits and
4201 underscores). To ensure maximum compatibility in the future, avoid
4202 using such variable names for your own purposes.
4203 </para>
4204
4205 <para>
4206 Variables that control <application>psql</application>'s behavior
4207 generally cannot be unset or set to invalid values. An <literal>\unset</literal>
4208 command is allowed but is interpreted as setting the variable to its
4209 default value. A <literal>\set</literal> command without a second argument is
4210 interpreted as setting the variable to <literal>on</literal>, for control
4211 variables that accept that value, and is rejected for others. Also,
4212 control variables that accept the values <literal>on</literal>
4213 and <literal>off</literal> will also accept other common spellings of Boolean
4214 values, such as <literal>true</literal> and <literal>false</literal>.
4215 </para>
4216
4217 <para>
4218 The specially treated variables are:
4219 </para>
4220
4221 <variablelist>
4222 <varlistentry id="app-psql-variables-autocommit">
4223 <term>
4224 <varname>AUTOCOMMIT</varname>
4225 <indexterm>
4226 <primary>autocommit</primary>
4227 <secondary>psql</secondary>
4228 </indexterm>
4229 </term>
4230 <listitem>
4231 <para>
4232 When <literal>on</literal> (the default), each SQL command is automatically
4233 committed upon successful completion. To postpone commit in this
4234 mode, you must enter a <command>BEGIN</command> or <command>START
4235 TRANSACTION</command> SQL command. When <literal>off</literal> or unset, SQL
4236 commands are not committed until you explicitly issue
4237 <command>COMMIT</command> or <command>END</command>. The autocommit-off
4238 mode works by issuing an implicit <command>BEGIN</command> for you, just
4239 before any command that is not already in a transaction block and
4240 is not itself a <command>BEGIN</command> or other transaction-control
4241 command, nor a command that cannot be executed inside a transaction
4242 block (such as <command>VACUUM</command>).
4243 </para>
4244
4245 <note>
4246 <para>
4247 In autocommit-off mode, you must explicitly abandon any failed
4248 transaction by entering <command>ABORT</command> or <command>ROLLBACK</command>.
4249 Also keep in mind that if you exit the session
4250 without committing, your work will be lost.
4251 </para>
4252 </note>
4253
4254 <note>
4255 <para>
4256 The autocommit-on mode is <productname>PostgreSQL</productname>'s traditional
4257 behavior, but autocommit-off is closer to the SQL spec. If you
4258 prefer autocommit-off, you might wish to set it in the system-wide
4259 <filename>psqlrc</filename> file or your
4260 <filename>~/.psqlrc</filename> file.
4261 </para>
4262 </note>
4263 </listitem>
4264 </varlistentry>
4265
4266 <varlistentry id="app-psql-variables-comp-keyword-case">
4267 <term><varname>COMP_KEYWORD_CASE</varname></term>
4268 <listitem>
4269 <para>
4270 Determines which letter case to use when completing an SQL key word.
4271 If set to <literal>lower</literal> or <literal>upper</literal>, the
4272 completed word will be in lower or upper case, respectively. If set
4273 to <literal>preserve-lower</literal>
4274 or <literal>preserve-upper</literal> (the default), the completed word
4275 will be in the case of the word already entered, but words being
4276 completed without anything entered will be in lower or upper case,
4277 respectively.
4278 </para>
4279 </listitem>
4280 </varlistentry>
4281
4282 <varlistentry id="app-psql-variables-dbname">
4283 <term><varname>DBNAME</varname></term>
4284 <listitem>
4285 <para>
4286 The name of the database you are currently connected to. This is
4287 set every time you connect to a database (including program
4288 start-up), but can be changed or unset.
4289 </para>
4290 </listitem>
4291 </varlistentry>
4292
4293 <varlistentry id="app-psql-variables-echo">
4294 <term><varname>ECHO</varname></term>
4295 <listitem>
4296 <para>
4297 If set to <literal>all</literal>, all nonempty input lines are printed
4298 to standard output as they are read. (This does not apply to lines
4299 read interactively.) To select this behavior on program
4300 start-up, use the switch <option>-a</option>. If set to
4301 <literal>queries</literal>,
4302 <application>psql</application> prints each query to standard output
4303 as it is sent to the server. The switch to select this behavior is
4304 <option>-e</option>. If set to <literal>errors</literal>, then only
4305 failed queries are displayed on standard error output. The switch
4306 for this behavior is <option>-b</option>. If set to
4307 <literal>none</literal> (the default), then no queries are displayed.
4308 </para>
4309 </listitem>
4310 </varlistentry>
4311
4312 <varlistentry id="app-psql-variables-echo-hidden">
4313 <term><varname>ECHO_HIDDEN</varname></term>
4314 <listitem>
4315 <para>
4316 When this variable is set to <literal>on</literal> and a backslash command
4317 queries the database, the query is first shown.
4318 This feature helps you to study
4319 <productname>PostgreSQL</productname> internals and provide
4320 similar functionality in your own programs. (To select this behavior
4321 on program start-up, use the switch <option>-E</option>.) If you set
4322 this variable to the value <literal>noexec</literal>, the queries are
4323 just shown but are not actually sent to the server and executed.
4324 The default value is <literal>off</literal>.
4325 </para>
4326 </listitem>
4327 </varlistentry>
4328
4329 <varlistentry id="app-psql-variables-encoding">
4330 <term><varname>ENCODING</varname></term>
4331 <listitem>
4332 <para>
4333 The current client character set encoding.
4334 This is set every time you connect to a database (including
4335 program start-up), and when you change the encoding
4336 with <literal>\encoding</literal>, but it can be changed or unset.
4337 </para>
4338 </listitem>
4339 </varlistentry>
4340
4341 <varlistentry id="app-psql-variables-error">
4342 <term><varname>ERROR</varname></term>
4343 <listitem>
4344 <para>
4345 <literal>true</literal> if the last SQL query failed, <literal>false</literal> if
4346 it succeeded. See also <varname>SQLSTATE</varname>.
4347 </para>
4348 </listitem>
4349 </varlistentry>
4350
4351 <varlistentry id="app-psql-variables-fetch-count">
4352 <term><varname>FETCH_COUNT</varname></term>
4353 <listitem>
4354 <para>
4355 If this variable is set to an integer value greater than zero,
4356 the results of <command>SELECT</command> queries are fetched
4357 and displayed in groups of that many rows, rather than the
4358 default behavior of collecting the entire result set before
4359 display. Therefore only a
4360 limited amount of memory is used, regardless of the size of
4361 the result set. Settings of 100 to 1000 are commonly used
4362 when enabling this feature.
4363 Keep in mind that when using this feature, a query might
4364 fail after having already displayed some rows.
4365 </para>
4366
4367 <tip>
4368 <para>
4369 Although you can use any output format with this feature,
4370 the default <literal>aligned</literal> format tends to look bad
4371 because each group of <varname>FETCH_COUNT</varname> rows
4372 will be formatted separately, leading to varying column
4373 widths across the row groups. The other output formats work better.
4374 </para>
4375 </tip>
4376 </listitem>
4377 </varlistentry>
4378
4379 <varlistentry id="app-psql-variables-hide-tableam">
4380 <term><varname>HIDE_TABLEAM</varname></term>
4381 <listitem>
4382 <para>
4383 If this variable is set to <literal>true</literal>, a table's access
4384 method details are not displayed. This is mainly useful for
4385 regression tests.
4386 </para>
4387 </listitem>
4388 </varlistentry>
4389
4390 <varlistentry id="app-psql-variables-hide-toast-compression">
4391 <term><varname>HIDE_TOAST_COMPRESSION</varname></term>
4392 <listitem>
4393 <para>
4394 If this variable is set to <literal>true</literal>, column
4395 compression method details are not displayed. This is mainly
4396 useful for regression tests.
4397 </para>
4398 </listitem>
4399 </varlistentry>
4400
4401 <varlistentry id="app-psql-variables-histcontrol">
4402 <term><varname>HISTCONTROL</varname></term>
4403 <listitem>
4404 <para>
4405 If this variable is set to <literal>ignorespace</literal>,
4406 lines which begin with a space are not entered into the history
4407 list. If set to a value of <literal>ignoredups</literal>, lines
4408 matching the previous history line are not entered. A value of
4409 <literal>ignoreboth</literal> combines the two options. If
4410 set to <literal>none</literal> (the default), all lines
4411 read in interactive mode are saved on the history list.
4412 </para>
4413 <note>
4414 <para>
4415 This feature was shamelessly plagiarized from
4416 <application>Bash</application>.
4417 </para>
4418 </note>
4419 </listitem>
4420 </varlistentry>
4421
4422 <varlistentry id="app-psql-variables-histfile">
4423 <term><varname>HISTFILE</varname></term>
4424 <listitem>
4425 <para>
4426 The file name that will be used to store the history list. If unset,
4427 the file name is taken from the <envar>PSQL_HISTORY</envar>
4428 environment variable. If that is not set either, the default
4429 is <filename>~/.psql_history</filename>,
4430 or <filename>%APPDATA%\postgresql\psql_history</filename> on Windows.
4431 For example, putting:
4432 <programlisting>
4433 \set HISTFILE ~/.psql_history-:DBNAME
4434 </programlisting>
4435 in <filename>~/.psqlrc</filename> will cause
4436 <application>psql</application> to maintain a separate history for
4437 each database.
4438 </para>
4439 <note>
4440 <para>
4441 This feature was shamelessly plagiarized from
4442 <application>Bash</application>.
4443 </para>
4444 </note>
4445 </listitem>
4446 </varlistentry>
4447
4448 <varlistentry id="app-psql-variables-histsize">
4449 <term><varname>HISTSIZE</varname></term>
4450 <listitem>
4451 <para>
4452 The maximum number of commands to store in the command history
4453 (default 500). If set to a negative value, no limit is applied.
4454 </para>
4455 <note>
4456 <para>
4457 This feature was shamelessly plagiarized from
4458 <application>Bash</application>.
4459 </para>
4460 </note>
4461 </listitem>
4462 </varlistentry>
4463
4464 <varlistentry id="app-psql-variables-host">
4465 <term><varname>HOST</varname></term>
4466 <listitem>
4467 <para>
4468 The database server host you are currently connected to. This is
4469 set every time you connect to a database (including program
4470 start-up), but can be changed or unset.
4471 </para>
4472 </listitem>
4473 </varlistentry>
4474
4475 <varlistentry id="app-psql-variables-ignoreeof">
4476 <term><varname>IGNOREEOF</varname></term>
4477 <listitem>
4478 <para>
4479 If set to 1 or less, sending an <acronym>EOF</acronym> character (usually
4480 <keycombo action="simul"><keycap>Control</keycap><keycap>D</keycap></keycombo>)
4481 to an interactive session of <application>psql</application>
4482 will terminate the application. If set to a larger numeric value,
4483 that many consecutive <acronym>EOF</acronym> characters must be typed to
4484 make an interactive session terminate. If the variable is set to a
4485 non-numeric value, it is interpreted as 10. The default is 0.
4486 </para>
4487 <note>
4488 <para>
4489 This feature was shamelessly plagiarized from
4490 <application>Bash</application>.
4491 </para>
4492 </note>
4493 </listitem>
4494 </varlistentry>
4495
4496 <varlistentry id="app-psql-variables-lastoid">
4497 <term><varname>LASTOID</varname></term>
4498 <listitem>
4499 <para>
4500 The value of the last affected OID, as returned from an
4501 <command>INSERT</command> or <command>\lo_import</command>
4502 command. This variable is only guaranteed to be valid until
4503 after the result of the next <acronym>SQL</acronym> command has
4504 been displayed.
4505 <productname>PostgreSQL</productname> servers since version 12 do not
4506 support OID system columns anymore, thus LASTOID will always be 0
4507 following <command>INSERT</command> when targeting such servers.
4508 </para>
4509 </listitem>
4510 </varlistentry>
4511
4512 <varlistentry id="app-psql-variables-last-error-message">
4513 <term><varname>LAST_ERROR_MESSAGE</varname></term>
4514 <term><varname>LAST_ERROR_SQLSTATE</varname></term>
4515 <listitem>
4516 <para>
4517 The primary error message and associated SQLSTATE code for the most
4518 recent failed query in the current <application>psql</application> session, or
4519 an empty string and <literal>00000</literal> if no error has occurred in
4520 the current session.
4521 </para>
4522 </listitem>
4523 </varlistentry>
4524
4525 <varlistentry id="app-psql-variables-on-error-rollback">
4526 <term>
4527 <varname>ON_ERROR_ROLLBACK</varname>
4528 <indexterm>
4529 <primary>rollback</primary>
4530 <secondary>psql</secondary>
4531 </indexterm>
4532 </term>
4533 <listitem>
4534 <para>
4535 When set to <literal>on</literal>, if a statement in a transaction block
4536 generates an error, the error is ignored and the transaction
4537 continues. When set to <literal>interactive</literal>, such errors are only
4538 ignored in interactive sessions, and not when reading script
4539 files. When set to <literal>off</literal> (the default), a statement in a
4540 transaction block that generates an error aborts the entire
4541 transaction. The error rollback mode works by issuing an
4542 implicit <command>SAVEPOINT</command> for you, just before each command
4543 that is in a transaction block, and then rolling back to the
4544 savepoint if the command fails.
4545 </para>
4546 </listitem>
4547 </varlistentry>
4548
4549 <varlistentry id="app-psql-variables-on-error-stop">
4550 <term><varname>ON_ERROR_STOP</varname></term>
4551 <listitem>
4552 <para>
4553 By default, command processing continues after an error. When this
4554 variable is set to <literal>on</literal>, processing will instead stop
4555 immediately. In interactive mode,
4556 <application>psql</application> will return to the command prompt;
4557 otherwise, <application>psql</application> will exit, returning
4558 error code 3 to distinguish this case from fatal error
4559 conditions, which are reported using error code 1. In either case,
4560 any currently running scripts (the top-level script, if any, and any
4561 other scripts which it may have in invoked) will be terminated
4562 immediately. If the top-level command string contained multiple SQL
4563 commands, processing will stop with the current command.
4564 </para>
4565 </listitem>
4566 </varlistentry>
4567
4568 <varlistentry id="app-psql-variables-pipeline-command-count">
4569 <term><varname>PIPELINE_COMMAND_COUNT</varname></term>
4570 <listitem>
4571 <para>
4572 The number of commands queued in an ongoing pipeline.
4573 </para>
4574 </listitem>
4575 </varlistentry>
4576
4577 <varlistentry id="app-psql-variables-pipeline-result-count">
4578 <term><varname>PIPELINE_RESULT_COUNT</varname></term>
4579 <listitem>
4580 <para>
4581 The number of commands of an ongoing pipeline that were followed
4582 by either a <command>\flushrequest</command> or a
4583 <command>\syncpipeline</command>, forcing the server to send the
4584 results. These results can be retrieved with
4585 <command>\getresults</command>.
4586 </para>
4587 </listitem>
4588 </varlistentry>
4589
4590 <varlistentry id="app-psql-variables-pipeline-sync-count">
4591 <term><varname>PIPELINE_SYNC_COUNT</varname></term>
4592 <listitem>
4593 <para>
4594 The number of sync messages queued in an ongoing pipeline.
4595 </para>
4596 </listitem>
4597 </varlistentry>
4598
4599 <varlistentry id="app-psql-variables-port">
4600 <term><varname>PORT</varname></term>
4601 <listitem>
4602 <para>
4603 The database server port to which you are currently connected.
4604 This is set every time you connect to a database (including
4605 program start-up), but can be changed or unset.
4606 </para>
4607 </listitem>
4608 </varlistentry>
4609
4610 <varlistentry id="app-psql-variables-prompt">
4611 <term><varname>PROMPT1</varname></term>
4612 <term><varname>PROMPT2</varname></term>
4613 <term><varname>PROMPT3</varname></term>
4614 <listitem>
4615 <para>
4616 These specify what the prompts <application>psql</application>
4617 issues should look like. See <xref
4618 linkend="app-psql-prompting"/> below.
4619 </para>
4620 </listitem>
4621 </varlistentry>
4622
4623 <varlistentry id="app-psql-variables-quiet">
4624 <term><varname>QUIET</varname></term>
4625 <listitem>
4626 <para>
4627 Setting this variable to <literal>on</literal> is equivalent to the command
4628 line option <option>-q</option>. It is probably not too useful in
4629 interactive mode.
4630 </para>
4631 </listitem>
4632 </varlistentry>
4633
4634 <varlistentry id="app-psql-variables-row-count">
4635 <term><varname>ROW_COUNT</varname></term>
4636 <listitem>
4637 <para>
4638 The number of rows returned or affected by the last SQL query, or 0
4639 if the query failed or did not report a row count.
4640 </para>
4641 </listitem>
4642 </varlistentry>
4643
4644 <varlistentry id="app-psql-variables-server-version-name">
4645 <term><varname>SERVER_VERSION_NAME</varname></term>
4646 <term><varname>SERVER_VERSION_NUM</varname></term>
4647 <listitem>
4648 <para>
4649 The server's version number as a string, for
4650 example <literal>9.6.2</literal>, <literal>10.1</literal> or <literal>11beta1</literal>,
4651 and in numeric form, for
4652 example <literal>90602</literal> or <literal>100001</literal>.
4653 These are set every time you connect to a database
4654 (including program start-up), but can be changed or unset.
4655 </para>
4656 </listitem>
4657 </varlistentry>
4658
4659 <varlistentry id="app-psql-variables-service">
4660 <term><varname>SERVICE</varname></term>
4661 <listitem>
4662 <para>
4663 The service name, if applicable.
4664 </para>
4665 </listitem>
4666 </varlistentry>
4667
4668 <varlistentry id="app-psql-variables-shell-error">
4669 <term><varname>SHELL_ERROR</varname></term>
4670 <listitem>
4671 <para>
4672 <literal>true</literal> if the last shell command
4673 failed, <literal>false</literal> if it succeeded.
4674 This applies to shell commands invoked via the <literal>\!</literal>,
4675 <literal>\g</literal>, <literal>\o</literal>, <literal>\w</literal>,
4676 and <literal>\copy</literal> meta-commands, as well as backquote
4677 (<literal>`</literal>) expansion. Note that
4678 for <literal>\o</literal>, this variable is updated when the output
4679 pipe is closed by the next <literal>\o</literal> command.
4680 See also <varname>SHELL_EXIT_CODE</varname>.
4681 </para>
4682 </listitem>
4683 </varlistentry>
4684
4685 <varlistentry id="app-psql-variables-shell-exit-code">
4686 <term><varname>SHELL_EXIT_CODE</varname></term>
4687 <listitem>
4688 <para>
4689 The exit status returned by the last shell command.
4690 0&ndash;127 represent program exit codes, 128&ndash;255
4691 indicate termination by a signal, and -1 indicates failure
4692 to launch a program or to collect its exit status.
4693 This applies to shell commands invoked via the <literal>\!</literal>,
4694 <literal>\g</literal>, <literal>\o</literal>, <literal>\w</literal>,
4695 and <literal>\copy</literal> meta-commands, as well as backquote
4696 (<literal>`</literal>) expansion. Note that
4697 for <literal>\o</literal>, this variable is updated when the output
4698 pipe is closed by the next <literal>\o</literal> command.
4699 See also <varname>SHELL_ERROR</varname>.
4700 </para>
4701 </listitem>
4702 </varlistentry>
4703
4704 <varlistentry id="app-psql-variables-show-all-results">
4705 <term><varname>SHOW_ALL_RESULTS</varname></term>
4706 <listitem>
4707 <para>
4708 When this variable is set to <literal>off</literal>, only the last
4709 result of a combined query (<literal>\;</literal>) is shown instead of
4710 all of them. The default is <literal>on</literal>. The off behavior
4711 is for compatibility with older versions of
4712 <application>psql</application>.
4713 </para>
4714 </listitem>
4715 </varlistentry>
4716
4717 <varlistentry id="app-psql-variables-show-context">
4718 <term><varname>SHOW_CONTEXT</varname></term>
4719 <listitem>
4720 <para>
4721 This variable can be set to the
4722 values <literal>never</literal>, <literal>errors</literal>, or <literal>always</literal>
4723 to control whether <literal>CONTEXT</literal> fields are displayed in
4724 messages from the server. The default is <literal>errors</literal> (meaning
4725 that context will be shown in error messages, but not in notice or
4726 warning messages). This setting has no effect
4727 when <varname>VERBOSITY</varname> is set to <literal>terse</literal>
4728 or <literal>sqlstate</literal>.
4729 (See also <command>\errverbose</command>, for use when you want a verbose
4730 version of the error you just got.)
4731 </para>
4732 </listitem>
4733 </varlistentry>
4734
4735 <varlistentry id="app-psql-variables-singleline">
4736 <term><varname>SINGLELINE</varname></term>
4737 <listitem>
4738 <para>
4739 Setting this variable to <literal>on</literal> is equivalent to the command
4740 line option <option>-S</option>.
4741 </para>
4742 </listitem>
4743 </varlistentry>
4744
4745 <varlistentry id="app-psql-variables-singlestep">
4746 <term><varname>SINGLESTEP</varname></term>
4747 <listitem>
4748 <para>
4749 Setting this variable to <literal>on</literal> is equivalent to the command
4750 line option <option>-s</option>.
4751 </para>
4752 </listitem>
4753 </varlistentry>
4754
4755 <varlistentry id="app-psql-variables-sqlstate">
4756 <term><varname>SQLSTATE</varname></term>
4757 <listitem>
4758 <para>
4759 The error code (see <xref linkend="errcodes-appendix"/>) associated
4760 with the last SQL query's failure, or <literal>00000</literal> if it
4761 succeeded.
4762 </para>
4763 </listitem>
4764 </varlistentry>
4765
4766 <varlistentry id="app-psql-variables-user">
4767 <term><varname>USER</varname></term>
4768 <listitem>
4769 <para>
4770 The database user you are currently connected as. This is set
4771 every time you connect to a database (including program
4772 start-up), but can be changed or unset.
4773 </para>
4774 </listitem>
4775 </varlistentry>
4776
4777 <varlistentry id="app-psql-variables-verbosity">
4778 <term><varname>VERBOSITY</varname></term>
4779 <listitem>
4780 <para>
4781 This variable can be set to the values <literal>default</literal>,
4782 <literal>verbose</literal>, <literal>terse</literal>,
4783 or <literal>sqlstate</literal> to control the verbosity of error
4784 reports.
4785 (See also <command>\errverbose</command>, for use when you want a verbose
4786 version of the error you just got.)
4787 </para>
4788 </listitem>
4789 </varlistentry>
4790
4791 <varlistentry id="app-psql-variables-version">
4792 <term><varname>VERSION</varname></term>
4793 <term><varname>VERSION_NAME</varname></term>
4794 <term><varname>VERSION_NUM</varname></term>
4795 <listitem>
4796 <para>
4797 These variables are set at program start-up to reflect
4798 <application>psql</application>'s version, respectively as a verbose string,
4799 a short string (e.g., <literal>9.6.2</literal>, <literal>10.1</literal>,
4800 or <literal>11beta1</literal>), and a number (e.g., <literal>90602</literal>
4801 or <literal>100001</literal>). They can be changed or unset.
4802 </para>
4803 </listitem>
4804 </varlistentry>
4805
4806 <varlistentry id="app-psql-variables-watch-interval">
4807 <term><varname>WATCH_INTERVAL</varname></term>
4808 <listitem>
4809 <para>
4810 This variable sets the default interval, in seconds, which
4811 <command>\watch</command> waits between executing the query. The
4812 default is 2 seconds. Specifying an interval in the command overrides
4813 this variable.
4814 </para>
4815 </listitem>
4816 </varlistentry>
4817
4818 </variablelist>
4819
4820 </refsect3>
4821
4822 <refsect3 id="app-psql-interpolation" xreflabel="SQL Interpolation">
4823 <title><acronym>SQL</acronym> Interpolation</title>
4824
4825 <para>
4826 A key feature of <application>psql</application>
4827 variables is that you can substitute (<quote>interpolate</quote>)
4828 them into regular <acronym>SQL</acronym> statements, as well as the
4829 arguments of meta-commands. Furthermore,
4830 <application>psql</application> provides facilities for
4831 ensuring that variable values used as SQL literals and identifiers are
4832 properly quoted. The syntax for interpolating a value without
4833 any quoting is to prepend the variable name with a colon
4834 (<literal>:</literal>). For example,
4835 <programlisting>
4836 testdb=&gt; <userinput>\set foo 'my_table'</userinput>
4837 testdb=&gt; <userinput>SELECT * FROM :foo;</userinput>
4838 </programlisting>
4839 would query the table <literal>my_table</literal>. Note that this
4840 may be unsafe: the value of the variable is copied literally, so it can
4841 contain unbalanced quotes, or even backslash commands. You must make sure
4842 that it makes sense where you put it.
4843 </para>
4844
4845 <para>
4846 When a value is to be used as an SQL literal or identifier, it is
4847 safest to arrange for it to be quoted. To quote the value of
4848 a variable as an SQL literal, write a colon followed by the variable
4849 name in single quotes. To quote the value as an SQL identifier, write
4850 a colon followed by the variable name in double quotes.
4851 These constructs deal correctly with quotes and other special
4852 characters embedded within the variable value.
4853 The previous example would be more safely written this way:
4854 <programlisting>
4855 testdb=&gt; <userinput>\set foo 'my_table'</userinput>
4856 testdb=&gt; <userinput>SELECT * FROM :"foo";</userinput>
4857 </programlisting>
4858 </para>
4859
4860 <para>
4861 Variable interpolation will not be performed within quoted
4862 <acronym>SQL</acronym> literals and identifiers. Therefore, a
4863 construction such as <literal>':foo'</literal> doesn't work to produce a quoted
4864 literal from a variable's value (and it would be unsafe if it did work,
4865 since it wouldn't correctly handle quotes embedded in the value).
4866 </para>
4867
4868 <para>
4869 One example use of this mechanism is to
4870 copy the contents of a file into a table column.
4871 First load the file into a variable and then interpolate the variable's
4872 value as a quoted string:
4873 <programlisting>
4874 testdb=&gt; <userinput>\set content `cat my_file.txt`</userinput>
4875 testdb=&gt; <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
4876 </programlisting>
4877 (Note that this still won't work if <filename>my_file.txt</filename> contains NUL bytes.
4878 <application>psql</application> does not support embedded NUL bytes in variable values.)
4879 </para>
4880
4881 <para>
4882 Since colons can legally appear in SQL commands, an apparent attempt
4883 at interpolation (that is, <literal>:name</literal>,
4884 <literal>:'name'</literal>, or <literal>:"name"</literal>) is not
4885 replaced unless the named variable is currently set. In any case, you
4886 can escape a colon with a backslash to protect it from substitution.
4887 </para>
4888
4889 <para>
4890 The <literal>:{?<replaceable>name</replaceable>}</literal> special syntax returns TRUE
4891 or FALSE depending on whether the variable exists or not, and is thus
4892 always substituted, unless the colon is backslash-escaped.
4893 </para>
4894
4895 <para>
4896 The colon syntax for variables is standard <acronym>SQL</acronym> for
4897 embedded query languages, such as <application>ECPG</application>.
4898 The colon syntaxes for array slices and type casts are
4899 <productname>PostgreSQL</productname> extensions, which can sometimes
4900 conflict with the standard usage. The colon-quote syntax for escaping a
4901 variable's value as an SQL literal or identifier is a
4902 <application>psql</application> extension.
4903 </para>
4904
4905 </refsect3>
4906
4907 <refsect3 id="app-psql-prompting" xreflabel="Prompting">
4908 <title>Prompting</title>
4909
4910 <para>
4911 The prompts <application>psql</application> issues can be customized
4912 to your preference. The three variables <varname>PROMPT1</varname>,
4913 <varname>PROMPT2</varname>, and <varname>PROMPT3</varname> contain strings
4914 and special escape sequences that describe the appearance of the
4915 prompt. Prompt 1 is the normal prompt that is issued when
4916 <application>psql</application> requests a new command. Prompt 2 is
4917 issued when more input is expected during command entry, for example
4918 because the command was not terminated with a semicolon or a quote
4919 was not closed.
4920 Prompt 3 is issued when you are running an <acronym>SQL</acronym>
4921 <command>COPY FROM STDIN</command> command and you need to type in
4922 a row value on the terminal.
4923 </para>
4924
4925 <para>
4926 The value of the selected prompt variable is printed literally,
4927 except where a percent sign (<literal>%</literal>) is encountered.
4928 Depending on the next character, certain other text is substituted
4929 instead. Defined substitutions are:
4930
4931 <variablelist>
4932 <varlistentry id="app-psql-prompting-m-uc">
4933 <term><literal>%M</literal></term>
4934 <listitem>
4935 <para>
4936 The full host name (with domain name) of the database server,
4937 or <literal>[local]</literal> if the connection is over a Unix
4938 domain socket, or
4939 <literal>[local:<replaceable>/dir/name</replaceable>]</literal>,
4940 if the Unix domain socket is not at the compiled in default
4941 location.
4942 </para>
4943 </listitem>
4944 </varlistentry>
4945
4946 <varlistentry id="app-psql-prompting-m-lc">
4947 <term><literal>%m</literal></term>
4948 <listitem>
4949 <para>
4950 The host name of the database server, truncated at the
4951 first dot, or <literal>[local]</literal> if the connection is
4952 over a Unix domain socket.
4953 </para>
4954 </listitem>
4955 </varlistentry>
4956
4957 <varlistentry id="app-psql-prompting-gt">
4958 <term><literal>%&gt;</literal></term>
4959 <listitem><para>The port number at which the database server is listening.</para></listitem>
4960 </varlistentry>
4961
4962 <varlistentry id="app-psql-prompting-n">
4963 <term><literal>%n</literal></term>
4964 <listitem>
4965 <para>
4966 The database session user name. (The expansion of this
4967 value might change during a database session as the result
4968 of the command <command>SET SESSION
4969 AUTHORIZATION</command>.)
4970 </para>
4971 </listitem>
4972 </varlistentry>
4973
4974 <varlistentry id="app-psql-prompting-s">
4975 <term><literal>%s</literal></term>
4976 <listitem><para>The name of the service.</para></listitem>
4977 </varlistentry>
4978
4979 <varlistentry id="app-psql-prompting-slash">
4980 <term><literal>%/</literal></term>
4981 <listitem><para>The name of the current database.</para></listitem>
4982 </varlistentry>
4983
4984 <varlistentry id="app-psql-prompting-tilde">
4985 <term><literal>%~</literal></term>
4986 <listitem><para>Like <literal>%/</literal>, but the output is <literal>~</literal>
4987 (tilde) if the database is your default database.</para></listitem>
4988 </varlistentry>
4989
4990 <varlistentry id="app-psql-prompting-numbersign">
4991 <term><literal>%#</literal></term>
4992 <listitem>
4993 <para>
4994 If the session user is a database superuser, then a
4995 <literal>#</literal>, otherwise a <literal>&gt;</literal>.
4996 (The expansion of this value might change during a database
4997 session as the result of the command <command>SET SESSION
4998 AUTHORIZATION</command>.)
4999 </para>
5000 </listitem>
5001 </varlistentry>
5002
5003 <varlistentry id="app-psql-prompting-p">
5004 <term><literal>%p</literal></term>
5005 <listitem>
5006 <para>The process ID of the backend currently connected to.</para>
5007 </listitem>
5008 </varlistentry>
5009
5010 <varlistentry id="app-psql-prompting-p-uc">
5011 <term><literal>%P</literal></term>
5012 <listitem>
5013 <para>
5014 Pipeline status: <literal>off</literal> when not in a pipeline,
5015 <literal>on</literal> when in an ongoing pipeline or
5016 <literal>abort</literal> when in an aborted pipeline.
5017 </para>
5018 </listitem>
5019 </varlistentry>
5020
5021 <varlistentry id="app-psql-prompting-r">
5022 <term><literal>%R</literal></term>
5023 <listitem>
5024 <para>
5025 In prompt 1 normally <literal>=</literal>,
5026 but <literal>@</literal> if the session is in an inactive branch of a
5027 conditional block, or <literal>^</literal> if in single-line mode,
5028 or <literal>!</literal> if the session is disconnected from the
5029 database (which can happen if <command>\connect</command> fails).
5030 In prompt 2 <literal>%R</literal> is replaced by a character that
5031 depends on why <application>psql</application> expects more input:
5032 <literal>-</literal> if the command simply wasn't terminated yet,
5033 but <literal>*</literal> if there is an unfinished
5034 <literal>/* ... */</literal> comment,
5035 a single quote if there is an unfinished quoted string,
5036 a double quote if there is an unfinished quoted identifier,
5037 a dollar sign if there is an unfinished dollar-quoted string,
5038 or <literal>(</literal> if there is an unmatched left parenthesis.
5039 In prompt 3 <literal>%R</literal> doesn't produce anything.
5040 </para>
5041 </listitem>
5042 </varlistentry>
5043
5044 <varlistentry id="app-psql-prompting-x">
5045 <term><literal>%x</literal></term>
5046 <listitem>
5047 <para>
5048 Transaction status: an empty string when not in a transaction
5049 block, or <literal>*</literal> when in a transaction block, or
5050 <literal>!</literal> when in a failed transaction block, or <literal>?</literal>
5051 when the transaction state is indeterminate (for example, because
5052 there is no connection).
5053 </para>
5054 </listitem>
5055 </varlistentry>
5056
5057 <varlistentry id="app-psql-prompting-l">
5058 <term><literal>%l</literal></term>
5059 <listitem>
5060 <para>
5061 The line number inside the current statement, starting from <literal>1</literal>.
5062 </para>
5063 </listitem>
5064 </varlistentry>
5065
5066 <varlistentry id="app-psql-prompting-digits">
5067 <term><literal>%</literal><replaceable class="parameter">digits</replaceable></term>
5068 <listitem>
5069 <para>
5070 The character with the indicated octal code is substituted.
5071 </para>
5072 </listitem>
5073 </varlistentry>
5074
5075 <varlistentry id="app-psql-prompting-name">
5076 <term><literal>%:</literal><replaceable class="parameter">name</replaceable><literal>:</literal></term>
5077 <listitem>
5078 <para>
5079 The value of the <application>psql</application> variable
5080 <replaceable class="parameter">name</replaceable>. See
5081 <xref linkend="app-psql-variables"/>, above, for details.
5082 </para>
5083 </listitem>
5084 </varlistentry>
5085
5086 <varlistentry id="app-psql-prompting-command">
5087 <term><literal>%`</literal><replaceable class="parameter">command</replaceable><literal>`</literal></term>
5088 <listitem>
5089 <para>
5090 The output of <replaceable
5091 class="parameter">command</replaceable>, similar to ordinary
5092 <quote>back-tick</quote> substitution.
5093 </para>
5094 </listitem>
5095 </varlistentry>
5096
5097 <varlistentry id="app-psql-prompting-square-brackets">
5098 <term><literal>%[</literal> ... <literal>%]</literal></term>
5099 <listitem>
5100 <para>
5101 Prompts can contain terminal control characters which, for
5102 example, change the color, background, or style of the prompt
5103 text, or change the title of the terminal window. In order for
5104 the line editing features of <application>Readline</application> to work properly, these
5105 non-printing control characters must be designated as invisible
5106 by surrounding them with <literal>%[</literal> and
5107 <literal>%]</literal>. Multiple pairs of these can occur within
5108 the prompt. For example:
5109 <programlisting>
5110 testdb=&gt; \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '
5111 </programlisting>
5112 results in a boldfaced (<literal>1;</literal>) yellow-on-black
5113 (<literal>33;40</literal>) prompt on VT100-compatible, color-capable
5114 terminals.
5115 </para>
5116 </listitem>
5117 </varlistentry>
5118
5119 <varlistentry id="app-psql-prompting-w">
5120 <term><literal>%w</literal></term>
5121 <listitem>
5122 <para>
5123 Whitespace of the same width as the most recent output of
5124 <varname>PROMPT1</varname>. This can be used as a
5125 <varname>PROMPT2</varname> setting, so that multi-line statements are
5126 aligned with the first line, but there is no visible secondary prompt.
5127 </para>
5128 </listitem>
5129 </varlistentry>
5130
5131 </variablelist>
5132
5133 To insert a percent sign into your prompt, write
5134 <literal>%%</literal>. The default prompts are
5135 <literal>'%/%R%x%# '</literal> for prompts 1 and 2, and
5136 <literal>'&gt;&gt; '</literal> for prompt 3.
5137 </para>
5138
5139 <note>
5140 <para>
5141 This feature was shamelessly plagiarized from
5142 <application>tcsh</application>.
5143 </para>
5144 </note>
5145
5146 </refsect3>
5147
5148 <refsect3 id="app-psql-readline">
5149 <title>Command-Line Editing</title>
5150
5151 <indexterm>
5152 <primary>Readline</primary>
5153 <secondary>in psql</secondary>
5154 </indexterm>
5155 <indexterm>
5156 <primary>libedit</primary>
5157 <secondary>in psql</secondary>
5158 </indexterm>
5159
5160 <para>
5161 <application>psql</application> uses
5162 the <application>Readline</application>
5163 or <application>libedit</application> library, if available, for
5164 convenient line editing and retrieval. The command history is
5165 automatically saved when <application>psql</application> exits and is
5166 reloaded when <application>psql</application> starts up. Type
5167 up-arrow or control-P to retrieve previous lines.
5168 </para>
5169
5170 <para>
5171 You can also use tab completion to fill in partially-typed keywords
5172 and SQL object names in many (by no means all) contexts. For example,
5173 at the start of a command, typing <literal>ins</literal> and pressing
5174 TAB will fill in <literal>insert into </literal>. Then, typing a few
5175 characters of a table or schema name and pressing <literal>TAB</literal>
5176 will fill in the unfinished name, or offer a menu of possible completions
5177 when there's more than one. (Depending on the library in use, you may need to
5178 press <literal>TAB</literal> more than once to get a menu.)
5179 </para>
5180
5181 <para>
5182 Tab completion for SQL object names requires sending queries to the
5183 server to find possible matches. In some contexts this can interfere
5184 with other operations. For example, after <command>BEGIN</command>
5185 it will be too late to issue <command>SET TRANSACTION ISOLATION
5186 LEVEL</command> if a tab-completion query is issued in between.
5187 If you do not want tab completion at all, you
5188 can turn it off permanently by putting this in a file named
5189 <filename>.inputrc</filename> in your home directory:
5190 <programlisting>
5191 $if psql
5192 set disable-completion on
5193 $endif
5194 </programlisting>
5195 (This is not a <application>psql</application> but a
5196 <application>Readline</application> feature. Read its documentation
5197 for further details.)
5198 </para>
5199
5200 <para>
5201 The <option>-n</option> (<option>--no-readline</option>) command line
5202 option can also be useful to disable use
5203 of <application>Readline</application> for a single run
5204 of <application>psql</application>. This prevents tab completion,
5205 use or recording of command line history, and editing of multi-line
5206 commands. It is particularly useful when you need to copy-and-paste
5207 text that contains <literal>TAB</literal> characters.
5208 </para>
5209 </refsect3>
5210 </refsect2>
5211 </refsect1>
5212
5213
5214 <refsect1 id="app-psql-environment" xreflabel="Environment">
5215 <title>Environment</title>
5216
5217 <variablelist>
5218
5219 <varlistentry id="app-psql-environment-columns">
5220 <term><envar>COLUMNS</envar></term>
5221
5222 <listitem>
5223 <para>
5224 If <literal>\pset columns</literal> is zero, controls the
5225 width for the <literal>wrapped</literal> format and width for determining
5226 if wide output requires the pager or should be switched to the
5227 vertical format in expanded auto mode.
5228 </para>
5229 </listitem>
5230 </varlistentry>
5231
5232 <varlistentry id="app-psql-environment-pgdatabase">
5233 <term><envar>PGDATABASE</envar></term>
5234 <term><envar>PGHOST</envar></term>
5235 <term><envar>PGPORT</envar></term>
5236 <term><envar>PGUSER</envar></term>
5237
5238 <listitem>
5239 <para>
5240 Default connection parameters (see <xref linkend="libpq-envars"/>).
5241 </para>
5242 </listitem>
5243 </varlistentry>
5244
5245 <varlistentry id="app-psql-environment-pg-color">
5246 <term><envar>PG_COLOR</envar></term>
5247 <listitem>
5248 <para>
5249 Specifies whether to use color in diagnostic messages. Possible values
5250 are <literal>always</literal>, <literal>auto</literal> and
5251 <literal>never</literal>.
5252 </para>
5253 </listitem>
5254 </varlistentry>
5255
5256 <varlistentry id="app-psql-environment-psql-editor">
5257 <term><envar>PSQL_EDITOR</envar></term>
5258 <term><envar>EDITOR</envar></term>
5259 <term><envar>VISUAL</envar></term>
5260
5261 <listitem>
5262 <para>
5263 Editor used by the <command>\e</command>, <command>\ef</command>,
5264 and <command>\ev</command> commands.
5265 These variables are examined in the order listed;
5266 the first that is set is used.
5267 If none of them is set, the default is to use <filename>vi</filename>
5268 on Unix systems or <filename>notepad.exe</filename> on Windows systems.
5269 </para>
5270 </listitem>
5271 </varlistentry>
5272
5273 <varlistentry id="app-psql-environment-psql-editor-linenumber-arg">
5274 <term><envar>PSQL_EDITOR_LINENUMBER_ARG</envar></term>
5275
5276 <listitem>
5277 <para>
5278 When <command>\e</command>, <command>\ef</command>, or
5279 <command>\ev</command> is used
5280 with a line number argument, this variable specifies the
5281 command-line argument used to pass the starting line number to
5282 the user's editor. For editors such as <productname>Emacs</productname> or
5283 <productname>vi</productname>, this is a plus sign. Include a trailing
5284 space in the value of the variable if there needs to be space
5285 between the option name and the line number. Examples:
5286 <programlisting>
5287 PSQL_EDITOR_LINENUMBER_ARG='+'
5288 PSQL_EDITOR_LINENUMBER_ARG='--line '
5289 </programlisting>
5290 </para>
5291
5292 <para>
5293 The default is <literal>+</literal> on Unix systems
5294 (corresponding to the default editor <filename>vi</filename>,
5295 and useful for many other common editors); but there is no
5296 default on Windows systems.
5297 </para>
5298 </listitem>
5299 </varlistentry>
5300
5301 <varlistentry id="app-psql-environment-psql-history">
5302 <term><envar>PSQL_HISTORY</envar></term>
5303
5304 <listitem>
5305 <para>
5306 Alternative location for the command history file. Tilde (<literal>~</literal>) expansion is performed.
5307 </para>
5308 </listitem>
5309 </varlistentry>
5310
5311 <varlistentry id="app-psql-environment-pager">
5312 <term><envar>PSQL_PAGER</envar></term>
5313 <term><envar>PAGER</envar></term>
5314
5315 <listitem>
5316 <para>
5317 If a query's results do not fit on the screen, they are piped
5318 through this command. Typical values are <literal>more</literal>
5319 or <literal>less</literal>.
5320 Use of the pager can be disabled by setting <envar>PSQL_PAGER</envar>
5321 or <envar>PAGER</envar> to an empty string, or by adjusting the
5322 pager-related options of the <command>\pset</command> command.
5323 These variables are examined in the order listed;
5324 the first that is set is used.
5325 If neither of them is set, the default is to use <literal>more</literal> on most
5326 platforms, but <literal>less</literal> on Cygwin.
5327 </para>
5328
5329 </listitem>
5330 </varlistentry>
5331
5332 <varlistentry id="app-psql-environment-psql-watch-pager">
5333 <term><envar>PSQL_WATCH_PAGER</envar></term>
5334
5335 <listitem>
5336 <para>
5337 When a query is executed repeatedly with the <command>\watch</command>
5338 command, a pager is not used by default. This behavior can be changed
5339 by setting <envar>PSQL_WATCH_PAGER</envar> to a pager command, on Unix
5340 systems. The <literal>pspg</literal> pager (not part of
5341 <productname>PostgreSQL</productname> but available in many open source
5342 software distributions) can display the output of
5343 <command>\watch</command> if started with the option
5344 <literal>--stream</literal>.
5345 </para>
5346
5347 </listitem>
5348 </varlistentry>
5349
5350 <varlistentry id="app-psql-environment-psqlrc">
5351 <term><envar>PSQLRC</envar></term>
5352
5353 <listitem>
5354 <para>
5355 Alternative location of the user's <filename>.psqlrc</filename> file. Tilde (<literal>~</literal>) expansion is performed.
5356 </para>
5357 </listitem>
5358 </varlistentry>
5359
5360 <varlistentry id="app-psql-environment-shell">
5361 <term><envar>SHELL</envar></term>
5362
5363 <listitem>
5364 <para>
5365 Command executed by the <command>\!</command> command.
5366 </para>
5367 </listitem>
5368 </varlistentry>
5369
5370 <varlistentry id="app-psql-environment-tmpdir">
5371 <term><envar>TMPDIR</envar></term>
5372
5373 <listitem>
5374 <para>
5375 Directory for storing temporary files. The default is
5376 <filename>/tmp</filename>.
5377 </para>
5378 </listitem>
5379 </varlistentry>
5380 </variablelist>
5381
5382 <para>
5383 This utility, like most other <productname>PostgreSQL</productname> utilities,
5384 also uses the environment variables supported by <application>libpq</application>
5385 (see <xref linkend="libpq-envars"/>).
5386 </para>
5387
5388 </refsect1>
5389
5390
5391 <refsect1>
5392 <title>Files</title>
5393
5394 <variablelist>
5395 <varlistentry id="app-psql-files-psqlrc">
5396 <term><filename>psqlrc</filename> and <filename>~/.psqlrc</filename></term>
5397 <listitem>
5398 <para>
5399 Unless it is passed an <option>-X</option> option,
5400 <application>psql</application> attempts to read and execute commands
5401 from the system-wide startup file (<filename>psqlrc</filename>) and then
5402 the user's personal startup file (<filename>~/.psqlrc</filename>), after
5403 connecting to the database but before accepting normal commands.
5404 These files can be used to set up the client and/or the server to taste,
5405 typically with <command>\set</command> and <command>SET</command>
5406 commands.
5407 </para>
5408 <para>
5409 The system-wide startup file is named <filename>psqlrc</filename>.
5410 By default it is
5411 sought in the installation's <quote>system configuration</quote> directory,
5412 which is most reliably identified by running <literal>pg_config
5413 --sysconfdir</literal>.
5414 Typically this directory will be <filename>../etc/</filename>
5415 relative to the directory containing
5416 the <productname>PostgreSQL</productname> executables.
5417 The directory to look in can be set explicitly via
5418 the <envar>PGSYSCONFDIR</envar> environment variable.
5419 </para>
5420 <para>
5421 The user's personal startup file is named <filename>.psqlrc</filename>
5422 and is sought in the invoking user's home directory.
5423 On Windows the personal startup file is instead named
5424 <filename>%APPDATA%\postgresql\psqlrc.conf</filename>.
5425 In either case, this default file path can be overridden by setting
5426 the <envar>PSQLRC</envar> environment variable.
5427 </para>
5428 <para>
5429 Both the system-wide startup file and the user's personal startup file
5430 can be made <application>psql</application>-version-specific
5431 by appending a dash and the <productname>PostgreSQL</productname>
5432 major or minor release identifier to the file name,
5433 for example <filename>~/.psqlrc-&majorversion;</filename> or
5434 <filename>~/.psqlrc-&version;</filename>.
5435 The most specific version-matching file will be read in preference
5436 to a non-version-specific file.
5437 These version suffixes are added after determining the file path
5438 as explained above.
5439 </para>
5440 </listitem>
5441 </varlistentry>
5442
5443 <varlistentry id="app-psql-files-psql-history">
5444 <term><filename>.psql_history</filename></term>
5445 <listitem>
5446 <para>
5447 The command-line history is stored in the file
5448 <filename>~/.psql_history</filename>, or
5449 <filename>%APPDATA%\postgresql\psql_history</filename> on Windows.
5450 </para>
5451 <para>
5452 The location of the history file can be set explicitly via
5453 the <varname>HISTFILE</varname> <application>psql</application> variable or
5454 the <envar>PSQL_HISTORY</envar> environment variable.
5455 </para>
5456 </listitem>
5457 </varlistentry>
5458 </variablelist>
5459 </refsect1>
5460
5461
5462 <refsect1>
5463 <title>Notes</title>
5464
5465 <itemizedlist>
5466 <listitem>
5467 <para><application>psql</application> works best with servers of the same
5468 or an older major version. Backslash commands are particularly likely
5469 to fail if the server is of a newer version than <application>psql</application>
5470 itself. However, backslash commands of the <literal>\d</literal> family should
5471 work with servers of versions back to 9.2, though not necessarily with
5472 servers newer than <application>psql</application> itself. The general
5473 functionality of running SQL commands and displaying query results
5474 should also work with servers of a newer major version, but this cannot
5475 be guaranteed in all cases.
5476 </para>
5477 <para>
5478 If you want to use <application>psql</application> to connect to several
5479 servers of different major versions, it is recommended that you use the
5480 newest version of <application>psql</application>. Alternatively, you
5481 can keep around a copy of <application>psql</application> from each
5482 major version and be sure to use the version that matches the
5483 respective server. But in practice, this additional complication should
5484 not be necessary.
5485 </para>
5486 </listitem>
5487
5488 <listitem>
5489 <para>
5490 Before <productname>PostgreSQL</productname> 9.6,
5491 the <option>-c</option> option implied <option>-X</option>
5492 (<option>--no-psqlrc</option>); this is no longer the case.
5493 </para>
5494 </listitem>
5495
5496 <listitem>
5497 <para>
5498 Before <productname>PostgreSQL</productname> 8.4,
5499 <application>psql</application> allowed the
5500 first argument of a single-letter backslash command to start
5501 directly after the command, without intervening whitespace.
5502 Now, some whitespace is required.
5503 </para>
5504 </listitem>
5505 </itemizedlist>
5506 </refsect1>
5507
5508
5509 <refsect1>
5510 <title>Notes for Windows Users</title>
5511
5512 <para>
5513 <application>psql</application> is built as a <quote>console
5514 application</quote>. Since the Windows console windows use a different
5515 encoding than the rest of the system, you must take special care
5516 when using 8-bit characters within <application>psql</application>.
5517 If <application>psql</application> detects a problematic
5518 console code page, it will warn you at startup. To change the
5519 console code page, two things are necessary:
5520
5521 <itemizedlist>
5522 <listitem>
5523 <para>
5524 Set the code page by entering <userinput>cmd.exe /c chcp
5525 1252</userinput>. (1252 is a code page that is appropriate for
5526 German; replace it with your value.) If you are using Cygwin,
5527 you can put this command in <filename>/etc/profile</filename>.
5528 </para>
5529 </listitem>
5530
5531 <listitem>
5532 <para>
5533 Set the console font to <literal>Lucida Console</literal>, because the
5534 raster font does not work with the ANSI code page.
5535 </para>
5536 </listitem>
5537 </itemizedlist></para>
5538
5539 </refsect1>
5540
5541
5542 <refsect1 id="app-psql-examples" xreflabel="Examples">
5543 <title>Examples</title>
5544
5545 <para>
5546 The first example shows how to spread a command over several lines of
5547 input. Notice the changing prompt:
5548 <programlisting>
5549 testdb=&gt; <userinput>CREATE TABLE my_table (</userinput>
5550 testdb(&gt; <userinput> first integer not null default 0,</userinput>
5551 testdb(&gt; <userinput> second text)</userinput>
5552 testdb-&gt; <userinput>;</userinput>
5553 CREATE TABLE
5554 </programlisting>
5555 Now look at the table definition again:
5556 <programlisting>
5557 testdb=&gt; <userinput>\d my_table</userinput>
5558 Table "public.my_table"
5559 Column | Type | Collation | Nullable | Default
5560 --------+---------+-----------+----------+---------
5561 first | integer | | not null | 0
5562 second | text | | |
5563 </programlisting>
5564 Now we change the prompt to something more interesting:
5565 <programlisting>
5566 testdb=&gt; <userinput>\set PROMPT1 '%n@%m %~%R%# '</userinput>
5567 peter@localhost testdb=&gt;
5568 </programlisting>
5569 Let's assume you have filled the table with data and want to take a
5570 look at it:
5571 <programlisting>
5572 peter@localhost testdb=&gt; SELECT * FROM my_table;
5573 first | second
5574 -------+--------
5575 1 | one
5576 2 | two
5577 3 | three
5578 4 | four
5579 (4 rows)
5580 </programlisting>
5581 You can display tables in different ways by using the
5582 <command>\pset</command> command:
5583 <programlisting>
5584 peter@localhost testdb=&gt; <userinput>\pset border 2</userinput>
5585 Border style is 2.
5586 peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table;</userinput>
5587 +-------+--------+
5588 | first | second |
5589 +-------+--------+
5590 | 1 | one |
5591 | 2 | two |
5592 | 3 | three |
5593 | 4 | four |
5594 +-------+--------+
5595 (4 rows)
5596
5597 peter@localhost testdb=&gt; <userinput>\pset border 0</userinput>
5598 Border style is 0.
5599 peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table;</userinput>
5600 first second
5601 ----- ------
5602 1 one
5603 2 two
5604 3 three
5605 4 four
5606 (4 rows)
5607
5608 peter@localhost testdb=&gt; <userinput>\pset border 1</userinput>
5609 Border style is 1.
5610 peter@localhost testdb=&gt; <userinput>\pset format csv</userinput>
5611 Output format is csv.
5612 peter@localhost testdb=&gt; <userinput>\pset tuples_only</userinput>
5613 Tuples only is on.
5614 peter@localhost testdb=&gt; <userinput>SELECT second, first FROM my_table;</userinput>
5615 one,1
5616 two,2
5617 three,3
5618 four,4
5619 peter@localhost testdb=&gt; <userinput>\pset format unaligned</userinput>
5620 Output format is unaligned.
5621 peter@localhost testdb=&gt; <userinput>\pset fieldsep '\t'</userinput>
5622 Field separator is " ".
5623 peter@localhost testdb=&gt; <userinput>SELECT second, first FROM my_table;</userinput>
5624 one 1
5625 two 2
5626 three 3
5627 four 4
5628 </programlisting>
5629 Alternatively, use the short commands:
5630 <programlisting>
5631 peter@localhost testdb=&gt; <userinput>\a \t \x</userinput>
5632 Output format is aligned.
5633 Tuples only is off.
5634 Expanded display is on.
5635 peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table;</userinput>
5636 -[ RECORD 1 ]-
5637 first | 1
5638 second | one
5639 -[ RECORD 2 ]-
5640 first | 2
5641 second | two
5642 -[ RECORD 3 ]-
5643 first | 3
5644 second | three
5645 -[ RECORD 4 ]-
5646 first | 4
5647 second | four
5648 </programlisting>
5649 </para>
5650
5651 <para>
5652 Also, these output format options can be set for just one query by using
5653 <literal>\g</literal>:
5654 <programlisting>
5655 peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table</userinput>
5656 peter@localhost testdb-&gt; <userinput>\g (format=aligned tuples_only=off expanded=on)</userinput>
5657 -[ RECORD 1 ]-
5658 first | 1
5659 second | one
5660 -[ RECORD 2 ]-
5661 first | 2
5662 second | two
5663 -[ RECORD 3 ]-
5664 first | 3
5665 second | three
5666 -[ RECORD 4 ]-
5667 first | 4
5668 second | four
5669 </programlisting>
5670 </para>
5671
5672 <para>
5673 Here is an example of using the <command>\df</command> command to
5674 find only functions with names matching <literal>int*pl</literal>
5675 and whose second argument is of type <type>bigint</type>:
5676 <programlisting>
5677 testdb=&gt; <userinput>\df int*pl * bigint</userinput>
5678 List of functions
5679 Schema | Name | Result data type | Argument data types | Type
5680 ------------+---------+------------------+---------------------+------
5681 pg_catalog | int28pl | bigint | smallint, bigint | func
5682 pg_catalog | int48pl | bigint | integer, bigint | func
5683 pg_catalog | int8pl | bigint | bigint, bigint | func
5684 (3 rows)
5685 </programlisting>
5686 </para>
5687
5688 <para>
5689 Here, the <literal>+</literal> option is used to display additional
5690 information about one of these functions, and <literal>x</literal> is used
5691 to display the results in expanded mode:
5692 <programlisting>
5693 testdb=&gt; <userinput>\df+x int*pl integer bigint</userinput>
5694 List of functions
5695 -[ RECORD 1 ]-------+-----------------------------
5696 Schema | pg_catalog
5697 Name | int48pl
5698 Result data type | bigint
5699 Argument data types | integer, bigint
5700 Type | func
5701 Volatility | immutable
5702 Parallel | safe
5703 Owner | postgres
5704 Security | invoker
5705 Leakproof? | no
5706 Access privileges |
5707 Language | internal
5708 Internal name | int48pl
5709 Description | implementation of + operator
5710 </programlisting>
5711 </para>
5712
5713 <para>
5714 When suitable, query results can be shown in a crosstab representation
5715 with the <command>\crosstabview</command> command:
5716 <programlisting>
5717 testdb=&gt; <userinput>SELECT first, second, first &gt; 2 AS gt2 FROM my_table;</userinput>
5718 first | second | gt2
5719 -------+--------+-----
5720 1 | one | f
5721 2 | two | f
5722 3 | three | t
5723 4 | four | t
5724 (4 rows)
5725
5726 testdb=&gt; <userinput>\crosstabview first second</userinput>
5727 first | one | two | three | four
5728 -------+-----+-----+-------+------
5729 1 | f | | |
5730 2 | | f | |
5731 3 | | | t |
5732 4 | | | | t
5733 (4 rows)
5734 </programlisting>
5735
5736 This second example shows a multiplication table with rows sorted in reverse
5737 numerical order and columns with an independent, ascending numerical order.
5738 <programlisting>
5739 testdb=&gt; <userinput>SELECT t1.first as "A", t2.first+100 AS "B", t1.first*(t2.first+100) as "AxB",</userinput>
5740 testdb-&gt; <userinput>row_number() over(order by t2.first) AS ord</userinput>
5741 testdb-&gt; <userinput>FROM my_table t1 CROSS JOIN my_table t2 ORDER BY 1 DESC</userinput>
5742 testdb-&gt; <userinput>\crosstabview "A" "B" "AxB" ord</userinput>
5743 A | 101 | 102 | 103 | 104
5744 ---+-----+-----+-----+-----
5745 4 | 404 | 408 | 412 | 416
5746 3 | 303 | 306 | 309 | 312
5747 2 | 202 | 204 | 206 | 208
5748 1 | 101 | 102 | 103 | 104
5749 (4 rows)
5750 </programlisting></para>
5751
5752 </refsect1>
5753
5754 </refentry>