2 doc/src/sgml/ref/psql-ref.sgml
3 PostgreSQL documentation
6 <refentry id="APP-PSQL">
7 <indexterm zone="app-psql">
8 <primary>psql</primary>
12 <refentrytitle><application>psql</application></refentrytitle>
13 <manvolnum>1</manvolnum>
14 <refmiscinfo>Application</refmiscinfo>
18 <refname><application>psql</application></refname>
20 <productname>PostgreSQL</productname> interactive terminal
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>
34 <title>Description</title>
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.
48 <refsect1 id="R1-APP-PSQL-3">
49 <title>Options</title>
53 <term><option>-a</></term>
54 <term><option>--echo-all</></term>
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>.
66 <term><option>-A</></term>
67 <term><option>--no-align</></term>
70 Switches to unaligned output mode. (The default output mode is
71 otherwise aligned.) This is equivalent to <command>\pset format
78 <term><option>-b</></term>
79 <term><option>--echo-errors</></term>
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>.
90 <term><option>-c <replaceable class="parameter">command</replaceable></></term>
91 <term><option>--command=<replaceable class="parameter">command</replaceable></></term>
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>
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:
113 psql -c '\x' -c 'SELECT * FROM foo;'
117 echo '\x \\ SELECT * FROM foo;' | psql
119 (<literal>\\</> is the separator meta-command.)
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>COMMIT</>
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 Also, <application>psql</application> only prints the
131 result of the last <acronym>SQL</acronym> command in the string.
132 This is different from the behavior when the same string is read from
133 a file or fed to <application>psql</application>'s standard input,
134 because then <application>psql</application> sends
135 each <acronym>SQL</acronym> command separately.
138 Because of this behavior, putting more than one SQL command in a
139 single <option>-c</option> string often has unexpected results.
140 It's better to use repeated <option>-c</option> commands or feed
141 multiple commands to <application>psql</application>'s standard input,
142 either using <application>echo</application> as illustrated above, or
143 via a shell here-document, for example:
155 <term><option>-d <replaceable class="parameter">dbname</replaceable></></term>
156 <term><option>--dbname=<replaceable class="parameter">dbname</replaceable></></term>
159 Specifies the name of the database to connect to. This is
160 equivalent to specifying <replaceable
161 class="parameter">dbname</replaceable> as the first non-option
162 argument on the command line.
165 If this parameter contains an <symbol>=</symbol> sign or starts
166 with a valid <acronym>URI</acronym> prefix
167 (<literal>postgresql://</literal>
168 or <literal>postgres://</literal>), it is treated as a
169 <parameter>conninfo</parameter> string. See <xref
170 linkend="libpq-connstring"> for more information.
176 <term><option>-e</></term>
177 <term><option>--echo-queries</></term>
180 Copy all SQL commands sent to the server to standard output as well.
182 to setting the variable <varname>ECHO</varname> to
183 <literal>queries</literal>.
189 <term><option>-E</></term>
190 <term><option>--echo-hidden</></term>
193 Echo the actual queries generated by <command>\d</command> and other backslash
194 commands. You can use this to study <application>psql</application>'s
195 internal operations. This is equivalent to
196 setting the variable <varname>ECHO_HIDDEN</varname> to <literal>on</>.
202 <term><option>-f <replaceable class="parameter">filename</replaceable></></term>
203 <term><option>--file=<replaceable class="parameter">filename</replaceable></></term>
206 Read commands from the
207 file <replaceable class="parameter">filename</replaceable>,
208 rather than standard input.
209 This option can be repeated and combined in any order with
210 the <option>-c</option> option. When either <option>-c</option>
211 or <option>-f</option> is specified, <application>psql</application>
212 does not read commands from standard input; instead it terminates
213 after processing all the <option>-c</option> and <option>-f</option>
215 Except for that, this option is largely equivalent to the
216 meta-command <command>\i</command>.
220 If <replaceable>filename</replaceable> is <literal>-</literal>
221 (hyphen), then standard input is read until an EOF indication
222 or <command>\q</> meta-command. This can be used to intersperse
223 interactive input with input from files. Note however that Readline
224 is not used in this case (much as if <option>-n</option> had been
229 Using this option is subtly different from writing <literal>psql
231 class="parameter">filename</replaceable></literal>. In general,
232 both will do what you expect, but using <literal>-f</literal>
233 enables some nice features such as error messages with line
234 numbers. There is also a slight chance that using this option will
235 reduce the start-up overhead. On the other hand, the variant using
236 the shell's input redirection is (in theory) guaranteed to yield
237 exactly the same output you would have received had you entered
244 <term><option>-F <replaceable class="parameter">separator</replaceable></></term>
245 <term><option>--field-separator=<replaceable class="parameter">separator</replaceable></></term>
248 Use <replaceable class="parameter">separator</replaceable> as the
249 field separator for unaligned output. This is equivalent to
250 <command>\pset fieldsep</command> or <command>\f</command>.
256 <term><option>-h <replaceable class="parameter">hostname</replaceable></></term>
257 <term><option>--host=<replaceable class="parameter">hostname</replaceable></></term>
260 Specifies the host name of the machine on which the
261 server is running. If the value begins
262 with a slash, it is used as the directory for the Unix-domain
269 <term><option>-H</></term>
270 <term><option>--html</></term>
273 Turn on <acronym>HTML</acronym> tabular output. This is
274 equivalent to <literal>\pset format html</literal> or the
275 <command>\H</command> command.
281 <term><option>-l</></term>
282 <term><option>--list</></term>
285 List all available databases, then exit. Other non-connection
286 options are ignored. This is similar to the meta-command
287 <command>\list</command>.
293 <term><option>-L <replaceable class="parameter">filename</replaceable></></term>
294 <term><option>--log-file=<replaceable class="parameter">filename</replaceable></></term>
297 Write all query output into file <replaceable
298 class="parameter">filename</replaceable>, in addition to the
299 normal output destination.
305 <term><option>-n</></term>
306 <term><option>--no-readline</></term>
309 Do not use <application>Readline</application> for line editing and do
310 not use the command history.
311 This can be useful to turn off tab expansion when cutting and pasting.
317 <term><option>-o <replaceable class="parameter">filename</replaceable></></term>
318 <term><option>--output=<replaceable class="parameter">filename</replaceable></></term>
321 Put all query output into file <replaceable
322 class="parameter">filename</replaceable>. This is equivalent to
323 the command <command>\o</command>.
329 <term><option>-p <replaceable class="parameter">port</replaceable></></term>
330 <term><option>--port=<replaceable class="parameter">port</replaceable></></term>
333 Specifies the TCP port or the local Unix-domain
334 socket file extension on which the server is listening for
335 connections. Defaults to the value of the <envar>PGPORT</envar>
336 environment variable or, if not set, to the port specified at
337 compile time, usually 5432.
343 <term><option>-P <replaceable class="parameter">assignment</replaceable></></term>
344 <term><option>--pset=<replaceable class="parameter">assignment</replaceable></></term>
347 Specifies printing options, in the style of
348 <command>\pset</command>. Note that here you
349 have to separate name and value with an equal sign instead of a
350 space. For example, to set the output format to <application>LaTeX</application>, you could write
351 <literal>-P format=latex</literal>.
357 <term><option>-q</></term>
358 <term><option>--quiet</></term>
361 Specifies that <application>psql</application> should do its work
362 quietly. By default, it prints welcome messages and various
363 informational output. If this option is used, none of this
364 happens. This is useful with the <option>-c</option> option.
365 This is equivalent to setting the variable <varname>QUIET</varname>
372 <term><option>-R <replaceable class="parameter">separator</replaceable></></term>
373 <term><option>--record-separator=<replaceable class="parameter">separator</replaceable></></term>
376 Use <replaceable class="parameter">separator</replaceable> as the
377 record separator for unaligned output. This is equivalent to
378 <command>\pset recordsep</command>.
384 <term><option>-s</></term>
385 <term><option>--single-step</></term>
388 Run in single-step mode. That means the user is prompted before
389 each command is sent to the server, with the option to cancel
390 execution as well. Use this to debug scripts.
396 <term><option>-S</></term>
397 <term><option>--single-line</></term>
400 Runs in single-line mode where a newline terminates an SQL command, as a
406 This mode is provided for those who insist on it, but you are not
407 necessarily encouraged to use it. In particular, if you mix
408 <acronym>SQL</acronym> and meta-commands on a line the order of
409 execution might not always be clear to the inexperienced user.
416 <term><option>-t</></term>
417 <term><option>--tuples-only</></term>
420 Turn off printing of column names and result row count footers,
421 etc. This is equivalent to <command>\t</command> or
422 <command>\pset tuples_only</command>.
428 <term><option>-T <replaceable class="parameter">table_options</replaceable></></term>
429 <term><option>--table-attr=<replaceable class="parameter">table_options</replaceable></></term>
432 Specifies options to be placed within the
433 <acronym>HTML</acronym> <sgmltag>table</sgmltag> tag. See
434 <command>\pset tableattr</command> for details.
440 <term><option>-U <replaceable class="parameter">username</replaceable></></term>
441 <term><option>--username=<replaceable class="parameter">username</replaceable></></term>
444 Connect to the database as the user <replaceable
445 class="parameter">username</replaceable> instead of the default.
446 (You must have permission to do so, of course.)
452 <term><option>-v <replaceable class="parameter">assignment</replaceable></></term>
453 <term><option>--set=<replaceable class="parameter">assignment</replaceable></></term>
454 <term><option>--variable=<replaceable class="parameter">assignment</replaceable></></term>
457 Perform a variable assignment, like the <command>\set</command>
458 meta-command. Note that you must separate name and value, if
459 any, by an equal sign on the command line. To unset a variable,
460 leave off the equal sign. To set a variable with an empty value,
461 use the equal sign but leave off the value. These assignments are
462 done during command line processing, so variables that reflect
463 connection state will get overwritten later.
469 <term><option>-V</></term>
470 <term><option>--version</></term>
473 Print the <application>psql</application> version and exit.
479 <term><option>-w</></term>
480 <term><option>--no-password</></term>
483 Never issue a password prompt. If the server requires password
484 authentication and a password is not available by other means
485 such as a <filename>.pgpass</filename> file, the connection
486 attempt will fail. This option can be useful in batch jobs and
487 scripts where no user is present to enter a password.
491 Note that this option will remain set for the entire session,
492 and so it affects uses of the meta-command
493 <command>\connect</command> as well as the initial connection attempt.
499 <term><option>-W</></term>
500 <term><option>--password</></term>
503 Force <application>psql</application> to prompt for a
504 password before connecting to a database.
508 This option is never essential, since <application>psql</application>
509 will automatically prompt for a password if the server demands
510 password authentication. However, <application>psql</application>
511 will waste a connection attempt finding out that the server wants a
512 password. In some cases it is worth typing <option>-W</> to avoid
513 the extra connection attempt.
517 Note that this option will remain set for the entire session,
518 and so it affects uses of the meta-command
519 <command>\connect</command> as well as the initial connection attempt.
525 <term><option>-x</></term>
526 <term><option>--expanded</></term>
529 Turn on the expanded table formatting mode. This is equivalent to
530 <command>\x</command> or <command>\pset expanded</command>.
536 <term><option>-X,</></term>
537 <term><option>--no-psqlrc</></term>
540 Do not read the start-up file (neither the system-wide
541 <filename>psqlrc</filename> file nor the user's
542 <filename>~/.psqlrc</filename> file).
548 <term><option>-z</option></term>
549 <term><option>--field-separator-zero</option></term>
552 Set the field separator for unaligned output to a zero byte. This is
553 equvalent to <command>\pset fieldsep_zero</command>.
559 <term><option>-0</option></term>
560 <term><option>--record-separator-zero</option></term>
563 Set the record separator for unaligned output to a zero byte. This is
564 useful for interfacing, for example, with <literal>xargs -0</literal>.
565 This is equivalent to <command>\pset recordsep_zero</command>.
571 <term><option>-1</option></term>
572 <term><option>--single-transaction</option></term>
575 This option can only be used in combination with one or more
576 <option>-c</option> and/or <option>-f</option> options. It causes
577 <application>psql</application> to issue a <command>BEGIN</> command
578 before the first such option and a <command>COMMIT</> command after
579 the last one, thereby wrapping all the commands into a single
580 transaction. This ensures that either all the commands complete
581 successfully, or no changes are applied.
585 If the commands themselves
586 contain <command>BEGIN</>, <command>COMMIT</>,
587 or <command>ROLLBACK</>, this option will not have the desired
588 effects. Also, if an individual command cannot be executed inside a
589 transaction block, specifying this option will cause the whole
596 <term><option>-?</></term>
597 <term><option>--help[=<replaceable class="parameter">topic</>]</option></term>
600 Show help about <application>psql</application> and exit. The optional
601 <replaceable class="parameter">topic</> parameter (defaulting
602 to <literal>options</literal>) selects which part of <application>psql</application> is
603 explained: <literal>commands</> describes <application>psql</>'s
604 backslash commands; <literal>options</> describes the command-line
605 options that can be passed to <application>psql</>;
606 and <literal>variables</> shows help about <application>psql</application> configuration
617 <title>Exit Status</title>
620 <application>psql</application> returns 0 to the shell if it
621 finished normally, 1 if a fatal error of its own occurs (e.g. out of memory,
622 file not found), 2 if the connection to the server went bad
623 and the session was not interactive, and 3 if an error occurred in a
624 script and the variable <varname>ON_ERROR_STOP</varname> was set.
632 <refsect2 id="R2-APP-PSQL-connecting">
633 <title>Connecting to a Database</title>
636 <application>psql</application> is a regular
637 <productname>PostgreSQL</productname> client application. In order
638 to connect to a database you need to know the name of your target
639 database, the host name and port number of the server, and what user
640 name you want to connect as. <application>psql</application> can be
641 told about those parameters via command line options, namely
642 <option>-d</option>, <option>-h</option>, <option>-p</option>, and
643 <option>-U</option> respectively. If an argument is found that does
644 not belong to any option it will be interpreted as the database name
645 (or the user name, if the database name is already given). Not all
646 of these options are required; there are useful defaults. If you omit the host
647 name, <application>psql</> will connect via a Unix-domain socket
648 to a server on the local host, or via TCP/IP to <literal>localhost</> on
649 machines that don't have Unix-domain sockets. The default port number is
650 determined at compile time.
651 Since the database server uses the same default, you will not have
652 to specify the port in most cases. The default user name is your
653 operating-system user name, as is the default database name.
655 just connect to any database under any user name. Your database
656 administrator should have informed you about your access rights.
660 When the defaults aren't quite right, you can save yourself
661 some typing by setting the environment variables
662 <envar>PGDATABASE</envar>, <envar>PGHOST</envar>,
663 <envar>PGPORT</envar> and/or <envar>PGUSER</envar> to appropriate
664 values. (For additional environment variables, see <xref
665 linkend="libpq-envars">.) It is also convenient to have a
666 <filename>~/.pgpass</> file to avoid regularly having to type in
667 passwords. See <xref linkend="libpq-pgpass"> for more information.
671 An alternative way to specify connection parameters is in a
672 <parameter>conninfo</parameter> string or
673 a <acronym>URI</acronym>, which is used instead of a database
674 name. This mechanism give you very wide control over the
675 connection. For example:
677 $ <userinput>psql "service=myservice sslmode=require"</userinput>
678 $ <userinput>psql postgresql://dbmaster:5433/mydb?sslmode=require</userinput>
680 This way you can also use <acronym>LDAP</acronym> for connection
681 parameter lookup as described in <xref linkend="libpq-ldap">.
682 See <xref linkend="libpq-paramkeywords"> for more information on all the
683 available connection options.
687 If the connection could not be made for any reason (e.g., insufficient
688 privileges, server is not running on the targeted host, etc.),
689 <application>psql</application> will return an error and terminate.
693 If both standard input and standard output are a
694 terminal, then <application>psql</application> sets the client
695 encoding to <quote>auto</quote>, which will detect the
696 appropriate client encoding from the locale settings
697 (<envar>LC_CTYPE</envar> environment variable on Unix systems).
698 If this doesn't work out as expected, the client encoding can be
699 overridden using the environment
700 variable <envar>PGCLIENTENCODING</envar>.
704 <refsect2 id="R2-APP-PSQL-4">
705 <title>Entering SQL Commands</title>
708 In normal operation, <application>psql</application> provides a
709 prompt with the name of the database to which
710 <application>psql</application> is currently connected, followed by
711 the string <literal>=></literal>. For example:
713 $ <userinput>psql testdb</userinput>
715 Type "help" for help.
722 At the prompt, the user can type in <acronym>SQL</acronym> commands.
723 Ordinarily, input lines are sent to the server when a
724 command-terminating semicolon is reached. An end of line does not
725 terminate a command. Thus commands can be spread over several lines for
726 clarity. If the command was sent and executed without error, the results
727 of the command are displayed on the screen.
731 Whenever a command is executed, <application>psql</application> also polls
732 for asynchronous notification events generated by
733 <xref linkend="SQL-LISTEN"> and
734 <xref linkend="SQL-NOTIFY">.
738 While C-style block comments are passed to the server for
739 processing and removal, SQL-standard comments are removed by
740 <application>psql</application>.
744 <refsect2 id="APP-PSQL-meta-commands">
745 <title>Meta-Commands</title>
748 Anything you enter in <application>psql</application> that begins
749 with an unquoted backslash is a <application>psql</application>
750 meta-command that is processed by <application>psql</application>
751 itself. These commands make
752 <application>psql</application> more useful for administration or
753 scripting. Meta-commands are often called slash or backslash commands.
757 The format of a <application>psql</application> command is the backslash,
758 followed immediately by a command verb, then any arguments. The arguments
759 are separated from the command verb and each other by any number of
760 whitespace characters.
764 To include whitespace in an argument you can quote it with
765 single quotes. To include a single quote in an argument,
766 write two single quotes within single-quoted text.
767 Anything contained in single quotes is
768 furthermore subject to C-like substitutions for
769 <literal>\n</literal> (new line), <literal>\t</literal> (tab),
770 <literal>\b</literal> (backspace), <literal>\r</literal> (carriage return),
771 <literal>\f</literal> (form feed),
772 <literal>\</literal><replaceable>digits</replaceable> (octal), and
773 <literal>\x</literal><replaceable>digits</replaceable> (hexadecimal).
774 A backslash preceding any other character within single-quoted text
775 quotes that single character, whatever it is.
779 If an unquoted colon (<literal>:</literal>) followed by a
780 <application>psql</> variable name appears within an argument, it is
781 replaced by the variable's value, as described in <xref
782 linkend="APP-PSQL-interpolation" endterm="APP-PSQL-interpolation-title">.
783 The forms <literal>:'<replaceable>variable_name</>'</literal> and
784 <literal>:"<replaceable>variable_name</>"</literal> described there
789 Within an argument, text that is enclosed in backquotes
790 (<literal>`</literal>) is taken as a command line that is passed to the
791 shell. The output of the command (with any trailing newline removed)
792 replaces the backquoted text. Within the text enclosed in backquotes,
793 no special quoting or other processing occurs, except that appearances
794 of <literal>:<replaceable>variable_name</></literal> where
795 <replaceable>variable_name</> is a <application>psql</> variable name
796 are replaced by the variable's value. Also, appearances of
797 <literal>:'<replaceable>variable_name</>'</literal> are replaced by the
798 variable's value suitably quoted to become a single shell command
799 argument. (The latter form is almost always preferable, unless you are
800 very sure of what is in the variable.) Because carriage return and line
801 feed characters cannot be safely quoted on all platforms, the
802 <literal>:'<replaceable>variable_name</>'</literal> form prints an
803 error message and does not substitute the variable value when such
804 characters appear in the value.
808 Some commands take an <acronym>SQL</acronym> identifier (such as a
809 table name) as argument. These arguments follow the syntax rules
810 of <acronym>SQL</acronym>: Unquoted letters are forced to
811 lowercase, while double quotes (<literal>"</>) protect letters
812 from case conversion and allow incorporation of whitespace into
813 the identifier. Within double quotes, paired double quotes reduce
814 to a single double quote in the resulting name. For example,
815 <literal>FOO"BAR"BAZ</> is interpreted as <literal>fooBARbaz</>,
816 and <literal>"A weird"" name"</> becomes <literal>A weird"
821 Parsing for arguments stops at the end of the line, or when another
822 unquoted backslash is found. An unquoted backslash
823 is taken as the beginning of a new meta-command. The special
824 sequence <literal>\\</literal> (two backslashes) marks the end of
825 arguments and continues parsing <acronym>SQL</acronym> commands, if
826 any. That way <acronym>SQL</acronym> and
827 <application>psql</application> commands can be freely mixed on a
828 line. But in any case, the arguments of a meta-command cannot
829 continue beyond the end of the line.
833 Many of the meta-commands act on the <firstterm>current query buffer</>.
834 This is simply a buffer holding whatever SQL command text has been typed
835 but not yet sent to the server for execution. This will include previous
836 input lines as well as any text appearing before the meta-command on the
841 The following meta-commands are defined:
845 <term><literal>\a</literal></term>
848 If the current table output format is unaligned, it is switched to aligned.
849 If it is not unaligned, it is set to unaligned. This command is
850 kept for backwards compatibility. See <command>\pset</command> for a
851 more general solution.
857 <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>
860 Establishes a new connection to a <productname>PostgreSQL</>
861 server. The connection parameters to use can be specified either
862 using a positional syntax, or using <replaceable>conninfo</> connection
863 strings as detailed in <xref linkend="libpq-connstring">.
867 Where the command omits database name, user, host, or port, the new
868 connection can reuse values from the previous connection. By default,
869 values from the previous connection are reused except when processing
870 a <replaceable>conninfo</> string. Passing a first argument
871 of <literal>-reuse-previous=on</>
872 or <literal>-reuse-previous=off</literal> overrides that default.
873 When the command neither specifies nor reuses a particular parameter,
874 the <application>libpq</application> default is used. Specifying any
875 of <replaceable class="parameter">dbname</replaceable>,
876 <replaceable class="parameter">username</replaceable>,
877 <replaceable class="parameter">host</replaceable> or
878 <replaceable class="parameter">port</replaceable>
879 as <literal>-</literal> is equivalent to omitting that parameter.
883 If the new connection is successfully made, the previous
884 connection is closed.
885 If the connection attempt failed (wrong user name, access
886 denied, etc.), the previous connection will only be kept if
887 <application>psql</application> is in interactive mode. When
888 executing a non-interactive script, processing will
889 immediately stop with an error. This distinction was chosen as
890 a user convenience against typos on the one hand, and a safety
891 mechanism that scripts are not accidentally acting on the
892 wrong database on the other hand.
899 => \c mydb myuser host.dom 6432
901 => \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
902 => \c postgresql://tom@localhost/mydb?application_name=myapp
908 <term><literal>\C [ <replaceable class="parameter">title</replaceable> ]</literal></term>
911 Sets the title of any tables being printed as the result of a
912 query or unset any such title. This command is equivalent to
913 <literal>\pset title <replaceable
914 class="parameter">title</replaceable></literal>. (The name of
915 this command derives from <quote>caption</quote>, as it was
916 previously only used to set the caption in an
917 <acronym>HTML</acronym> table.)
923 <term><literal>\cd [ <replaceable>directory</replaceable> ]</literal></term>
926 Changes the current working directory to
927 <replaceable>directory</replaceable>. Without argument, changes
928 to the current user's home directory.
933 To print your current working directory, use <literal>\! pwd</literal>.
940 <term><literal>\conninfo</literal></term>
943 Outputs information about the current database connection.
948 <varlistentry id="APP-PSQL-meta-commands-copy">
949 <term><literal>\copy { <replaceable class="parameter">table</replaceable> [ ( <replaceable class="parameter">column_list</replaceable> ) ] | ( <replaceable class="parameter">query</replaceable> ) }
950 { <literal>from</literal> | <literal>to</literal> }
951 { <replaceable class="parameter">'filename'</replaceable> | program <replaceable class="parameter">'command'</replaceable> | stdin | stdout | pstdin | pstdout }
952 [ [ with ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]</literal></term>
956 Performs a frontend (client) copy. This is an operation that
957 runs an <acronym>SQL</acronym> <xref linkend="SQL-COPY">
958 command, but instead of the server
959 reading or writing the specified file,
960 <application>psql</application> reads or writes the file and
961 routes the data between the server and the local file system.
962 This means that file accessibility and privileges are those of
963 the local user, not the server, and no SQL superuser
964 privileges are required.
968 When <literal>program</> is specified,
969 <replaceable class="parameter">command</replaceable> is
970 executed by <application>psql</application> and the data passed from
971 or to <replaceable class="parameter">command</replaceable> is
972 routed between the server and the client.
973 Again, the execution privileges are those of
974 the local user, not the server, and no SQL superuser
975 privileges are required.
979 For <literal>\copy ... from stdin</>, data rows are read from the same
980 source that issued the command, continuing until <literal>\.</literal>
981 is read or the stream reaches <acronym>EOF</>. This option is useful
982 for populating tables in-line within a SQL script file.
983 For <literal>\copy ... to stdout</>, output is sent to the same place
984 as <application>psql</> command output, and
985 the <literal>COPY <replaceable>count</></literal> command status is
986 not printed (since it might be confused with a data row).
987 To read/write <application>psql</application>'s standard input or
988 output regardless of the current command source or <literal>\o</>
989 option, write <literal>from pstdin</> or <literal>to pstdout</>.
993 The syntax of this command is similar to that of the
994 <acronym>SQL</acronym> <xref linkend="sql-copy">
995 command. All options other than the data source/destination are
996 as specified for <xref linkend="sql-copy">.
997 Because of this, special parsing rules apply to the <command>\copy</>
998 meta-command. Unlike most other meta-commands, the entire remainder
999 of the line is always taken to be the arguments of <command>\copy</>,
1000 and neither variable interpolation nor backquote expansion are
1001 performed in the arguments.
1006 This operation is not as efficient as the <acronym>SQL</acronym>
1007 <command>COPY</command> command because all data must pass
1008 through the client/server connection. For large
1009 amounts of data the <acronym>SQL</acronym> command might be preferable.
1017 <term><literal>\copyright</literal></term>
1020 Shows the copyright and distribution terms of
1021 <productname>PostgreSQL</productname>.
1027 <varlistentry id="APP-PSQL-meta-commands-crosstabview">
1028 <term><literal>\crosstabview [
1029 <replaceable class="parameter">colV</replaceable>
1030 [ <replaceable class="parameter">colH</replaceable>
1031 [ <replaceable class="parameter">colD</replaceable>
1032 [ <replaceable class="parameter">sortcolH</replaceable>
1033 ] ] ] ] </literal></term>
1036 Executes the current query buffer (like <literal>\g</literal>) and
1037 shows the results in a crosstab grid.
1038 The query must return at least three columns.
1039 The output column identified by <replaceable class="parameter">colV</>
1040 becomes a vertical header and the output column identified by
1041 <replaceable class="parameter">colH</replaceable>
1042 becomes a horizontal header.
1043 <replaceable class="parameter">colD</replaceable> identifies
1044 the output column to display within the grid.
1045 <replaceable class="parameter">sortcolH</replaceable> identifies
1046 an optional sort column for the horizontal header.
1050 Each column specification can be a column number (starting at 1) or
1051 a column name. The usual SQL case folding and quoting rules apply to
1052 column names. If omitted,
1053 <replaceable class="parameter">colV</replaceable> is taken as column 1
1054 and <replaceable class="parameter">colH</replaceable> as column 2.
1055 <replaceable class="parameter">colH</replaceable> must differ from
1056 <replaceable class="parameter">colV</replaceable>.
1057 If <replaceable class="parameter">colD</replaceable> is not
1058 specified, then there must be exactly three columns in the query
1059 result, and the column that is neither
1060 <replaceable class="parameter">colV</replaceable> nor
1061 <replaceable class="parameter">colH</replaceable>
1062 is taken to be <replaceable class="parameter">colD</replaceable>.
1066 The vertical header, displayed as the leftmost column, contains the
1067 values found in column <replaceable class="parameter">colV</>, in the
1068 same order as in the query results, but with duplicates removed.
1072 The horizontal header, displayed as the first row, contains the values
1073 found in column <replaceable class="parameter">colH</replaceable>,
1074 with duplicates removed. By default, these appear in the same order
1075 as in the query results. But if the
1076 optional <replaceable class="parameter">sortcolH</> argument is given,
1077 it identifies a column whose values must be integer numbers, and the
1078 values from <replaceable class="parameter">colH</replaceable> will
1079 appear in the horizontal header sorted according to the
1080 corresponding <replaceable class="parameter">sortcolH</> values.
1084 Inside the crosstab grid, for each distinct value <literal>x</literal>
1085 of <replaceable class="parameter">colH</replaceable> and each distinct
1086 value <literal>y</literal>
1087 of <replaceable class="parameter">colV</replaceable>, the cell located
1088 at the intersection <literal>(x,y)</literal> contains the value of
1089 the <literal>colD</literal> column in the query result row for which
1090 the value of <replaceable class="parameter">colH</replaceable>
1091 is <literal>x</literal> and the value
1092 of <replaceable class="parameter">colV</replaceable>
1093 is <literal>y</>. If there is no such row, the cell is empty. If
1094 there are multiple such rows, an error is reported.
1101 <term><literal>\d[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1105 For each relation (table, view, materialized view, index, sequence,
1107 or composite type matching the
1108 <replaceable class="parameter">pattern</replaceable>, show all
1109 columns, their types, the tablespace (if not the default) and any
1110 special attributes such as <literal>NOT NULL</literal> or defaults.
1111 Associated indexes, constraints, rules, and triggers are
1112 also shown. For foreign tables, the associated foreign
1113 server is shown as well.
1114 (<quote>Matching the pattern</> is defined in
1115 <xref linkend="APP-PSQL-patterns" endterm="APP-PSQL-patterns-title">
1120 For some types of relation, <literal>\d</> shows additional information
1121 for each column: column values for sequences, indexed expressions for
1122 indexes, and foreign data wrapper options for foreign tables.
1126 The command form <literal>\d+</literal> is identical, except that
1127 more information is displayed: any comments associated with the
1128 columns of the table are shown, as is the presence of OIDs in the
1129 table, the view definition if the relation is a view, a non-default
1130 <link linkend="SQL-CREATETABLE-REPLICA-IDENTITY">replica
1131 identity</link> setting.
1135 By default, only user-created objects are shown; supply a
1136 pattern or the <literal>S</literal> modifier to include system
1142 If <command>\d</command> is used without a
1143 <replaceable class="parameter">pattern</replaceable> argument, it is
1144 equivalent to <command>\dtvmsE</command> which will show a list of
1145 all visible tables, views, materialized views, sequences and
1147 This is purely a convenience measure.
1154 <term><literal>\da[S] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1158 Lists aggregate functions, together with their
1159 return type and the data types they operate on. If <replaceable
1160 class="parameter">pattern</replaceable>
1161 is specified, only aggregates whose names match the pattern are shown.
1162 By default, only user-created objects are shown; supply a
1163 pattern or the <literal>S</literal> modifier to include system
1170 <term><literal>\dA[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1174 Lists access methods. If <replaceable
1175 class="parameter">pattern</replaceable> is specified, only access
1176 methods whose names match the pattern are shown. If
1177 <literal>+</literal> is appended to the command name, each access
1178 method is listed with its associated handler function and description.
1184 <term><literal>\db[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1188 Lists tablespaces. If <replaceable
1189 class="parameter">pattern</replaceable>
1190 is specified, only tablespaces whose names match the pattern are shown.
1191 If <literal>+</literal> is appended to the command name, each tablespace
1192 is listed with its associated options, on-disk size, permissions and
1200 <term><literal>\dc[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1203 Lists conversions between character-set encodings.
1204 If <replaceable class="parameter">pattern</replaceable>
1205 is specified, only conversions whose names match the pattern are
1207 By default, only user-created objects are shown; supply a
1208 pattern or the <literal>S</literal> modifier to include system
1210 If <literal>+</literal> is appended to the command name, each object
1211 is listed with its associated description.
1218 <term><literal>\dC[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1222 If <replaceable class="parameter">pattern</replaceable>
1223 is specified, only casts whose source or target types match the
1225 If <literal>+</literal> is appended to the command name, each object
1226 is listed with its associated description.
1233 <term><literal>\dd[S] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1236 Shows the descriptions of objects of type <literal>constraint</>,
1237 <literal>operator class</>, <literal>operator family</>,
1238 <literal>rule</>, and <literal>trigger</>. All
1239 other comments may be viewed by the respective backslash commands for
1243 <para><literal>\dd</literal> displays descriptions for objects matching the
1244 <replaceable class="parameter">pattern</replaceable>, or of visible
1245 objects of the appropriate type if no argument is given. But in either
1246 case, only objects that have a description are listed.
1247 By default, only user-created objects are shown; supply a
1248 pattern or the <literal>S</literal> modifier to include system
1253 Descriptions for objects can be created with the <xref
1254 linkend="sql-comment">
1255 <acronym>SQL</acronym> command.
1262 <term><literal>\dD[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1265 Lists domains. If <replaceable
1266 class="parameter">pattern</replaceable>
1267 is specified, only domains whose names match the pattern are shown.
1268 By default, only user-created objects are shown; supply a
1269 pattern or the <literal>S</literal> modifier to include system
1271 If <literal>+</literal> is appended to the command name, each object
1272 is listed with its associated permissions and description.
1279 <term><literal>\ddp [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1282 Lists default access privilege settings. An entry is shown for
1283 each role (and schema, if applicable) for which the default
1284 privilege settings have been changed from the built-in defaults.
1285 If <replaceable class="parameter">pattern</replaceable> is
1286 specified, only entries whose role name or schema name matches
1287 the pattern are listed.
1291 The <xref linkend="sql-alterdefaultprivileges"> command is used to set
1292 default access privileges. The meaning of the
1293 privilege display is explained under
1294 <xref linkend="sql-grant">.
1301 <term><literal>\dE[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1302 <term><literal>\di[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1303 <term><literal>\dm[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1304 <term><literal>\ds[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1305 <term><literal>\dt[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1306 <term><literal>\dv[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1310 In this group of commands, the letters <literal>E</literal>,
1311 <literal>i</literal>, <literal>m</literal>, <literal>s</literal>,
1312 <literal>t</literal>, and <literal>v</literal>
1313 stand for foreign table, index, materialized view, sequence, table, and view,
1315 You can specify any or all of
1316 these letters, in any order, to obtain a listing of objects
1317 of these types. For example, <literal>\dit</> lists indexes
1318 and tables. If <literal>+</literal> is
1319 appended to the command name, each object is listed with its
1320 physical size on disk and its associated description, if any.
1321 If <replaceable class="parameter">pattern</replaceable> is
1322 specified, only objects whose names match the pattern are listed.
1323 By default, only user-created objects are shown; supply a
1324 pattern or the <literal>S</literal> modifier to include system
1332 <term><literal>\des[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1335 Lists foreign servers (mnemonic: <quote>external
1337 If <replaceable class="parameter">pattern</replaceable> is
1338 specified, only those servers whose name matches the pattern
1339 are listed. If the form <literal>\des+</literal> is used, a
1340 full description of each server is shown, including the
1341 server's ACL, type, version, options, and description.
1348 <term><literal>\det[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1351 Lists foreign tables (mnemonic: <quote>external tables</quote>).
1352 If <replaceable class="parameter">pattern</replaceable> is
1353 specified, only entries whose table name or schema name matches
1354 the pattern are listed. If the form <literal>\det+</literal>
1355 is used, generic options and the foreign table description
1363 <term><literal>\deu[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1366 Lists user mappings (mnemonic: <quote>external
1368 If <replaceable class="parameter">pattern</replaceable> is
1369 specified, only those mappings whose user names match the
1370 pattern are listed. If the form <literal>\deu+</literal> is
1371 used, additional information about each mapping is shown.
1376 <literal>\deu+</literal> might also display the user name and
1377 password of the remote user, so care should be taken not to
1386 <term><literal>\dew[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1389 Lists foreign-data wrappers (mnemonic: <quote>external
1391 If <replaceable class="parameter">pattern</replaceable> is
1392 specified, only those foreign-data wrappers whose name matches
1393 the pattern are listed. If the form <literal>\dew+</literal>
1394 is used, the ACL, options, and description of the foreign-data
1395 wrapper are also shown.
1402 <term><literal>\df[antwS+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1406 Lists functions, together with their result data types, argument data
1407 types, and function types, which are classified as <quote>agg</>
1408 (aggregate), <quote>normal</>, <quote>trigger</>, or <quote>window</>.
1409 To display only functions
1410 of specific type(s), add the corresponding letters <literal>a</>,
1411 <literal>n</>, <literal>t</>, or <literal>w</> to the command.
1413 class="parameter">pattern</replaceable> is specified, only
1414 functions whose names match the pattern are shown.
1415 By default, only user-created
1416 objects are shown; supply a pattern or the <literal>S</literal>
1417 modifier to include system objects.
1418 If the form <literal>\df+</literal> is used, additional information
1419 about each function is shown, including volatility,
1420 parallel safety, owner, security classification, access privileges,
1421 language, source code and description.
1426 To look up functions taking arguments or returning values of a specific
1427 data type, use your pager's search capability to scroll through the
1428 <literal>\df</> output.
1436 <term><literal>\dF[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1439 Lists text search configurations.
1440 If <replaceable class="parameter">pattern</replaceable> is specified,
1441 only configurations whose names match the pattern are shown.
1442 If the form <literal>\dF+</literal> is used, a full description of
1443 each configuration is shown, including the underlying text search
1444 parser and the dictionary list for each parser token type.
1450 <term><literal>\dFd[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1453 Lists text search dictionaries.
1454 If <replaceable class="parameter">pattern</replaceable> is specified,
1455 only dictionaries whose names match the pattern are shown.
1456 If the form <literal>\dFd+</literal> is used, additional information
1457 is shown about each selected dictionary, including the underlying
1458 text search template and the option values.
1464 <term><literal>\dFp[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1467 Lists text search parsers.
1468 If <replaceable class="parameter">pattern</replaceable> is specified,
1469 only parsers whose names match the pattern are shown.
1470 If the form <literal>\dFp+</literal> is used, a full description of
1471 each parser is shown, including the underlying functions and the
1472 list of recognized token types.
1478 <term><literal>\dFt[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1481 Lists text search templates.
1482 If <replaceable class="parameter">pattern</replaceable> is specified,
1483 only templates whose names match the pattern are shown.
1484 If the form <literal>\dFt+</literal> is used, additional information
1485 is shown about each template, including the underlying function names.
1492 <term><literal>\dg[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1495 Lists database roles.
1496 (Since the concepts of <quote>users</> and <quote>groups</> have been
1497 unified into <quote>roles</>, this command is now equivalent to
1498 <literal>\du</literal>.)
1499 By default, only user-created roles are shown; supply the
1500 <literal>S</literal> modifier to include system roles.
1501 If <replaceable class="parameter">pattern</replaceable> is specified,
1502 only those roles whose names match the pattern are listed.
1503 If the form <literal>\dg+</literal> is used, additional information
1504 is shown about each role; currently this adds the comment for each
1512 <term><literal>\dl</literal></term>
1515 This is an alias for <command>\lo_list</command>, which shows a
1516 list of large objects.
1522 <term><literal>\dL[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1525 Lists procedural languages. If <replaceable
1526 class="parameter">pattern</replaceable>
1527 is specified, only languages whose names match the pattern are listed.
1528 By default, only user-created languages
1529 are shown; supply the <literal>S</literal> modifier to include system
1530 objects. If <literal>+</literal> is appended to the command name, each
1531 language is listed with its call handler, validator, access privileges,
1532 and whether it is a system object.
1539 <term><literal>\dn[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1543 Lists schemas (namespaces). If <replaceable
1544 class="parameter">pattern</replaceable>
1545 is specified, only schemas whose names match the pattern are listed.
1546 By default, only user-created objects are shown; supply a
1547 pattern or the <literal>S</literal> modifier to include system objects.
1548 If <literal>+</literal> is appended to the command name, each object
1549 is listed with its associated permissions and description, if any.
1556 <term><literal>\do[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1559 Lists operators with their operand and result types.
1560 If <replaceable class="parameter">pattern</replaceable> is
1561 specified, only operators whose names match the pattern are listed.
1562 By default, only user-created objects are shown; supply a
1563 pattern or the <literal>S</literal> modifier to include system
1565 If <literal>+</literal> is appended to the command name,
1566 additional information about each operator is shown, currently just
1567 the name of the underlying function.
1574 <term><literal>\dO[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1578 If <replaceable class="parameter">pattern</replaceable> is
1579 specified, only collations whose names match the pattern are
1580 listed. By default, only user-created objects are shown;
1581 supply a pattern or the <literal>S</literal> modifier to
1582 include system objects. If <literal>+</literal> is appended
1583 to the command name, each collation is listed with its associated
1584 description, if any.
1585 Note that only collations usable with the current database's encoding
1586 are shown, so the results may vary in different databases of the
1594 <term><literal>\dp [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1597 Lists tables, views and sequences with their
1598 associated access privileges.
1599 If <replaceable class="parameter">pattern</replaceable> is
1600 specified, only tables, views and sequences whose names match the
1605 The <xref linkend="sql-grant"> and
1606 <xref linkend="sql-revoke">
1607 commands are used to set access privileges. The meaning of the
1608 privilege display is explained under
1609 <xref linkend="sql-grant">.
1615 <term><literal>\drds [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">role-pattern</replaceable></link> [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">database-pattern</replaceable></link> ] ]</literal></term>
1618 Lists defined configuration settings. These settings can be
1619 role-specific, database-specific, or both.
1620 <replaceable>role-pattern</replaceable> and
1621 <replaceable>database-pattern</replaceable> are used to select
1622 specific roles and databases to list, respectively. If omitted, or if
1623 <literal>*</> is specified, all settings are listed, including those
1624 not role-specific or database-specific, respectively.
1628 The <xref linkend="sql-alterrole"> and
1629 <xref linkend="sql-alterdatabase">
1630 commands are used to define per-role and per-database configuration
1637 <term><literal>\dRp[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1640 Lists replication publications.
1641 If <replaceable class="parameter">pattern</replaceable> is
1642 specified, only those publications whose names match the pattern are
1644 If <literal>+</literal> is appended to the command name, the tables
1645 associated with each publication are shown as well.
1651 <term><literal>\dRs[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1654 Lists replication subscriptions.
1655 If <replaceable class="parameter">pattern</replaceable> is
1656 specified, only those subscriptions whose names match the pattern are
1658 If <literal>+</literal> is appended to the command name, additional
1659 properties of the subscriptions are shown.
1665 <term><literal>\dT[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1669 If <replaceable class="parameter">pattern</replaceable> is
1670 specified, only types whose names match the pattern are listed.
1671 If <literal>+</literal> is appended to the command name, each type is
1672 listed with its internal name and size, its allowed values
1673 if it is an <type>enum</> type, and its associated permissions.
1674 By default, only user-created objects are shown; supply a
1675 pattern or the <literal>S</literal> modifier to include system
1682 <term><literal>\du[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1685 Lists database roles.
1686 (Since the concepts of <quote>users</> and <quote>groups</> have been
1687 unified into <quote>roles</>, this command is now equivalent to
1688 <literal>\dg</literal>.)
1689 By default, only user-created roles are shown; supply the
1690 <literal>S</literal> modifier to include system roles.
1691 If <replaceable class="parameter">pattern</replaceable> is specified,
1692 only those roles whose names match the pattern are listed.
1693 If the form <literal>\du+</literal> is used, additional information
1694 is shown about each role; currently this adds the comment for each
1701 <term><literal>\dx[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1704 Lists installed extensions.
1705 If <replaceable class="parameter">pattern</replaceable>
1706 is specified, only those extensions whose names match the pattern
1708 If the form <literal>\dx+</literal> is used, all the objects belonging
1709 to each matching extension are listed.
1715 <term><literal>\dy[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1718 Lists event triggers.
1719 If <replaceable class="parameter">pattern</replaceable>
1720 is specified, only those event triggers whose names match the pattern
1722 If <literal>+</literal> is appended to the command name, each object
1723 is listed with its associated description.
1729 <term><literal>\e</literal> or <literal>\edit</> <literal> <optional> <replaceable class="parameter">filename</> </optional> <optional> <replaceable class="parameter">line_number</> </optional> </literal></term>
1733 If <replaceable class="parameter">filename</replaceable> is
1734 specified, the file is edited; after the editor exits, the file's
1735 content is copied into the current query buffer. If no <replaceable
1736 class="parameter">filename</replaceable> is given, the current query
1737 buffer is copied to a temporary file which is then edited in the same
1738 fashion. Or, if the current query buffer is empty, the most recently
1739 executed query is copied to a temporary file and edited in the same
1744 The new contents of the query buffer are then re-parsed according to
1745 the normal rules of <application>psql</application>, treating the
1746 whole buffer as a single line. Any complete queries are immediately
1747 executed; that is, if the query buffer contains or ends with a
1748 semicolon, everything up to that point is executed. Whatever remains
1749 will wait in the query buffer; type semicolon or <literal>\g</> to
1750 send it, or <literal>\r</> to cancel it by clearing the query buffer.
1751 Treating the buffer as a single line primarily affects meta-commands:
1752 whatever is in the buffer after a meta-command will be taken as
1753 argument(s) to the meta-command, even if it spans multiple lines.
1754 (Thus you cannot make meta-command-using scripts this way.
1755 Use <command>\i</command> for that.)
1759 If a line number is specified, <application>psql</application> will
1760 position the cursor on the specified line of the file or query buffer.
1761 Note that if a single all-digits argument is given,
1762 <application>psql</application> assumes it is a line number,
1768 See under <xref linkend="app-psql-environment"
1769 endterm="app-psql-environment-title"> for how to configure and
1770 customize your editor.
1777 <term><literal>\echo <replaceable class="parameter">text</replaceable> [ ... ]</literal></term>
1780 Prints the arguments to the standard output, separated by one
1781 space and followed by a newline. This can be useful to
1782 intersperse information in the output of scripts. For example:
1784 => <userinput>\echo `date`</userinput>
1785 Tue Oct 26 21:40:57 CEST 1999
1787 If the first argument is an unquoted <literal>-n</literal> the trailing
1788 newline is not written.
1793 If you use the <command>\o</command> command to redirect your
1794 query output you might wish to use <command>\qecho</command>
1795 instead of this command.
1802 <term><literal>\ef <optional> <replaceable class="parameter">function_description</> <optional> <replaceable class="parameter">line_number</> </optional> </optional> </literal></term>
1806 This command fetches and edits the definition of the named function,
1807 in the form of a <command>CREATE OR REPLACE FUNCTION</> command.
1808 Editing is done in the same way as for <literal>\edit</>.
1809 After the editor exits, the updated command waits in the query buffer;
1810 type semicolon or <literal>\g</> to send it, or <literal>\r</>
1815 The target function can be specified by name alone, or by name
1816 and arguments, for example <literal>foo(integer, text)</>.
1817 The argument types must be given if there is more
1818 than one function of the same name.
1822 If no function is specified, a blank <command>CREATE FUNCTION</>
1823 template is presented for editing.
1827 If a line number is specified, <application>psql</application> will
1828 position the cursor on the specified line of the function body.
1829 (Note that the function body typically does not begin on the first
1834 Unlike most other meta-commands, the entire remainder of the line is
1835 always taken to be the argument(s) of <command>\ef</>, and neither
1836 variable interpolation nor backquote expansion are performed in the
1842 See under <xref linkend="app-psql-environment"
1843 endterm="app-psql-environment-title"> for how to configure and
1844 customize your editor.
1852 <term><literal>\encoding [ <replaceable class="parameter">encoding</replaceable> ]</literal></term>
1856 Sets the client character set encoding. Without an argument, this command
1857 shows the current encoding.
1864 <term><literal>\errverbose</literal></term>
1868 Repeats the most recent server error message at maximum
1869 verbosity, as though <varname>VERBOSITY</varname> were set
1870 to <literal>verbose</> and <varname>SHOW_CONTEXT</varname> were
1871 set to <literal>always</>.
1878 <term><literal>\ev <optional> <replaceable class="parameter">view_name</> <optional> <replaceable class="parameter">line_number</> </optional> </optional> </literal></term>
1882 This command fetches and edits the definition of the named view,
1883 in the form of a <command>CREATE OR REPLACE VIEW</> command.
1884 Editing is done in the same way as for <literal>\edit</>.
1885 After the editor exits, the updated command waits in the query buffer;
1886 type semicolon or <literal>\g</> to send it, or <literal>\r</>
1891 If no view is specified, a blank <command>CREATE VIEW</>
1892 template is presented for editing.
1896 If a line number is specified, <application>psql</application> will
1897 position the cursor on the specified line of the view definition.
1901 Unlike most other meta-commands, the entire remainder of the line is
1902 always taken to be the argument(s) of <command>\ev</>, and neither
1903 variable interpolation nor backquote expansion are performed in the
1911 <term><literal>\f [ <replaceable class="parameter">string</replaceable> ]</literal></term>
1915 Sets the field separator for unaligned query output. The default
1916 is the vertical bar (<literal>|</literal>). It is equivalent to
1917 <command>\pset fieldsep</command>.
1924 <term><literal>\g [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
1925 <term><literal>\g [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
1928 Sends the current query buffer to the server for execution.
1929 If an argument is given, the query's output is written to the named
1930 file or piped to the given shell command, instead of displaying it as
1931 usual. The file or command is written to only if the query
1932 successfully returns zero or more tuples, not if the query fails or
1933 is a non-data-returning SQL command.
1936 If the current query buffer is empty, the most recently sent query is
1937 re-executed instead. Except for that behavior, <literal>\g</literal>
1938 without an argument is essentially equivalent to a semicolon.
1939 A <literal>\g</literal> with argument is a <quote>one-shot</quote>
1940 alternative to the <command>\o</command> command.
1943 If the argument begins with <literal>|</>, then the entire remainder
1944 of the line is taken to be
1945 the <replaceable class="parameter">command</replaceable> to execute,
1946 and neither variable interpolation nor backquote expansion are
1947 performed in it. The rest of the line is simply passed literally to
1955 <term><literal>\gdesc</literal></term>
1959 Shows the description (that is, the column names and data types)
1960 of the result of the current query buffer. The query is not
1961 actually executed; however, if it contains some type of syntax
1962 error, that error will be reported in the normal way.
1966 If the current query buffer is empty, the most recently sent query
1967 is described instead.
1974 <term><literal>\gexec</literal></term>
1978 Sends the current query buffer to the server, then treats
1979 each column of each row of the query's output (if any) as a SQL
1980 statement to be executed. For example, to create an index on each
1981 column of <structname>my_table</>:
1983 => <userinput>SELECT format('create index on my_table(%I)', attname)</>
1984 -> <userinput>FROM pg_attribute</>
1985 -> <userinput>WHERE attrelid = 'my_table'::regclass AND attnum > 0</>
1986 -> <userinput>ORDER BY attnum</>
1987 -> <userinput>\gexec</>
1996 The generated queries are executed in the order in which the rows
1997 are returned, and left-to-right within each row if there is more
1998 than one column. NULL fields are ignored. The generated queries
1999 are sent literally to the server for processing, so they cannot be
2000 <application>psql</> meta-commands nor contain <application>psql</>
2001 variable references. If any individual query fails, execution of
2002 the remaining queries continues
2003 unless <varname>ON_ERROR_STOP</varname> is set. Execution of each
2004 query is subject to <varname>ECHO</varname> processing.
2005 (Setting <varname>ECHO</varname> to <literal>all</literal>
2006 or <literal>queries</literal> is often advisable when
2007 using <command>\gexec</>.) Query logging, single-step mode,
2008 timing, and other query execution features apply to each generated
2012 If the current query buffer is empty, the most recently sent query
2013 is re-executed instead.
2020 <term><literal>\gset [ <replaceable class="parameter">prefix</replaceable> ]</literal></term>
2024 Sends the current query buffer to the server and stores the
2025 query's output into <application>psql</> variables (see <xref
2026 linkend="APP-PSQL-variables" endterm="APP-PSQL-variables-title">).
2027 The query to be executed must return exactly one row. Each column of
2028 the row is stored into a separate variable, named the same as the
2029 column. For example:
2031 => <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput>
2032 -> <userinput>\gset</userinput>
2033 => <userinput>\echo :var1 :var2</userinput>
2038 If you specify a <replaceable class="parameter">prefix</replaceable>,
2039 that string is prepended to the query's column names to create the
2040 variable names to use:
2042 => <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput>
2043 -> <userinput>\gset result_</userinput>
2044 => <userinput>\echo :result_var1 :result_var2</userinput>
2049 If a column result is NULL, the corresponding variable is unset
2050 rather than being set.
2053 If the query fails or does not return one row,
2054 no variables are changed.
2057 If the current query buffer is empty, the most recently sent query
2058 is re-executed instead.
2065 <term><literal>\gx [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2066 <term><literal>\gx [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
2069 <literal>\gx</literal> is equivalent to <literal>\g</literal>, but
2070 forces expanded output mode for this query. See <literal>\x</literal>.
2077 <term><literal>\h</literal> or <literal>\help</literal> <literal>[ <replaceable class="parameter">command</replaceable> ]</literal></term>
2080 Gives syntax help on the specified <acronym>SQL</acronym>
2081 command. If <replaceable class="parameter">command</replaceable>
2082 is not specified, then <application>psql</application> will list
2083 all the commands for which syntax help is available. If
2084 <replaceable class="parameter">command</replaceable> is an
2085 asterisk (<literal>*</literal>), then syntax help on all
2086 <acronym>SQL</acronym> commands is shown.
2090 Unlike most other meta-commands, the entire remainder of the line is
2091 always taken to be the argument(s) of <command>\help</>, and neither
2092 variable interpolation nor backquote expansion are performed in the
2098 To simplify typing, commands that consists of several words do
2099 not have to be quoted. Thus it is fine to type <userinput>\help
2100 alter table</userinput>.
2108 <term><literal>\H</literal> or <literal>\html</literal></term>
2111 Turns on <acronym>HTML</acronym> query output format. If the
2112 <acronym>HTML</acronym> format is already on, it is switched
2113 back to the default aligned text format. This command is for
2114 compatibility and convenience, but see <command>\pset</command>
2115 about setting other output options.
2122 <term><literal>\i</literal> or <literal>\include</literal> <replaceable class="parameter">filename</replaceable></term>
2125 Reads input from the file <replaceable
2126 class="parameter">filename</replaceable> and executes it as
2127 though it had been typed on the keyboard.
2130 If <replaceable>filename</replaceable> is <literal>-</literal>
2131 (hyphen), then standard input is read until an EOF indication
2132 or <command>\q</> meta-command. This can be used to intersperse
2133 interactive input with input from files. Note that Readline behavior
2134 will be used only if it is active at the outermost level.
2138 If you want to see the lines on the screen as they are read you
2139 must set the variable <varname>ECHO</varname> to
2140 <literal>all</literal>.
2148 <term><literal>\if</literal> <replaceable class="parameter">expression</replaceable></term>
2149 <term><literal>\elif</literal> <replaceable class="parameter">expression</replaceable></term>
2150 <term><literal>\else</literal></term>
2151 <term><literal>\endif</literal></term>
2154 This group of commands implements nestable conditional blocks.
2155 A conditional block must begin with an <command>\if</command> and end
2156 with an <command>\endif</command>. In between there may be any number
2157 of <command>\elif</command> clauses, which may optionally be followed
2158 by a single <command>\else</command> clause. Ordinary queries and
2159 other types of backslash commands may (and usually do) appear between
2160 the commands forming a conditional block.
2163 The <command>\if</command> and <command>\elif</command> commands read
2164 their argument(s) and evaluate them as a boolean expression. If the
2165 expression yields <literal>true</literal> then processing continues
2166 normally; otherwise, lines are skipped until a
2167 matching <command>\elif</command>, <command>\else</command>,
2168 or <command>\endif</command> is reached. Once
2169 an <command>\if</command> or <command>\elif</command> test has
2170 succeeded, the arguments of later <command>\elif</command> commands in
2171 the same block are not evaluated but are treated as false. Lines
2172 following an <command>\else</command> are processed only if no earlier
2173 matching <command>\if</command> or <command>\elif</command> succeeded.
2176 The <replaceable class="parameter">expression</replaceable> argument
2177 of an <command>\if</command> or <command>\elif</command> command
2178 is subject to variable interpolation and backquote expansion, just
2179 like any other backslash command argument. After that it is evaluated
2180 like the value of an on/off option variable. So a valid value
2181 is any unambiguous case-insensitive match for one of:
2182 <literal>true</literal>, <literal>false</literal>, <literal>1</literal>,
2183 <literal>0</literal>, <literal>on</literal>, <literal>off</literal>,
2184 <literal>yes</literal>, <literal>no</literal>. For example,
2185 <literal>t</literal>, <literal>T</literal>, and <literal>tR</literal>
2186 will all be considered to be <literal>true</literal>.
2189 Expressions that do not properly evaluate to true or false will
2190 generate a warning and be treated as false.
2193 Lines being skipped are parsed normally to identify queries and
2194 backslash commands, but queries are not sent to the server, and
2195 backslash commands other than conditionals
2196 (<command>\if</command>, <command>\elif</command>,
2197 <command>\else</command>, <command>\endif</command>) are
2198 ignored. Conditional commands are checked only for valid nesting.
2199 Variable references in skipped lines are not expanded, and backquote
2200 expansion is not performed either.
2203 All the backslash commands of a given conditional block must appear in
2204 the same source file. If EOF is reached on the main input file or an
2205 <command>\include</command>-ed file before all local
2206 <command>\if</command>-blocks have been closed,
2207 then <application>psql</> will raise an error.
2213 -- check for the existence of two separate records in the database and store
2214 -- the results in separate psql variables
2216 EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer,
2217 EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee
2220 SELECT * FROM customer WHERE customer_id = 123;
2222 \echo 'is not a customer but is an employee'
2223 SELECT * FROM employee WHERE employee_id = 456;
2226 \echo 'not a customer or employee'
2228 \echo 'this will never print'
2237 <term><literal>\ir</literal> or <literal>\include_relative</literal> <replaceable class="parameter">filename</replaceable></term>
2240 The <literal>\ir</> command is similar to <literal>\i</>, but resolves
2241 relative file names differently. When executing in interactive mode,
2242 the two commands behave identically. However, when invoked from a
2243 script, <literal>\ir</literal> interprets file names relative to the
2244 directory in which the script is located, rather than the current
2252 <term><literal>\l[+]</literal> or <literal>\list[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2255 List the databases in the server and show their names, owners,
2256 character set encodings, and access privileges.
2257 If <replaceable class="parameter">pattern</replaceable> is specified,
2258 only databases whose names match the pattern are listed.
2259 If <literal>+</literal> is appended to the command name, database
2260 sizes, default tablespaces, and descriptions are also displayed.
2261 (Size information is only available for databases that the current
2262 user can connect to.)
2269 <term><literal>\lo_export <replaceable class="parameter">loid</replaceable> <replaceable class="parameter">filename</replaceable></literal></term>
2273 Reads the large object with <acronym>OID</acronym> <replaceable
2274 class="parameter">loid</replaceable> from the database and
2275 writes it to <replaceable
2276 class="parameter">filename</replaceable>. Note that this is
2277 subtly different from the server function
2278 <function>lo_export</function>, which acts with the permissions
2279 of the user that the database server runs as and on the server's
2284 Use <command>\lo_list</command> to find out the large object's
2285 <acronym>OID</acronym>.
2293 <term><literal>\lo_import <replaceable class="parameter">filename</replaceable> [ <replaceable class="parameter">comment</replaceable> ]</literal></term>
2297 Stores the file into a <productname>PostgreSQL</productname>
2298 large object. Optionally, it associates the given
2299 comment with the object. Example:
2301 foo=> <userinput>\lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'</userinput>
2304 The response indicates that the large object received object
2305 ID 152801, which can be used to access the newly-created large
2306 object in the future. For the sake of readability, it is
2307 recommended to always associate a human-readable comment with
2308 every object. Both OIDs and comments can be viewed with the
2309 <command>\lo_list</command> command.
2313 Note that this command is subtly different from the server-side
2314 <function>lo_import</function> because it acts as the local user
2315 on the local file system, rather than the server's user and file
2322 <term><literal>\lo_list</literal></term>
2325 Shows a list of all <productname>PostgreSQL</productname>
2326 large objects currently stored in the database,
2327 along with any comments provided for them.
2333 <term><literal>\lo_unlink <replaceable class="parameter">loid</replaceable></literal></term>
2337 Deletes the large object with <acronym>OID</acronym>
2338 <replaceable class="parameter">loid</replaceable> from the
2344 Use <command>\lo_list</command> to find out the large object's
2345 <acronym>OID</acronym>.
2353 <term><literal>\o</literal> or <literal>\out [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2354 <term><literal>\o</literal> or <literal>\out [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
2357 Arranges to save future query results to the file <replaceable
2358 class="parameter">filename</replaceable> or pipe future results
2359 to the shell command <replaceable
2360 class="parameter">command</replaceable>. If no argument is
2361 specified, the query output is reset to the standard output.
2365 If the argument begins with <literal>|</>, then the entire remainder
2366 of the line is taken to be
2367 the <replaceable class="parameter">command</replaceable> to execute,
2368 and neither variable interpolation nor backquote expansion are
2369 performed in it. The rest of the line is simply passed literally to
2374 <quote>Query results</quote> includes all tables, command
2375 responses, and notices obtained from the database server, as
2376 well as output of various backslash commands that query the
2377 database (such as <command>\d</command>); but not error
2383 To intersperse text output in between query results, use
2384 <command>\qecho</command>.
2392 <term><literal>\p</literal> or <literal>\print</literal></term>
2395 Print the current query buffer to the standard output.
2396 If the current query buffer is empty, the most recently executed query
2403 <term><literal>\password [ <replaceable class="parameter">username</replaceable> ]</literal></term>
2406 Changes the password of the specified user (by default, the current
2407 user). This command prompts for the new password, encrypts it, and
2408 sends it to the server as an <command>ALTER ROLE</> command. This
2409 makes sure that the new password does not appear in cleartext in the
2410 command history, the server log, or elsewhere.
2416 <term><literal>\prompt [ <replaceable class="parameter">text</replaceable> ] <replaceable class="parameter">name</replaceable></literal></term>
2419 Prompts the user to supply text, which is assigned to the variable
2420 <replaceable class="parameter">name</>.
2421 An optional prompt string, <replaceable
2422 class="parameter">text</>, can be specified. (For multiword
2423 prompts, surround the text with single quotes.)
2427 By default, <literal>\prompt</> uses the terminal for input and
2428 output. However, if the <option>-f</> command line switch was
2429 used, <literal>\prompt</> uses standard input and standard output.
2435 <term><literal>\pset [ <replaceable class="parameter">option</replaceable> [ <replaceable class="parameter">value</replaceable> ] ]</literal></term>
2439 This command sets options affecting the output of query result tables.
2440 <replaceable class="parameter">option</replaceable>
2441 indicates which option is to be set. The semantics of
2442 <replaceable class="parameter">value</replaceable> vary depending
2443 on the selected option. For some options, omitting <replaceable
2444 class="parameter">value</replaceable> causes the option to be toggled
2445 or unset, as described under the particular option. If no such
2446 behavior is mentioned, then omitting
2447 <replaceable class="parameter">value</replaceable> just results in
2448 the current setting being displayed.
2452 <command>\pset</command> without any arguments displays the current status
2453 of all printing options.
2457 Adjustable printing options are:
2460 <term><literal>border</literal></term>
2463 The <replaceable class="parameter">value</replaceable> must be a
2464 number. In general, the higher
2465 the number the more borders and lines the tables will have,
2466 but details depend on the particular format.
2467 In <acronym>HTML</acronym> format, this will translate directly
2468 into the <literal>border=...</literal> attribute.
2469 In most other formats only values 0 (no border), 1 (internal
2470 dividing lines), and 2 (table frame) make sense, and values above 2
2471 will be treated the same as <literal>border = 2</literal>.
2472 The <literal>latex</literal> and <literal>latex-longtable</literal>
2473 formats additionally allow a value of 3 to add dividing lines
2480 <term><literal>columns</literal></term>
2483 Sets the target width for the <literal>wrapped</> format, and also
2484 the width limit for determining whether output is wide enough to
2485 require the pager or switch to the vertical display in expanded auto
2487 Zero (the default) causes the target width to be controlled by the
2488 environment variable <envar>COLUMNS</>, or the detected screen width
2489 if <envar>COLUMNS</> is not set.
2490 In addition, if <literal>columns</> is zero then the
2491 <literal>wrapped</> format only affects screen output.
2492 If <literal>columns</> is nonzero then file and pipe output is
2493 wrapped to that width as well.
2499 <term><literal>expanded</literal> (or <literal>x</literal>)</term>
2502 If <replaceable class="parameter">value</replaceable> is specified it
2503 must be either <literal>on</literal> or <literal>off</literal>, which
2504 will enable or disable expanded mode, or <literal>auto</literal>.
2505 If <replaceable class="parameter">value</replaceable> is omitted the
2506 command toggles between the on and off settings. When expanded mode
2507 is enabled, query results are displayed in two columns, with the
2508 column name on the left and the data on the right. This mode is
2509 useful if the data wouldn't fit on the screen in the
2510 normal <quote>horizontal</quote> mode. In the auto setting, the
2511 expanded mode is used whenever the query output has more than one
2512 column and is wider than the screen; otherwise, the regular mode is
2513 used. The auto setting is only
2514 effective in the aligned and wrapped formats. In other formats, it
2515 always behaves as if the expanded mode is off.
2521 <term><literal>fieldsep</literal></term>
2524 Specifies the field separator to be used in unaligned output
2525 format. That way one can create, for example, tab- or
2526 comma-separated output, which other programs might prefer. To
2527 set a tab as field separator, type <literal>\pset fieldsep
2528 '\t'</literal>. The default field separator is
2529 <literal>'|'</literal> (a vertical bar).
2535 <term><literal>fieldsep_zero</literal></term>
2538 Sets the field separator to use in unaligned output format to a zero
2545 <term><literal>footer</literal></term>
2548 If <replaceable class="parameter">value</replaceable> is specified
2549 it must be either <literal>on</literal> or <literal>off</literal>
2550 which will enable or disable display of the table footer
2551 (the <literal>(<replaceable>n</> rows)</literal> count).
2552 If <replaceable class="parameter">value</replaceable> is omitted the
2553 command toggles footer display on or off.
2559 <term><literal>format</literal></term>
2562 Sets the output format to one of <literal>unaligned</literal>,
2563 <literal>aligned</literal>, <literal>wrapped</literal>,
2564 <literal>html</literal>, <literal>asciidoc</literal>,
2565 <literal>latex</literal> (uses <literal>tabular</literal>),
2566 <literal>latex-longtable</literal>, or
2567 <literal>troff-ms</literal>.
2568 Unique abbreviations are allowed. (That would mean one letter
2572 <para><literal>unaligned</> format writes all columns of a row on one
2573 line, separated by the currently active field separator. This
2574 is useful for creating output that might be intended to be read
2575 in by other programs (for example, tab-separated or comma-separated
2579 <para><literal>aligned</literal> format is the standard, human-readable,
2580 nicely formatted text output; this is the default.
2583 <para><literal>wrapped</> format is like <literal>aligned</> but wraps
2584 wide data values across lines to make the output fit in the target
2585 column width. The target width is determined as described under
2586 the <literal>columns</> option. Note that <application>psql</> will
2587 not attempt to wrap column header titles; therefore,
2588 <literal>wrapped</> format behaves the same as <literal>aligned</>
2589 if the total width needed for column headers exceeds the target.
2593 The <literal>html</>, <literal>asciidoc</>, <literal>latex</>,
2594 <literal>latex-longtable</literal>, and <literal>troff-ms</>
2595 formats put out tables that are intended to
2596 be included in documents using the respective mark-up
2597 language. They are not complete documents! This might not be
2598 necessary in <acronym>HTML</acronym>, but in
2599 <application>LaTeX</application> you must have a complete
2600 document wrapper. <literal>latex-longtable</literal>
2601 also requires the <application>LaTeX</application>
2602 <literal>longtable</literal> and <literal>booktabs</> packages.
2608 <term><literal>linestyle</literal></term>
2611 Sets the border line drawing style to one
2612 of <literal>ascii</literal>, <literal>old-ascii</literal>,
2613 or <literal>unicode</literal>.
2614 Unique abbreviations are allowed. (That would mean one
2616 The default setting is <literal>ascii</>.
2617 This option only affects the <literal>aligned</> and
2618 <literal>wrapped</> output formats.
2621 <para><literal>ascii</literal> style uses plain <acronym>ASCII</acronym>
2622 characters. Newlines in data are shown using
2623 a <literal>+</literal> symbol in the right-hand margin.
2624 When the <literal>wrapped</literal> format wraps data from
2625 one line to the next without a newline character, a dot
2626 (<literal>.</>) is shown in the right-hand margin of the first line,
2627 and again in the left-hand margin of the following line.
2630 <para><literal>old-ascii</literal> style uses plain <acronym>ASCII</>
2631 characters, using the formatting style used
2632 in <productname>PostgreSQL</productname> 8.4 and earlier.
2633 Newlines in data are shown using a <literal>:</literal>
2634 symbol in place of the left-hand column separator.
2635 When the data is wrapped from one line
2636 to the next without a newline character, a <literal>;</>
2637 symbol is used in place of the left-hand column separator.
2640 <para><literal>unicode</literal> style uses Unicode box-drawing characters.
2641 Newlines in data are shown using a carriage return symbol
2642 in the right-hand margin. When the data is wrapped from one line
2643 to the next without a newline character, an ellipsis symbol
2644 is shown in the right-hand margin of the first line, and
2645 again in the left-hand margin of the following line.
2649 When the <literal>border</> setting is greater than zero,
2650 the <literal>linestyle</literal> option also determines the
2651 characters with which the border lines are drawn.
2652 Plain <acronym>ASCII</acronym> characters work everywhere, but
2653 Unicode characters look nicer on displays that recognize them.
2659 <term><literal>null</literal></term>
2662 Sets the string to be printed in place of a null value.
2663 The default is to print nothing, which can easily be mistaken for
2664 an empty string. For example, one might prefer <literal>\pset null
2671 <term><literal>numericlocale</literal></term>
2674 If <replaceable class="parameter">value</replaceable> is specified
2675 it must be either <literal>on</literal> or <literal>off</literal>
2676 which will enable or disable display of a locale-specific character
2677 to separate groups of digits to the left of the decimal marker.
2678 If <replaceable class="parameter">value</replaceable> is omitted the
2679 command toggles between regular and locale-specific numeric output.
2685 <term><literal>pager</literal></term>
2688 Controls use of a pager program for query and <application>psql</>
2689 help output. If the environment variable <envar>PSQL_PAGER</envar>
2690 or <envar>PAGER</envar> is set, the output is piped to the
2691 specified program. Otherwise a platform-dependent default program
2692 (such as <filename>more</filename>) is used.
2696 When the <literal>pager</> option is <literal>off</>, the pager
2697 program is not used. When the <literal>pager</> option is
2698 <literal>on</>, the pager is used when appropriate, i.e., when the
2699 output is to a terminal and will not fit on the screen.
2700 The <literal>pager</> option can also be set to <literal>always</>,
2701 which causes the pager to be used for all terminal output regardless
2702 of whether it fits on the screen. <literal>\pset pager</>
2703 without a <replaceable class="parameter">value</replaceable>
2704 toggles pager use on and off.
2710 <term><literal>pager_min_lines</literal></term>
2713 If <literal>pager_min_lines</> is set to a number greater than the
2714 page height, the pager program will not be called unless there are
2715 at least this many lines of output to show. The default setting
2722 <term><literal>recordsep</literal></term>
2725 Specifies the record (line) separator to use in unaligned
2726 output format. The default is a newline character.
2732 <term><literal>recordsep_zero</literal></term>
2735 Sets the record separator to use in unaligned output format to a zero
2742 <term><literal>tableattr</literal> (or <literal>T</literal>)</term>
2745 In <acronym>HTML</acronym> format, this specifies attributes
2746 to be placed inside the <sgmltag>table</sgmltag> tag. This
2747 could for example be <literal>cellpadding</literal> or
2748 <literal>bgcolor</literal>. Note that you probably don't want
2749 to specify <literal>border</literal> here, as that is already
2750 taken care of by <literal>\pset border</literal>.
2752 <replaceable class="parameter">value</replaceable> is given,
2753 the table attributes are unset.
2756 In <literal>latex-longtable</literal> format, this controls
2757 the proportional width of each column containing a left-aligned
2758 data type. It is specified as a whitespace-separated list of values,
2759 e.g. <literal>'0.2 0.2 0.6'</>. Unspecified output columns
2760 use the last specified value.
2766 <term><literal>title</literal> (or <literal>C</literal>)</term>
2769 Sets the table title for any subsequently printed tables. This
2770 can be used to give your output descriptive tags. If no
2771 <replaceable class="parameter">value</replaceable> is given,
2778 <term><literal>tuples_only</literal> (or <literal>t</literal>)</term>
2781 If <replaceable class="parameter">value</replaceable> is specified
2782 it must be either <literal>on</literal> or <literal>off</literal>
2783 which will enable or disable tuples-only mode.
2784 If <replaceable class="parameter">value</replaceable> is omitted the
2785 command toggles between regular and tuples-only output.
2786 Regular output includes extra information such
2787 as column headers, titles, and various footers. In tuples-only
2788 mode, only actual table data is shown.
2794 <term><literal>unicode_border_linestyle</literal></term>
2797 Sets the border drawing style for the <literal>unicode</literal>
2798 line style to one of <literal>single</literal>
2799 or <literal>double</literal>.
2805 <term><literal>unicode_column_linestyle</literal></term>
2808 Sets the column drawing style for the <literal>unicode</literal>
2809 line style to one of <literal>single</literal>
2810 or <literal>double</literal>.
2816 <term><literal>unicode_header_linestyle</literal></term>
2819 Sets the header drawing style for the <literal>unicode</literal>
2820 line style to one of <literal>single</literal>
2821 or <literal>double</literal>.
2829 Illustrations of how these different formats look can be seen in
2830 the <xref linkend="APP-PSQL-examples"
2831 endterm="APP-PSQL-examples-title"> section.
2836 There are various shortcut commands for <command>\pset</command>. See
2837 <command>\a</command>, <command>\C</command>, <command>\f</command>,
2838 <command>\H</command>, <command>\t</command>, <command>\T</command>,
2839 and <command>\x</command>.
2848 <term><literal>\q</literal> or <literal>\quit</literal></term>
2851 Quits the <application>psql</application> program.
2852 In a script file, only execution of that script is terminated.
2859 <term><literal>\qecho <replaceable class="parameter">text</replaceable> [ ... ] </literal></term>
2862 This command is identical to <command>\echo</command> except
2863 that the output will be written to the query output channel, as
2864 set by <command>\o</command>.
2871 <term><literal>\r</literal> or <literal>\reset</literal></term>
2874 Resets (clears) the query buffer.
2881 <term><literal>\s [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2884 Print <application>psql</application>'s command line history
2885 to <replaceable class="parameter">filename</replaceable>.
2886 If <replaceable class="parameter">filename</replaceable> is omitted,
2887 the history is written to the standard output (using the pager if
2888 appropriate). This command is not available
2889 if <application>psql</application> was built
2890 without <application>Readline</application> support.
2897 <term><literal>\set [ <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> [ ... ] ] ]</literal></term>
2901 Sets the <application>psql</> variable <replaceable
2902 class="parameter">name</replaceable> to <replaceable
2903 class="parameter">value</replaceable>, or if more than one value
2904 is given, to the concatenation of all of them. If only one
2905 argument is given, the variable is set to an empty-string value. To
2906 unset a variable, use the <command>\unset</command> command.
2909 <para><command>\set</> without any arguments displays the names and values
2910 of all currently-set <application>psql</> variables.
2914 Valid variable names can contain letters, digits, and
2915 underscores. See the section <xref
2916 linkend="APP-PSQL-variables"
2917 endterm="APP-PSQL-variables-title"> below for details.
2918 Variable names are case-sensitive.
2922 Certain variables are special, in that they
2923 control <application>psql</application>'s behavior or are
2924 automatically set to reflect connection state. These variables are
2925 documented in <xref linkend="APP-PSQL-variables"
2926 endterm="APP-PSQL-variables-title">, below.
2931 This command is unrelated to the <acronym>SQL</acronym>
2932 command <xref linkend="SQL-SET">.
2940 <term><literal>\setenv <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> ]</literal></term>
2944 Sets the environment variable <replaceable
2945 class="parameter">name</replaceable> to <replaceable
2946 class="parameter">value</replaceable>, or if the
2947 <replaceable class="parameter">value</replaceable> is
2948 not supplied, unsets the environment variable. Example:
2950 testdb=> <userinput>\setenv PAGER less</userinput>
2951 testdb=> <userinput>\setenv LESS -imx4F</userinput>
2952 </programlisting></para>
2957 <term><literal>\sf[+] <replaceable class="parameter">function_description</> </literal></term>
2961 This command fetches and shows the definition of the named function,
2962 in the form of a <command>CREATE OR REPLACE FUNCTION</> command.
2963 The definition is printed to the current query output channel,
2964 as set by <command>\o</command>.
2968 The target function can be specified by name alone, or by name
2969 and arguments, for example <literal>foo(integer, text)</>.
2970 The argument types must be given if there is more
2971 than one function of the same name.
2975 If <literal>+</literal> is appended to the command name, then the
2976 output lines are numbered, with the first line of the function body
2981 Unlike most other meta-commands, the entire remainder of the line is
2982 always taken to be the argument(s) of <command>\sf</>, and neither
2983 variable interpolation nor backquote expansion are performed in the
2991 <term><literal>\sv[+] <replaceable class="parameter">view_name</> </literal></term>
2995 This command fetches and shows the definition of the named view,
2996 in the form of a <command>CREATE OR REPLACE VIEW</> command.
2997 The definition is printed to the current query output channel,
2998 as set by <command>\o</command>.
3002 If <literal>+</literal> is appended to the command name, then the
3003 output lines are numbered from 1.
3007 Unlike most other meta-commands, the entire remainder of the line is
3008 always taken to be the argument(s) of <command>\sv</>, and neither
3009 variable interpolation nor backquote expansion are performed in the
3017 <term><literal>\t</literal></term>
3020 Toggles the display of output column name headings and row count
3021 footer. This command is equivalent to <literal>\pset
3022 tuples_only</literal> and is provided for convenience.
3029 <term><literal>\T <replaceable class="parameter">table_options</replaceable></literal></term>
3032 Specifies attributes to be placed within the
3033 <sgmltag>table</sgmltag> tag in <acronym>HTML</acronym>
3034 output format. This command is equivalent to <literal>\pset
3035 tableattr <replaceable
3036 class="parameter">table_options</replaceable></literal>.
3043 <term><literal>\timing [ <replaceable class="parameter">on</replaceable> | <replaceable class="parameter">off</replaceable> ]</literal></term>
3046 With a parameter, turns displaying of how long each SQL statement
3047 takes on or off. Without a parameter, toggles the display between
3048 on and off. The display is in milliseconds; intervals longer than
3049 1 second are also shown in minutes:seconds format, with hours and
3050 days fields added if needed.
3057 <term><literal>\unset <replaceable class="parameter">name</replaceable></literal></term>
3061 Unsets (deletes) the <application>psql</> variable <replaceable
3062 class="parameter">name</replaceable>.
3066 Most variables that control <application>psql</application>'s behavior
3067 cannot be unset; instead, an <literal>\unset</> command is interpreted
3068 as setting them to their default values.
3069 See <xref linkend="APP-PSQL-variables"
3070 endterm="APP-PSQL-variables-title">, below.
3077 <term><literal>\w</literal> or <literal>\write</literal> <replaceable class="parameter">filename</replaceable></term>
3078 <term><literal>\w</literal> or <literal>\write</literal> <literal>|</><replaceable class="parameter">command</replaceable></term>
3081 Writes the current query buffer to the file <replaceable
3082 class="parameter">filename</replaceable> or pipes it to the shell
3083 command <replaceable class="parameter">command</replaceable>.
3084 If the current query buffer is empty, the most recently executed query
3089 If the argument begins with <literal>|</>, then the entire remainder
3090 of the line is taken to be
3091 the <replaceable class="parameter">command</replaceable> to execute,
3092 and neither variable interpolation nor backquote expansion are
3093 performed in it. The rest of the line is simply passed literally to
3101 <term><literal>\watch [ <replaceable class="parameter">seconds</replaceable> ]</literal></term>
3104 Repeatedly execute the current query buffer (as <literal>\g</> does)
3105 until interrupted or the query fails. Wait the specified number of
3106 seconds (default 2) between executions. Each query result is
3107 displayed with a header that includes the <literal>\pset title</>
3108 string (if any), the time as of query start, and the delay interval.
3111 If the current query buffer is empty, the most recently sent query
3112 is re-executed instead.
3119 <term><literal>\x [ <replaceable class="parameter">on</replaceable> | <replaceable class="parameter">off</replaceable> | <replaceable class="parameter">auto</replaceable> ]</literal></term>
3122 Sets or toggles expanded table formatting mode. As such it is equivalent to
3123 <literal>\pset expanded</literal>.
3130 <term><literal>\z [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
3133 Lists tables, views and sequences with their
3134 associated access privileges.
3135 If a <replaceable class="parameter">pattern</replaceable> is
3136 specified, only tables, views and sequences whose names match the
3141 This is an alias for <command>\dp</command> (<quote>display
3142 privileges</quote>).
3149 <term><literal>\! [ <replaceable class="parameter">command</replaceable> ]</literal></term>
3152 With no argument, escapes to a sub-shell; <application>psql</>
3153 resumes when the sub-shell exits. With an argument, executes the
3154 shell command <replaceable class="parameter">command</replaceable>.
3158 Unlike most other meta-commands, the entire remainder of the line is
3159 always taken to be the argument(s) of <command>\!</>, and neither
3160 variable interpolation nor backquote expansion are performed in the
3161 arguments. The rest of the line is simply passed literally to the
3169 <term><literal>\? [ <replaceable class="parameter">topic</> ]</literal></term>
3172 Shows help information. The optional
3173 <replaceable class="parameter">topic</> parameter
3174 (defaulting to <literal>commands</>) selects which part of <application>psql</application> is
3175 explained: <literal>commands</> describes <application>psql</>'s
3176 backslash commands; <literal>options</> describes the command-line
3177 options that can be passed to <application>psql</>;
3178 and <literal>variables</> shows help about <application>psql</application> configuration
3186 <term><literal>\;</literal></term>
3189 Backslash-semicolon is not a meta-command in the same way as the
3190 preceding commands; rather, it simply causes a semicolon to be
3191 added to the query buffer without any further processing.
3195 Normally, <application>psql</> will dispatch a SQL command to the
3196 server as soon as it reaches the command-ending semicolon, even if
3197 more input remains on the current line. Thus for example entering
3199 select 1; select 2; select 3;
3201 will result in the three SQL commands being individually sent to
3202 the server, with each one's results being displayed before
3203 continuing to the next command. However, a semicolon entered
3204 as <literal>\;</> will not trigger command processing, so that the
3205 command before it and the one after are effectively combined and
3206 sent to the server in one request. So for example
3208 select 1\; select 2\; select 3;
3210 results in sending the three SQL commands to the server in a single
3211 request, when the non-backslashed semicolon is reached.
3212 The server executes such a request as a single transaction,
3213 unless there are explicit <command>BEGIN</>/<command>COMMIT</>
3214 commands included in the string to divide it into multiple
3215 transactions. (See <xref linkend="protocol-flow-multi-statement">
3216 for more details about how the server handles multi-query strings.)
3217 <application>psql</application> prints only the last query result
3218 it receives for each request; in this example, although all
3219 three <command>SELECT</>s are indeed executed, <application>psql</>
3220 only prints the <literal>3</>.
3228 <refsect3 id="APP-PSQL-patterns">
3229 <title id="APP-PSQL-patterns-title">Patterns</title>
3232 <primary>patterns</primary>
3233 <secondary>in psql and pg_dump</secondary>
3237 The various <literal>\d</> commands accept a <replaceable
3238 class="parameter">pattern</replaceable> parameter to specify the
3239 object name(s) to be displayed. In the simplest case, a pattern
3240 is just the exact name of the object. The characters within a
3241 pattern are normally folded to lower case, just as in SQL names;
3242 for example, <literal>\dt FOO</> will display the table named
3243 <literal>foo</>. As in SQL names, placing double quotes around
3244 a pattern stops folding to lower case. Should you need to include
3245 an actual double quote character in a pattern, write it as a pair
3246 of double quotes within a double-quote sequence; again this is in
3247 accord with the rules for SQL quoted identifiers. For example,
3248 <literal>\dt "FOO""BAR"</> will display the table named
3249 <literal>FOO"BAR</> (not <literal>foo"bar</>). Unlike the normal
3250 rules for SQL names, you can put double quotes around just part
3251 of a pattern, for instance <literal>\dt FOO"FOO"BAR</> will display
3252 the table named <literal>fooFOObar</>.
3256 Whenever the <replaceable class="parameter">pattern</replaceable> parameter
3257 is omitted completely, the <literal>\d</> commands display all objects
3258 that are visible in the current schema search path — this is
3259 equivalent to using <literal>*</> as the pattern.
3260 (An object is said to be <firstterm>visible</> if its
3261 containing schema is in the search path and no object of the same
3262 kind and name appears earlier in the search path. This is equivalent to the
3263 statement that the object can be referenced by name without explicit
3264 schema qualification.)
3265 To see all objects in the database regardless of visibility,
3266 use <literal>*.*</> as the pattern.
3270 Within a pattern, <literal>*</> matches any sequence of characters
3271 (including no characters) and <literal>?</> matches any single character.
3272 (This notation is comparable to Unix shell file name patterns.)
3273 For example, <literal>\dt int*</> displays tables whose names
3274 begin with <literal>int</>. But within double quotes, <literal>*</>
3275 and <literal>?</> lose these special meanings and are just matched
3280 A pattern that contains a dot (<literal>.</>) is interpreted as a schema
3281 name pattern followed by an object name pattern. For example,
3282 <literal>\dt foo*.*bar*</> displays all tables whose table name
3283 includes <literal>bar</> that are in schemas whose schema name
3284 starts with <literal>foo</>. When no dot appears, then the pattern
3285 matches only objects that are visible in the current schema search path.
3286 Again, a dot within double quotes loses its special meaning and is matched
3291 Advanced users can use regular-expression notations such as character
3292 classes, for example <literal>[0-9]</> to match any digit. All regular
3293 expression special characters work as specified in
3294 <xref linkend="functions-posix-regexp">, except for <literal>.</> which
3295 is taken as a separator as mentioned above, <literal>*</> which is
3296 translated to the regular-expression notation <literal>.*</>,
3297 <literal>?</> which is translated to <literal>.</>, and
3298 <literal>$</> which is matched literally. You can emulate
3299 these pattern characters at need by writing
3300 <literal>?</> for <literal>.</>,
3301 <literal>(<replaceable class="parameter">R</replaceable>+|)</literal> for
3302 <literal><replaceable class="parameter">R</replaceable>*</literal>, or
3303 <literal>(<replaceable class="parameter">R</replaceable>|)</literal> for
3304 <literal><replaceable class="parameter">R</replaceable>?</literal>.
3305 <literal>$</> is not needed as a regular-expression character since
3306 the pattern must match the whole name, unlike the usual
3307 interpretation of regular expressions (in other words, <literal>$</>
3308 is automatically appended to your pattern). Write <literal>*</> at the
3309 beginning and/or end if you don't wish the pattern to be anchored.
3310 Note that within double quotes, all regular expression special characters
3311 lose their special meanings and are matched literally. Also, the regular
3312 expression special characters are matched literally in operator name
3313 patterns (i.e., the argument of <literal>\do</>).
3319 <title>Advanced Features</title>
3321 <refsect3 id="APP-PSQL-variables">
3322 <title id="APP-PSQL-variables-title">Variables</title>
3325 <application>psql</application> provides variable substitution
3326 features similar to common Unix command shells.
3327 Variables are simply name/value pairs, where the value
3328 can be any string of any length. The name must consist of letters
3329 (including non-Latin letters), digits, and underscores.
3333 To set a variable, use the <application>psql</application> meta-command
3334 <command>\set</command>. For example,
3336 testdb=> <userinput>\set foo bar</userinput>
3338 sets the variable <literal>foo</literal> to the value
3339 <literal>bar</literal>. To retrieve the content of the variable, precede
3340 the name with a colon, for example:
3342 testdb=> <userinput>\echo :foo</userinput>
3345 This works in both regular SQL commands and meta-commands; there is
3346 more detail in <xref linkend="APP-PSQL-interpolation"
3347 endterm="APP-PSQL-interpolation-title">, below.
3351 If you call <command>\set</command> without a second argument, the
3352 variable is set to an empty-string value. To unset (i.e., delete)
3353 a variable, use the command <command>\unset</command>. To show the
3354 values of all variables, call <command>\set</command> without any argument.
3359 The arguments of <command>\set</command> are subject to the same
3360 substitution rules as with other commands. Thus you can construct
3361 interesting references such as <literal>\set :foo
3362 'something'</literal> and get <quote>soft links</quote> or
3363 <quote>variable variables</quote> of <productname>Perl</productname>
3364 or <productname><acronym>PHP</acronym></productname> fame,
3365 respectively. Unfortunately (or fortunately?), there is no way to do
3366 anything useful with these constructs. On the other hand,
3367 <literal>\set bar :foo</literal> is a perfectly valid way to copy a
3373 A number of these variables are treated specially
3374 by <application>psql</application>. They represent certain option
3375 settings that can be changed at run time by altering the value of
3376 the variable, or in some cases represent changeable state of
3377 <application>psql</application>.
3378 By convention, all specially treated variables' names
3379 consist of all upper-case ASCII letters (and possibly digits and
3380 underscores). To ensure maximum compatibility in the future, avoid
3381 using such variable names for your own purposes.
3385 Variables that control <application>psql</application>'s behavior
3386 generally cannot be unset or set to invalid values. An <literal>\unset</>
3387 command is allowed but is interpreted as setting the variable to its
3388 default value. A <literal>\set</> command without a second argument is
3389 interpreted as setting the variable to <literal>on</>, for control
3390 variables that accept that value, and is rejected for others. Also,
3391 control variables that accept the values <literal>on</>
3392 and <literal>off</> will also accept other common spellings of Boolean
3393 values, such as <literal>true</> and <literal>false</>.
3397 The specially treated variables are:
3403 <varname>AUTOCOMMIT</varname>
3405 <primary>autocommit</primary>
3406 <secondary>psql</secondary>
3411 When <literal>on</> (the default), each SQL command is automatically
3412 committed upon successful completion. To postpone commit in this
3413 mode, you must enter a <command>BEGIN</> or <command>START
3414 TRANSACTION</> SQL command. When <literal>off</> or unset, SQL
3415 commands are not committed until you explicitly issue
3416 <command>COMMIT</> or <command>END</>. The autocommit-off
3417 mode works by issuing an implicit <command>BEGIN</> for you, just
3418 before any command that is not already in a transaction block and
3419 is not itself a <command>BEGIN</> or other transaction-control
3420 command, nor a command that cannot be executed inside a transaction
3421 block (such as <command>VACUUM</>).
3426 In autocommit-off mode, you must explicitly abandon any failed
3427 transaction by entering <command>ABORT</> or <command>ROLLBACK</>.
3428 Also keep in mind that if you exit the session
3429 without committing, your work will be lost.
3435 The autocommit-on mode is <productname>PostgreSQL</>'s traditional
3436 behavior, but autocommit-off is closer to the SQL spec. If you
3437 prefer autocommit-off, you might wish to set it in the system-wide
3438 <filename>psqlrc</filename> file or your
3439 <filename>~/.psqlrc</filename> file.
3446 <term><varname>COMP_KEYWORD_CASE</varname></term>
3449 Determines which letter case to use when completing an SQL key word.
3450 If set to <literal>lower</literal> or <literal>upper</literal>, the
3451 completed word will be in lower or upper case, respectively. If set
3452 to <literal>preserve-lower</literal>
3453 or <literal>preserve-upper</literal> (the default), the completed word
3454 will be in the case of the word already entered, but words being
3455 completed without anything entered will be in lower or upper case,
3462 <term><varname>DBNAME</varname></term>
3465 The name of the database you are currently connected to. This is
3466 set every time you connect to a database (including program
3467 start-up), but can be changed or unset.
3473 <term><varname>ECHO</varname></term>
3476 If set to <literal>all</literal>, all nonempty input lines are printed
3477 to standard output as they are read. (This does not apply to lines
3478 read interactively.) To select this behavior on program
3479 start-up, use the switch <option>-a</option>. If set to
3480 <literal>queries</literal>,
3481 <application>psql</application> prints each query to standard output
3482 as it is sent to the server. The switch to select this behavior is
3483 <option>-e</option>. If set to <literal>errors</literal>, then only
3484 failed queries are displayed on standard error output. The switch
3485 for this behavior is <option>-b</option>. If set to
3486 <literal>none</literal> (the default), then no queries are displayed.
3492 <term><varname>ECHO_HIDDEN</varname></term>
3495 When this variable is set to <literal>on</> and a backslash command
3496 queries the database, the query is first shown.
3497 This feature helps you to study
3498 <productname>PostgreSQL</productname> internals and provide
3499 similar functionality in your own programs. (To select this behavior
3500 on program start-up, use the switch <option>-E</option>.) If you set
3501 this variable to the value <literal>noexec</literal>, the queries are
3502 just shown but are not actually sent to the server and executed.
3503 The default value is <literal>off</>.
3509 <term><varname>ENCODING</varname></term>
3512 The current client character set encoding.
3513 This is set every time you connect to a database (including
3514 program start-up), and when you change the encoding
3515 with <literal>\encoding</>, but it can be changed or unset.
3521 <term><varname>ERROR</varname></term>
3524 <literal>true</> if the last SQL query failed, <literal>false</> if
3525 it succeeded. See also <varname>SQLSTATE</>.
3531 <term><varname>FETCH_COUNT</varname></term>
3534 If this variable is set to an integer value greater than zero,
3535 the results of <command>SELECT</command> queries are fetched
3536 and displayed in groups of that many rows, rather than the
3537 default behavior of collecting the entire result set before
3538 display. Therefore only a
3539 limited amount of memory is used, regardless of the size of
3540 the result set. Settings of 100 to 1000 are commonly used
3541 when enabling this feature.
3542 Keep in mind that when using this feature, a query might
3543 fail after having already displayed some rows.
3548 Although you can use any output format with this feature,
3549 the default <literal>aligned</> format tends to look bad
3550 because each group of <varname>FETCH_COUNT</varname> rows
3551 will be formatted separately, leading to varying column
3552 widths across the row groups. The other output formats work better.
3559 <term><varname>HISTCONTROL</varname></term>
3562 If this variable is set to <literal>ignorespace</literal>,
3563 lines which begin with a space are not entered into the history
3564 list. If set to a value of <literal>ignoredups</literal>, lines
3565 matching the previous history line are not entered. A value of
3566 <literal>ignoreboth</literal> combines the two options. If
3567 set to <literal>none</literal> (the default), all lines
3568 read in interactive mode are saved on the history list.
3572 This feature was shamelessly plagiarized from
3573 <application>Bash</application>.
3580 <term><varname>HISTFILE</varname></term>
3583 The file name that will be used to store the history list. If unset,
3584 the file name is taken from the <envar>PSQL_HISTORY</envar>
3585 environment variable. If that is not set either, the default
3586 is <filename>~/.psql_history</filename>,
3587 or <filename>%APPDATA%\postgresql\psql_history</filename> on Windows.
3588 For example, putting:
3590 \set HISTFILE ~/.psql_history- :DBNAME
3592 in <filename>~/.psqlrc</filename> will cause
3593 <application>psql</application> to maintain a separate history for
3598 This feature was shamelessly plagiarized from
3599 <application>Bash</application>.
3606 <term><varname>HISTSIZE</varname></term>
3609 The maximum number of commands to store in the command history
3610 (default 500). If set to a negative value, no limit is applied.
3614 This feature was shamelessly plagiarized from
3615 <application>Bash</application>.
3622 <term><varname>HOST</varname></term>
3625 The database server host you are currently connected to. This is
3626 set every time you connect to a database (including program
3627 start-up), but can be changed or unset.
3633 <term><varname>IGNOREEOF</varname></term>
3636 If set to 1 or less, sending an <acronym>EOF</> character (usually
3637 <keycombo action="simul"><keycap>Control</><keycap>D</></>)
3638 to an interactive session of <application>psql</application>
3639 will terminate the application. If set to a larger numeric value,
3640 that many consecutive <acronym>EOF</> characters must be typed to
3641 make an interactive session terminate. If the variable is set to a
3642 non-numeric value, it is interpreted as 10. The default is 0.
3646 This feature was shamelessly plagiarized from
3647 <application>Bash</application>.
3654 <term><varname>LASTOID</varname></term>
3657 The value of the last affected OID, as returned from an
3658 <command>INSERT</command> or <command>\lo_import</command>
3659 command. This variable is only guaranteed to be valid until
3660 after the result of the next <acronym>SQL</acronym> command has
3667 <term><varname>LAST_ERROR_MESSAGE</varname></term>
3668 <term><varname>LAST_ERROR_SQLSTATE</varname></term>
3671 The primary error message and associated SQLSTATE code for the most
3672 recent failed query in the current <application>psql</> session, or
3673 an empty string and <literal>00000</> if no error has occurred in
3674 the current session.
3681 <varname>ON_ERROR_ROLLBACK</varname>
3683 <primary>rollback</primary>
3684 <secondary>psql</secondary>
3689 When set to <literal>on</>, if a statement in a transaction block
3690 generates an error, the error is ignored and the transaction
3691 continues. When set to <literal>interactive</>, such errors are only
3692 ignored in interactive sessions, and not when reading script
3693 files. When set to <literal>off</> (the default), a statement in a
3694 transaction block that generates an error aborts the entire
3695 transaction. The error rollback mode works by issuing an
3696 implicit <command>SAVEPOINT</> for you, just before each command
3697 that is in a transaction block, and then rolling back to the
3698 savepoint if the command fails.
3704 <term><varname>ON_ERROR_STOP</varname></term>
3707 By default, command processing continues after an error. When this
3708 variable is set to <literal>on</>, processing will instead stop
3709 immediately. In interactive mode,
3710 <application>psql</application> will return to the command prompt;
3711 otherwise, <application>psql</application> will exit, returning
3712 error code 3 to distinguish this case from fatal error
3713 conditions, which are reported using error code 1. In either case,
3714 any currently running scripts (the top-level script, if any, and any
3715 other scripts which it may have in invoked) will be terminated
3716 immediately. If the top-level command string contained multiple SQL
3717 commands, processing will stop with the current command.
3723 <term><varname>PORT</varname></term>
3726 The database server port to which you are currently connected.
3727 This is set every time you connect to a database (including
3728 program start-up), but can be changed or unset.
3734 <term><varname>PROMPT1</varname></term>
3735 <term><varname>PROMPT2</varname></term>
3736 <term><varname>PROMPT3</varname></term>
3739 These specify what the prompts <application>psql</application>
3740 issues should look like. See <xref
3741 linkend="APP-PSQL-prompting"
3742 endterm="APP-PSQL-prompting-title"> below.
3748 <term><varname>QUIET</varname></term>
3751 Setting this variable to <literal>on</> is equivalent to the command
3752 line option <option>-q</option>. It is probably not too useful in
3759 <term><varname>ROW_COUNT</varname></term>
3762 The number of rows returned or affected by the last SQL query, or 0
3763 if the query failed or did not report a row count.
3769 <term><varname>SERVER_VERSION_NAME</varname></term>
3770 <term><varname>SERVER_VERSION_NUM</varname></term>
3773 The server's version number as a string, for
3774 example <literal>9.6.2</>, <literal>10.1</> or <literal>11beta1</>,
3775 and in numeric form, for
3776 example <literal>90602</> or <literal>100001</>.
3777 These are set every time you connect to a database
3778 (including program start-up), but can be changed or unset.
3784 <term><varname>SHOW_CONTEXT</varname></term>
3787 This variable can be set to the
3788 values <literal>never</>, <literal>errors</>, or <literal>always</>
3789 to control whether <literal>CONTEXT</> fields are displayed in
3790 messages from the server. The default is <literal>errors</> (meaning
3791 that context will be shown in error messages, but not in notice or
3792 warning messages). This setting has no effect
3793 when <varname>VERBOSITY</> is set to <literal>terse</>.
3794 (See also <command>\errverbose</>, for use when you want a verbose
3795 version of the error you just got.)
3801 <term><varname>SINGLELINE</varname></term>
3804 Setting this variable to <literal>on</> is equivalent to the command
3805 line option <option>-S</option>.
3811 <term><varname>SINGLESTEP</varname></term>
3814 Setting this variable to <literal>on</> is equivalent to the command
3815 line option <option>-s</option>.
3821 <term><varname>SQLSTATE</varname></term>
3824 The error code (see <xref linkend="errcodes-appendix">) associated
3825 with the last SQL query's failure, or <literal>00000</> if it
3832 <term><varname>USER</varname></term>
3835 The database user you are currently connected as. This is set
3836 every time you connect to a database (including program
3837 start-up), but can be changed or unset.
3843 <term><varname>VERBOSITY</varname></term>
3846 This variable can be set to the values <literal>default</>,
3847 <literal>verbose</>, or <literal>terse</> to control the verbosity
3849 (See also <command>\errverbose</>, for use when you want a verbose
3850 version of the error you just got.)
3856 <term><varname>VERSION</varname></term>
3857 <term><varname>VERSION_NAME</varname></term>
3858 <term><varname>VERSION_NUM</varname></term>
3861 These variables are set at program start-up to reflect
3862 <application>psql</>'s version, respectively as a verbose string,
3863 a short string (e.g., <literal>9.6.2</>, <literal>10.1</>,
3864 or <literal>11beta1</>), and a number (e.g., <literal>90602</>
3865 or <literal>100001</>). They can be changed or unset.
3874 <refsect3 id="APP-PSQL-interpolation">
3875 <title id="APP-PSQL-interpolation-title"><acronym>SQL</acronym> Interpolation</title>
3878 A key feature of <application>psql</application>
3879 variables is that you can substitute (<quote>interpolate</quote>)
3880 them into regular <acronym>SQL</acronym> statements, as well as the
3881 arguments of meta-commands. Furthermore,
3882 <application>psql</application> provides facilities for
3883 ensuring that variable values used as SQL literals and identifiers are
3884 properly quoted. The syntax for interpolating a value without
3885 any quoting is to prepend the variable name with a colon
3886 (<literal>:</literal>). For example,
3888 testdb=> <userinput>\set foo 'my_table'</userinput>
3889 testdb=> <userinput>SELECT * FROM :foo;</userinput>
3891 would query the table <literal>my_table</literal>. Note that this
3892 may be unsafe: the value of the variable is copied literally, so it can
3893 contain unbalanced quotes, or even backslash commands. You must make sure
3894 that it makes sense where you put it.
3898 When a value is to be used as an SQL literal or identifier, it is
3899 safest to arrange for it to be quoted. To quote the value of
3900 a variable as an SQL literal, write a colon followed by the variable
3901 name in single quotes. To quote the value as an SQL identifier, write
3902 a colon followed by the variable name in double quotes.
3903 These constructs deal correctly with quotes and other special
3904 characters embedded within the variable value.
3905 The previous example would be more safely written this way:
3907 testdb=> <userinput>\set foo 'my_table'</userinput>
3908 testdb=> <userinput>SELECT * FROM :"foo";</userinput>
3913 Variable interpolation will not be performed within quoted
3914 <acronym>SQL</acronym> literals and identifiers. Therefore, a
3915 construction such as <literal>':foo'</> doesn't work to produce a quoted
3916 literal from a variable's value (and it would be unsafe if it did work,
3917 since it wouldn't correctly handle quotes embedded in the value).
3921 One example use of this mechanism is to
3922 copy the contents of a file into a table column.
3923 First load the file into a variable and then interpolate the variable's
3924 value as a quoted string:
3926 testdb=> <userinput>\set content `cat my_file.txt`</userinput>
3927 testdb=> <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
3929 (Note that this still won't work if <filename>my_file.txt</filename> contains NUL bytes.
3930 <application>psql</application> does not support embedded NUL bytes in variable values.)
3934 Since colons can legally appear in SQL commands, an apparent attempt
3935 at interpolation (that is, <literal>:name</literal>,
3936 <literal>:'name'</literal>, or <literal>:"name"</literal>) is not
3937 replaced unless the named variable is currently set. In any case, you
3938 can escape a colon with a backslash to protect it from substitution.
3942 The colon syntax for variables is standard <acronym>SQL</acronym> for
3943 embedded query languages, such as <application>ECPG</application>.
3944 The colon syntaxes for array slices and type casts are
3945 <productname>PostgreSQL</productname> extensions, which can sometimes
3946 conflict with the standard usage. The colon-quote syntax for escaping a
3947 variable's value as an SQL literal or identifier is a
3948 <application>psql</application> extension.
3953 <refsect3 id="APP-PSQL-prompting">
3954 <title id="APP-PSQL-prompting-title">Prompting</title>
3957 The prompts <application>psql</application> issues can be customized
3958 to your preference. The three variables <varname>PROMPT1</varname>,
3959 <varname>PROMPT2</varname>, and <varname>PROMPT3</varname> contain strings
3960 and special escape sequences that describe the appearance of the
3961 prompt. Prompt 1 is the normal prompt that is issued when
3962 <application>psql</application> requests a new command. Prompt 2 is
3963 issued when more input is expected during command entry, for example
3964 because the command was not terminated with a semicolon or a quote
3966 Prompt 3 is issued when you are running an <acronym>SQL</acronym>
3967 <command>COPY FROM STDIN</command> command and you need to type in
3968 a row value on the terminal.
3972 The value of the selected prompt variable is printed literally,
3973 except where a percent sign (<literal>%</literal>) is encountered.
3974 Depending on the next character, certain other text is substituted
3975 instead. Defined substitutions are:
3979 <term><literal>%M</literal></term>
3982 The full host name (with domain name) of the database server,
3983 or <literal>[local]</literal> if the connection is over a Unix
3985 <literal>[local:<replaceable>/dir/name</replaceable>]</literal>,
3986 if the Unix domain socket is not at the compiled in default
3993 <term><literal>%m</literal></term>
3996 The host name of the database server, truncated at the
3997 first dot, or <literal>[local]</literal> if the connection is
3998 over a Unix domain socket.
4004 <term><literal>%></literal></term>
4005 <listitem><para>The port number at which the database server is listening.</para></listitem>
4009 <term><literal>%n</literal></term>
4012 The database session user name. (The expansion of this
4013 value might change during a database session as the result
4014 of the command <command>SET SESSION
4015 AUTHORIZATION</command>.)
4021 <term><literal>%/</literal></term>
4022 <listitem><para>The name of the current database.</para></listitem>
4026 <term><literal>%~</literal></term>
4027 <listitem><para>Like <literal>%/</literal>, but the output is <literal>~</literal>
4028 (tilde) if the database is your default database.</para></listitem>
4032 <term><literal>%#</literal></term>
4035 If the session user is a database superuser, then a
4036 <literal>#</literal>, otherwise a <literal>></literal>.
4037 (The expansion of this value might change during a database
4038 session as the result of the command <command>SET SESSION
4039 AUTHORIZATION</command>.)
4045 <term><literal>%p</literal></term>
4047 <para>The process ID of the backend currently connected to.</para>
4052 <term><literal>%R</literal></term>
4055 In prompt 1 normally <literal>=</literal>,
4056 but <literal>@</literal> if the session is in an inactive branch of a
4057 conditional block, or <literal>^</literal> if in single-line mode,
4058 or <literal>!</literal> if the session is disconnected from the
4059 database (which can happen if <command>\connect</command> fails).
4060 In prompt 2 <literal>%R</literal> is replaced by a character that
4061 depends on why <application>psql</application> expects more input:
4062 <literal>-</literal> if the command simply wasn't terminated yet,
4063 but <literal>*</literal> if there is an unfinished
4064 <literal>/* ... */</literal> comment,
4065 a single quote if there is an unfinished quoted string,
4066 a double quote if there is an unfinished quoted identifier,
4067 a dollar sign if there is an unfinished dollar-quoted string,
4068 or <literal>(</literal> if there is an unmatched left parenthesis.
4069 In prompt 3 <literal>%R</literal> doesn't produce anything.
4075 <term><literal>%x</literal></term>
4078 Transaction status: an empty string when not in a transaction
4079 block, or <literal>*</> when in a transaction block, or
4080 <literal>!</> when in a failed transaction block, or <literal>?</>
4081 when the transaction state is indeterminate (for example, because
4082 there is no connection).
4088 <term><literal>%l</literal></term>
4091 The line number inside the current statement, starting from <literal>1</>.
4097 <term><literal>%</literal><replaceable class="parameter">digits</replaceable></term>
4100 The character with the indicated octal code is substituted.
4106 <term><literal>%:</literal><replaceable class="parameter">name</replaceable><literal>:</literal></term>
4109 The value of the <application>psql</application> variable
4110 <replaceable class="parameter">name</replaceable>. See the
4111 section <xref linkend="APP-PSQL-variables"
4112 endterm="APP-PSQL-variables-title"> for details.
4118 <term><literal>%`</literal><replaceable class="parameter">command</replaceable><literal>`</literal></term>
4121 The output of <replaceable
4122 class="parameter">command</replaceable>, similar to ordinary
4123 <quote>back-tick</quote> substitution.
4129 <term><literal>%[</literal> ... <literal>%]</literal></term>
4132 Prompts can contain terminal control characters which, for
4133 example, change the color, background, or style of the prompt
4134 text, or change the title of the terminal window. In order for
4135 the line editing features of <application>Readline</application> to work properly, these
4136 non-printing control characters must be designated as invisible
4137 by surrounding them with <literal>%[</literal> and
4138 <literal>%]</literal>. Multiple pairs of these can occur within
4139 the prompt. For example:
4141 testdb=> \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '
4143 results in a boldfaced (<literal>1;</literal>) yellow-on-black
4144 (<literal>33;40</literal>) prompt on VT100-compatible, color-capable
4152 To insert a percent sign into your prompt, write
4153 <literal>%%</literal>. The default prompts are
4154 <literal>'%/%R%# '</literal> for prompts 1 and 2, and
4155 <literal>'>> '</literal> for prompt 3.
4160 This feature was shamelessly plagiarized from
4161 <application>tcsh</application>.
4168 <title>Command-Line Editing</title>
4171 <application>psql</application> supports the <application>Readline</application>
4172 library for convenient line editing and retrieval. The command
4173 history is automatically saved when <application>psql</application>
4174 exits and is reloaded when
4175 <application>psql</application> starts up. Tab-completion is also
4176 supported, although the completion logic makes no claim to be an
4177 <acronym>SQL</acronym> parser. The queries generated by tab-completion
4178 can also interfere with other SQL commands, e.g. <literal>SET
4179 TRANSACTION ISOLATION LEVEL</>.
4180 If for some reason you do not like the tab completion, you
4181 can turn it off by putting this in a file named
4182 <filename>.inputrc</filename> in your home directory:
4185 set disable-completion on
4188 (This is not a <application>psql</application> but a
4189 <application>Readline</application> feature. Read its documentation
4190 for further details.)
4197 <refsect1 id="app-psql-environment">
4198 <title id="app-psql-environment-title">Environment</title>
4203 <term><envar>COLUMNS</envar></term>
4207 If <literal>\pset columns</> is zero, controls the
4208 width for the <literal>wrapped</> format and width for determining
4209 if wide output requires the pager or should be switched to the
4210 vertical format in expanded auto mode.
4216 <term><envar>PGDATABASE</envar></term>
4217 <term><envar>PGHOST</envar></term>
4218 <term><envar>PGPORT</envar></term>
4219 <term><envar>PGUSER</envar></term>
4223 Default connection parameters (see <xref linkend="libpq-envars">).
4229 <term><envar>PSQL_EDITOR</envar></term>
4230 <term><envar>EDITOR</envar></term>
4231 <term><envar>VISUAL</envar></term>
4235 Editor used by the <command>\e</command>, <command>\ef</command>,
4236 and <command>\ev</command> commands.
4237 These variables are examined in the order listed;
4238 the first that is set is used.
4239 If none of them is set, the default is to use <filename>vi</filename>
4240 on Unix systems or <filename>notepad.exe</filename> on Windows systems.
4246 <term><envar>PSQL_EDITOR_LINENUMBER_ARG</envar></term>
4250 When <command>\e</command>, <command>\ef</command>, or
4251 <command>\ev</command> is used
4252 with a line number argument, this variable specifies the
4253 command-line argument used to pass the starting line number to
4254 the user's editor. For editors such as <productname>Emacs</> or
4255 <productname>vi</>, this is a plus sign. Include a trailing
4256 space in the value of the variable if there needs to be space
4257 between the option name and the line number. Examples:
4259 PSQL_EDITOR_LINENUMBER_ARG='+'
4260 PSQL_EDITOR_LINENUMBER_ARG='--line '
4265 The default is <literal>+</literal> on Unix systems
4266 (corresponding to the default editor <filename>vi</filename>,
4267 and useful for many other common editors); but there is no
4268 default on Windows systems.
4274 <term><envar>PSQL_HISTORY</envar></term>
4278 Alternative location for the command history file. Tilde (<literal>~</literal>) expansion is performed.
4284 <term><envar>PSQL_PAGER</envar></term>
4285 <term><envar>PAGER</envar></term>
4289 If a query's results do not fit on the screen, they are piped
4290 through this command. Typical values are <literal>more</literal>
4291 or <literal>less</literal>.
4292 Use of the pager can be disabled by setting <envar>PSQL_PAGER</envar>
4293 or <envar>PAGER</envar> to an empty string, or by adjusting the
4294 pager-related options of the <command>\pset</command> command.
4295 These variables are examined in the order listed;
4296 the first that is set is used.
4297 If none of them is set, the default is to use <literal>more</> on most
4298 platforms, but <literal>less</> on Cygwin.
4305 <term><envar>PSQLRC</envar></term>
4309 Alternative location of the user's <filename>.psqlrc</filename> file. Tilde (<literal>~</literal>) expansion is performed.
4315 <term><envar>SHELL</envar></term>
4319 Command executed by the <command>\!</command> command.
4325 <term><envar>TMPDIR</envar></term>
4329 Directory for storing temporary files. The default is
4330 <filename>/tmp</filename>.
4337 This utility, like most other <productname>PostgreSQL</> utilities,
4338 also uses the environment variables supported by <application>libpq</>
4339 (see <xref linkend="libpq-envars">).
4346 <title>Files</title>
4350 <term><filename>psqlrc</filename> and <filename>~/.psqlrc</filename></term>
4353 Unless it is passed an <option>-X</option> option,
4354 <application>psql</application> attempts to read and execute commands
4355 from the system-wide startup file (<filename>psqlrc</filename>) and then
4356 the user's personal startup file (<filename>~/.psqlrc</filename>), after
4357 connecting to the database but before accepting normal commands.
4358 These files can be used to set up the client and/or the server to taste,
4359 typically with <command>\set</command> and <command>SET</command>
4363 The system-wide startup file is named <filename>psqlrc</filename> and is
4364 sought in the installation's <quote>system configuration</> directory,
4365 which is most reliably identified by running <literal>pg_config
4366 --sysconfdir</>. By default this directory will be <filename>../etc/</>
4367 relative to the directory containing
4368 the <productname>PostgreSQL</productname> executables. The name of this
4369 directory can be set explicitly via the <envar>PGSYSCONFDIR</envar>
4370 environment variable.
4373 The user's personal startup file is named <filename>.psqlrc</filename>
4374 and is sought in the invoking user's home directory. On Windows, which
4375 lacks such a concept, the personal startup file is named
4376 <filename>%APPDATA%\postgresql\psqlrc.conf</filename>.
4377 The location of the user's startup file can be set explicitly via
4378 the <envar>PSQLRC</envar> environment variable.
4381 Both the system-wide startup file and the user's personal startup file
4382 can be made <application>psql</application>-version-specific
4383 by appending a dash and the <productname>PostgreSQL</productname>
4384 major or minor release number to the file name,
4385 for example <filename>~/.psqlrc-9.2</filename> or
4386 <filename>~/.psqlrc-9.2.5</filename>. The most specific
4387 version-matching file will be read in preference to a
4388 non-version-specific file.
4394 <term><filename>.psql_history</filename></term>
4397 The command-line history is stored in the file
4398 <filename>~/.psql_history</filename>, or
4399 <filename>%APPDATA%\postgresql\psql_history</filename> on Windows.
4402 The location of the history file can be set explicitly via
4403 the <varname>HISTFILE</varname> <application>psql</> variable or
4404 the <envar>PSQL_HISTORY</envar> environment variable.
4413 <title>Notes</title>
4417 <para><application>psql</application> works best with servers of the same
4418 or an older major version. Backslash commands are particularly likely
4419 to fail if the server is of a newer version than <application>psql</>
4420 itself. However, backslash commands of the <literal>\d</> family should
4421 work with servers of versions back to 7.4, though not necessarily with
4422 servers newer than <application>psql</> itself. The general
4423 functionality of running SQL commands and displaying query results
4424 should also work with servers of a newer major version, but this cannot
4425 be guaranteed in all cases.
4428 If you want to use <application>psql</application> to connect to several
4429 servers of different major versions, it is recommended that you use the
4430 newest version of <application>psql</application>. Alternatively, you
4431 can keep around a copy of <application>psql</application> from each
4432 major version and be sure to use the version that matches the
4433 respective server. But in practice, this additional complication should
4440 Before <productname>PostgreSQL</productname> 9.6,
4441 the <option>-c</option> option implied <option>-X</option>
4442 (<option>--no-psqlrc</>); this is no longer the case.
4448 Before <productname>PostgreSQL</productname> 8.4,
4449 <application>psql</application> allowed the
4450 first argument of a single-letter backslash command to start
4451 directly after the command, without intervening whitespace.
4452 Now, some whitespace is required.
4460 <title>Notes for Windows Users</title>
4463 <application>psql</application> is built as a <quote>console
4464 application</>. Since the Windows console windows use a different
4465 encoding than the rest of the system, you must take special care
4466 when using 8-bit characters within <application>psql</application>.
4467 If <application>psql</application> detects a problematic
4468 console code page, it will warn you at startup. To change the
4469 console code page, two things are necessary:
4474 Set the code page by entering <userinput>cmd.exe /c chcp
4475 1252</userinput>. (1252 is a code page that is appropriate for
4476 German; replace it with your value.) If you are using Cygwin,
4477 you can put this command in <filename>/etc/profile</filename>.
4483 Set the console font to <literal>Lucida Console</>, because the
4484 raster font does not work with the ANSI code page.
4487 </itemizedlist></para>
4492 <refsect1 id="APP-PSQL-examples">
4493 <title id="APP-PSQL-examples-title">Examples</title>
4496 The first example shows how to spread a command over several lines of
4497 input. Notice the changing prompt:
4499 testdb=> <userinput>CREATE TABLE my_table (</userinput>
4500 testdb(> <userinput> first integer not null default 0,</userinput>
4501 testdb(> <userinput> second text)</userinput>
4502 testdb-> <userinput>;</userinput>
4505 Now look at the table definition again:
4507 testdb=> <userinput>\d my_table</userinput>
4508 Table "public.my_table"
4509 Column | Type | Collation | Nullable | Default
4510 --------+---------+-----------+----------+---------
4511 first | integer | | not null | 0
4514 Now we change the prompt to something more interesting:
4516 testdb=> <userinput>\set PROMPT1 '%n@%m %~%R%# '</userinput>
4517 peter@localhost testdb=>
4519 Let's assume you have filled the table with data and want to take a
4522 peter@localhost testdb=> SELECT * FROM my_table;
4532 You can display tables in different ways by using the
4533 <command>\pset</command> command:
4535 peter@localhost testdb=> <userinput>\pset border 2</userinput>
4537 peter@localhost testdb=> <userinput>SELECT * FROM my_table;</userinput>
4548 peter@localhost testdb=> <userinput>\pset border 0</userinput>
4550 peter@localhost testdb=> <userinput>SELECT * FROM my_table;</userinput>
4559 peter@localhost testdb=> <userinput>\pset border 1</userinput>
4561 peter@localhost testdb=> <userinput>\pset format unaligned</userinput>
4562 Output format is unaligned.
4563 peter@localhost testdb=> <userinput>\pset fieldsep ","</userinput>
4564 Field separator is ",".
4565 peter@localhost testdb=> <userinput>\pset tuples_only</userinput>
4566 Showing only tuples.
4567 peter@localhost testdb=> <userinput>SELECT second, first FROM my_table;</userinput>
4573 Alternatively, use the short commands:
4575 peter@localhost testdb=> <userinput>\a \t \x</userinput>
4576 Output format is aligned.
4578 Expanded display is on.
4579 peter@localhost testdb=> <userinput>SELECT * FROM my_table;</userinput>
4592 </programlisting></para>
4595 When suitable, query results can be shown in a crosstab representation
4596 with the <command>\crosstabview</command> command:
4598 testdb=> <userinput>SELECT first, second, first > 2 AS gt2 FROM my_table;</userinput>
4599 first | second | gt2
4600 -------+--------+-----
4607 testdb=> <userinput>\crosstabview first second</userinput>
4608 first | one | two | three | four
4609 -------+-----+-----+-------+------
4617 This second example shows a multiplication table with rows sorted in reverse
4618 numerical order and columns with an independent, ascending numerical order.
4620 testdb=> <userinput>SELECT t1.first as "A", t2.first+100 AS "B", t1.first*(t2.first+100) as "AxB",</userinput>
4621 testdb(> <userinput>row_number() over(order by t2.first) AS ord</userinput>
4622 testdb(> <userinput>FROM my_table t1 CROSS JOIN my_table t2 ORDER BY 1 DESC</userinput>
4623 testdb(> <userinput>\crosstabview "A" "B" "AxB" ord</userinput>
4624 A | 101 | 102 | 103 | 104
4625 ---+-----+-----+-----+-----
4626 4 | 404 | 408 | 412 | 416
4627 3 | 303 | 306 | 309 | 312
4628 2 | 202 | 204 | 206 | 208
4629 1 | 101 | 102 | 103 | 104