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. In addition, it provides a
42 number of meta-commands and various shell-like features to
43 facilitate writing scripts and automating a wide variety of tasks.
47 <refsect1 id="R1-APP-PSQL-3">
48 <title>Options</title>
52 <term><option>-a</></term>
53 <term><option>--echo-all</></term>
56 Print all nonempty input lines to standard output as they are read.
57 (This does not apply to lines read interactively.) This is
58 equivalent to setting the variable <varname>ECHO</varname> to
59 <literal>all</literal>.
65 <term><option>-A</></term>
66 <term><option>--no-align</></term>
69 Switches to unaligned output mode. (The default output mode is
76 <term><option>-b</></term>
77 <term><option>--echo-errors</></term>
80 Print failed SQL commands to standard error output. This is
81 equivalent to setting the variable <varname>ECHO</varname> to
82 <literal>errors</literal>.
88 <term><option>-c <replaceable class="parameter">command</replaceable></></term>
89 <term><option>--command=<replaceable class="parameter">command</replaceable></></term>
92 Specifies that <application>psql</application> is to execute one
93 command string, <replaceable class="parameter">command</replaceable>,
94 and then exit. This is useful in shell scripts. Start-up files
95 (<filename>psqlrc</filename> and <filename>~/.psqlrc</filename>) are
96 ignored with this option.
99 <replaceable class="parameter">command</replaceable> must be either
100 a command string that is completely parsable by the server (i.e.,
101 it contains no <application>psql</application>-specific features),
102 or a single backslash command. Thus you cannot mix
103 <acronym>SQL</acronym> and <application>psql</application>
104 meta-commands with this option. To achieve that, you could
105 pipe the string into <application>psql</application>, for example:
106 <literal>echo '\x \\ SELECT * FROM foo;' | psql</literal>.
107 (<literal>\\</> is the separator meta-command.)
110 If the command string contains multiple SQL commands, they are
111 processed in a single transaction, unless there are explicit
112 <command>BEGIN</>/<command>COMMIT</> commands included in the
113 string to divide it into multiple transactions. This is
114 different from the behavior when the same string is fed to
115 <application>psql</application>'s standard input. Also, only
116 the result of the last SQL command is returned.
119 Because of these legacy behaviors, putting more than one command in
120 the <option>-c</option> string often has unexpected results. It's
121 better to feed multiple commands to <application>psql</application>'s
122 standard input, either using <application>echo</application> as
123 illustrated above, or via a shell here-document, for example:
135 <term><option>-d <replaceable class="parameter">dbname</replaceable></></term>
136 <term><option>--dbname=<replaceable class="parameter">dbname</replaceable></></term>
139 Specifies the name of the database to connect to. This is
140 equivalent to specifying <replaceable
141 class="parameter">dbname</replaceable> as the first non-option
142 argument on the command line.
145 If this parameter contains an <symbol>=</symbol> sign or starts
146 with a valid <acronym>URI</acronym> prefix
147 (<literal>postgresql://</literal>
148 or <literal>postgres://</literal>), it is treated as a
149 <parameter>conninfo</parameter> string. See <xref
150 linkend="libpq-connstring"> for more information.
156 <term><option>-e</></term>
157 <term><option>--echo-queries</></term>
160 Copy all SQL commands sent to the server to standard output as well.
162 to setting the variable <varname>ECHO</varname> to
163 <literal>queries</literal>.
169 <term><option>-E</></term>
170 <term><option>--echo-hidden</></term>
173 Echo the actual queries generated by <command>\d</command> and other backslash
174 commands. You can use this to study <application>psql</application>'s
175 internal operations. This is equivalent to
176 setting the variable <varname>ECHO_HIDDEN</varname> to <literal>on</>.
182 <term><option>-f <replaceable class="parameter">filename</replaceable></></term>
183 <term><option>--file=<replaceable class="parameter">filename</replaceable></></term>
186 Use the file <replaceable class="parameter">filename</replaceable>
187 as the source of commands instead of reading commands interactively.
188 After the file is processed, <application>psql</application>
189 terminates. This is in many ways equivalent to the meta-command
190 <command>\i</command>.
194 If <replaceable>filename</replaceable> is <literal>-</literal>
195 (hyphen), then standard input is read.
199 Using this option is subtly different from writing <literal>psql
201 class="parameter">filename</replaceable></literal>. In general,
202 both will do what you expect, but using <literal>-f</literal>
203 enables some nice features such as error messages with line
204 numbers. There is also a slight chance that using this option will
205 reduce the start-up overhead. On the other hand, the variant using
206 the shell's input redirection is (in theory) guaranteed to yield
207 exactly the same output you would have received had you entered
214 <term><option>-F <replaceable class="parameter">separator</replaceable></></term>
215 <term><option>--field-separator=<replaceable class="parameter">separator</replaceable></></term>
218 Use <replaceable class="parameter">separator</replaceable> as the
219 field separator for unaligned output. This is equivalent to
220 <command>\pset fieldsep</command> or <command>\f</command>.
226 <term><option>-h <replaceable class="parameter">hostname</replaceable></></term>
227 <term><option>--host=<replaceable class="parameter">hostname</replaceable></></term>
230 Specifies the host name of the machine on which the
231 server is running. If the value begins
232 with a slash, it is used as the directory for the Unix-domain
239 <term><option>-H</></term>
240 <term><option>--html</></term>
243 Turn on <acronym>HTML</acronym> tabular output. This is
244 equivalent to <literal>\pset format html</literal> or the
245 <command>\H</command> command.
251 <term><option>-l</></term>
252 <term><option>--list</></term>
255 List all available databases, then exit. Other non-connection
256 options are ignored. This is similar to the meta-command
257 <command>\list</command>.
263 <term><option>-L <replaceable class="parameter">filename</replaceable></></term>
264 <term><option>--log-file=<replaceable class="parameter">filename</replaceable></></term>
267 Write all query output into file <replaceable
268 class="parameter">filename</replaceable>, in addition to the
269 normal output destination.
275 <term><option>-n</></term>
276 <term><option>--no-readline</></term>
279 Do not use <application>Readline</application> for line editing and do
280 not use the command history.
281 This can be useful to turn off tab expansion when cutting and pasting.
287 <term><option>-o <replaceable class="parameter">filename</replaceable></></term>
288 <term><option>--output=<replaceable class="parameter">filename</replaceable></></term>
291 Put all query output into file <replaceable
292 class="parameter">filename</replaceable>. This is equivalent to
293 the command <command>\o</command>.
299 <term><option>-p <replaceable class="parameter">port</replaceable></></term>
300 <term><option>--port=<replaceable class="parameter">port</replaceable></></term>
303 Specifies the TCP port or the local Unix-domain
304 socket file extension on which the server is listening for
305 connections. Defaults to the value of the <envar>PGPORT</envar>
306 environment variable or, if not set, to the port specified at
307 compile time, usually 5432.
313 <term><option>-P <replaceable class="parameter">assignment</replaceable></></term>
314 <term><option>--pset=<replaceable class="parameter">assignment</replaceable></></term>
317 Specifies printing options, in the style of
318 <command>\pset</command>. Note that here you
319 have to separate name and value with an equal sign instead of a
320 space. For example, to set the output format to <application>LaTeX</application>, you could write
321 <literal>-P format=latex</literal>.
327 <term><option>-q</></term>
328 <term><option>--quiet</></term>
331 Specifies that <application>psql</application> should do its work
332 quietly. By default, it prints welcome messages and various
333 informational output. If this option is used, none of this
334 happens. This is useful with the <option>-c</option> option.
335 This is equivalent to setting the variable <varname>QUIET</varname>
342 <term><option>-R <replaceable class="parameter">separator</replaceable></></term>
343 <term><option>--record-separator=<replaceable class="parameter">separator</replaceable></></term>
346 Use <replaceable class="parameter">separator</replaceable> as the
347 record separator for unaligned output. This is equivalent to the
348 <command>\pset recordsep</command> command.
354 <term><option>-s</></term>
355 <term><option>--single-step</></term>
358 Run in single-step mode. That means the user is prompted before
359 each command is sent to the server, with the option to cancel
360 execution as well. Use this to debug scripts.
366 <term><option>-S</></term>
367 <term><option>--single-line</></term>
370 Runs in single-line mode where a newline terminates an SQL command, as a
376 This mode is provided for those who insist on it, but you are not
377 necessarily encouraged to use it. In particular, if you mix
378 <acronym>SQL</acronym> and meta-commands on a line the order of
379 execution might not always be clear to the inexperienced user.
386 <term><option>-t</></term>
387 <term><option>--tuples-only</></term>
390 Turn off printing of column names and result row count footers,
391 etc. This is equivalent to the <command>\t</command> command.
397 <term><option>-T <replaceable class="parameter">table_options</replaceable></></term>
398 <term><option>--table-attr=<replaceable class="parameter">table_options</replaceable></></term>
401 Specifies options to be placed within the
402 <acronym>HTML</acronym> <sgmltag>table</sgmltag> tag. See
403 <command>\pset</command> for details.
409 <term><option>-U <replaceable class="parameter">username</replaceable></></term>
410 <term><option>--username=<replaceable class="parameter">username</replaceable></></term>
413 Connect to the database as the user <replaceable
414 class="parameter">username</replaceable> instead of the default.
415 (You must have permission to do so, of course.)
421 <term><option>-v <replaceable class="parameter">assignment</replaceable></></term>
422 <term><option>--set=<replaceable class="parameter">assignment</replaceable></></term>
423 <term><option>--variable=<replaceable class="parameter">assignment</replaceable></></term>
426 Perform a variable assignment, like the <command>\set</command>
427 meta-command. Note that you must separate name and value, if
428 any, by an equal sign on the command line. To unset a variable,
429 leave off the equal sign. To set a variable with an empty value,
430 use the equal sign but leave off the value. These assignments are
431 done during a very early stage of start-up, so variables reserved
432 for internal purposes might get overwritten later.
438 <term><option>-V</></term>
439 <term><option>--version</></term>
442 Print the <application>psql</application> version and exit.
448 <term><option>-w</></term>
449 <term><option>--no-password</></term>
452 Never issue a password prompt. If the server requires password
453 authentication and a password is not available by other means
454 such as a <filename>.pgpass</filename> file, the connection
455 attempt will fail. This option can be useful in batch jobs and
456 scripts where no user is present to enter a password.
460 Note that this option will remain set for the entire session,
461 and so it affects uses of the meta-command
462 <command>\connect</command> as well as the initial connection attempt.
468 <term><option>-W</></term>
469 <term><option>--password</></term>
472 Force <application>psql</application> to prompt for a
473 password before connecting to a database.
477 This option is never essential, since <application>psql</application>
478 will automatically prompt for a password if the server demands
479 password authentication. However, <application>psql</application>
480 will waste a connection attempt finding out that the server wants a
481 password. In some cases it is worth typing <option>-W</> to avoid
482 the extra connection attempt.
486 Note that this option will remain set for the entire session,
487 and so it affects uses of the meta-command
488 <command>\connect</command> as well as the initial connection attempt.
494 <term><option>-x</></term>
495 <term><option>--expanded</></term>
498 Turn on the expanded table formatting mode. This is equivalent to the
499 <command>\x</command> command.
505 <term><option>-X,</></term>
506 <term><option>--no-psqlrc</></term>
509 Do not read the start-up file (neither the system-wide
510 <filename>psqlrc</filename> file nor the user's
511 <filename>~/.psqlrc</filename> file).
517 <term><option>-z</option></term>
518 <term><option>--field-separator-zero</option></term>
521 Set the field separator for unaligned output to a zero byte.
527 <term><option>-0</option></term>
528 <term><option>--record-separator-zero</option></term>
531 Set the record separator for unaligned output to a zero byte. This is
532 useful for interfacing, for example, with <literal>xargs -0</literal>.
538 <term><option>-1</option></term>
539 <term><option>--single-transaction</option></term>
542 When <application>psql</application> executes a script, adding
543 this option wraps <command>BEGIN</>/<command>COMMIT</> around the
544 script to execute it as a single transaction. This ensures that
545 either all the commands complete successfully, or no changes are
550 If the script itself uses <command>BEGIN</>, <command>COMMIT</>,
551 or <command>ROLLBACK</>, this option will not have the desired
553 Also, if the script contains any command that cannot be executed
554 inside a transaction block, specifying this option will cause that
555 command (and hence the whole transaction) to fail.
561 <term><option>-?</></term>
562 <term><option>--help[=<replaceable class="parameter">topic</>]</option></term>
565 Show help about <application>psql</application> and exit. The optional
566 <replaceable class="parameter">topic</> parameter (defaulting
567 to <literal>options</literal>) selects which part of <application>psql</application> is
568 explained: <literal>commands</> describes <application>psql</>'s
569 backslash commands; <literal>options</> describes the command-line
570 options that can be passed to <application>psql</>;
571 and <literal>variables</> shows help about about <application>psql</application> configuration
582 <title>Exit Status</title>
585 <application>psql</application> returns 0 to the shell if it
586 finished normally, 1 if a fatal error of its own occurs (e.g. out of memory,
587 file not found), 2 if the connection to the server went bad
588 and the session was not interactive, and 3 if an error occurred in a
589 script and the variable <varname>ON_ERROR_STOP</varname> was set.
597 <refsect2 id="R2-APP-PSQL-connecting">
598 <title>Connecting to a Database</title>
601 <application>psql</application> is a regular
602 <productname>PostgreSQL</productname> client application. In order
603 to connect to a database you need to know the name of your target
604 database, the host name and port number of the server, and what user
605 name you want to connect as. <application>psql</application> can be
606 told about those parameters via command line options, namely
607 <option>-d</option>, <option>-h</option>, <option>-p</option>, and
608 <option>-U</option> respectively. If an argument is found that does
609 not belong to any option it will be interpreted as the database name
610 (or the user name, if the database name is already given). Not all
611 of these options are required; there are useful defaults. If you omit the host
612 name, <application>psql</> will connect via a Unix-domain socket
613 to a server on the local host, or via TCP/IP to <literal>localhost</> on
614 machines that don't have Unix-domain sockets. The default port number is
615 determined at compile time.
616 Since the database server uses the same default, you will not have
617 to specify the port in most cases. The default user name is your
618 operating-system user name, as is the default database name.
620 just connect to any database under any user name. Your database
621 administrator should have informed you about your access rights.
625 When the defaults aren't quite right, you can save yourself
626 some typing by setting the environment variables
627 <envar>PGDATABASE</envar>, <envar>PGHOST</envar>,
628 <envar>PGPORT</envar> and/or <envar>PGUSER</envar> to appropriate
629 values. (For additional environment variables, see <xref
630 linkend="libpq-envars">.) It is also convenient to have a
631 <filename>~/.pgpass</> file to avoid regularly having to type in
632 passwords. See <xref linkend="libpq-pgpass"> for more information.
636 An alternative way to specify connection parameters is in a
637 <parameter>conninfo</parameter> string or
638 a <acronym>URI</acronym>, which is used instead of a database
639 name. This mechanism give you very wide control over the
640 connection. For example:
642 $ <userinput>psql "service=myservice sslmode=require"</userinput>
643 $ <userinput>psql postgresql://dbmaster:5433/mydb?sslmode=require</userinput>
645 This way you can also use <acronym>LDAP</acronym> for connection
646 parameter lookup as described in <xref linkend="libpq-ldap">.
647 See <xref linkend="libpq-paramkeywords"> for more information on all the
648 available connection options.
652 If the connection could not be made for any reason (e.g., insufficient
653 privileges, server is not running on the targeted host, etc.),
654 <application>psql</application> will return an error and terminate.
658 If both standard input and standard output are a
659 terminal, then <application>psql</application> sets the client
660 encoding to <quote>auto</quote>, which will detect the
661 appropriate client encoding from the locale settings
662 (<envar>LC_CTYPE</envar> environment variable on Unix systems).
663 If this doesn't work out as expected, the client encoding can be
664 overridden using the environment
665 variable <envar>PGCLIENTENCODING</envar>.
669 <refsect2 id="R2-APP-PSQL-4">
670 <title>Entering SQL Commands</title>
673 In normal operation, <application>psql</application> provides a
674 prompt with the name of the database to which
675 <application>psql</application> is currently connected, followed by
676 the string <literal>=></literal>. For example:
678 $ <userinput>psql testdb</userinput>
680 Type "help" for help.
687 At the prompt, the user can type in <acronym>SQL</acronym> commands.
688 Ordinarily, input lines are sent to the server when a
689 command-terminating semicolon is reached. An end of line does not
690 terminate a command. Thus commands can be spread over several lines for
691 clarity. If the command was sent and executed without error, the results
692 of the command are displayed on the screen.
696 Whenever a command is executed, <application>psql</application> also polls
697 for asynchronous notification events generated by
698 <xref linkend="SQL-LISTEN"> and
699 <xref linkend="SQL-NOTIFY">.
703 While C-style block comments are passed to the server for
704 processing and removal, SQL-standard comments are removed by
705 <application>psql</application>.
709 <refsect2 id="APP-PSQL-meta-commands">
710 <title>Meta-Commands</title>
713 Anything you enter in <application>psql</application> that begins
714 with an unquoted backslash is a <application>psql</application>
715 meta-command that is processed by <application>psql</application>
716 itself. These commands make
717 <application>psql</application> more useful for administration or
718 scripting. Meta-commands are often called slash or backslash commands.
722 The format of a <application>psql</application> command is the backslash,
723 followed immediately by a command verb, then any arguments. The arguments
724 are separated from the command verb and each other by any number of
725 whitespace characters.
729 To include whitespace in an argument you can quote it with
730 single quotes. To include a single quote in an argument,
731 write two single quotes within single-quoted text.
732 Anything contained in single quotes is
733 furthermore subject to C-like substitutions for
734 <literal>\n</literal> (new line), <literal>\t</literal> (tab),
735 <literal>\b</literal> (backspace), <literal>\r</literal> (carriage return),
736 <literal>\f</literal> (form feed),
737 <literal>\</literal><replaceable>digits</replaceable> (octal), and
738 <literal>\x</literal><replaceable>digits</replaceable> (hexadecimal).
739 A backslash preceding any other character within single-quoted text
740 quotes that single character, whatever it is.
744 Within an argument, text that is enclosed in backquotes
745 (<literal>`</literal>) is taken as a command line that is passed to the
746 shell. The output of the command (with any trailing newline removed)
747 replaces the backquoted text.
751 If an unquoted colon (<literal>:</literal>) followed by a
752 <application>psql</> variable name appears within an argument, it is
753 replaced by the variable's value, as described in <xref
754 linkend="APP-PSQL-interpolation" endterm="APP-PSQL-interpolation-title">.
758 Some commands take an <acronym>SQL</acronym> identifier (such as a
759 table name) as argument. These arguments follow the syntax rules
760 of <acronym>SQL</acronym>: Unquoted letters are forced to
761 lowercase, while double quotes (<literal>"</>) protect letters
762 from case conversion and allow incorporation of whitespace into
763 the identifier. Within double quotes, paired double quotes reduce
764 to a single double quote in the resulting name. For example,
765 <literal>FOO"BAR"BAZ</> is interpreted as <literal>fooBARbaz</>,
766 and <literal>"A weird"" name"</> becomes <literal>A weird"
771 Parsing for arguments stops at the end of the line, or when another
772 unquoted backslash is found. An unquoted backslash
773 is taken as the beginning of a new meta-command. The special
774 sequence <literal>\\</literal> (two backslashes) marks the end of
775 arguments and continues parsing <acronym>SQL</acronym> commands, if
776 any. That way <acronym>SQL</acronym> and
777 <application>psql</application> commands can be freely mixed on a
778 line. But in any case, the arguments of a meta-command cannot
779 continue beyond the end of the line.
783 The following meta-commands are defined:
787 <term><literal>\a</literal></term>
790 If the current table output format is unaligned, it is switched to aligned.
791 If it is not unaligned, it is set to unaligned. This command is
792 kept for backwards compatibility. See <command>\pset</command> for a
793 more general solution.
799 <term><literal>\c</literal> or <literal>\connect</literal> <literal>[ <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>
802 Establishes a new connection to a <productname>PostgreSQL</>
803 server. The connection parameters to use can be specified either
804 using a positional syntax, or using <literal>conninfo</> connection
805 strings as detailed in <xref linkend="libpq-connstring">.
809 When using positional parameters, if any of
810 <replaceable class="parameter">dbname</replaceable>,
811 <replaceable class="parameter">username</replaceable>,
812 <replaceable class="parameter">host</replaceable> or
813 <replaceable class="parameter">port</replaceable> are omitted or
814 specified as <literal>-</literal>, the value of that parameter from
815 the previous connection is used; if there is no previous connection,
816 the <application>libpq</application> default for the parameter's value
817 is used. When using <literal>conninfo</> strings, no values from the
818 previous connection are used for the new connection.
822 If the new connection is successfully made, the previous
823 connection is closed.
824 If the connection attempt failed (wrong user name, access
825 denied, etc.), the previous connection will only be kept if
826 <application>psql</application> is in interactive mode. When
827 executing a non-interactive script, processing will
828 immediately stop with an error. This distinction was chosen as
829 a user convenience against typos on the one hand, and a safety
830 mechanism that scripts are not accidentally acting on the
831 wrong database on the other hand.
838 => \c mydb myuser host.dom 6432
840 => \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
841 => \c postgresql://tom@localhost/mydb?application_name=myapp
847 <term><literal>\C [ <replaceable class="parameter">title</replaceable> ]</literal></term>
850 Sets the title of any tables being printed as the result of a
851 query or unset any such title. This command is equivalent to
852 <literal>\pset title <replaceable
853 class="parameter">title</replaceable></literal>. (The name of
854 this command derives from <quote>caption</quote>, as it was
855 previously only used to set the caption in an
856 <acronym>HTML</acronym> table.)
862 <term><literal>\cd [ <replaceable>directory</replaceable> ]</literal></term>
865 Changes the current working directory to
866 <replaceable>directory</replaceable>. Without argument, changes
867 to the current user's home directory.
872 To print your current working directory, use <literal>\! pwd</literal>.
879 <term><literal>\conninfo</literal></term>
882 Outputs information about the current database connection.
887 <varlistentry id="APP-PSQL-meta-commands-copy">
888 <term><literal>\copy { <replaceable class="parameter">table</replaceable> [ ( <replaceable class="parameter">column_list</replaceable> ) ] | ( <replaceable class="parameter">query</replaceable> ) }
889 { <literal>from</literal> | <literal>to</literal> }
890 { <replaceable class="parameter">'filename'</replaceable> | program <replaceable class="parameter">'command'</replaceable> | stdin | stdout | pstdin | pstdout }
891 [ [ with ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]</literal></term>
895 Performs a frontend (client) copy. This is an operation that
896 runs an <acronym>SQL</acronym> <xref linkend="SQL-COPY">
897 command, but instead of the server
898 reading or writing the specified file,
899 <application>psql</application> reads or writes the file and
900 routes the data between the server and the local file system.
901 This means that file accessibility and privileges are those of
902 the local user, not the server, and no SQL superuser
903 privileges are required.
907 When <literal>program</> is specified,
908 <replaceable class="parameter">command</replaceable> is
909 executed by <application>psql</application> and the data passed from
910 or to <replaceable class="parameter">command</replaceable> is
911 routed between the server and the client.
912 Again, the execution privileges are those of
913 the local user, not the server, and no SQL superuser
914 privileges are required.
918 For <literal>\copy ... from stdin</>, data rows are read from the same
919 source that issued the command, continuing until <literal>\.</literal>
920 is read or the stream reaches <acronym>EOF</>. This option is useful
921 for populating tables in-line within a SQL script file.
922 For <literal>\copy ... to stdout</>, output is sent to the same place
923 as <application>psql</> command output, and
924 the <literal>COPY <replaceable>count</></literal> command status is
925 not printed (since it might be confused with a data row).
926 To read/write <application>psql</application>'s standard input or
927 output regardless of the current command source or <literal>\o</>
928 option, write <literal>from pstdin</> or <literal>to pstdout</>.
932 The syntax of this command is similar to that of the
933 <acronym>SQL</acronym> <xref linkend="sql-copy">
934 command. All options other than the data source/destination are
935 as specified for <xref linkend="sql-copy">.
936 Because of this, special parsing rules apply to the <command>\copy</>
937 command. In particular, <application>psql</>'s variable substitution
938 rules and backslash escapes do not apply.
943 This operation is not as efficient as the <acronym>SQL</acronym>
944 <command>COPY</command> command because all data must pass
945 through the client/server connection. For large
946 amounts of data the <acronym>SQL</acronym> command might be preferable.
954 <term><literal>\copyright</literal></term>
957 Shows the copyright and distribution terms of
958 <productname>PostgreSQL</productname>.
964 <term><literal>\d[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
968 For each relation (table, view, index, sequence, or foreign table)
969 or composite type matching the
970 <replaceable class="parameter">pattern</replaceable>, show all
971 columns, their types, the tablespace (if not the default) and any
972 special attributes such as <literal>NOT NULL</literal> or defaults.
973 Associated indexes, constraints, rules, and triggers are
974 also shown. For foreign tables, the associated foreign
975 server is shown as well.
976 (<quote>Matching the pattern</> is defined in
977 <xref linkend="APP-PSQL-patterns" endterm="APP-PSQL-patterns-title">
982 For some types of relation, <literal>\d</> shows additional information
983 for each column: column values for sequences, indexed expression for
984 indexes and foreign data wrapper options for foreign tables.
988 The command form <literal>\d+</literal> is identical, except that
989 more information is displayed: any comments associated with the
990 columns of the table are shown, as is the presence of OIDs in the
991 table, the view definition if the relation is a view, a non-default
992 <link linkend="SQL-CREATETABLE-REPLICA-IDENTITY">replica
993 identity</link> setting.
997 By default, only user-created objects are shown; supply a
998 pattern or the <literal>S</literal> modifier to include system
1004 If <command>\d</command> is used without a
1005 <replaceable class="parameter">pattern</replaceable> argument, it is
1006 equivalent to <command>\dtvsE</command> which will show a list of
1007 all visible tables, views, sequences and foreign tables.
1008 This is purely a convenience measure.
1015 <term><literal>\da[S] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1019 Lists aggregate functions, together with their
1020 return type and the data types they operate on. If <replaceable
1021 class="parameter">pattern</replaceable>
1022 is specified, only aggregates whose names match the pattern are shown.
1023 By default, only user-created objects are shown; supply a
1024 pattern or the <literal>S</literal> modifier to include system
1032 <term><literal>\db[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1036 Lists tablespaces. If <replaceable
1037 class="parameter">pattern</replaceable>
1038 is specified, only tablespaces whose names match the pattern are shown.
1039 If <literal>+</literal> is appended to the command name, each tablespace
1040 is listed with its associated options, on-disk size, permissions and
1048 <term><literal>\dc[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1051 Lists conversions between character-set encodings.
1052 If <replaceable class="parameter">pattern</replaceable>
1053 is specified, only conversions whose names match the pattern are
1055 By default, only user-created objects are shown; supply a
1056 pattern or the <literal>S</literal> modifier to include system
1058 If <literal>+</literal> is appended to the command name, each object
1059 is listed with its associated description.
1066 <term><literal>\dC[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1070 If <replaceable class="parameter">pattern</replaceable>
1071 is specified, only casts whose source or target types match the
1073 If <literal>+</literal> is appended to the command name, each object
1074 is listed with its associated description.
1081 <term><literal>\dd[S] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1084 Shows the descriptions of objects of type <literal>constraint</>,
1085 <literal>operator class</>, <literal>operator family</>,
1086 <literal>rule</>, and <literal>trigger</>. All
1087 other comments may be viewed by the respective backslash commands for
1091 <para><literal>\dd</literal> displays descriptions for objects matching the
1092 <replaceable class="parameter">pattern</replaceable>, or of visible
1093 objects of the appropriate type if no argument is given. But in either
1094 case, only objects that have a description are listed.
1095 By default, only user-created objects are shown; supply a
1096 pattern or the <literal>S</literal> modifier to include system
1101 Descriptions for objects can be created with the <xref
1102 linkend="sql-comment">
1103 <acronym>SQL</acronym> command.
1110 <term><literal>\ddp [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1113 Lists default access privilege settings. An entry is shown for
1114 each role (and schema, if applicable) for which the default
1115 privilege settings have been changed from the built-in defaults.
1116 If <replaceable class="parameter">pattern</replaceable> is
1117 specified, only entries whose role name or schema name matches
1118 the pattern are listed.
1122 The <xref linkend="sql-alterdefaultprivileges"> command is used to set
1123 default access privileges. The meaning of the
1124 privilege display is explained under
1125 <xref linkend="sql-grant">.
1132 <term><literal>\dD[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1135 Lists domains. If <replaceable
1136 class="parameter">pattern</replaceable>
1137 is specified, only domains whose names match the pattern are shown.
1138 By default, only user-created objects are shown; supply a
1139 pattern or the <literal>S</literal> modifier to include system
1141 If <literal>+</literal> is appended to the command name, each object
1142 is listed with its associated permissions and description.
1149 <term><literal>\dE[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1150 <term><literal>\di[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1151 <term><literal>\dm[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1152 <term><literal>\ds[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1153 <term><literal>\dt[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1154 <term><literal>\dv[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1158 In this group of commands, the letters <literal>E</literal>,
1159 <literal>i</literal>, <literal>m</literal>, <literal>s</literal>,
1160 <literal>t</literal>, and <literal>v</literal>
1161 stand for foreign table, index, materialized view, sequence, table, and view,
1163 You can specify any or all of
1164 these letters, in any order, to obtain a listing of objects
1165 of these types. For example, <literal>\dit</> lists indexes
1166 and tables. If <literal>+</literal> is
1167 appended to the command name, each object is listed with its
1168 physical size on disk and its associated description, if any.
1169 If <replaceable class="parameter">pattern</replaceable> is
1170 specified, only objects whose names match the pattern are listed.
1171 By default, only user-created objects are shown; supply a
1172 pattern or the <literal>S</literal> modifier to include system
1180 <term><literal>\des[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1183 Lists foreign servers (mnemonic: <quote>external
1185 If <replaceable class="parameter">pattern</replaceable> is
1186 specified, only those servers whose name matches the pattern
1187 are listed. If the form <literal>\des+</literal> is used, a
1188 full description of each server is shown, including the
1189 server's ACL, type, version, options, and description.
1196 <term><literal>\det[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1199 Lists foreign tables (mnemonic: <quote>external tables</quote>).
1200 If <replaceable class="parameter">pattern</replaceable> is
1201 specified, only entries whose table name or schema name matches
1202 the pattern are listed. If the form <literal>\det+</literal>
1203 is used, generic options and the foreign table description
1211 <term><literal>\deu[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1214 Lists user mappings (mnemonic: <quote>external
1216 If <replaceable class="parameter">pattern</replaceable> is
1217 specified, only those mappings whose user names match the
1218 pattern are listed. If the form <literal>\deu+</literal> is
1219 used, additional information about each mapping is shown.
1224 <literal>\deu+</literal> might also display the user name and
1225 password of the remote user, so care should be taken not to
1234 <term><literal>\dew[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1237 Lists foreign-data wrappers (mnemonic: <quote>external
1239 If <replaceable class="parameter">pattern</replaceable> is
1240 specified, only those foreign-data wrappers whose name matches
1241 the pattern are listed. If the form <literal>\dew+</literal>
1242 is used, the ACL, options, and description of the foreign-data
1243 wrapper are also shown.
1250 <term><literal>\df[antwS+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1254 Lists functions, together with their arguments, return types, and
1255 function types, which are classified as <quote>agg</> (aggregate),
1256 <quote>normal</>, <quote>trigger</>, or <quote>window</>.
1257 To display only functions
1258 of specific type(s), add the corresponding letters <literal>a</>,
1259 <literal>n</>, <literal>t</>, or <literal>w</> to the command.
1261 class="parameter">pattern</replaceable> is specified, only
1262 functions whose names match the pattern are shown.
1263 By default, only user-created
1264 objects are shown; supply a pattern or the <literal>S</literal>
1265 modifier to include system objects.
1266 If the form <literal>\df+</literal> is used, additional information
1267 about each function is shown, including security classification,
1268 volatility, owner, language, source code and description.
1273 To look up functions taking arguments or returning values of a specific
1274 type, use your pager's search capability to scroll through the
1275 <literal>\df</> output.
1283 <term><literal>\dF[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1286 Lists text search configurations.
1287 If <replaceable class="parameter">pattern</replaceable> is specified,
1288 only configurations whose names match the pattern are shown.
1289 If the form <literal>\dF+</literal> is used, a full description of
1290 each configuration is shown, including the underlying text search
1291 parser and the dictionary list for each parser token type.
1297 <term><literal>\dFd[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1300 Lists text search dictionaries.
1301 If <replaceable class="parameter">pattern</replaceable> is specified,
1302 only dictionaries whose names match the pattern are shown.
1303 If the form <literal>\dFd+</literal> is used, additional information
1304 is shown about each selected dictionary, including the underlying
1305 text search template and the option values.
1311 <term><literal>\dFp[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1314 Lists text search parsers.
1315 If <replaceable class="parameter">pattern</replaceable> is specified,
1316 only parsers whose names match the pattern are shown.
1317 If the form <literal>\dFp+</literal> is used, a full description of
1318 each parser is shown, including the underlying functions and the
1319 list of recognized token types.
1325 <term><literal>\dFt[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1328 Lists text search templates.
1329 If <replaceable class="parameter">pattern</replaceable> is specified,
1330 only templates whose names match the pattern are shown.
1331 If the form <literal>\dFt+</literal> is used, additional information
1332 is shown about each template, including the underlying function names.
1339 <term><literal>\dg[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1342 Lists database roles.
1343 (Since the concepts of <quote>users</> and <quote>groups</> have been
1344 unified into <quote>roles</>, this command is now equivalent to
1345 <literal>\du</literal>.)
1346 If <replaceable class="parameter">pattern</replaceable> is specified,
1347 only those roles whose names match the pattern are listed.
1348 If the form <literal>\dg+</literal> is used, additional information
1349 is shown about each role; currently this adds the comment for each
1357 <term><literal>\dl</literal></term>
1360 This is an alias for <command>\lo_list</command>, which shows a
1361 list of large objects.
1367 <term><literal>\dL[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1370 Lists procedural languages. If <replaceable
1371 class="parameter">pattern</replaceable>
1372 is specified, only languages whose names match the pattern are listed.
1373 By default, only user-created languages
1374 are shown; supply the <literal>S</literal> modifier to include system
1375 objects. If <literal>+</literal> is appended to the command name, each
1376 language is listed with its call handler, validator, access privileges,
1377 and whether it is a system object.
1384 <term><literal>\dn[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1388 Lists schemas (namespaces). If <replaceable
1389 class="parameter">pattern</replaceable>
1390 is specified, only schemas whose names match the pattern are listed.
1391 By default, only user-created objects are shown; supply a
1392 pattern or the <literal>S</literal> modifier to include system objects.
1393 If <literal>+</literal> is appended to the command name, each object
1394 is listed with its associated permissions and description, if any.
1401 <term><literal>\do[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1404 Lists operators with their operand and result types.
1405 If <replaceable class="parameter">pattern</replaceable> is
1406 specified, only operators whose names match the pattern are listed.
1407 By default, only user-created objects are shown; supply a
1408 pattern or the <literal>S</literal> modifier to include system
1410 If <literal>+</literal> is appended to the command name,
1411 additional information about each operator is shown, currently just
1412 the name of the underlying function.
1419 <term><literal>\dO[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1423 If <replaceable class="parameter">pattern</replaceable> is
1424 specified, only collations whose names match the pattern are
1425 listed. By default, only user-created objects are shown;
1426 supply a pattern or the <literal>S</literal> modifier to
1427 include system objects. If <literal>+</literal> is appended
1428 to the command name, each collation is listed with its associated
1429 description, if any.
1430 Note that only collations usable with the current database's encoding
1431 are shown, so the results may vary in different databases of the
1439 <term><literal>\dp [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1442 Lists tables, views and sequences with their
1443 associated access privileges.
1444 If <replaceable class="parameter">pattern</replaceable> is
1445 specified, only tables, views and sequences whose names match the
1450 The <xref linkend="sql-grant"> and
1451 <xref linkend="sql-revoke">
1452 commands are used to set access privileges. The meaning of the
1453 privilege display is explained under
1454 <xref linkend="sql-grant">.
1460 <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>
1463 Lists defined configuration settings. These settings can be
1464 role-specific, database-specific, or both.
1465 <replaceable>role-pattern</replaceable> and
1466 <replaceable>database-pattern</replaceable> are used to select
1467 specific roles and databases to list, respectively. If omitted, or if
1468 <literal>*</> is specified, all settings are listed, including those
1469 not role-specific or database-specific, respectively.
1473 The <xref linkend="sql-alterrole"> and
1474 <xref linkend="sql-alterdatabase">
1475 commands are used to define per-role and per-database configuration
1482 <term><literal>\dT[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1486 If <replaceable class="parameter">pattern</replaceable> is
1487 specified, only types whose names match the pattern are listed.
1488 If <literal>+</literal> is appended to the command name, each type is
1489 listed with its internal name and size, its allowed values
1490 if it is an <type>enum</> type, and its associated permissions.
1491 By default, only user-created objects are shown; supply a
1492 pattern or the <literal>S</literal> modifier to include system
1499 <term><literal>\du[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1502 Lists database roles.
1503 (Since the concepts of <quote>users</> and <quote>groups</> have been
1504 unified into <quote>roles</>, this command is now equivalent to
1505 <literal>\dg</literal>.)
1506 If <replaceable class="parameter">pattern</replaceable> is specified,
1507 only those roles whose names match the pattern are listed.
1508 If the form <literal>\du+</literal> is used, additional information
1509 is shown about each role; currently this adds the comment for each
1516 <term><literal>\dx[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1519 Lists installed extensions.
1520 If <replaceable class="parameter">pattern</replaceable>
1521 is specified, only those extensions whose names match the pattern
1523 If the form <literal>\dx+</literal> is used, all the objects belonging
1524 to each matching extension are listed.
1530 <term><literal>\dy[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1533 Lists event triggers.
1534 If <replaceable class="parameter">pattern</replaceable>
1535 is specified, only those event triggers whose names match the pattern
1537 If <literal>+</literal> is appended to the command name, each object
1538 is listed with its associated description.
1544 <term><literal>\e</literal> or <literal>\edit</> <literal> <optional> <replaceable class="parameter">filename</> </optional> <optional> <replaceable class="parameter">line_number</> </optional> </literal></term>
1548 If <replaceable class="parameter">filename</replaceable> is
1549 specified, the file is edited; after the editor exits, its
1550 content is copied back to the query buffer. If no <replaceable
1551 class="parameter">filename</replaceable> is given, the current query
1552 buffer is copied to a temporary file which is then edited in the same
1557 The new query buffer is then re-parsed according to the normal
1558 rules of <application>psql</application>, where the whole buffer
1559 is treated as a single line. (Thus you cannot make scripts this
1560 way. Use <command>\i</command> for that.) This means that
1561 if the query ends with (or contains) a semicolon, it is
1562 immediately executed. Otherwise it will merely wait in the
1563 query buffer; type semicolon or <literal>\g</> to send it, or
1564 <literal>\r</> to cancel.
1568 If a line number is specified, <application>psql</application> will
1569 position the cursor on the specified line of the file or query buffer.
1570 Note that if a single all-digits argument is given,
1571 <application>psql</application> assumes it is a line number,
1577 See under <xref linkend="app-psql-environment"
1578 endterm="app-psql-environment-title"> for how to configure and
1579 customize your editor.
1586 <term><literal>\echo <replaceable class="parameter">text</replaceable> [ ... ]</literal></term>
1589 Prints the arguments to the standard output, separated by one
1590 space and followed by a newline. This can be useful to
1591 intersperse information in the output of scripts. For example:
1593 => <userinput>\echo `date`</userinput>
1594 Tue Oct 26 21:40:57 CEST 1999
1596 If the first argument is an unquoted <literal>-n</literal> the trailing
1597 newline is not written.
1602 If you use the <command>\o</command> command to redirect your
1603 query output you might wish to use <command>\qecho</command>
1604 instead of this command.
1611 <term><literal>\ef <optional> <replaceable class="parameter">function_description</> <optional> <replaceable class="parameter">line_number</> </optional> </optional> </literal></term>
1615 This command fetches and edits the definition of the named function,
1616 in the form of a <command>CREATE OR REPLACE FUNCTION</> command.
1617 Editing is done in the same way as for <literal>\edit</>.
1618 After the editor exits, the updated command waits in the query buffer;
1619 type semicolon or <literal>\g</> to send it, or <literal>\r</>
1624 The target function can be specified by name alone, or by name
1625 and arguments, for example <literal>foo(integer, text)</>.
1626 The argument types must be given if there is more
1627 than one function of the same name.
1631 If no function is specified, a blank <command>CREATE FUNCTION</>
1632 template is presented for editing.
1636 If a line number is specified, <application>psql</application> will
1637 position the cursor on the specified line of the function body.
1638 (Note that the function body typically does not begin on the first
1644 See under <xref linkend="app-psql-environment"
1645 endterm="app-psql-environment-title"> for how to configure and
1646 customize your editor.
1654 <term><literal>\ev <optional> <replaceable class="parameter">view_name</> <optional> <replaceable class="parameter">line_number</> </optional> </optional> </literal></term>
1658 This command fetches and edits the definition of the named view,
1659 in the form of a <command>CREATE OR REPLACE VIEW</> command.
1660 Editing is done in the same way as for <literal>\edit</>.
1661 After the editor exits, the updated command waits in the query buffer;
1662 type semicolon or <literal>\g</> to send it, or <literal>\r</>
1667 If no view is specified, a blank <command>CREATE VIEW</>
1668 template is presented for editing.
1672 If a line number is specified, <application>psql</application> will
1673 position the cursor on the specified line of the view definition.
1680 <term><literal>\encoding [ <replaceable class="parameter">encoding</replaceable> ]</literal></term>
1684 Sets the client character set encoding. Without an argument, this command
1685 shows the current encoding.
1692 <term><literal>\f [ <replaceable class="parameter">string</replaceable> ]</literal></term>
1696 Sets the field separator for unaligned query output. The default
1697 is the vertical bar (<literal>|</literal>). See also
1698 <command>\pset</command> for a generic way of setting output
1706 <term><literal>\g [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
1707 <term><literal>\g [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
1710 Sends the current query input buffer to the server, and
1711 optionally stores the query's output in <replaceable
1712 class="parameter">filename</replaceable> or pipes the output
1713 to the shell command <replaceable
1714 class="parameter">command</replaceable>. The file or command is
1715 written to only if the query successfully returns zero or more tuples,
1716 not if the query fails or is a non-data-returning SQL command.
1719 A bare <literal>\g</literal> is essentially equivalent to a semicolon.
1720 A <literal>\g</literal> with argument is a <quote>one-shot</quote>
1721 alternative to the <command>\o</command> command.
1727 <term><literal>\gset [ <replaceable class="parameter">prefix</replaceable> ]</literal></term>
1731 Sends the current query input buffer to the server and stores the
1732 query's output into <application>psql</> variables (see <xref
1733 linkend="APP-PSQL-variables" endterm="APP-PSQL-variables-title">).
1734 The query to be executed must return exactly one row. Each column of
1735 the row is stored into a separate variable, named the same as the
1736 column. For example:
1738 => <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput>
1739 -> <userinput>\gset</userinput>
1740 => <userinput>\echo :var1 :var2</userinput>
1745 If you specify a <replaceable class="parameter">prefix</replaceable>,
1746 that string is prepended to the query's column names to create the
1747 variable names to use:
1749 => <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput>
1750 -> <userinput>\gset result_</userinput>
1751 => <userinput>\echo :result_var1 :result_var2</userinput>
1756 If a column result is NULL, the corresponding variable is unset
1757 rather than being set.
1760 If the query fails or does not return one row,
1761 no variables are changed.
1767 <term><literal>\h</literal> or <literal>\help</literal> <literal>[ <replaceable class="parameter">command</replaceable> ]</literal></term>
1770 Gives syntax help on the specified <acronym>SQL</acronym>
1771 command. If <replaceable class="parameter">command</replaceable>
1772 is not specified, then <application>psql</application> will list
1773 all the commands for which syntax help is available. If
1774 <replaceable class="parameter">command</replaceable> is an
1775 asterisk (<literal>*</literal>), then syntax help on all
1776 <acronym>SQL</acronym> commands is shown.
1781 To simplify typing, commands that consists of several words do
1782 not have to be quoted. Thus it is fine to type <userinput>\help
1783 alter table</userinput>.
1791 <term><literal>\H</literal> or <literal>\html</literal></term>
1794 Turns on <acronym>HTML</acronym> query output format. If the
1795 <acronym>HTML</acronym> format is already on, it is switched
1796 back to the default aligned text format. This command is for
1797 compatibility and convenience, but see <command>\pset</command>
1798 about setting other output options.
1805 <term><literal>\i</literal> or <literal>\include</literal> <replaceable class="parameter">filename</replaceable></term>
1808 Reads input from the file <replaceable
1809 class="parameter">filename</replaceable> and executes it as
1810 though it had been typed on the keyboard.
1814 If you want to see the lines on the screen as they are read you
1815 must set the variable <varname>ECHO</varname> to
1816 <literal>all</literal>.
1824 <term><literal>\ir</literal> or <literal>\include_relative</literal> <replaceable class="parameter">filename</replaceable></term>
1827 The <literal>\ir</> command is similar to <literal>\i</>, but resolves
1828 relative file names differently. When executing in interactive mode,
1829 the two commands behave identically. However, when invoked from a
1830 script, <literal>\ir</literal> interprets file names relative to the
1831 directory in which the script is located, rather than the current
1839 <term><literal>\l[+]</literal> or <literal>\list[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1842 List the databases in the server and show their names, owners,
1843 character set encodings, and access privileges.
1844 If <replaceable class="parameter">pattern</replaceable> is specified,
1845 only databases whose names match the pattern are listed.
1846 If <literal>+</literal> is appended to the command name, database
1847 sizes, default tablespaces, and descriptions are also displayed.
1848 (Size information is only available for databases that the current
1849 user can connect to.)
1856 <term><literal>\lo_export <replaceable class="parameter">loid</replaceable> <replaceable class="parameter">filename</replaceable></literal></term>
1860 Reads the large object with <acronym>OID</acronym> <replaceable
1861 class="parameter">loid</replaceable> from the database and
1862 writes it to <replaceable
1863 class="parameter">filename</replaceable>. Note that this is
1864 subtly different from the server function
1865 <function>lo_export</function>, which acts with the permissions
1866 of the user that the database server runs as and on the server's
1871 Use <command>\lo_list</command> to find out the large object's
1872 <acronym>OID</acronym>.
1880 <term><literal>\lo_import <replaceable class="parameter">filename</replaceable> [ <replaceable class="parameter">comment</replaceable> ]</literal></term>
1884 Stores the file into a <productname>PostgreSQL</productname>
1885 large object. Optionally, it associates the given
1886 comment with the object. Example:
1888 foo=> <userinput>\lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'</userinput>
1891 The response indicates that the large object received object
1892 ID 152801, which can be used to access the newly-created large
1893 object in the future. For the sake of readability, it is
1894 recommended to always associate a human-readable comment with
1895 every object. Both OIDs and comments can be viewed with the
1896 <command>\lo_list</command> command.
1900 Note that this command is subtly different from the server-side
1901 <function>lo_import</function> because it acts as the local user
1902 on the local file system, rather than the server's user and file
1909 <term><literal>\lo_list</literal></term>
1912 Shows a list of all <productname>PostgreSQL</productname>
1913 large objects currently stored in the database,
1914 along with any comments provided for them.
1920 <term><literal>\lo_unlink <replaceable class="parameter">loid</replaceable></literal></term>
1924 Deletes the large object with <acronym>OID</acronym>
1925 <replaceable class="parameter">loid</replaceable> from the
1931 Use <command>\lo_list</command> to find out the large object's
1932 <acronym>OID</acronym>.
1940 <term><literal>\o</literal> or <literal>\out [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
1941 <term><literal>\o</literal> or <literal>\out [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
1944 Arranges to save future query results to the file <replaceable
1945 class="parameter">filename</replaceable> or pipe future results
1946 to the shell command <replaceable
1947 class="parameter">command</replaceable>. If no argument is
1948 specified, the query output is reset to the standard output.
1951 <para><quote>Query results</quote> includes all tables, command
1952 responses, and notices obtained from the database server, as
1953 well as output of various backslash commands that query the
1954 database (such as <command>\d</command>), but not error
1960 To intersperse text output in between query results, use
1961 <command>\qecho</command>.
1969 <term><literal>\p</literal> or <literal>\print</literal></term>
1972 Print the current query buffer to the standard output.
1978 <term><literal>\password [ <replaceable class="parameter">username</replaceable> ]</literal></term>
1981 Changes the password of the specified user (by default, the current
1982 user). This command prompts for the new password, encrypts it, and
1983 sends it to the server as an <command>ALTER ROLE</> command. This
1984 makes sure that the new password does not appear in cleartext in the
1985 command history, the server log, or elsewhere.
1991 <term><literal>\prompt [ <replaceable class="parameter">text</replaceable> ] <replaceable class="parameter">name</replaceable></literal></term>
1994 Prompts the user to supply text, which is assigned to the variable
1995 <replaceable class="parameter">name</>.
1996 An optional prompt string, <replaceable
1997 class="parameter">text</>, can be specified. (For multiword
1998 prompts, surround the text with single quotes.)
2002 By default, <literal>\prompt</> uses the terminal for input and
2003 output. However, if the <option>-f</> command line switch was
2004 used, <literal>\prompt</> uses standard input and standard output.
2010 <term><literal>\pset [ <replaceable class="parameter">option</replaceable> [ <replaceable class="parameter">value</replaceable> ] ]</literal></term>
2014 This command sets options affecting the output of query result tables.
2015 <replaceable class="parameter">option</replaceable>
2016 indicates which option is to be set. The semantics of
2017 <replaceable class="parameter">value</replaceable> vary depending
2018 on the selected option. For some options, omitting <replaceable
2019 class="parameter">value</replaceable> causes the option to be toggled
2020 or unset, as described under the particular option. If no such
2021 behavior is mentioned, then omitting
2022 <replaceable class="parameter">value</replaceable> just results in
2023 the current setting being displayed.
2027 <command>\pset</command> without any arguments displays the current status
2028 of all printing options.
2032 Adjustable printing options are:
2035 <term><literal>border</literal></term>
2038 The <replaceable class="parameter">value</replaceable> must be a
2039 number. In general, the higher
2040 the number the more borders and lines the tables will have,
2041 but this depends on the particular format. In
2042 <acronym>HTML</acronym> format, this will translate directly
2043 into the <literal>border=...</literal> attribute; in
2044 <literal>latex</literal> and <literal>latex-longtable</literal>
2045 formats, a value of 3 will add a dividing line between each row; in
2046 the other formats only values 0 (no border), 1 (internal dividing
2047 lines), and 2 (table frame) make sense and values above 2 will be
2048 treated the same as <literal>border = 2</literal>.
2054 <term><literal>columns</literal></term>
2057 Sets the target width for the <literal>wrapped</> format, and also
2058 the width limit for determining whether output is wide enough to
2059 require the pager or switch to the vertical display in expanded auto
2061 Zero (the default) causes the target width to be controlled by the
2062 environment variable <envar>COLUMNS</>, or the detected screen width
2063 if <envar>COLUMNS</> is not set.
2064 In addition, if <literal>columns</> is zero then the
2065 <literal>wrapped</> format only affects screen output.
2066 If <literal>columns</> is nonzero then file and pipe output is
2067 wrapped to that width as well.
2073 <term><literal>expanded</literal> (or <literal>x</literal>)</term>
2076 If <replaceable class="parameter">value</replaceable> is specified it
2077 must be either <literal>on</literal> or <literal>off</literal>, which
2078 will enable or disable expanded mode, or <literal>auto</literal>.
2079 If <replaceable class="parameter">value</replaceable> is omitted the
2080 command toggles between the on and off settings. When expanded mode
2081 is enabled, query results are displayed in two columns, with the
2082 column name on the left and the data on the right. This mode is
2083 useful if the data wouldn't fit on the screen in the
2084 normal <quote>horizontal</quote> mode. In the auto setting, the
2085 expanded mode is used whenever the query output is wider than the
2086 screen, otherwise the regular mode is used. The auto setting is only
2087 effective in the aligned and wrapped formats. In other formats, it
2088 always behaves as if the expanded mode is off.
2094 <term><literal>fieldsep</literal></term>
2097 Specifies the field separator to be used in unaligned output
2098 format. That way one can create, for example, tab- or
2099 comma-separated output, which other programs might prefer. To
2100 set a tab as field separator, type <literal>\pset fieldsep
2101 '\t'</literal>. The default field separator is
2102 <literal>'|'</literal> (a vertical bar).
2108 <term><literal>fieldsep_zero</literal></term>
2111 Sets the field separator to use in unaligned output format to a zero
2118 <term><literal>footer</literal></term>
2121 If <replaceable class="parameter">value</replaceable> is specified
2122 it must be either <literal>on</literal> or <literal>off</literal>
2123 which will enable or disable display of the table footer
2124 (the <literal>(<replaceable>n</> rows)</literal> count).
2125 If <replaceable class="parameter">value</replaceable> is omitted the
2126 command toggles footer display on or off.
2132 <term><literal>format</literal></term>
2135 Sets the output format to one of <literal>unaligned</literal>,
2136 <literal>aligned</literal>, <literal>wrapped</literal>,
2137 <literal>html</literal>, <literal>asciidoc</literal>,
2138 <literal>latex</literal> (uses <literal>tabular</literal>),
2139 <literal>latex-longtable</literal>, or
2140 <literal>troff-ms</literal>.
2141 Unique abbreviations are allowed. (That would mean one letter
2145 <para><literal>unaligned</> format writes all columns of a row on one
2146 line, separated by the currently active field separator. This
2147 is useful for creating output that might be intended to be read
2148 in by other programs (for example, tab-separated or comma-separated
2152 <para><literal>aligned</literal> format is the standard, human-readable,
2153 nicely formatted text output; this is the default.
2156 <para><literal>wrapped</> format is like <literal>aligned</> but wraps
2157 wide data values across lines to make the output fit in the target
2158 column width. The target width is determined as described under
2159 the <literal>columns</> option. Note that <application>psql</> will
2160 not attempt to wrap column header titles; therefore,
2161 <literal>wrapped</> format behaves the same as <literal>aligned</>
2162 if the total width needed for column headers exceeds the target.
2166 The <literal>html</>, <literal>asciidoc</>, <literal>latex</>,
2167 <literal>latex-longtable</literal>, and <literal>troff-ms</>
2168 formats put out tables that are intended to
2169 be included in documents using the respective mark-up
2170 language. They are not complete documents! This might not be
2171 necessary in <acronym>HTML</acronym>, but in
2172 <application>LaTeX</application> you must have a complete
2173 document wrapper. <literal>latex-longtable</literal>
2174 also requires the <application>LaTeX</application>
2175 <literal>longtable</literal> and <literal>booktabs</> packages.
2181 <term><literal>linestyle</literal></term>
2184 Sets the border line drawing style to one
2185 of <literal>ascii</literal>, <literal>old-ascii</literal>
2186 or <literal>unicode</literal>.
2187 Unique abbreviations are allowed. (That would mean one
2189 The default setting is <literal>ascii</>.
2190 This option only affects the <literal>aligned</> and
2191 <literal>wrapped</> output formats.
2194 <para><literal>ascii</literal> style uses plain <acronym>ASCII</acronym>
2195 characters. Newlines in data are shown using
2196 a <literal>+</literal> symbol in the right-hand margin.
2197 When the <literal>wrapped</literal> format wraps data from
2198 one line to the next without a newline character, a dot
2199 (<literal>.</>) is shown in the right-hand margin of the first line,
2200 and again in the left-hand margin of the following line.
2203 <para><literal>old-ascii</literal> style uses plain <acronym>ASCII</>
2204 characters, using the formatting style used
2205 in <productname>PostgreSQL</productname> 8.4 and earlier.
2206 Newlines in data are shown using a <literal>:</literal>
2207 symbol in place of the left-hand column separator.
2208 When the data is wrapped from one line
2209 to the next without a newline character, a <literal>;</>
2210 symbol is used in place of the left-hand column separator.
2213 <para><literal>unicode</literal> style uses Unicode box-drawing characters.
2214 Newlines in data are shown using a carriage return symbol
2215 in the right-hand margin. When the data is wrapped from one line
2216 to the next without a newline character, an ellipsis symbol
2217 is shown in the right-hand margin of the first line, and
2218 again in the left-hand margin of the following line.
2222 When the <literal>border</> setting is greater than zero,
2223 this option also determines the characters
2224 with which the border lines are drawn.
2225 Plain <acronym>ASCII</acronym> characters work everywhere, but
2226 Unicode characters look nicer on displays that recognize them.
2232 <term><literal>null</literal></term>
2235 Sets the string to be printed in place of a null value.
2236 The default is to print nothing, which can easily be mistaken for
2237 an empty string. For example, one might prefer <literal>\pset null
2244 <term><literal>numericlocale</literal></term>
2247 If <replaceable class="parameter">value</replaceable> is specified
2248 it must be either <literal>on</literal> or <literal>off</literal>
2249 which will enable or disable display of a locale-specific character
2250 to separate groups of digits to the left of the decimal marker.
2251 If <replaceable class="parameter">value</replaceable> is omitted the
2252 command toggles between regular and locale-specific numeric output.
2258 <term><literal>pager</literal></term>
2261 Controls use of a pager program for query and <application>psql</>
2262 help output. If the environment variable <envar>PAGER</envar>
2263 is set, the output is piped to the specified program.
2264 Otherwise a platform-dependent default (such as
2265 <filename>more</filename>) is used.
2269 When the <literal>pager</> option is <literal>off</>, the pager
2270 program is not used. When the <literal>pager</> option is
2271 <literal>on</>, the pager is used when appropriate, i.e., when the
2272 output is to a terminal and will not fit on the screen.
2273 The <literal>pager</> option can also be set to <literal>always</>,
2274 which causes the pager to be used for all terminal output regardless
2275 of whether it fits on the screen. <literal>\pset pager</>
2276 without a <replaceable class="parameter">value</replaceable>
2277 toggles pager use on and off.
2283 <term><literal>pager_min_lines</literal></term>
2286 If <literal>pager_min_lines</> is set to a number greater than the
2287 page height, the pager program will not be called unless there are
2288 at least this many lines of output to show. The default setting
2295 <term><literal>recordsep</literal></term>
2298 Specifies the record (line) separator to use in unaligned
2299 output format. The default is a newline character.
2305 <term><literal>recordsep_zero</literal></term>
2308 Sets the record separator to use in unaligned output format to a zero
2315 <term><literal>tableattr</literal> (or <literal>T</literal>)</term>
2318 In <acronym>HTML</acronym> format, this specifies attributes
2319 to be placed inside the <sgmltag>table</sgmltag> tag. This
2320 could for example be <literal>cellpadding</literal> or
2321 <literal>bgcolor</literal>. Note that you probably don't want
2322 to specify <literal>border</literal> here, as that is already
2323 taken care of by <literal>\pset border</literal>.
2325 <replaceable class="parameter">value</replaceable> is given,
2326 the table attributes are unset.
2329 In <literal>latex-longtable</literal> format, this controls
2330 the proportional width of each column containing a left-aligned
2331 data type. It is specified as a whitespace-separated list of values,
2332 e.g. <literal>'0.2 0.2 0.6'</>. Unspecified output columns
2333 use the last specified value.
2339 <term><literal>title</literal></term>
2342 Sets the table title for any subsequently printed tables. This
2343 can be used to give your output descriptive tags. If no
2344 <replaceable class="parameter">value</replaceable> is given,
2351 <term><literal>tuples_only</literal> (or <literal>t</literal>)</term>
2354 If <replaceable class="parameter">value</replaceable> is specified
2355 it must be either <literal>on</literal> or <literal>off</literal>
2356 which will enable or disable tuples-only mode.
2357 If <replaceable class="parameter">value</replaceable> is omitted the
2358 command toggles between regular and tuples-only output.
2359 Regular output includes extra information such
2360 as column headers, titles, and various footers. In tuples-only
2361 mode, only actual table data is shown.
2367 <term><literal>unicode_border_style</literal></term>
2370 Sets the border drawing style for the <literal>unicode</literal> line style to one
2371 of <literal>single</literal> or <literal>double</literal>.
2377 <term><literal>unicode_column_style</literal></term>
2380 Sets the column drawing style for the <literal>unicode</literal> line style to one
2381 of <literal>single</literal> or <literal>double</literal>.
2387 <term><literal>unicode_header_style</literal></term>
2390 Sets the header drawing style for the <literal>unicode</literal> line style to one
2391 of <literal>single</literal> or <literal>double</literal>.
2399 Illustrations of how these different formats look can be seen in
2400 the <xref linkend="APP-PSQL-examples"
2401 endterm="APP-PSQL-examples-title"> section.
2406 There are various shortcut commands for <command>\pset</command>. See
2407 <command>\a</command>, <command>\C</command>, <command>\H</command>,
2408 <command>\t</command>, <command>\T</command>, and <command>\x</command>.
2417 <term><literal>\q</literal> or <literal>\quit</literal></term>
2420 Quits the <application>psql</application> program.
2421 In a script file, only execution of that script is terminated.
2428 <term><literal>\qecho <replaceable class="parameter">text</replaceable> [ ... ] </literal></term>
2431 This command is identical to <command>\echo</command> except
2432 that the output will be written to the query output channel, as
2433 set by <command>\o</command>.
2440 <term><literal>\r</literal> or <literal>\reset</literal></term>
2443 Resets (clears) the query buffer.
2450 <term><literal>\s [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
2453 Print <application>psql</application>'s command line history
2454 to <replaceable class="parameter">filename</replaceable>.
2455 If <replaceable class="parameter">filename</replaceable> is omitted,
2456 the history is written to the standard output (using the pager if
2457 appropriate). This command is not available
2458 if <application>psql</application> was built
2459 without <application>Readline</application> support.
2466 <term><literal>\set [ <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> [ ... ] ] ]</literal></term>
2470 Sets the <application>psql</> variable <replaceable
2471 class="parameter">name</replaceable> to <replaceable
2472 class="parameter">value</replaceable>, or if more than one value
2473 is given, to the concatenation of all of them. If only one
2474 argument is given, the variable is set with an empty value. To
2475 unset a variable, use the <command>\unset</command> command.
2478 <para><command>\set</> without any arguments displays the names and values
2479 of all currently-set <application>psql</> variables.
2483 Valid variable names can contain letters, digits, and
2484 underscores. See the section <xref
2485 linkend="APP-PSQL-variables"
2486 endterm="APP-PSQL-variables-title"> below for details.
2487 Variable names are case-sensitive.
2491 Although you are welcome to set any variable to anything you
2492 want, <application>psql</application> treats several variables
2493 as special. They are documented in the section about variables.
2498 This command is unrelated to the <acronym>SQL</acronym>
2499 command <xref linkend="SQL-SET">.
2507 <term><literal>\setenv <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> ]</literal></term>
2511 Sets the environment variable <replaceable
2512 class="parameter">name</replaceable> to <replaceable
2513 class="parameter">value</replaceable>, or if the
2514 <replaceable class="parameter">value</replaceable> is
2515 not supplied, unsets the environment variable. Example:
2517 testdb=> <userinput>\setenv PAGER less</userinput>
2518 testdb=> <userinput>\setenv LESS -imx4F</userinput>
2519 </programlisting></para>
2524 <term><literal>\sf[+] <replaceable class="parameter">function_description</> </literal></term>
2528 This command fetches and shows the definition of the named function,
2529 in the form of a <command>CREATE OR REPLACE FUNCTION</> command.
2530 The definition is printed to the current query output channel,
2531 as set by <command>\o</command>.
2535 The target function can be specified by name alone, or by name
2536 and arguments, for example <literal>foo(integer, text)</>.
2537 The argument types must be given if there is more
2538 than one function of the same name.
2542 If <literal>+</literal> is appended to the command name, then the
2543 output lines are numbered, with the first line of the function body
2551 <term><literal>\sv[+] <replaceable class="parameter">view_name</> </literal></term>
2555 This command fetches and shows the definition of the named view,
2556 in the form of a <command>CREATE OR REPLACE VIEW</> command.
2557 The definition is printed to the current query output channel,
2558 as set by <command>\o</command>.
2562 If <literal>+</literal> is appended to the command name, then the
2563 output lines are numbered from 1.
2570 <term><literal>\t</literal></term>
2573 Toggles the display of output column name headings and row count
2574 footer. This command is equivalent to <literal>\pset
2575 tuples_only</literal> and is provided for convenience.
2582 <term><literal>\T <replaceable class="parameter">table_options</replaceable></literal></term>
2585 Specifies attributes to be placed within the
2586 <sgmltag>table</sgmltag> tag in <acronym>HTML</acronym>
2587 output format. This command is equivalent to <literal>\pset
2588 tableattr <replaceable
2589 class="parameter">table_options</replaceable></literal>.
2596 <term><literal>\timing [ <replaceable class="parameter">on</replaceable> | <replaceable class="parameter">off</replaceable> ]</literal></term>
2599 Without parameter, toggles a display of how long each SQL statement
2600 takes, in milliseconds. With parameter, sets same.
2607 <term><literal>\unset <replaceable class="parameter">name</replaceable></literal></term>
2611 Unsets (deletes) the <application>psql</> variable <replaceable
2612 class="parameter">name</replaceable>.
2619 <term><literal>\w</literal> or <literal>\write</literal> <replaceable class="parameter">filename</replaceable></term>
2620 <term><literal>\w</literal> or <literal>\write</literal> <literal>|</><replaceable class="parameter">command</replaceable></term>
2623 Outputs the current query buffer to the file <replaceable
2624 class="parameter">filename</replaceable> or pipes it to the shell
2625 command <replaceable class="parameter">command</replaceable>.
2632 <term><literal>\watch [ <replaceable class="parameter">seconds</replaceable> ]</literal></term>
2635 Repeatedly execute the current query buffer (like <literal>\g</>)
2636 until interrupted or the query fails. Wait the specified number of
2637 seconds (default 2) between executions.
2644 <term><literal>\x [ <replaceable class="parameter">on</replaceable> | <replaceable class="parameter">off</replaceable> | <replaceable class="parameter">auto</replaceable> ]</literal></term>
2647 Sets or toggles expanded table formatting mode. As such it is equivalent to
2648 <literal>\pset expanded</literal>.
2655 <term><literal>\z [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
2658 Lists tables, views and sequences with their
2659 associated access privileges.
2660 If a <replaceable class="parameter">pattern</replaceable> is
2661 specified, only tables, views and sequences whose names match the
2666 This is an alias for <command>\dp</command> (<quote>display
2667 privileges</quote>).
2674 <term><literal>\! [ <replaceable class="parameter">command</replaceable> ]</literal></term>
2677 Escapes to a separate shell or executes the shell command
2678 <replaceable class="parameter">command</replaceable>. The
2679 arguments are not further interpreted; the shell will see them
2680 as-is. In particular, the variable substitution rules and
2681 backslash escapes do not apply.
2688 <term><literal>\? [ <replaceable class="parameter">topic</> ]</literal></term>
2691 Shows help information. The optional
2692 <replaceable class="parameter">topic</> parameter
2693 (defaulting to <literal>commands</>) selects which part of <application>psql</application> is
2694 explained: <literal>commands</> describes <application>psql</>'s
2695 backslash commands; <literal>options</> describes the command-line
2696 options that can be passed to <application>psql</>;
2697 and <literal>variables</> shows help about about <application>psql</application> configuration
2706 <refsect3 id="APP-PSQL-patterns">
2707 <title id="APP-PSQL-patterns-title">Patterns</title>
2710 <primary>patterns</primary>
2711 <secondary>in psql and pg_dump</secondary>
2715 The various <literal>\d</> commands accept a <replaceable
2716 class="parameter">pattern</replaceable> parameter to specify the
2717 object name(s) to be displayed. In the simplest case, a pattern
2718 is just the exact name of the object. The characters within a
2719 pattern are normally folded to lower case, just as in SQL names;
2720 for example, <literal>\dt FOO</> will display the table named
2721 <literal>foo</>. As in SQL names, placing double quotes around
2722 a pattern stops folding to lower case. Should you need to include
2723 an actual double quote character in a pattern, write it as a pair
2724 of double quotes within a double-quote sequence; again this is in
2725 accord with the rules for SQL quoted identifiers. For example,
2726 <literal>\dt "FOO""BAR"</> will display the table named
2727 <literal>FOO"BAR</> (not <literal>foo"bar</>). Unlike the normal
2728 rules for SQL names, you can put double quotes around just part
2729 of a pattern, for instance <literal>\dt FOO"FOO"BAR</> will display
2730 the table named <literal>fooFOObar</>.
2734 Whenever the <replaceable class="parameter">pattern</replaceable> parameter
2735 is omitted completely, the <literal>\d</> commands display all objects
2736 that are visible in the current schema search path — this is
2737 equivalent to using <literal>*</> as the pattern.
2738 (An object is said to be <firstterm>visible</> if its
2739 containing schema is in the search path and no object of the same
2740 kind and name appears earlier in the search path. This is equivalent to the
2741 statement that the object can be referenced by name without explicit
2742 schema qualification.)
2743 To see all objects in the database regardless of visibility,
2744 use <literal>*.*</> as the pattern.
2748 Within a pattern, <literal>*</> matches any sequence of characters
2749 (including no characters) and <literal>?</> matches any single character.
2750 (This notation is comparable to Unix shell file name patterns.)
2751 For example, <literal>\dt int*</> displays tables whose names
2752 begin with <literal>int</>. But within double quotes, <literal>*</>
2753 and <literal>?</> lose these special meanings and are just matched
2758 A pattern that contains a dot (<literal>.</>) is interpreted as a schema
2759 name pattern followed by an object name pattern. For example,
2760 <literal>\dt foo*.*bar*</> displays all tables whose table name
2761 includes <literal>bar</> that are in schemas whose schema name
2762 starts with <literal>foo</>. When no dot appears, then the pattern
2763 matches only objects that are visible in the current schema search path.
2764 Again, a dot within double quotes loses its special meaning and is matched
2769 Advanced users can use regular-expression notations such as character
2770 classes, for example <literal>[0-9]</> to match any digit. All regular
2771 expression special characters work as specified in
2772 <xref linkend="functions-posix-regexp">, except for <literal>.</> which
2773 is taken as a separator as mentioned above, <literal>*</> which is
2774 translated to the regular-expression notation <literal>.*</>,
2775 <literal>?</> which is translated to <literal>.</>, and
2776 <literal>$</> which is matched literally. You can emulate
2777 these pattern characters at need by writing
2778 <literal>?</> for <literal>.</>,
2779 <literal>(<replaceable class="parameter">R</replaceable>+|)</literal> for
2780 <literal><replaceable class="parameter">R</replaceable>*</literal>, or
2781 <literal>(<replaceable class="parameter">R</replaceable>|)</literal> for
2782 <literal><replaceable class="parameter">R</replaceable>?</literal>.
2783 <literal>$</> is not needed as a regular-expression character since
2784 the pattern must match the whole name, unlike the usual
2785 interpretation of regular expressions (in other words, <literal>$</>
2786 is automatically appended to your pattern). Write <literal>*</> at the
2787 beginning and/or end if you don't wish the pattern to be anchored.
2788 Note that within double quotes, all regular expression special characters
2789 lose their special meanings and are matched literally. Also, the regular
2790 expression special characters are matched literally in operator name
2791 patterns (i.e., the argument of <literal>\do</>).
2797 <title>Advanced Features</title>
2799 <refsect3 id="APP-PSQL-variables">
2800 <title id="APP-PSQL-variables-title">Variables</title>
2803 <application>psql</application> provides variable substitution
2804 features similar to common Unix command shells.
2805 Variables are simply name/value pairs, where the value
2806 can be any string of any length. The name must consist of letters
2807 (including non-Latin letters), digits, and underscores.
2811 To set a variable, use the <application>psql</application> meta-command
2812 <command>\set</command>. For example,
2814 testdb=> <userinput>\set foo bar</userinput>
2816 sets the variable <literal>foo</literal> to the value
2817 <literal>bar</literal>. To retrieve the content of the variable, precede
2818 the name with a colon, for example:
2820 testdb=> <userinput>\echo :foo</userinput>
2823 This works in both regular SQL commands and meta-commands; there is
2824 more detail in <xref linkend="APP-PSQL-interpolation"
2825 endterm="APP-PSQL-interpolation-title">, below.
2829 If you call <command>\set</command> without a second argument, the
2830 variable is set, with an empty string as value. To unset (i.e., delete)
2831 a variable, use the command <command>\unset</command>. To show the
2832 values of all variables, call <command>\set</command> without any argument.
2837 The arguments of <command>\set</command> are subject to the same
2838 substitution rules as with other commands. Thus you can construct
2839 interesting references such as <literal>\set :foo
2840 'something'</literal> and get <quote>soft links</quote> or
2841 <quote>variable variables</quote> of <productname>Perl</productname>
2842 or <productname><acronym>PHP</acronym></productname> fame,
2843 respectively. Unfortunately (or fortunately?), there is no way to do
2844 anything useful with these constructs. On the other hand,
2845 <literal>\set bar :foo</literal> is a perfectly valid way to copy a
2851 A number of these variables are treated specially
2852 by <application>psql</application>. They represent certain option
2853 settings that can be changed at run time by altering the value of
2854 the variable, or in some cases represent changeable state of
2855 <application>psql</application>. Although
2856 you can use these variables for other purposes, this is not
2857 recommended, as the program behavior might grow really strange
2858 really quickly. By convention, all specially treated variables' names
2859 consist of all upper-case ASCII letters (and possibly digits and
2860 underscores). To ensure maximum compatibility in the future, avoid
2861 using such variable names for your own purposes. A list of all specially
2862 treated variables follows.
2868 <varname>AUTOCOMMIT</varname>
2870 <primary>autocommit</primary>
2871 <secondary>psql</secondary>
2876 When <literal>on</> (the default), each SQL command is automatically
2877 committed upon successful completion. To postpone commit in this
2878 mode, you must enter a <command>BEGIN</> or <command>START
2879 TRANSACTION</> SQL command. When <literal>off</> or unset, SQL
2880 commands are not committed until you explicitly issue
2881 <command>COMMIT</> or <command>END</>. The autocommit-off
2882 mode works by issuing an implicit <command>BEGIN</> for you, just
2883 before any command that is not already in a transaction block and
2884 is not itself a <command>BEGIN</> or other transaction-control
2885 command, nor a command that cannot be executed inside a transaction
2886 block (such as <command>VACUUM</>).
2891 In autocommit-off mode, you must explicitly abandon any failed
2892 transaction by entering <command>ABORT</> or <command>ROLLBACK</>.
2893 Also keep in mind that if you exit the session
2894 without committing, your work will be lost.
2900 The autocommit-on mode is <productname>PostgreSQL</>'s traditional
2901 behavior, but autocommit-off is closer to the SQL spec. If you
2902 prefer autocommit-off, you might wish to set it in the system-wide
2903 <filename>psqlrc</filename> file or your
2904 <filename>~/.psqlrc</filename> file.
2911 <term><varname>COMP_KEYWORD_CASE</varname></term>
2914 Determines which letter case to use when completing an SQL key word.
2915 If set to <literal>lower</literal> or <literal>upper</literal>, the
2916 completed word will be in lower or upper case, respectively. If set
2917 to <literal>preserve-lower</literal>
2918 or <literal>preserve-upper</literal> (the default), the completed word
2919 will be in the case of the word already entered, but words being
2920 completed without anything entered will be in lower or upper case,
2927 <term><varname>DBNAME</varname></term>
2930 The name of the database you are currently connected to. This is
2931 set every time you connect to a database (including program
2932 start-up), but can be unset.
2938 <term><varname>ECHO</varname></term>
2941 If set to <literal>all</literal>, all nonempty input lines are printed
2942 to standard output as they are read. (This does not apply to lines
2943 read interactively.) To select this behavior on program
2944 start-up, use the switch <option>-a</option>. If set to
2945 <literal>queries</literal>,
2946 <application>psql</application> prints each query to standard output
2947 as it is sent to the server. The switch for this is
2948 <option>-e</option>. If set to <literal>errors</literal>, then only
2949 failed queries are displayed on standard error output. The switch
2950 for this is <option>-b</option>. If unset, or if set to
2951 <literal>none</literal> (or any other value than those above) then
2952 no queries are displayed.
2958 <term><varname>ECHO_HIDDEN</varname></term>
2961 When this variable is set to <literal>on</> and a backslash command
2962 queries the database, the query is first shown.
2963 This feature helps you to study
2964 <productname>PostgreSQL</productname> internals and provide
2965 similar functionality in your own programs. (To select this behavior
2966 on program start-up, use the switch <option>-E</option>.) If you set
2967 the variable to the value <literal>noexec</literal>, the queries are
2968 just shown but are not actually sent to the server and executed.
2974 <term><varname>ENCODING</varname></term>
2977 The current client character set encoding.
2983 <term><varname>FETCH_COUNT</varname></term>
2986 If this variable is set to an integer value > 0,
2987 the results of <command>SELECT</command> queries are fetched
2988 and displayed in groups of that many rows, rather than the
2989 default behavior of collecting the entire result set before
2990 display. Therefore only a
2991 limited amount of memory is used, regardless of the size of
2992 the result set. Settings of 100 to 1000 are commonly used
2993 when enabling this feature.
2994 Keep in mind that when using this feature, a query might
2995 fail after having already displayed some rows.
2999 Although you can use any output format with this feature,
3000 the default <literal>aligned</> format tends to look bad
3001 because each group of <varname>FETCH_COUNT</varname> rows
3002 will be formatted separately, leading to varying column
3003 widths across the row groups. The other output formats work better.
3010 <term><varname>HISTCONTROL</varname></term>
3013 If this variable is set to <literal>ignorespace</literal>,
3014 lines which begin with a space are not entered into the history
3015 list. If set to a value of <literal>ignoredups</literal>, lines
3016 matching the previous history line are not entered. A value of
3017 <literal>ignoreboth</literal> combines the two options. If
3018 unset, or if set to <literal>none</literal> (or any other value
3019 than those above), all lines read in interactive mode are
3020 saved on the history list.
3024 This feature was shamelessly plagiarized from
3025 <application>Bash</application>.
3032 <term><varname>HISTFILE</varname></term>
3035 The file name that will be used to store the history list. The default
3036 value is <filename>~/.psql_history</filename>. For example, putting:
3038 \set HISTFILE ~/.psql_history- :DBNAME
3040 in <filename>~/.psqlrc</filename> will cause
3041 <application>psql</application> to maintain a separate history for
3046 This feature was shamelessly plagiarized from
3047 <application>Bash</application>.
3054 <term><varname>HISTSIZE</varname></term>
3057 The number of commands to store in the command history. The
3058 default value is 500.
3062 This feature was shamelessly plagiarized from
3063 <application>Bash</application>.
3070 <term><varname>HOST</varname></term>
3073 The database server host you are currently connected to. This is
3074 set every time you connect to a database (including program
3075 start-up), but can be unset.
3081 <term><varname>IGNOREEOF</varname></term>
3084 If unset, sending an <acronym>EOF</> character (usually
3085 <keycombo action="simul"><keycap>Control</><keycap>D</></>)
3086 to an interactive session of <application>psql</application>
3087 will terminate the application. If set to a numeric value,
3088 that many <acronym>EOF</> characters are ignored before the
3089 application terminates. If the variable is set but has no
3090 numeric value, the default is 10.
3094 This feature was shamelessly plagiarized from
3095 <application>Bash</application>.
3102 <term><varname>LASTOID</varname></term>
3105 The value of the last affected OID, as returned from an
3106 <command>INSERT</command> or <command>\lo_import</command>
3107 command. This variable is only guaranteed to be valid until
3108 after the result of the next <acronym>SQL</acronym> command has
3116 <varname>ON_ERROR_ROLLBACK</varname>
3118 <primary>rollback</primary>
3119 <secondary>psql</secondary>
3124 When set to <literal>on</>, if a statement in a transaction block
3125 generates an error, the error is ignored and the transaction
3126 continues. When set to <literal>interactive</>, such errors are only
3127 ignored in interactive sessions, and not when reading script
3128 files. When unset or set to <literal>off</>, a statement in a
3129 transaction block that generates an error aborts the entire
3130 transaction. The error rollback mode works by issuing an
3131 implicit <command>SAVEPOINT</> for you, just before each command
3132 that is in a transaction block, and then rolling back to the
3133 savepoint if the command fails.
3139 <term><varname>ON_ERROR_STOP</varname></term>
3142 By default, command processing continues after an error. When this
3143 variable is set to <literal>on</>, processing will instead stop
3144 immediately. In interactive mode,
3145 <application>psql</application> will return to the command prompt;
3146 otherwise, <application>psql</application> will exit, returning
3147 error code 3 to distinguish this case from fatal error
3148 conditions, which are reported using error code 1. In either case,
3149 any currently running scripts (the top-level script, if any, and any
3150 other scripts which it may have in invoked) will be terminated
3151 immediately. If the top-level command string contained multiple SQL
3152 commands, processing will stop with the current command.
3158 <term><varname>PORT</varname></term>
3161 The database server port to which you are currently connected.
3162 This is set every time you connect to a database (including
3163 program start-up), but can be unset.
3169 <term><varname>PROMPT1</varname></term>
3170 <term><varname>PROMPT2</varname></term>
3171 <term><varname>PROMPT3</varname></term>
3174 These specify what the prompts <application>psql</application>
3175 issues should look like. See <xref
3176 linkend="APP-PSQL-prompting"
3177 endterm="APP-PSQL-prompting-title"> below.
3183 <term><varname>QUIET</varname></term>
3186 Setting this variable to <literal>on</> is equivalent to the command
3187 line option <option>-q</option>. It is probably not too useful in
3194 <term><varname>SHOW_CONTEXT</varname></term>
3197 This variable can be set to the
3198 values <literal>never</>, <literal>errors</>, or <literal>always</>
3199 to control whether <literal>CONTEXT</> fields are displayed in
3200 messages from the server. The default is <literal>errors</> (meaning
3201 that context will be shown in error messages, but not in notice or
3202 warning messages). This setting has no effect
3203 when <varname>VERBOSITY</> is set to <literal>terse</>.
3209 <term><varname>SINGLELINE</varname></term>
3212 Setting this variable to <literal>on</> is equivalent to the command
3213 line option <option>-S</option>.
3219 <term><varname>SINGLESTEP</varname></term>
3222 Setting this variable to <literal>on</> is equivalent to the command
3223 line option <option>-s</option>.
3229 <term><varname>USER</varname></term>
3232 The database user you are currently connected as. This is set
3233 every time you connect to a database (including program
3234 start-up), but can be unset.
3240 <term><varname>VERBOSITY</varname></term>
3243 This variable can be set to the values <literal>default</>,
3244 <literal>verbose</>, or <literal>terse</> to control the verbosity
3254 <refsect3 id="APP-PSQL-interpolation">
3255 <title id="APP-PSQL-interpolation-title"><acronym>SQL</acronym> Interpolation</title>
3258 A key feature of <application>psql</application>
3259 variables is that you can substitute (<quote>interpolate</quote>)
3260 them into regular <acronym>SQL</acronym> statements, as well as the
3261 arguments of meta-commands. Furthermore,
3262 <application>psql</application> provides facilities for
3263 ensuring that variable values used as SQL literals and identifiers are
3264 properly quoted. The syntax for interpolating a value without
3265 any quoting is to prepend the variable name with a colon
3266 (<literal>:</literal>). For example,
3268 testdb=> <userinput>\set foo 'my_table'</userinput>
3269 testdb=> <userinput>SELECT * FROM :foo;</userinput>
3271 would query the table <literal>my_table</literal>. Note that this
3272 may be unsafe: the value of the variable is copied literally, so it can
3273 contain unbalanced quotes, or even backslash commands. You must make sure
3274 that it makes sense where you put it.
3278 When a value is to be used as an SQL literal or identifier, it is
3279 safest to arrange for it to be quoted. To quote the value of
3280 a variable as an SQL literal, write a colon followed by the variable
3281 name in single quotes. To quote the value as an SQL identifier, write
3282 a colon followed by the variable name in double quotes.
3283 These constructs deal correctly with quotes and other special
3284 characters embedded within the variable value.
3285 The previous example would be more safely written this way:
3287 testdb=> <userinput>\set foo 'my_table'</userinput>
3288 testdb=> <userinput>SELECT * FROM :"foo";</userinput>
3293 Variable interpolation will not be performed within quoted
3294 <acronym>SQL</acronym> literals and identifiers. Therefore, a
3295 construction such as <literal>':foo'</> doesn't work to produce a quoted
3296 literal from a variable's value (and it would be unsafe if it did work,
3297 since it wouldn't correctly handle quotes embedded in the value).
3301 One example use of this mechanism is to
3302 copy the contents of a file into a table column.
3303 First load the file into a variable and then interpolate the variable's
3304 value as a quoted string:
3306 testdb=> <userinput>\set content `cat my_file.txt`</userinput>
3307 testdb=> <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
3309 (Note that this still won't work if <filename>my_file.txt</filename> contains NUL bytes.
3310 <application>psql</application> does not support embedded NUL bytes in variable values.)
3314 Since colons can legally appear in SQL commands, an apparent attempt
3315 at interpolation (that is, <literal>:name</literal>,
3316 <literal>:'name'</literal>, or <literal>:"name"</literal>) is not
3317 replaced unless the named variable is currently set. In any case, you
3318 can escape a colon with a backslash to protect it from substitution.
3322 The colon syntax for variables is standard <acronym>SQL</acronym> for
3323 embedded query languages, such as <application>ECPG</application>.
3324 The colon syntaxes for array slices and type casts are
3325 <productname>PostgreSQL</productname> extensions, which can sometimes
3326 conflict with the standard usage. The colon-quote syntax for escaping a
3327 variable's value as an SQL literal or identifier is a
3328 <application>psql</application> extension.
3333 <refsect3 id="APP-PSQL-prompting">
3334 <title id="APP-PSQL-prompting-title">Prompting</title>
3337 The prompts <application>psql</application> issues can be customized
3338 to your preference. The three variables <varname>PROMPT1</varname>,
3339 <varname>PROMPT2</varname>, and <varname>PROMPT3</varname> contain strings
3340 and special escape sequences that describe the appearance of the
3341 prompt. Prompt 1 is the normal prompt that is issued when
3342 <application>psql</application> requests a new command. Prompt 2 is
3343 issued when more input is expected during command input because the
3344 command was not terminated with a semicolon or a quote was not closed.
3345 Prompt 3 is issued when you run an <acronym>SQL</acronym>
3346 <command>COPY</command> command and you are expected to type in the
3347 row values on the terminal.
3351 The value of the selected prompt variable is printed literally,
3352 except where a percent sign (<literal>%</literal>) is encountered.
3353 Depending on the next character, certain other text is substituted
3354 instead. Defined substitutions are:
3358 <term><literal>%M</literal></term>
3361 The full host name (with domain name) of the database server,
3362 or <literal>[local]</literal> if the connection is over a Unix
3364 <literal>[local:<replaceable>/dir/name</replaceable>]</literal>,
3365 if the Unix domain socket is not at the compiled in default
3372 <term><literal>%m</literal></term>
3375 The host name of the database server, truncated at the
3376 first dot, or <literal>[local]</literal> if the connection is
3377 over a Unix domain socket.
3383 <term><literal>%p</literal></term>
3385 <para>The process ID of the backend currently connected to.</para>
3390 <term><literal>%></literal></term>
3391 <listitem><para>The port number at which the database server is listening.</para></listitem>
3395 <term><literal>%n</literal></term>
3398 The database session user name. (The expansion of this
3399 value might change during a database session as the result
3400 of the command <command>SET SESSION
3401 AUTHORIZATION</command>.)
3407 <term><literal>%/</literal></term>
3408 <listitem><para>The name of the current database.</para></listitem>
3412 <term><literal>%~</literal></term>
3413 <listitem><para>Like <literal>%/</literal>, but the output is <literal>~</literal>
3414 (tilde) if the database is your default database.</para></listitem>
3418 <term><literal>%#</literal></term>
3421 If the session user is a database superuser, then a
3422 <literal>#</literal>, otherwise a <literal>></literal>.
3423 (The expansion of this value might change during a database
3424 session as the result of the command <command>SET SESSION
3425 AUTHORIZATION</command>.)
3431 <term><literal>%R</literal></term>
3434 In prompt 1 normally <literal>=</literal>, but <literal>^</literal> if
3435 in single-line mode, and <literal>!</literal> if the session is
3436 disconnected from the database (which can happen if
3437 <command>\connect</command> fails). In prompt 2 the sequence is
3438 replaced by <literal>-</literal>, <literal>*</literal>, a single quote,
3439 a double quote, or a dollar sign, depending on whether
3440 <application>psql</application> expects more input because the
3441 command wasn't terminated yet, because you are inside a
3442 <literal>/* ... */</literal> comment, or because you are inside
3443 a quoted or dollar-escaped string. In prompt 3 the sequence doesn't
3450 <term><literal>%x</literal></term>
3453 Transaction status: an empty string when not in a transaction
3454 block, or <literal>*</> when in a transaction block, or
3455 <literal>!</> when in a failed transaction block, or <literal>?</>
3456 when the transaction state is indeterminate (for example, because
3457 there is no connection).
3463 <term><literal>%l</literal></term>
3466 The line number inside the current statement, starting from <literal>1</>.
3472 <term><literal>%</literal><replaceable class="parameter">digits</replaceable></term>
3475 The character with the indicated octal code is substituted.
3481 <term><literal>%:</literal><replaceable class="parameter">name</replaceable><literal>:</literal></term>
3484 The value of the <application>psql</application> variable
3485 <replaceable class="parameter">name</replaceable>. See the
3486 section <xref linkend="APP-PSQL-variables"
3487 endterm="APP-PSQL-variables-title"> for details.
3493 <term><literal>%`</literal><replaceable class="parameter">command</replaceable><literal>`</literal></term>
3496 The output of <replaceable
3497 class="parameter">command</replaceable>, similar to ordinary
3498 <quote>back-tick</quote> substitution.
3504 <term><literal>%[</literal> ... <literal>%]</literal></term>
3507 Prompts can contain terminal control characters which, for
3508 example, change the color, background, or style of the prompt
3509 text, or change the title of the terminal window. In order for
3510 the line editing features of <application>Readline</application> to work properly, these
3511 non-printing control characters must be designated as invisible
3512 by surrounding them with <literal>%[</literal> and
3513 <literal>%]</literal>. Multiple pairs of these can occur within
3514 the prompt. For example:
3516 testdb=> \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '
3518 results in a boldfaced (<literal>1;</literal>) yellow-on-black
3519 (<literal>33;40</literal>) prompt on VT100-compatible, color-capable
3527 To insert a percent sign into your prompt, write
3528 <literal>%%</literal>. The default prompts are
3529 <literal>'%/%R%# '</literal> for prompts 1 and 2, and
3530 <literal>'>> '</literal> for prompt 3.
3535 This feature was shamelessly plagiarized from
3536 <application>tcsh</application>.
3543 <title>Command-Line Editing</title>
3546 <application>psql</application> supports the <application>Readline</application>
3547 library for convenient line editing and retrieval. The command
3548 history is automatically saved when <application>psql</application>
3549 exits and is reloaded when
3550 <application>psql</application> starts up. Tab-completion is also
3551 supported, although the completion logic makes no claim to be an
3552 <acronym>SQL</acronym> parser. The queries generated by tab-completion
3553 can also interfere with other SQL commands, e.g. <literal>SET
3554 TRANSACTION ISOLATION LEVEL</>.
3555 If for some reason you do not like the tab completion, you
3556 can turn it off by putting this in a file named
3557 <filename>.inputrc</filename> in your home directory:
3560 set disable-completion on
3563 (This is not a <application>psql</application> but a
3564 <application>Readline</application> feature. Read its documentation
3565 for further details.)
3572 <refsect1 id="app-psql-environment">
3573 <title id="app-psql-environment-title">Environment</title>
3578 <term><envar>COLUMNS</envar></term>
3582 If <literal>\pset columns</> is zero, controls the
3583 width for the <literal>wrapped</> format and width for determining
3584 if wide output requires the pager or should be switched to the
3585 vertical format in expanded auto mode.
3591 <term><envar>PAGER</envar></term>
3595 If the query results do not fit on the screen, they are piped
3596 through this command. Typical values are
3597 <literal>more</literal> or <literal>less</literal>. The default
3598 is platform-dependent. The use of the pager can be disabled by
3599 using the <command>\pset</command> command.
3605 <term><envar>PGDATABASE</envar></term>
3606 <term><envar>PGHOST</envar></term>
3607 <term><envar>PGPORT</envar></term>
3608 <term><envar>PGUSER</envar></term>
3612 Default connection parameters (see <xref linkend="libpq-envars">).
3618 <term><envar>PSQL_EDITOR</envar></term>
3619 <term><envar>EDITOR</envar></term>
3620 <term><envar>VISUAL</envar></term>
3624 Editor used by the <command>\e</command>, <command>\ef</command>,
3625 and <command>\ev</command> commands.
3626 These variables are examined in the order listed;
3627 the first that is set is used.
3631 The built-in default editors are <filename>vi</filename> on Unix
3632 systems and <filename>notepad.exe</filename> on Windows systems.
3638 <term><envar>PSQL_EDITOR_LINENUMBER_ARG</envar></term>
3642 When <command>\e</command>, <command>\ef</command>, or
3643 <command>\ev</command> is used
3644 with a line number argument, this variable specifies the
3645 command-line argument used to pass the starting line number to
3646 the user's editor. For editors such as <productname>Emacs</> or
3647 <productname>vi</>, this is a plus sign. Include a trailing
3648 space in the value of the variable if there needs to be space
3649 between the option name and the line number. Examples:
3651 PSQL_EDITOR_LINENUMBER_ARG='+'
3652 PSQL_EDITOR_LINENUMBER_ARG='--line '
3657 The default is <literal>+</literal> on Unix systems
3658 (corresponding to the default editor <filename>vi</filename>,
3659 and useful for many other common editors); but there is no
3660 default on Windows systems.
3666 <term><envar>PSQL_HISTORY</envar></term>
3670 Alternative location for the command history file. Tilde (<literal>~</literal>) expansion is performed.
3676 <term><envar>PSQLRC</envar></term>
3680 Alternative location of the user's <filename>.psqlrc</filename> file. Tilde (<literal>~</literal>) expansion is performed.
3686 <term><envar>SHELL</envar></term>
3690 Command executed by the <command>\!</command> command.
3696 <term><envar>TMPDIR</envar></term>
3700 Directory for storing temporary files. The default is
3701 <filename>/tmp</filename>.
3708 This utility, like most other <productname>PostgreSQL</> utilities,
3709 also uses the environment variables supported by <application>libpq</>
3710 (see <xref linkend="libpq-envars">).
3717 <title>Files</title>
3721 <term><filename>psqlrc</filename> and <filename>~/.psqlrc</filename></term>
3724 Unless it is passed an <option>-X</option> or <option>-c</option> option,
3725 <application>psql</application> attempts to read and execute commands
3726 from the system-wide startup file (<filename>psqlrc</filename>) and then
3727 the user's personal startup file (<filename>~/.psqlrc</filename>), after
3728 connecting to the database but before accepting normal commands.
3729 These files can be used to set up the client and/or the server to taste,
3730 typically with <command>\set</command> and <command>SET</command>
3734 The system-wide startup file is named <filename>psqlrc</filename> and is
3735 sought in the installation's <quote>system configuration</> directory,
3736 which is most reliably identified by running <literal>pg_config
3737 --sysconfdir</>. By default this directory will be <filename>../etc/</>
3738 relative to the directory containing
3739 the <productname>PostgreSQL</productname> executables. The name of this
3740 directory can be set explicitly via the <envar>PGSYSCONFDIR</envar>
3741 environment variable.
3744 The user's personal startup file is named <filename>.psqlrc</filename>
3745 and is sought in the invoking user's home directory. On Windows, which
3746 lacks such a concept, the personal startup file is named
3747 <filename>%APPDATA%\postgresql\psqlrc.conf</filename>.
3748 The location of the user's startup file can be set explicitly via
3749 the <envar>PSQLRC</envar> environment variable.
3752 Both the system-wide startup file and the user's personal startup file
3753 can be made <application>psql</application>-version-specific
3754 by appending a dash and the <productname>PostgreSQL</productname>
3755 major or minor release number to the file name,
3756 for example <filename>~/.psqlrc-9.2</filename> or
3757 <filename>~/.psqlrc-9.2.5</filename>. The most specific
3758 version-matching file will be read in preference to a
3759 non-version-specific file.
3765 <term><filename>.psql_history</filename></term>
3768 The command-line history is stored in the file
3769 <filename>~/.psql_history</filename>, or
3770 <filename>%APPDATA%\postgresql\psql_history</filename> on Windows.
3773 The location of the history file can be set explicitly via
3774 the <envar>PSQL_HISTORY</envar> environment variable.
3783 <title>Notes</title>
3788 In an earlier life <application>psql</application> allowed the
3789 first argument of a single-letter backslash command to start
3790 directly after the command, without intervening whitespace.
3791 As of <productname>PostgreSQL</productname> 8.4 this is no
3797 <para><application>psql</application> works best with servers of the same
3798 or an older major version. Backslash commands are particularly likely
3799 to fail if the server is of a newer version than <application>psql</>
3800 itself. However, backslash commands of the <literal>\d</> family should
3801 work with servers of versions back to 7.4, though not necessarily with
3802 servers newer than <application>psql</> itself. The general
3803 functionality of running SQL commands and displaying query results
3804 should also work with servers of a newer major version, but this cannot
3805 be guaranteed in all cases.
3808 If you want to use <application>psql</application> to connect to several
3809 servers of different major versions, it is recommended that you use the
3810 newest version of <application>psql</application>. Alternatively, you
3811 can keep a copy of <application>psql</application> from each major
3812 version around and be sure to use the version that matches the
3813 respective server. But in practice, this additional complication should
3823 <title>Notes for Windows Users</title>
3826 <application>psql</application> is built as a <quote>console
3827 application</>. Since the Windows console windows use a different
3828 encoding than the rest of the system, you must take special care
3829 when using 8-bit characters within <application>psql</application>.
3830 If <application>psql</application> detects a problematic
3831 console code page, it will warn you at startup. To change the
3832 console code page, two things are necessary:
3837 Set the code page by entering <userinput>cmd.exe /c chcp
3838 1252</userinput>. (1252 is a code page that is appropriate for
3839 German; replace it with your value.) If you are using Cygwin,
3840 you can put this command in <filename>/etc/profile</filename>.
3846 Set the console font to <literal>Lucida Console</>, because the
3847 raster font does not work with the ANSI code page.
3850 </itemizedlist></para>
3855 <refsect1 id="APP-PSQL-examples">
3856 <title id="APP-PSQL-examples-title">Examples</title>
3859 The first example shows how to spread a command over several lines of
3860 input. Notice the changing prompt:
3862 testdb=> <userinput>CREATE TABLE my_table (</userinput>
3863 testdb(> <userinput> first integer not null default 0,</userinput>
3864 testdb(> <userinput> second text)</userinput>
3865 testdb-> <userinput>;</userinput>
3868 Now look at the table definition again:
3870 testdb=> <userinput>\d my_table</userinput>
3872 Attribute | Type | Modifier
3873 -----------+---------+--------------------
3874 first | integer | not null default 0
3878 Now we change the prompt to something more interesting:
3880 testdb=> <userinput>\set PROMPT1 '%n@%m %~%R%# '</userinput>
3881 peter@localhost testdb=>
3883 Let's assume you have filled the table with data and want to take a
3886 peter@localhost testdb=> SELECT * FROM my_table;
3896 You can display tables in different ways by using the
3897 <command>\pset</command> command:
3899 peter@localhost testdb=> <userinput>\pset border 2</userinput>
3901 peter@localhost testdb=> <userinput>SELECT * FROM my_table;</userinput>
3912 peter@localhost testdb=> <userinput>\pset border 0</userinput>
3914 peter@localhost testdb=> <userinput>SELECT * FROM my_table;</userinput>
3923 peter@localhost testdb=> <userinput>\pset border 1</userinput>
3925 peter@localhost testdb=> <userinput>\pset format unaligned</userinput>
3926 Output format is unaligned.
3927 peter@localhost testdb=> <userinput>\pset fieldsep ","</userinput>
3928 Field separator is ",".
3929 peter@localhost testdb=> <userinput>\pset tuples_only</userinput>
3930 Showing only tuples.
3931 peter@localhost testdb=> <userinput>SELECT second, first FROM my_table;</userinput>
3937 Alternatively, use the short commands:
3939 peter@localhost testdb=> <userinput>\a \t \x</userinput>
3940 Output format is aligned.
3942 Expanded display is on.
3943 peter@localhost testdb=> <userinput>SELECT * FROM my_table;</userinput>
3956 </programlisting></para>