1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>psql</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="app-pgverifybackup.html" title="pg_verifybackup" /><link rel="next" href="app-reindexdb.html" title="reindexdb" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center"><span class="application">psql</span></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="app-pgverifybackup.html" title="pg_verifybackup">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="reference-client.html" title="PostgreSQL Client Applications">Up</a></td><th width="60%" align="center">PostgreSQL Client Applications</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="app-reindexdb.html" title="reindexdb">Next</a></td></tr></table><hr /></div><div class="refentry" id="APP-PSQL"><div class="titlepage"></div><a id="id-1.9.4.21.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle"><span class="application">psql</span></span></h2><p><span class="application">psql</span> —
3 <span class="productname">PostgreSQL</span> interactive terminal
4 </p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p id="id-1.9.4.21.4.1"><code class="command">psql</code> [<em class="replaceable"><code>option</code></em>...] [<em class="replaceable"><code>dbname</code></em>
5 [<em class="replaceable"><code>username</code></em>]]</p></div></div><div class="refsect1" id="id-1.9.4.21.5"><h2>Description</h2><p>
6 <span class="application">psql</span> is a terminal-based front-end to
7 <span class="productname">PostgreSQL</span>. It enables you to type in
8 queries interactively, issue them to
9 <span class="productname">PostgreSQL</span>, and see the query results.
10 Alternatively, input can be from a file or from command line
11 arguments. In addition, <span class="application">psql</span> provides a
12 number of meta-commands and various shell-like features to
13 facilitate writing scripts and automating a wide variety of tasks.
14 </p></div><div class="refsect1" id="R1-APP-PSQL-3"><h2>Options</h2><div class="variablelist"><dl class="variablelist"><dt id="APP-PSQL-OPTION-ECHO-ALL"><span class="term"><code class="option">-a</code><br /></span><span class="term"><code class="option">--echo-all</code></span> <a href="#APP-PSQL-OPTION-ECHO-ALL" class="id_link">#</a></dt><dd><p>
15 Print all nonempty input lines to standard output as they are read.
16 (This does not apply to lines read interactively.) This is
17 equivalent to setting the variable <code class="varname">ECHO</code> to
18 <code class="literal">all</code>.
19 </p></dd><dt id="APP-PSQL-OPTION-NO-ALIGN"><span class="term"><code class="option">-A</code><br /></span><span class="term"><code class="option">--no-align</code></span> <a href="#APP-PSQL-OPTION-NO-ALIGN" class="id_link">#</a></dt><dd><p>
20 Switches to unaligned output mode. (The default output mode is
21 <code class="literal">aligned</code>.) This is equivalent to
22 <code class="command">\pset format unaligned</code>.
23 </p></dd><dt id="APP-PSQL-OPTION-ECHO-ERRORS"><span class="term"><code class="option">-b</code><br /></span><span class="term"><code class="option">--echo-errors</code></span> <a href="#APP-PSQL-OPTION-ECHO-ERRORS" class="id_link">#</a></dt><dd><p>
24 Print failed SQL commands to standard error output. This is
25 equivalent to setting the variable <code class="varname">ECHO</code> to
26 <code class="literal">errors</code>.
27 </p></dd><dt id="APP-PSQL-OPTION-COMMAND"><span class="term"><code class="option">-c <em class="replaceable"><code>command</code></em></code><br /></span><span class="term"><code class="option">--command=<em class="replaceable"><code>command</code></em></code></span> <a href="#APP-PSQL-OPTION-COMMAND" class="id_link">#</a></dt><dd><p>
28 Specifies that <span class="application">psql</span> is to execute the given
29 command string, <em class="replaceable"><code>command</code></em>.
30 This option can be repeated and combined in any order with
31 the <code class="option">-f</code> option. When either <code class="option">-c</code>
32 or <code class="option">-f</code> is specified, <span class="application">psql</span>
33 does not read commands from standard input; instead it terminates
34 after processing all the <code class="option">-c</code> and <code class="option">-f</code>
37 <em class="replaceable"><code>command</code></em> must be either
38 a command string that is completely parsable by the server (i.e.,
39 it contains no <span class="application">psql</span>-specific features),
40 or a single backslash command. Thus you cannot mix
41 <acronym class="acronym">SQL</acronym> and <span class="application">psql</span>
42 meta-commands within a <code class="option">-c</code> option. To achieve that,
43 you could use repeated <code class="option">-c</code> options or pipe the string
44 into <span class="application">psql</span>, for example:
45 </p><pre class="programlisting">
46 psql -c '\x' -c 'SELECT * FROM foo;'
49 </p><pre class="programlisting">
50 echo '\x \\ SELECT * FROM foo;' | psql
52 (<code class="literal">\\</code> is the separator meta-command.)
54 Each <acronym class="acronym">SQL</acronym> command string passed
55 to <code class="option">-c</code> is sent to the server as a single request.
56 Because of this, the server executes it as a single transaction even
57 if the string contains multiple <acronym class="acronym">SQL</acronym> commands,
58 unless there are explicit <code class="command">BEGIN</code>/<code class="command">COMMIT</code>
59 commands included in the string to divide it into multiple
60 transactions. (See <a class="xref" href="protocol-flow.html#PROTOCOL-FLOW-MULTI-STATEMENT" title="54.2.2.1. Multiple Statements in a Simple Query">Section 54.2.2.1</a>
61 for more details about how the server handles multi-query strings.)
63 If having several commands executed in one transaction is not desired,
64 use repeated <code class="option">-c</code> commands or feed multiple commands to
65 <span class="application">psql</span>'s standard input,
66 either using <span class="application">echo</span> as illustrated above, or
67 via a shell here-document, for example:
68 </p><pre class="programlisting">
73 </pre></dd><dt id="APP-PSQL-OPTION-CSV"><span class="term"><code class="option">--csv</code></span> <a href="#APP-PSQL-OPTION-CSV" class="id_link">#</a></dt><dd><p>
74 Switches to <acronym class="acronym">CSV</acronym> (Comma-Separated Values) output
75 mode. This is equivalent to <code class="command">\pset format csv</code>.
76 </p></dd><dt id="APP-PSQL-OPTION-DBNAME"><span class="term"><code class="option">-d <em class="replaceable"><code>dbname</code></em></code><br /></span><span class="term"><code class="option">--dbname=<em class="replaceable"><code>dbname</code></em></code></span> <a href="#APP-PSQL-OPTION-DBNAME" class="id_link">#</a></dt><dd><p>
77 Specifies the name of the database to connect to. This is
78 equivalent to specifying <em class="replaceable"><code>dbname</code></em> as the first non-option
79 argument on the command line. The <em class="replaceable"><code>dbname</code></em>
80 can be a <a class="link" href="libpq-connect.html#LIBPQ-CONNSTRING" title="32.1.1. Connection Strings">connection string</a>.
81 If so, connection string parameters will override any conflicting
83 </p></dd><dt id="APP-PSQL-OPTION-ECHO-QUERIES"><span class="term"><code class="option">-e</code><br /></span><span class="term"><code class="option">--echo-queries</code></span> <a href="#APP-PSQL-OPTION-ECHO-QUERIES" class="id_link">#</a></dt><dd><p>
84 Copy all SQL commands sent to the server to standard output as well.
86 to setting the variable <code class="varname">ECHO</code> to
87 <code class="literal">queries</code>.
88 </p></dd><dt id="APP-PSQL-OPTION-ECHO-HIDDEN"><span class="term"><code class="option">-E</code><br /></span><span class="term"><code class="option">--echo-hidden</code></span> <a href="#APP-PSQL-OPTION-ECHO-HIDDEN" class="id_link">#</a></dt><dd><p>
89 Echo the actual queries generated by <code class="command">\d</code> and other backslash
90 commands. You can use this to study <span class="application">psql</span>'s
91 internal operations. This is equivalent to
92 setting the variable <code class="varname">ECHO_HIDDEN</code> to <code class="literal">on</code>.
93 </p></dd><dt id="APP-PSQL-OPTION-FILE"><span class="term"><code class="option">-f <em class="replaceable"><code>filename</code></em></code><br /></span><span class="term"><code class="option">--file=<em class="replaceable"><code>filename</code></em></code></span> <a href="#APP-PSQL-OPTION-FILE" class="id_link">#</a></dt><dd><p>
94 Read commands from the
95 file <em class="replaceable"><code>filename</code></em>,
96 rather than standard input.
97 This option can be repeated and combined in any order with
98 the <code class="option">-c</code> option. When either <code class="option">-c</code>
99 or <code class="option">-f</code> is specified, <span class="application">psql</span>
100 does not read commands from standard input; instead it terminates
101 after processing all the <code class="option">-c</code> and <code class="option">-f</code>
103 Except for that, this option is largely equivalent to the
104 meta-command <code class="command">\i</code>.
106 If <em class="replaceable"><code>filename</code></em> is <code class="literal">-</code>
107 (hyphen), then standard input is read until an EOF indication
108 or <code class="command">\q</code> meta-command. This can be used to intersperse
109 interactive input with input from files. Note however that Readline
110 is not used in this case (much as if <code class="option">-n</code> had been
113 Using this option is subtly different from writing <code class="literal">psql
114 < <em class="replaceable"><code>filename</code></em></code>. In general,
115 both will do what you expect, but using <code class="literal">-f</code>
116 enables some nice features such as error messages with line
117 numbers. There is also a slight chance that using this option will
118 reduce the start-up overhead. On the other hand, the variant using
119 the shell's input redirection is (in theory) guaranteed to yield
120 exactly the same output you would have received had you entered
122 </p></dd><dt id="APP-PSQL-OPTION-FIELD-SEPARATOR"><span class="term"><code class="option">-F <em class="replaceable"><code>separator</code></em></code><br /></span><span class="term"><code class="option">--field-separator=<em class="replaceable"><code>separator</code></em></code></span> <a href="#APP-PSQL-OPTION-FIELD-SEPARATOR" class="id_link">#</a></dt><dd><p>
123 Use <em class="replaceable"><code>separator</code></em> as the
124 field separator for unaligned output. This is equivalent to
125 <code class="command">\pset fieldsep</code> or <code class="command">\f</code>.
126 </p></dd><dt id="APP-PSQL-OPTION-FIELD-HOST"><span class="term"><code class="option">-h <em class="replaceable"><code>hostname</code></em></code><br /></span><span class="term"><code class="option">--host=<em class="replaceable"><code>hostname</code></em></code></span> <a href="#APP-PSQL-OPTION-FIELD-HOST" class="id_link">#</a></dt><dd><p>
127 Specifies the host name of the machine on which the
128 server is running. If the value begins
129 with a slash, it is used as the directory for the Unix-domain
131 </p></dd><dt id="APP-PSQL-OPTION-HTML"><span class="term"><code class="option">-H</code><br /></span><span class="term"><code class="option">--html</code></span> <a href="#APP-PSQL-OPTION-HTML" class="id_link">#</a></dt><dd><p>
132 Switches to <acronym class="acronym">HTML</acronym> output mode. This is
133 equivalent to <code class="command">\pset format html</code> or the
134 <code class="command">\H</code> command.
135 </p></dd><dt id="APP-PSQL-OPTION-LIST"><span class="term"><code class="option">-l</code><br /></span><span class="term"><code class="option">--list</code></span> <a href="#APP-PSQL-OPTION-LIST" class="id_link">#</a></dt><dd><p>
136 List all available databases, then exit. Other non-connection
137 options are ignored. This is similar to the meta-command
138 <code class="command">\list</code>.
140 When this option is used, <span class="application">psql</span> will connect
141 to the database <code class="literal">postgres</code>, unless a different database
142 is named on the command line (option <code class="option">-d</code> or non-option
143 argument, possibly via a service entry, but not via an environment
145 </p></dd><dt id="APP-PSQL-OPTION-LOG-FILE"><span class="term"><code class="option">-L <em class="replaceable"><code>filename</code></em></code><br /></span><span class="term"><code class="option">--log-file=<em class="replaceable"><code>filename</code></em></code></span> <a href="#APP-PSQL-OPTION-LOG-FILE" class="id_link">#</a></dt><dd><p>
146 Write all query output into file <em class="replaceable"><code>filename</code></em>, in addition to the
147 normal output destination.
148 </p></dd><dt id="APP-PSQL-OPTION-NO-READLINE"><span class="term"><code class="option">-n</code><br /></span><span class="term"><code class="option">--no-readline</code></span> <a href="#APP-PSQL-OPTION-NO-READLINE" class="id_link">#</a></dt><dd><p>
149 Do not use <span class="application">Readline</span> for line editing and
150 do not use the command history (see
151 <a class="xref" href="app-psql.html#APP-PSQL-READLINE" title="Command-Line Editing">the section called “Command-Line Editing”</a> below).
152 </p></dd><dt id="APP-PSQL-OPTION-OUTPUT"><span class="term"><code class="option">-o <em class="replaceable"><code>filename</code></em></code><br /></span><span class="term"><code class="option">--output=<em class="replaceable"><code>filename</code></em></code></span> <a href="#APP-PSQL-OPTION-OUTPUT" class="id_link">#</a></dt><dd><p>
153 Put all query output into file <em class="replaceable"><code>filename</code></em>. This is equivalent to
154 the command <code class="command">\o</code>.
155 </p></dd><dt id="APP-PSQL-OPTION-PORT"><span class="term"><code class="option">-p <em class="replaceable"><code>port</code></em></code><br /></span><span class="term"><code class="option">--port=<em class="replaceable"><code>port</code></em></code></span> <a href="#APP-PSQL-OPTION-PORT" class="id_link">#</a></dt><dd><p>
156 Specifies the TCP port or the local Unix-domain
157 socket file extension on which the server is listening for
158 connections. Defaults to the value of the <code class="envar">PGPORT</code>
159 environment variable or, if not set, to the port specified at
160 compile time, usually 5432.
161 </p></dd><dt id="APP-PSQL-OPTION-PSET"><span class="term"><code class="option">-P <em class="replaceable"><code>assignment</code></em></code><br /></span><span class="term"><code class="option">--pset=<em class="replaceable"><code>assignment</code></em></code></span> <a href="#APP-PSQL-OPTION-PSET" class="id_link">#</a></dt><dd><p>
162 Specifies printing options, in the style of
163 <code class="command">\pset</code>. Note that here you
164 have to separate name and value with an equal sign instead of a
165 space. For example, to set the output format to <span class="application">LaTeX</span>, you could write
166 <code class="literal">-P format=latex</code>.
167 </p></dd><dt id="APP-PSQL-OPTION-QUIET"><span class="term"><code class="option">-q</code><br /></span><span class="term"><code class="option">--quiet</code></span> <a href="#APP-PSQL-OPTION-QUIET" class="id_link">#</a></dt><dd><p>
168 Specifies that <span class="application">psql</span> should do its work
169 quietly. By default, it prints welcome messages and various
170 informational output. If this option is used, none of this
171 happens. This is useful with the <code class="option">-c</code> option.
172 This is equivalent to setting the variable <code class="varname">QUIET</code>
173 to <code class="literal">on</code>.
174 </p></dd><dt id="APP-PSQL-OPTION-RECORD-SEPARATOR"><span class="term"><code class="option">-R <em class="replaceable"><code>separator</code></em></code><br /></span><span class="term"><code class="option">--record-separator=<em class="replaceable"><code>separator</code></em></code></span> <a href="#APP-PSQL-OPTION-RECORD-SEPARATOR" class="id_link">#</a></dt><dd><p>
175 Use <em class="replaceable"><code>separator</code></em> as the
176 record separator for unaligned output. This is equivalent to
177 <code class="command">\pset recordsep</code>.
178 </p></dd><dt id="APP-PSQL-OPTION-SINGLE-STEP"><span class="term"><code class="option">-s</code><br /></span><span class="term"><code class="option">--single-step</code></span> <a href="#APP-PSQL-OPTION-SINGLE-STEP" class="id_link">#</a></dt><dd><p>
179 Run in single-step mode. That means the user is prompted before
180 each command is sent to the server, with the option to cancel
181 execution as well. Use this to debug scripts.
182 </p></dd><dt id="APP-PSQL-OPTION-SINGLE-LINE"><span class="term"><code class="option">-S</code><br /></span><span class="term"><code class="option">--single-line</code></span> <a href="#APP-PSQL-OPTION-SINGLE-LINE" class="id_link">#</a></dt><dd><p>
183 Runs in single-line mode where a newline terminates an SQL command, as a
185 </p><div class="note"><h3 class="title">Note</h3><p>
186 This mode is provided for those who insist on it, but you are not
187 necessarily encouraged to use it. In particular, if you mix
188 <acronym class="acronym">SQL</acronym> and meta-commands on a line the order of
189 execution might not always be clear to the inexperienced user.
190 </p></div></dd><dt id="APP-PSQL-OPTION-TUPLES-ONLY"><span class="term"><code class="option">-t</code><br /></span><span class="term"><code class="option">--tuples-only</code></span> <a href="#APP-PSQL-OPTION-TUPLES-ONLY" class="id_link">#</a></dt><dd><p>
191 Turn off printing of column names and result row count footers,
192 etc. This is equivalent to <code class="command">\t</code> or
193 <code class="command">\pset tuples_only</code>.
194 </p></dd><dt id="APP-PSQL-OPTION-TABLE-ATTR"><span class="term"><code class="option">-T <em class="replaceable"><code>table_options</code></em></code><br /></span><span class="term"><code class="option">--table-attr=<em class="replaceable"><code>table_options</code></em></code></span> <a href="#APP-PSQL-OPTION-TABLE-ATTR" class="id_link">#</a></dt><dd><p>
195 Specifies options to be placed within the
196 <acronym class="acronym">HTML</acronym> <code class="sgmltag-element">table</code> tag. See
197 <code class="command">\pset tableattr</code> for details.
198 </p></dd><dt id="APP-PSQL-OPTION-USERNAME"><span class="term"><code class="option">-U <em class="replaceable"><code>username</code></em></code><br /></span><span class="term"><code class="option">--username=<em class="replaceable"><code>username</code></em></code></span> <a href="#APP-PSQL-OPTION-USERNAME" class="id_link">#</a></dt><dd><p>
199 Connect to the database as the user <em class="replaceable"><code>username</code></em> instead of the default.
200 (You must have permission to do so, of course.)
201 </p></dd><dt id="APP-PSQL-OPTION-VARIABLE"><span class="term"><code class="option">-v <em class="replaceable"><code>assignment</code></em></code><br /></span><span class="term"><code class="option">--set=<em class="replaceable"><code>assignment</code></em></code><br /></span><span class="term"><code class="option">--variable=<em class="replaceable"><code>assignment</code></em></code></span> <a href="#APP-PSQL-OPTION-VARIABLE" class="id_link">#</a></dt><dd><p>
202 Perform a variable assignment, like the <code class="command">\set</code>
203 meta-command. Note that you must separate name and value, if
204 any, by an equal sign on the command line. To unset a variable,
205 leave off the equal sign. To set a variable with an empty value,
206 use the equal sign but leave off the value. These assignments are
207 done during command line processing, so variables that reflect
208 connection state will get overwritten later.
209 </p></dd><dt id="APP-PSQL-OPTION-VERSION"><span class="term"><code class="option">-V</code><br /></span><span class="term"><code class="option">--version</code></span> <a href="#APP-PSQL-OPTION-VERSION" class="id_link">#</a></dt><dd><p>
210 Print the <span class="application">psql</span> version and exit.
211 </p></dd><dt id="APP-PSQL-OPTION-NO-PASSWORD"><span class="term"><code class="option">-w</code><br /></span><span class="term"><code class="option">--no-password</code></span> <a href="#APP-PSQL-OPTION-NO-PASSWORD" class="id_link">#</a></dt><dd><p>
212 Never issue a password prompt. If the server requires password
213 authentication and a password is not available from other sources
214 such as a <code class="filename">.pgpass</code> file, the connection
215 attempt will fail. This option can be useful in batch jobs and
216 scripts where no user is present to enter a password.
218 Note that this option will remain set for the entire session,
219 and so it affects uses of the meta-command
220 <code class="command">\connect</code> as well as the initial connection attempt.
221 </p></dd><dt id="APP-PSQL-OPTION-PASSWORD"><span class="term"><code class="option">-W</code><br /></span><span class="term"><code class="option">--password</code></span> <a href="#APP-PSQL-OPTION-PASSWORD" class="id_link">#</a></dt><dd><p>
222 Force <span class="application">psql</span> to prompt for a
223 password before connecting to a database, even if the password will
226 If the server requires password authentication and a password is not
227 available from other sources such as a <code class="filename">.pgpass</code>
228 file, <span class="application">psql</span> will prompt for a
229 password in any case. However, <span class="application">psql</span>
230 will waste a connection attempt finding out that the server wants a
231 password. In some cases it is worth typing <code class="option">-W</code> to avoid
232 the extra connection attempt.
234 Note that this option will remain set for the entire session,
235 and so it affects uses of the meta-command
236 <code class="command">\connect</code> as well as the initial connection attempt.
237 </p></dd><dt id="APP-PSQL-OPTION-EXPANDED"><span class="term"><code class="option">-x</code><br /></span><span class="term"><code class="option">--expanded</code></span> <a href="#APP-PSQL-OPTION-EXPANDED" class="id_link">#</a></dt><dd><p>
238 Turn on the expanded table formatting mode. This is equivalent to
239 <code class="command">\x</code> or <code class="command">\pset expanded</code>.
240 </p></dd><dt id="APP-PSQL-OPTION-NO-PSQLRC"><span class="term"><code class="option">-X</code><br /></span><span class="term"><code class="option">--no-psqlrc</code></span> <a href="#APP-PSQL-OPTION-NO-PSQLRC" class="id_link">#</a></dt><dd><p>
241 Do not read the start-up file (neither the system-wide
242 <code class="filename">psqlrc</code> file nor the user's
243 <code class="filename">~/.psqlrc</code> file).
244 </p></dd><dt id="APP-PSQL-OPTION-FIELD-SEPARATOR-ZERO"><span class="term"><code class="option">-z</code><br /></span><span class="term"><code class="option">--field-separator-zero</code></span> <a href="#APP-PSQL-OPTION-FIELD-SEPARATOR-ZERO" class="id_link">#</a></dt><dd><p>
245 Set the field separator for unaligned output to a zero byte. This is
246 equivalent to <code class="command">\pset fieldsep_zero</code>.
247 </p></dd><dt id="APP-PSQL-OPTION-RECORD-SEPARATOR-ZERO"><span class="term"><code class="option">-0</code><br /></span><span class="term"><code class="option">--record-separator-zero</code></span> <a href="#APP-PSQL-OPTION-RECORD-SEPARATOR-ZERO" class="id_link">#</a></dt><dd><p>
248 Set the record separator for unaligned output to a zero byte. This is
249 useful for interfacing, for example, with <code class="literal">xargs -0</code>.
250 This is equivalent to <code class="command">\pset recordsep_zero</code>.
251 </p></dd><dt id="APP-PSQL-OPTION-SINGLE-TRANSACTION"><span class="term"><code class="option">-1</code><br /></span><span class="term"><code class="option">--single-transaction</code></span> <a href="#APP-PSQL-OPTION-SINGLE-TRANSACTION" class="id_link">#</a></dt><dd><p>
252 This option can only be used in combination with one or more
253 <code class="option">-c</code> and/or <code class="option">-f</code> options. It causes
254 <span class="application">psql</span> to issue a <code class="command">BEGIN</code> command
255 before the first such option and a <code class="command">COMMIT</code> command after
256 the last one, thereby wrapping all the commands into a single
257 transaction. If any of the commands fails and the variable
258 <code class="varname">ON_ERROR_STOP</code> was set, a
259 <code class="command">ROLLBACK</code> command is sent instead. This ensures that
260 either all the commands complete successfully, or no changes are
263 If the commands themselves
264 contain <code class="command">BEGIN</code>, <code class="command">COMMIT</code>,
265 or <code class="command">ROLLBACK</code>, this option will not have the desired
266 effects. Also, if an individual command cannot be executed inside a
267 transaction block, specifying this option will cause the whole
269 </p></dd><dt id="APP-PSQL-OPTION-HELP"><span class="term"><code class="option">-?</code><br /></span><span class="term"><code class="option">--help[=<em class="replaceable"><code>topic</code></em>]</code></span> <a href="#APP-PSQL-OPTION-HELP" class="id_link">#</a></dt><dd><p>
270 Show help about <span class="application">psql</span> and exit. The optional
271 <em class="replaceable"><code>topic</code></em> parameter (defaulting
272 to <code class="literal">options</code>) selects which part of <span class="application">psql</span> is
273 explained: <code class="literal">commands</code> describes <span class="application">psql</span>'s
274 backslash commands; <code class="literal">options</code> describes the command-line
275 options that can be passed to <span class="application">psql</span>;
276 and <code class="literal">variables</code> shows help about <span class="application">psql</span> configuration
278 </p></dd></dl></div></div><div class="refsect1" id="id-1.9.4.21.7"><h2>Exit Status</h2><p>
279 <span class="application">psql</span> returns 0 to the shell if it
280 finished normally, 1 if a fatal error of its own occurs (e.g., out of memory,
281 file not found), 2 if the connection to the server went bad
282 and the session was not interactive, and 3 if an error occurred in a
283 script and the variable <code class="varname">ON_ERROR_STOP</code> was set.
284 </p></div><div class="refsect1" id="id-1.9.4.21.8"><h2>Usage</h2><div class="refsect2" id="R2-APP-PSQL-CONNECTING"><h3>Connecting to a Database</h3><p>
285 <span class="application">psql</span> is a regular
286 <span class="productname">PostgreSQL</span> client application. In order
287 to connect to a database you need to know the name of your target
288 database, the host name and port number of the server, and what
289 database user name you want to connect as. <span class="application">psql</span>
290 can be told about those parameters via command line options, namely
291 <code class="option">-d</code>, <code class="option">-h</code>, <code class="option">-p</code>, and
292 <code class="option">-U</code> respectively. If an argument is found that does
293 not belong to any option it will be interpreted as the database name
294 (or the database user name, if the database name is already given). Not all
295 of these options are required; there are useful defaults. If you omit the host
296 name, <span class="application">psql</span> will connect via a Unix-domain socket
297 to a server on the local host, or via TCP/IP to <code class="literal">localhost</code> on
298 Windows. The default port number is
299 determined at compile time.
300 Since the database server uses the same default, you will not have
301 to specify the port in most cases. The default database user name is your
302 operating-system user name. Once the database user name is determined, it
303 is used as the default database name.
305 just connect to any database under any database user name. Your database
306 administrator should have informed you about your access rights.
308 When the defaults aren't quite right, you can save yourself
309 some typing by setting the environment variables
310 <code class="envar">PGDATABASE</code>, <code class="envar">PGHOST</code>,
311 <code class="envar">PGPORT</code> and/or <code class="envar">PGUSER</code> to appropriate
312 values. (For additional environment variables, see <a class="xref" href="libpq-envars.html" title="32.15. Environment Variables">Section 32.15</a>.) It is also convenient to have a
313 <code class="filename">~/.pgpass</code> file to avoid regularly having to type in
314 passwords. See <a class="xref" href="libpq-pgpass.html" title="32.16. The Password File">Section 32.16</a> for more information.
316 An alternative way to specify connection parameters is in a
317 <em class="parameter"><code>conninfo</code></em> string or
318 a <acronym class="acronym">URI</acronym>, which is used instead of a database
319 name. This mechanism give you very wide control over the
320 connection. For example:
321 </p><pre class="programlisting">
322 $ <strong class="userinput"><code>psql "service=myservice sslmode=require"</code></strong>
323 $ <strong class="userinput"><code>psql postgresql://dbmaster:5433/mydb?sslmode=require</code></strong>
325 This way you can also use <acronym class="acronym">LDAP</acronym> for connection
326 parameter lookup as described in <a class="xref" href="libpq-ldap.html" title="32.18. LDAP Lookup of Connection Parameters">Section 32.18</a>.
327 See <a class="xref" href="libpq-connect.html#LIBPQ-PARAMKEYWORDS" title="32.1.2. Parameter Key Words">Section 32.1.2</a> for more information on all the
328 available connection options.
330 If the connection could not be made for any reason (e.g., insufficient
331 privileges, server is not running on the targeted host, etc.),
332 <span class="application">psql</span> will return an error and terminate.
334 If both standard input and standard output are a
335 terminal, then <span class="application">psql</span> sets the client
336 encoding to <span class="quote">“<span class="quote">auto</span>”</span>, which will detect the
337 appropriate client encoding from the locale settings
338 (<code class="envar">LC_CTYPE</code> environment variable on Unix systems).
339 If this doesn't work out as expected, the client encoding can be
340 overridden using the environment
341 variable <code class="envar">PGCLIENTENCODING</code>.
342 </p></div><div class="refsect2" id="R2-APP-PSQL-4"><h3>Entering SQL Commands</h3><p>
343 In normal operation, <span class="application">psql</span> provides a
344 prompt with the name of the database to which
345 <span class="application">psql</span> is currently connected, followed by
346 the string <code class="literal">=></code>. For example:
347 </p><pre class="programlisting">
348 $ <strong class="userinput"><code>psql testdb</code></strong>
350 Type "help" for help.
355 At the prompt, the user can type in <acronym class="acronym">SQL</acronym> commands.
356 Ordinarily, input lines are sent to the server when a
357 command-terminating semicolon is reached. An end of line does not
358 terminate a command. Thus commands can be spread over several lines for
359 clarity. If the command was sent and executed without error, the results
360 of the command are displayed on the screen.
362 If untrusted users have access to a database that has not adopted a
363 <a class="link" href="ddl-schemas.html#DDL-SCHEMAS-PATTERNS" title="5.10.6. Usage Patterns">secure schema usage pattern</a>,
364 begin your session by removing publicly-writable schemas
365 from <code class="varname">search_path</code>. One can
366 add <code class="literal">options=-csearch_path=</code> to the connection string or
367 issue <code class="literal">SELECT pg_catalog.set_config('search_path', '',
368 false)</code> before other SQL commands. This consideration is not
369 specific to <span class="application">psql</span>; it applies to every interface
370 for executing arbitrary SQL commands.
372 Whenever a command is executed, <span class="application">psql</span> also polls
373 for asynchronous notification events generated by
374 <a class="link" href="sql-listen.html" title="LISTEN"><code class="command">LISTEN</code></a> and
375 <a class="link" href="sql-notify.html" title="NOTIFY"><code class="command">NOTIFY</code></a>.
377 While C-style block comments are passed to the server for
378 processing and removal, SQL-standard comments are removed by
379 <span class="application">psql</span>.
380 </p></div><div class="refsect2" id="APP-PSQL-META-COMMANDS"><h3>Meta-Commands</h3><p>
381 Anything you enter in <span class="application">psql</span> that begins
382 with an unquoted backslash is a <span class="application">psql</span>
383 meta-command that is processed by <span class="application">psql</span>
384 itself. These commands make
385 <span class="application">psql</span> more useful for administration or
386 scripting. Meta-commands are often called slash or backslash commands.
388 The format of a <span class="application">psql</span> command is the backslash,
389 followed immediately by a command verb, then any arguments. The arguments
390 are separated from the command verb and each other by any number of
391 whitespace characters.
393 To include whitespace in an argument you can quote it with
394 single quotes. To include a single quote in an argument,
395 write two single quotes within single-quoted text.
396 Anything contained in single quotes is
397 furthermore subject to C-like substitutions for
398 <code class="literal">\n</code> (new line), <code class="literal">\t</code> (tab),
399 <code class="literal">\b</code> (backspace), <code class="literal">\r</code> (carriage return),
400 <code class="literal">\f</code> (form feed),
401 <code class="literal">\</code><em class="replaceable"><code>digits</code></em> (octal), and
402 <code class="literal">\x</code><em class="replaceable"><code>digits</code></em> (hexadecimal).
403 A backslash preceding any other character within single-quoted text
404 quotes that single character, whatever it is.
406 If an unquoted colon (<code class="literal">:</code>) followed by a
407 <span class="application">psql</span> variable name appears within an argument, it is
408 replaced by the variable's value, as described in <a class="xref" href="app-psql.html#APP-PSQL-INTERPOLATION" title="SQL Interpolation">SQL Interpolation</a> below.
409 The forms <code class="literal">:'<em class="replaceable"><code>variable_name</code></em>'</code> and
410 <code class="literal">:"<em class="replaceable"><code>variable_name</code></em>"</code> described there
412 The <code class="literal">:{?<em class="replaceable"><code>variable_name</code></em>}</code> syntax allows
413 testing whether a variable is defined. It is substituted by
415 Escaping the colon with a backslash protects it from substitution.
417 Within an argument, text that is enclosed in backquotes
418 (<code class="literal">`</code>) is taken as a command line that is passed to the
419 shell. The output of the command (with any trailing newline removed)
420 replaces the backquoted text. Within the text enclosed in backquotes,
421 no special quoting or other processing occurs, except that appearances
422 of <code class="literal">:<em class="replaceable"><code>variable_name</code></em></code> where
423 <em class="replaceable"><code>variable_name</code></em> is a <span class="application">psql</span> variable name
424 are replaced by the variable's value. Also, appearances of
425 <code class="literal">:'<em class="replaceable"><code>variable_name</code></em>'</code> are replaced by the
426 variable's value suitably quoted to become a single shell command
427 argument. (The latter form is almost always preferable, unless you are
428 very sure of what is in the variable.) Because carriage return and line
429 feed characters cannot be safely quoted on all platforms, the
430 <code class="literal">:'<em class="replaceable"><code>variable_name</code></em>'</code> form prints an
431 error message and does not substitute the variable value when such
432 characters appear in the value.
434 Some commands take an <acronym class="acronym">SQL</acronym> identifier (such as a
435 table name) as argument. These arguments follow the syntax rules
436 of <acronym class="acronym">SQL</acronym>: Unquoted letters are forced to
437 lowercase, while double quotes (<code class="literal">"</code>) protect letters
438 from case conversion and allow incorporation of whitespace into
439 the identifier. Within double quotes, paired double quotes reduce
440 to a single double quote in the resulting name. For example,
441 <code class="literal">FOO"BAR"BAZ</code> is interpreted as <code class="literal">fooBARbaz</code>,
442 and <code class="literal">"A weird"" name"</code> becomes <code class="literal">A weird"
445 Parsing for arguments stops at the end of the line, or when another
446 unquoted backslash is found. An unquoted backslash
447 is taken as the beginning of a new meta-command. The special
448 sequence <code class="literal">\\</code> (two backslashes) marks the end of
449 arguments and continues parsing <acronym class="acronym">SQL</acronym> commands, if
450 any. That way <acronym class="acronym">SQL</acronym> and
451 <span class="application">psql</span> commands can be freely mixed on a
452 line. But in any case, the arguments of a meta-command cannot
453 continue beyond the end of the line.
455 Many of the meta-commands act on the <em class="firstterm">current query buffer</em>.
456 This is simply a buffer holding whatever SQL command text has been typed
457 but not yet sent to the server for execution. This will include previous
458 input lines as well as any text appearing before the meta-command on the
461 Many of the meta-commands also allow <code class="literal">x</code> to be appended
462 as an option. This will cause the results to be displayed in expanded
463 mode, as if <code class="command">\x</code> or <code class="command">\pset expanded</code>
466 The following meta-commands are defined:
468 </p><div class="variablelist"><dl class="variablelist"><dt id="APP-PSQL-META-COMMAND-A"><span class="term"><code class="literal">\a</code></span> <a href="#APP-PSQL-META-COMMAND-A" class="id_link">#</a></dt><dd><p>
469 If the current table output format is unaligned, it is switched to aligned.
470 If it is not unaligned, it is set to unaligned. This command is
471 kept for backwards compatibility. See <code class="command">\pset</code> for a
472 more general solution.
473 </p></dd><dt id="APP-PSQL-META-COMMAND-BIND"><span class="term"><code class="literal">\bind</code> [ <em class="replaceable"><code>parameter</code></em> ] ... </span> <a href="#APP-PSQL-META-COMMAND-BIND" class="id_link">#</a></dt><dd><p>
474 Sets query parameters for the next query execution, with the
475 specified parameters passed for any parameter placeholders
476 (<code class="literal">$1</code> etc.).
479 </p><pre class="programlisting">
480 INSERT INTO tbl1 VALUES ($1, $2) \bind 'first value' 'second value' \g
483 This also works for query-execution commands besides
484 <code class="literal">\g</code>, such as <code class="literal">\gx</code> and
485 <code class="literal">\gset</code>.
487 This command causes the extended query protocol (see <a class="xref" href="protocol-overview.html#PROTOCOL-QUERY-CONCEPTS" title="54.1.2. Extended Query Overview">Section 54.1.2</a>) to be used, unlike normal
488 <span class="application">psql</span> operation, which uses the simple
489 query protocol. So this command can be useful to test the extended
490 query protocol from <span class="application">psql</span>. (The extended
491 query protocol is used even if the query has no parameters and this
492 command specifies zero parameters.) This command affects only the
493 next query executed; all subsequent queries will use the simple query
495 </p></dd><dt id="APP-PSQL-META-COMMAND-BIND-NAMED"><span class="term"><code class="literal">\bind_named</code> <em class="replaceable"><code>statement_name</code></em> [ <em class="replaceable"><code>parameter</code></em> ] ... </span> <a href="#APP-PSQL-META-COMMAND-BIND-NAMED" class="id_link">#</a></dt><dd><p>
496 <code class="literal">\bind_named</code> is equivalent to <code class="literal">\bind</code>,
497 except that it takes the name of an existing prepared statement as
498 first parameter. An empty string denotes the unnamed prepared
502 </p><pre class="programlisting">
503 INSERT INTO tbls1 VALUES ($1, $2) \parse stmt1
504 \bind_named stmt1 'first value' 'second value' \g
507 This command causes the extended query protocol (see
508 <a class="xref" href="protocol-overview.html#PROTOCOL-QUERY-CONCEPTS" title="54.1.2. Extended Query Overview">Section 54.1.2</a>) to be used, unlike normal
509 <span class="application">psql</span> operation, which uses the simple
510 query protocol. So this command can be useful to test the extended
511 query protocol from <span class="application">psql</span>.
512 </p></dd><dt id="APP-PSQL-META-COMMAND-C-LC"><span class="term"><code class="literal">\c</code> or <code class="literal">\connect [ -reuse-previous=<em class="replaceable"><code>on|off</code></em> ] [ <em class="replaceable"><code>dbname</code></em> [ <em class="replaceable"><code>username</code></em> ] [ <em class="replaceable"><code>host</code></em> ] [ <em class="replaceable"><code>port</code></em> ] | <em class="replaceable"><code>conninfo</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-C-LC" class="id_link">#</a></dt><dd><p>
513 Establishes a new connection to a <span class="productname">PostgreSQL</span>
514 server. The connection parameters to use can be specified either
515 using a positional syntax (one or more of database name, user,
516 host, and port), or using a <em class="replaceable"><code>conninfo</code></em>
517 connection string as detailed in
518 <a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="32.1.1. Connection Strings">Section 32.1.1</a>. If no arguments are given, a
519 new connection is made using the same parameters as before.
522 of <em class="replaceable"><code>dbname</code></em>,
523 <em class="replaceable"><code>username</code></em>,
524 <em class="replaceable"><code>host</code></em> or
525 <em class="replaceable"><code>port</code></em>
526 as <code class="literal">-</code> is equivalent to omitting that parameter.
528 The new connection can re-use connection parameters from the previous
529 connection; not only database name, user, host, and port, but other
530 settings such as <em class="replaceable"><code>sslmode</code></em>. By default,
531 parameters are re-used in the positional syntax, but not when
532 a <em class="replaceable"><code>conninfo</code></em> string is given. Passing a
533 first argument of <code class="literal">-reuse-previous=on</code>
534 or <code class="literal">-reuse-previous=off</code> overrides that default. If
535 parameters are re-used, then any parameter not explicitly specified as
536 a positional parameter or in the <em class="replaceable"><code>conninfo</code></em>
537 string is taken from the existing connection's parameters. An
538 exception is that if the <em class="replaceable"><code>host</code></em> setting
539 is changed from its previous value using the positional syntax,
540 any <em class="replaceable"><code>hostaddr</code></em> setting present in the
541 existing connection's parameters is dropped.
542 Also, any password used for the existing connection will be re-used
543 only if the user, host, and port settings are not changed.
544 When the command neither specifies nor reuses a particular parameter,
545 the <span class="application">libpq</span> default is used.
547 If the new connection is successfully made, the previous
548 connection is closed.
549 If the connection attempt fails (wrong user name, access
550 denied, etc.), the previous connection will be kept if
551 <span class="application">psql</span> is in interactive mode. But when
552 executing a non-interactive script, the old connection is closed
553 and an error is reported. That may or may not terminate the
554 script; if it does not, all database-accessing commands will fail
555 until another <code class="literal">\connect</code> command is successfully
556 executed. This distinction was chosen as
557 a user convenience against typos on the one hand, and a safety
558 mechanism that scripts are not accidentally acting on the
559 wrong database on the other hand.
560 Note that whenever a <code class="literal">\connect</code> command attempts
561 to re-use parameters, the values re-used are those of the last
562 successful connection, not of any failed attempts made subsequently.
563 However, in the case of a
564 non-interactive <code class="literal">\connect</code> failure, no parameters
565 are allowed to be re-used later, since the script would likely be
566 expecting the values from the failed <code class="literal">\connect</code>
570 </p><pre class="programlisting">
571 => \c mydb myuser host.dom 6432
573 => \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
574 => \c -reuse-previous=on sslmode=require -- changes only sslmode
575 => \c postgresql://tom@localhost/mydb?application_name=myapp
576 </pre></dd><dt id="APP-PSQL-META-COMMAND-C-UC"><span class="term"><code class="literal">\C [ <em class="replaceable"><code>title</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-C-UC" class="id_link">#</a></dt><dd><p>
577 Sets the title of any tables being printed as the result of a
578 query or unset any such title. This command is equivalent to
579 <code class="literal">\pset title <em class="replaceable"><code>title</code></em></code>. (The name of
580 this command derives from <span class="quote">“<span class="quote">caption</span>”</span>, as it was
581 previously only used to set the caption in an
582 <acronym class="acronym">HTML</acronym> table.)
583 </p></dd><dt id="APP-PSQL-META-COMMAND-CD"><span class="term"><code class="literal">\cd [ <em class="replaceable"><code>directory</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-CD" class="id_link">#</a></dt><dd><p>
584 Changes the current working directory to
585 <em class="replaceable"><code>directory</code></em>. Without argument, changes
586 to the current user's home directory. For details on how home
587 directories are found, see <a class="xref" href="libpq-pgpass.html" title="32.16. The Password File">Section 32.16</a>.
588 </p><div class="tip"><h3 class="title">Tip</h3><p>
589 To print your current working directory, use <code class="literal">\! pwd</code>.
590 </p></div></dd><dt id="APP-PSQL-META-COMMAND-CLOSE-PREPARED"><span class="term"><code class="literal">\close_prepared</code> <em class="replaceable"><code>prepared_statement_name</code></em></span> <a href="#APP-PSQL-META-COMMAND-CLOSE-PREPARED" class="id_link">#</a></dt><dd><p>
591 Closes the specified prepared statement. An empty string denotes the
592 unnamed prepared statement. If no prepared statement exists with this
593 name, the operation is a no-op.
596 </p><pre class="programlisting">
597 SELECT $1 \parse stmt1
598 \close_prepared stmt1
601 This command causes the extended query protocol to be used,
602 unlike normal <span class="application">psql</span> operation, which
603 uses the simple query protocol. So this command can be useful
604 to test the extended query protocol from
605 <span class="application">psql</span>.
606 </p></dd><dt id="APP-PSQL-META-COMMAND-CONNINFO"><span class="term"><code class="literal">\conninfo</code></span> <a href="#APP-PSQL-META-COMMAND-CONNINFO" class="id_link">#</a></dt><dd><p>
607 Outputs information about the current database connection,
608 including SSL-related information if SSL is in use.
610 Note that the <code class="structfield">Client User</code> field shows
611 the user at the time of connection, while the
612 <code class="structfield">Superuser</code> field indicates whether
613 the current user (in the current execution context) has
614 superuser privileges. These users are usually the same, but they can
615 differ, for example, if the current user was changed with the
616 <code class="command">SET ROLE</code> command.
617 </p></dd><dt id="APP-PSQL-META-COMMANDS-COPY"><span class="term"><code class="literal">\copy { <em class="replaceable"><code>table</code></em> [ ( <em class="replaceable"><code>column_list</code></em> ) ] }
618 <code class="literal">from</code>
619 { <em class="replaceable"><code>'filename'</code></em> | program <em class="replaceable"><code>'command'</code></em> | stdin | pstdin }
620 [ [ with ] ( <em class="replaceable"><code>option</code></em> [, ...] ) ]
621 [ where <em class="replaceable"><code>condition</code></em> ]</code><br /></span><span class="term"><code class="literal">\copy { <em class="replaceable"><code>table</code></em> [ ( <em class="replaceable"><code>column_list</code></em> ) ] | ( <em class="replaceable"><code>query</code></em> ) }
622 <code class="literal">to</code>
623 { <em class="replaceable"><code>'filename'</code></em> | program <em class="replaceable"><code>'command'</code></em> | stdout | pstdout }
624 [ [ with ] ( <em class="replaceable"><code>option</code></em> [, ...] ) ]</code></span> <a href="#APP-PSQL-META-COMMANDS-COPY" class="id_link">#</a></dt><dd><p>
625 Performs a frontend (client) copy. This is an operation that
626 runs an <acronym class="acronym">SQL</acronym> <a class="link" href="sql-copy.html" title="COPY"><code class="command">COPY</code></a>
627 command, but instead of the server
628 reading or writing the specified file,
629 <span class="application">psql</span> reads or writes the file and
630 routes the data between the server and the local file system.
631 This means that file accessibility and privileges are those of
632 the local user, not the server, and no SQL superuser
633 privileges are required.
635 When <code class="literal">program</code> is specified,
636 <em class="replaceable"><code>command</code></em> is
637 executed by <span class="application">psql</span> and the data passed from
638 or to <em class="replaceable"><code>command</code></em> is
639 routed between the server and the client.
640 Again, the execution privileges are those of
641 the local user, not the server, and no SQL superuser
642 privileges are required.
644 For <code class="literal">\copy ... from stdin</code>, data rows are read from the same
645 source that issued the command, continuing until a line containing
646 only <code class="literal">\.</code>
647 is read or the stream reaches <acronym class="acronym">EOF</acronym>. This option is useful
648 for populating tables in-line within an SQL script file.
649 For <code class="literal">\copy ... to stdout</code>, output is sent to the same place
650 as <span class="application">psql</span> command output, and
651 the <code class="literal">COPY <em class="replaceable"><code>count</code></em></code> command status is
652 not printed (since it might be confused with a data row).
653 To read/write <span class="application">psql</span>'s standard input or
654 output regardless of the current command source or <code class="literal">\o</code>
655 option, write <code class="literal">from pstdin</code> or <code class="literal">to pstdout</code>.
657 The syntax of this command is similar to that of the
658 <acronym class="acronym">SQL</acronym> <a class="link" href="sql-copy.html" title="COPY"><code class="command">COPY</code></a>
659 command. All options other than the data source/destination are
660 as specified for <code class="command">COPY</code>.
661 Because of this, special parsing rules apply to the <code class="command">\copy</code>
662 meta-command. Unlike most other meta-commands, the entire remainder
663 of the line is always taken to be the arguments of <code class="command">\copy</code>,
664 and neither variable interpolation nor backquote expansion are
665 performed in the arguments.
666 </p><div class="tip"><h3 class="title">Tip</h3><p>
667 Another way to obtain the same result as <code class="literal">\copy
668 ... to</code> is to use the <acronym class="acronym">SQL</acronym> <code class="literal">COPY
669 ... TO STDOUT</code> command and terminate it
670 with <code class="literal">\g <em class="replaceable"><code>filename</code></em></code>
671 or <code class="literal">\g |<em class="replaceable"><code>program</code></em></code>.
672 Unlike <code class="literal">\copy</code>, this method allows the command to
673 span multiple lines; also, variable interpolation and backquote
674 expansion can be used.
675 </p></div><div class="tip"><h3 class="title">Tip</h3><p>
676 These operations are not as efficient as the <acronym class="acronym">SQL</acronym>
677 <code class="command">COPY</code> command with a file or program data source or
678 destination, because all data must pass through the client/server
679 connection. For large amounts of data the <acronym class="acronym">SQL</acronym>
680 command might be preferable.
681 </p></div></dd><dt id="APP-PSQL-META-COMMAND-COPYRIGHT"><span class="term"><code class="literal">\copyright</code></span> <a href="#APP-PSQL-META-COMMAND-COPYRIGHT" class="id_link">#</a></dt><dd><p>
682 Shows the copyright and distribution terms of
683 <span class="productname">PostgreSQL</span>.
684 </p></dd><dt id="APP-PSQL-META-COMMANDS-CROSSTABVIEW"><span class="term"><code class="literal">\crosstabview [
685 <em class="replaceable"><code>colV</code></em>
686 [ <em class="replaceable"><code>colH</code></em>
687 [ <em class="replaceable"><code>colD</code></em>
688 [ <em class="replaceable"><code>sortcolH</code></em>
689 ] ] ] ] </code></span> <a href="#APP-PSQL-META-COMMANDS-CROSSTABVIEW" class="id_link">#</a></dt><dd><p>
690 Executes the current query buffer (like <code class="literal">\g</code>) and
691 shows the results in a crosstab grid.
692 The query must return at least three columns.
693 The output column identified by <em class="replaceable"><code>colV</code></em>
694 becomes a vertical header and the output column identified by
695 <em class="replaceable"><code>colH</code></em>
696 becomes a horizontal header.
697 <em class="replaceable"><code>colD</code></em> identifies
698 the output column to display within the grid.
699 <em class="replaceable"><code>sortcolH</code></em> identifies
700 an optional sort column for the horizontal header.
702 Each column specification can be a column number (starting at 1) or
703 a column name. The usual SQL case folding and quoting rules apply to
704 column names. If omitted,
705 <em class="replaceable"><code>colV</code></em> is taken as column 1
706 and <em class="replaceable"><code>colH</code></em> as column 2.
707 <em class="replaceable"><code>colH</code></em> must differ from
708 <em class="replaceable"><code>colV</code></em>.
709 If <em class="replaceable"><code>colD</code></em> is not
710 specified, then there must be exactly three columns in the query
711 result, and the column that is neither
712 <em class="replaceable"><code>colV</code></em> nor
713 <em class="replaceable"><code>colH</code></em>
714 is taken to be <em class="replaceable"><code>colD</code></em>.
716 The vertical header, displayed as the leftmost column, contains the
717 values found in column <em class="replaceable"><code>colV</code></em>, in the
718 same order as in the query results, but with duplicates removed.
720 The horizontal header, displayed as the first row, contains the values
721 found in column <em class="replaceable"><code>colH</code></em>,
722 with duplicates removed. By default, these appear in the same order
723 as in the query results. But if the
724 optional <em class="replaceable"><code>sortcolH</code></em> argument is given,
725 it identifies a column whose values must be integer numbers, and the
726 values from <em class="replaceable"><code>colH</code></em> will
727 appear in the horizontal header sorted according to the
728 corresponding <em class="replaceable"><code>sortcolH</code></em> values.
730 Inside the crosstab grid, for each distinct value <code class="literal">x</code>
731 of <em class="replaceable"><code>colH</code></em> and each distinct
732 value <code class="literal">y</code>
733 of <em class="replaceable"><code>colV</code></em>, the cell located
734 at the intersection <code class="literal">(x,y)</code> contains the value of
735 the <code class="literal">colD</code> column in the query result row for which
736 the value of <em class="replaceable"><code>colH</code></em>
737 is <code class="literal">x</code> and the value
738 of <em class="replaceable"><code>colV</code></em>
739 is <code class="literal">y</code>. If there is no such row, the cell is empty. If
740 there are multiple such rows, an error is reported.
741 </p></dd><dt id="APP-PSQL-META-COMMAND-D"><span class="term"><code class="literal">\d[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-D" class="id_link">#</a></dt><dd><p>
742 For each relation (table, view, materialized view, index, sequence,
744 or composite type matching the
745 <em class="replaceable"><code>pattern</code></em>, show all
746 columns, their types, the tablespace (if not the default) and any
747 special attributes such as <code class="literal">NOT NULL</code> or defaults.
748 Associated indexes, constraints, rules, and triggers are
749 also shown. For foreign tables, the associated foreign
750 server is shown as well.
751 (<span class="quote">“<span class="quote">Matching the pattern</span>”</span> is defined in
752 <a class="xref" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns">Patterns</a> below.)
754 For some types of relation, <code class="literal">\d</code> shows additional information
755 for each column: column values for sequences, indexed expressions for
756 indexes, and foreign data wrapper options for foreign tables.
758 The command form <code class="literal">\d+</code> is identical, except that
759 more information is displayed: any comments associated with the
760 columns of the table are shown, as is the presence of OIDs in the
761 table, the view definition if the relation is a view, a non-default
762 <a class="link" href="sql-altertable.html#SQL-ALTERTABLE-REPLICA-IDENTITY">replica
763 identity</a> setting and the
764 <a class="link" href="sql-create-access-method.html" title="CREATE ACCESS METHOD">access method</a> name
765 if the relation has an access method.
767 By default, only user-created objects are shown; supply a
768 pattern or the <code class="literal">S</code> modifier to include system
770 </p><div class="note"><h3 class="title">Note</h3><p>
771 If <code class="command">\d</code> is used without a
772 <em class="replaceable"><code>pattern</code></em> argument, it is
773 equivalent to <code class="command">\dtvmsE</code> which will show a list of
774 all visible tables, views, materialized views, sequences and
776 This is purely a convenience measure.
778 As with many other commands, if <code class="literal">x</code> is appended to
779 the command name, the results are displayed in expanded mode, but note
780 that this only applies when <code class="command">\d</code> is used without a
781 <em class="replaceable"><code>pattern</code></em> argument, and
782 the <code class="literal">x</code> modifier cannot appear immediately after the
783 <code class="command">\d</code> (because <code class="command">\dx</code> is a different
784 command); the <code class="literal">x</code> modifier may only appear after an
785 <code class="literal">S</code> or <code class="literal">+</code> modifier. For example,
786 <code class="command">\d+x</code> is equivalent to <code class="command">\dtvmsE+x</code>
787 and will show a list of all relations in expanded mode.
788 </p></div></dd><dt id="APP-PSQL-META-COMMAND-DA-LC"><span class="term"><code class="literal">\da[Sx] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DA-LC" class="id_link">#</a></dt><dd><p>
789 Lists aggregate functions, together with their
790 return type and the data types they operate on. If <em class="replaceable"><code>pattern</code></em>
791 is specified, only aggregates whose names match the pattern are shown.
792 By default, only user-created objects are shown; supply a
793 pattern or the <code class="literal">S</code> modifier to include system
795 If <code class="literal">x</code> is appended to the command name, the results
796 are displayed in expanded mode.
797 </p></dd><dt id="APP-PSQL-META-COMMAND-DA-UC"><span class="term"><code class="literal">\dA[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DA-UC" class="id_link">#</a></dt><dd><p>
798 Lists access methods. If <em class="replaceable"><code>pattern</code></em> is specified, only access
799 methods whose names match the pattern are shown.
800 If <code class="literal">x</code> is appended to the command name, the results
801 are displayed in expanded mode.
802 If <code class="literal">+</code> is appended to the command name, each access
803 method is listed with its associated handler function and description.
804 </p></dd><dt id="APP-PSQL-META-COMMAND-DAC"><span class="term">
805 <code class="literal">\dAc[x+]
806 [<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>access-method-pattern</code></em></a>
807 [<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>input-type-pattern</code></em></a>]]
809 </span> <a href="#APP-PSQL-META-COMMAND-DAC" class="id_link">#</a></dt><dd><p>
810 Lists operator classes
811 (see <a class="xref" href="xindex.html#XINDEX-OPCLASS" title="36.16.1. Index Methods and Operator Classes">Section 36.16.1</a>).
812 If <em class="replaceable"><code>access-method-pattern</code></em>
813 is specified, only operator classes associated with access methods whose
814 names match that pattern are listed.
815 If <em class="replaceable"><code>input-type-pattern</code></em>
816 is specified, only operator classes associated with input types whose
817 names match that pattern are listed.
818 If <code class="literal">x</code> is appended to the command name, the results
819 are displayed in expanded mode.
820 If <code class="literal">+</code> is appended to the command name, each operator
821 class is listed with its associated operator family and owner.
822 </p></dd><dt id="APP-PSQL-META-COMMAND-DAF"><span class="term">
823 <code class="literal">\dAf[x+]
824 [<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>access-method-pattern</code></em></a>
825 [<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>input-type-pattern</code></em></a>]]
827 </span> <a href="#APP-PSQL-META-COMMAND-DAF" class="id_link">#</a></dt><dd><p>
828 Lists operator families
829 (see <a class="xref" href="xindex.html#XINDEX-OPFAMILY" title="36.16.5. Operator Classes and Operator Families">Section 36.16.5</a>).
830 If <em class="replaceable"><code>access-method-pattern</code></em>
831 is specified, only operator families associated with access methods whose
832 names match that pattern are listed.
833 If <em class="replaceable"><code>input-type-pattern</code></em>
834 is specified, only operator families associated with input types whose
835 names match that pattern are listed.
836 If <code class="literal">x</code> is appended to the command name, the results
837 are displayed in expanded mode.
838 If <code class="literal">+</code> is appended to the command name, each operator
839 family is listed with its owner.
840 </p></dd><dt id="APP-PSQL-META-COMMAND-DAO"><span class="term">
841 <code class="literal">\dAo[x+]
842 [<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>access-method-pattern</code></em></a>
843 [<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>operator-family-pattern</code></em></a>]]
845 </span> <a href="#APP-PSQL-META-COMMAND-DAO" class="id_link">#</a></dt><dd><p>
846 Lists operators associated with operator families
847 (see <a class="xref" href="xindex.html#XINDEX-STRATEGIES" title="36.16.2. Index Method Strategies">Section 36.16.2</a>).
848 If <em class="replaceable"><code>access-method-pattern</code></em>
849 is specified, only members of operator families associated with access
850 methods whose names match that pattern are listed.
851 If <em class="replaceable"><code>operator-family-pattern</code></em>
852 is specified, only members of operator families whose names match that
854 If <code class="literal">x</code> is appended to the command name, the results
855 are displayed in expanded mode.
856 If <code class="literal">+</code> is appended to the command name, each operator
857 is listed with its sort operator family (if it is an ordering operator),
858 and whether its underlying function is leakproof.
859 </p></dd><dt id="APP-PSQL-META-COMMAND-DAP"><span class="term">
860 <code class="literal">\dAp[x+]
861 [<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>access-method-pattern</code></em></a>
862 [<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>operator-family-pattern</code></em></a>]]
864 </span> <a href="#APP-PSQL-META-COMMAND-DAP" class="id_link">#</a></dt><dd><p>
865 Lists support functions associated with operator families
866 (see <a class="xref" href="xindex.html#XINDEX-SUPPORT" title="36.16.3. Index Method Support Routines">Section 36.16.3</a>).
867 If <em class="replaceable"><code>access-method-pattern</code></em>
868 is specified, only functions of operator families associated with
869 access methods whose names match that pattern are listed.
870 If <em class="replaceable"><code>operator-family-pattern</code></em>
871 is specified, only functions of operator families whose names match
872 that pattern are listed.
873 If <code class="literal">x</code> is appended to the command name, the results
874 are displayed in expanded mode.
875 If <code class="literal">+</code> is appended to the command name, functions are
876 displayed verbosely, with their actual parameter lists.
877 </p></dd><dt id="APP-PSQL-META-COMMAND-DB"><span class="term"><code class="literal">\db[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DB" class="id_link">#</a></dt><dd><p>
878 Lists tablespaces. If <em class="replaceable"><code>pattern</code></em>
879 is specified, only tablespaces whose names match the pattern are shown.
880 If <code class="literal">x</code> is appended to the command name, the results
881 are displayed in expanded mode.
882 If <code class="literal">+</code> is appended to the command name, each tablespace
883 is listed with its associated options, on-disk size, permissions and
885 </p></dd><dt id="APP-PSQL-META-COMMAND-DC-LC"><span class="term"><code class="literal">\dc[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DC-LC" class="id_link">#</a></dt><dd><p>
886 Lists conversions between character-set encodings.
887 If <em class="replaceable"><code>pattern</code></em>
888 is specified, only conversions whose names match the pattern are
890 By default, only user-created objects are shown; supply a
891 pattern or the <code class="literal">S</code> modifier to include system
893 If <code class="literal">x</code> is appended to the command name, the results
894 are displayed in expanded mode.
895 If <code class="literal">+</code> is appended to the command name, each object
896 is listed with its associated description.
897 </p></dd><dt id="APP-PSQL-META-COMMAND-DCONFIG"><span class="term"><code class="literal">\dconfig[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DCONFIG" class="id_link">#</a></dt><dd><p>
898 Lists server configuration parameters and their values.
899 If <em class="replaceable"><code>pattern</code></em> is specified,
900 only parameters whose names match the pattern are listed. Without
901 a <em class="replaceable"><code>pattern</code></em>, only
902 parameters that are set to non-default values are listed.
903 (Use <code class="literal">\dconfig *</code> to see all parameters.)
904 If <code class="literal">x</code> is appended to the command name, the results
905 are displayed in expanded mode.
906 If <code class="literal">+</code> is appended to the command name, each
907 parameter is listed with its data type, context in which the
908 parameter can be set, and access privileges (if non-default access
909 privileges have been granted).
910 </p></dd><dt id="APP-PSQL-META-COMMAND-DC-UC"><span class="term"><code class="literal">\dC[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DC-UC" class="id_link">#</a></dt><dd><p>
912 If <em class="replaceable"><code>pattern</code></em>
913 is specified, only casts whose source or target types match the
915 If <code class="literal">x</code> is appended to the command name, the results
916 are displayed in expanded mode.
917 If <code class="literal">+</code> is appended to the command name, additional
918 information about each cast is shown, including whether its underlying
919 function is leakproof, and the cast's description.
920 </p></dd><dt id="APP-PSQL-META-COMMAND-DD-LC"><span class="term"><code class="literal">\dd[Sx] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DD-LC" class="id_link">#</a></dt><dd><p>
921 Shows the descriptions of objects of type <code class="literal">constraint</code>,
922 <code class="literal">operator class</code>, <code class="literal">operator family</code>,
923 <code class="literal">rule</code>, and <code class="literal">trigger</code>. All
924 other comments may be viewed by the respective backslash commands for
926 </p><p><code class="literal">\dd</code> displays descriptions for objects matching the
927 <em class="replaceable"><code>pattern</code></em>, or of visible
928 objects of the appropriate type if no argument is given. But in either
929 case, only objects that have a description are listed.
930 By default, only user-created objects are shown; supply a
931 pattern or the <code class="literal">S</code> modifier to include system
933 If <code class="literal">x</code> is appended to the command name, the results
934 are displayed in expanded mode.
936 Descriptions for objects can be created with the <a class="link" href="sql-comment.html" title="COMMENT"><code class="command">COMMENT</code></a>
937 <acronym class="acronym">SQL</acronym> command.
938 </p></dd><dt id="APP-PSQL-META-COMMAND-DD-UC"><span class="term"><code class="literal">\dD[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DD-UC" class="id_link">#</a></dt><dd><p>
939 Lists domains. If <em class="replaceable"><code>pattern</code></em>
940 is specified, only domains whose names match the pattern are shown.
941 By default, only user-created objects are shown; supply a
942 pattern or the <code class="literal">S</code> modifier to include system
944 If <code class="literal">x</code> is appended to the command name, the results
945 are displayed in expanded mode.
946 If <code class="literal">+</code> is appended to the command name, each object
947 is listed with its associated permissions and description.
948 </p></dd><dt id="APP-PSQL-META-COMMAND-DDP"><span class="term"><code class="literal">\ddp[x] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DDP" class="id_link">#</a></dt><dd><p>
949 Lists default access privilege settings. An entry is shown for
950 each role (and schema, if applicable) for which the default
951 privilege settings have been changed from the built-in defaults.
952 If <em class="replaceable"><code>pattern</code></em> is
953 specified, only entries whose role name or schema name matches
954 the pattern are listed.
955 If <code class="literal">x</code> is appended to the command name, the results
956 are displayed in expanded mode.
958 The <a class="link" href="sql-alterdefaultprivileges.html" title="ALTER DEFAULT PRIVILEGES"><code class="command">ALTER DEFAULT
959 PRIVILEGES</code></a> command is used to set default access
960 privileges. The meaning of the privilege display is explained in
961 <a class="xref" href="ddl-priv.html" title="5.8. Privileges">Section 5.8</a>.
962 </p></dd><dt id="APP-PSQL-META-COMMAND-DE"><span class="term"><code class="literal">\dE[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\di[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\dm[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\ds[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\dt[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\dv[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DE" class="id_link">#</a></dt><dd><p>
963 In this group of commands, the letters <code class="literal">E</code>,
964 <code class="literal">i</code>, <code class="literal">m</code>, <code class="literal">s</code>,
965 <code class="literal">t</code>, and <code class="literal">v</code>
966 stand for foreign table, index, materialized view,
967 sequence, table, and view,
969 You can specify any or all of
970 these letters, in any order, to obtain a listing of objects
971 of these types. For example, <code class="literal">\dti</code> lists
973 If <code class="literal">x</code> is appended to the command name, the results
974 are displayed in expanded mode.
975 If <code class="literal">+</code> is
976 appended to the command name, each object is listed with its
977 persistence status (permanent, temporary, or unlogged),
978 physical size on disk, and associated description if any.
979 If <em class="replaceable"><code>pattern</code></em> is
980 specified, only objects whose names match the pattern are listed.
981 By default, only user-created objects are shown; supply a
982 pattern or the <code class="literal">S</code> modifier to include system
984 </p></dd><dt id="APP-PSQL-META-COMMAND-DES"><span class="term"><code class="literal">\des[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DES" class="id_link">#</a></dt><dd><p>
985 Lists foreign servers (mnemonic: <span class="quote">“<span class="quote">external
986 servers</span>”</span>).
987 If <em class="replaceable"><code>pattern</code></em> is
988 specified, only those servers whose name matches the pattern
990 If <code class="literal">x</code> is appended to the command name, the results
991 are displayed in expanded mode.
992 If <code class="literal">+</code> is appended to the command name, a
993 full description of each server is shown, including the
994 server's access privileges, type, version, options, and description.
995 </p></dd><dt id="APP-PSQL-META-COMMAND-DET"><span class="term"><code class="literal">\det[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DET" class="id_link">#</a></dt><dd><p>
996 Lists foreign tables (mnemonic: <span class="quote">“<span class="quote">external tables</span>”</span>).
997 If <em class="replaceable"><code>pattern</code></em> is
998 specified, only entries whose table name or schema name matches
999 the pattern are listed.
1000 If <code class="literal">x</code> is appended to the command name, the results
1001 are displayed in expanded mode.
1002 If <code class="literal">+</code> is appended to the command name,
1003 generic options and the foreign table description
1005 </p></dd><dt id="APP-PSQL-META-COMMAND-DEU"><span class="term"><code class="literal">\deu[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DEU" class="id_link">#</a></dt><dd><p>
1006 Lists user mappings (mnemonic: <span class="quote">“<span class="quote">external
1007 users</span>”</span>).
1008 If <em class="replaceable"><code>pattern</code></em> is
1009 specified, only those mappings whose user names match the
1011 If <code class="literal">x</code> is appended to the command name, the results
1012 are displayed in expanded mode.
1013 If <code class="literal">+</code> is appended to the command name,
1014 additional information about each mapping is shown.
1015 </p><div class="caution"><h3 class="title">Caution</h3><p>
1016 <code class="literal">\deu+</code> might also display the user name and
1017 password of the remote user, so care should be taken not to
1019 </p></div></dd><dt id="APP-PSQL-META-COMMAND-DEW"><span class="term"><code class="literal">\dew[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DEW" class="id_link">#</a></dt><dd><p>
1020 Lists foreign-data wrappers (mnemonic: <span class="quote">“<span class="quote">external
1021 wrappers</span>”</span>).
1022 If <em class="replaceable"><code>pattern</code></em> is
1023 specified, only those foreign-data wrappers whose name matches
1024 the pattern are listed.
1025 If <code class="literal">x</code> is appended to the command name, the results
1026 are displayed in expanded mode.
1027 If <code class="literal">+</code> is appended to the command name,
1028 the access privileges, options, and description of the
1029 foreign-data wrapper are also shown.
1030 </p></dd><dt id="APP-PSQL-META-COMMAND-DF-LC"><span class="term"><code class="literal">\df[anptwSx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> [ <em class="replaceable"><code>arg_pattern</code></em> ... ] ]</code></span> <a href="#APP-PSQL-META-COMMAND-DF-LC" class="id_link">#</a></dt><dd><p>
1031 Lists functions, together with their result data types, argument data
1032 types, and function types, which are classified as <span class="quote">“<span class="quote">agg</span>”</span>
1033 (aggregate), <span class="quote">“<span class="quote">normal</span>”</span>, <span class="quote">“<span class="quote">procedure</span>”</span>, <span class="quote">“<span class="quote">trigger</span>”</span>, or <span class="quote">“<span class="quote">window</span>”</span>.
1034 To display only functions
1035 of specific type(s), add the corresponding letters <code class="literal">a</code>,
1036 <code class="literal">n</code>, <code class="literal">p</code>, <code class="literal">t</code>, or <code class="literal">w</code> to the command.
1037 If <em class="replaceable"><code>pattern</code></em> is specified, only
1038 functions whose names match the pattern are shown.
1039 Any additional arguments are type-name patterns, which are matched
1040 to the type names of the first, second, and so on arguments of the
1041 function. (Matching functions can have more arguments than what
1042 you specify. To prevent that, write a dash <code class="literal">-</code> as
1043 the last <em class="replaceable"><code>arg_pattern</code></em>.)
1044 By default, only user-created
1045 objects are shown; supply a pattern or the <code class="literal">S</code>
1046 modifier to include system objects.
1047 If <code class="literal">x</code> is appended to the command name, the results
1048 are displayed in expanded mode.
1049 If <code class="literal">+</code> is appended to the command name, additional information
1050 about each function is shown, including volatility,
1051 parallel safety, owner, security classification, whether it is
1052 leakproof, access privileges, language, internal name (for C and
1053 internal functions only), and description.
1054 Source code for a specific function can be seen
1055 using <code class="literal">\sf</code>.
1056 </p></dd><dt id="APP-PSQL-META-COMMAND-DF-UC"><span class="term"><code class="literal">\dF[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DF-UC" class="id_link">#</a></dt><dd><p>
1057 Lists text search configurations.
1058 If <em class="replaceable"><code>pattern</code></em> is specified,
1059 only configurations whose names match the pattern are shown.
1060 If <code class="literal">x</code> is appended to the command name, the results
1061 are displayed in expanded mode.
1062 If <code class="literal">+</code> is appended to the command name, a full description of
1063 each configuration is shown, including the underlying text search
1064 parser and the dictionary list for each parser token type.
1065 </p></dd><dt id="APP-PSQL-META-COMMAND-DFD"><span class="term"><code class="literal">\dFd[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DFD" class="id_link">#</a></dt><dd><p>
1066 Lists text search dictionaries.
1067 If <em class="replaceable"><code>pattern</code></em> is specified,
1068 only dictionaries whose names match the pattern are shown.
1069 If <code class="literal">x</code> is appended to the command name, the results
1070 are displayed in expanded mode.
1071 If <code class="literal">+</code> is appended to the command name, additional information
1072 is shown about each selected dictionary, including the underlying
1073 text search template and the option values.
1074 </p></dd><dt id="APP-PSQL-META-COMMAND-DFP"><span class="term"><code class="literal">\dFp[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DFP" class="id_link">#</a></dt><dd><p>
1075 Lists text search parsers.
1076 If <em class="replaceable"><code>pattern</code></em> is specified,
1077 only parsers whose names match the pattern are shown.
1078 If <code class="literal">x</code> is appended to the command name, the results
1079 are displayed in expanded mode.
1080 If <code class="literal">+</code> is appended to the command name, a full description of
1081 each parser is shown, including the underlying functions and the
1082 list of recognized token types.
1083 </p></dd><dt id="APP-PSQL-META-COMMAND-DFT"><span class="term"><code class="literal">\dFt[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DFT" class="id_link">#</a></dt><dd><p>
1084 Lists text search templates.
1085 If <em class="replaceable"><code>pattern</code></em> is specified,
1086 only templates whose names match the pattern are shown.
1087 If <code class="literal">x</code> is appended to the command name, the results
1088 are displayed in expanded mode.
1089 If <code class="literal">+</code> is appended to the command name, additional information
1090 is shown about each template, including the underlying function names.
1091 </p></dd><dt id="APP-PSQL-META-COMMAND-DG"><span class="term"><code class="literal">\dg[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DG" class="id_link">#</a></dt><dd><p>
1092 Lists database roles.
1093 (Since the concepts of <span class="quote">“<span class="quote">users</span>”</span> and <span class="quote">“<span class="quote">groups</span>”</span> have been
1094 unified into <span class="quote">“<span class="quote">roles</span>”</span>, this command is now equivalent to
1095 <code class="literal">\du</code>.)
1096 By default, only user-created roles are shown; supply the
1097 <code class="literal">S</code> modifier to include system roles.
1098 If <em class="replaceable"><code>pattern</code></em> is specified,
1099 only those roles whose names match the pattern are listed.
1100 If <code class="literal">x</code> is appended to the command name, the results
1101 are displayed in expanded mode.
1102 If <code class="literal">+</code> is appended to the command name, additional information
1103 is shown about each role; currently this adds the comment for each
1105 </p></dd><dt id="APP-PSQL-META-COMMAND-DL-LC"><span class="term"><code class="literal">\dl[x+]</code></span> <a href="#APP-PSQL-META-COMMAND-DL-LC" class="id_link">#</a></dt><dd><p>
1106 This is an alias for <code class="command">\lo_list</code>, which shows a
1107 list of large objects.
1108 If <code class="literal">x</code> is appended to the command name, the results
1109 are displayed in expanded mode.
1110 If <code class="literal">+</code> is appended to the command name,
1111 each large object is listed with its associated permissions,
1113 </p></dd><dt id="APP-PSQL-META-COMMAND-DL-UC"><span class="term"><code class="literal">\dL[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DL-UC" class="id_link">#</a></dt><dd><p>
1114 Lists procedural languages. If <em class="replaceable"><code>pattern</code></em>
1115 is specified, only languages whose names match the pattern are listed.
1116 By default, only user-created languages
1117 are shown; supply the <code class="literal">S</code> modifier to include system
1119 If <code class="literal">x</code> is appended to the command name, the results
1120 are displayed in expanded mode.
1121 If <code class="literal">+</code> is appended to the command name, each
1122 language is listed with its call handler, validator, access privileges,
1123 and whether it is a system object.
1124 </p></dd><dt id="APP-PSQL-META-COMMAND-DN"><span class="term"><code class="literal">\dn[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DN" class="id_link">#</a></dt><dd><p>
1125 Lists schemas (namespaces). If <em class="replaceable"><code>pattern</code></em>
1126 is specified, only schemas whose names match the pattern are listed.
1127 By default, only user-created objects are shown; supply a
1128 pattern or the <code class="literal">S</code> modifier to include system objects.
1129 If <code class="literal">x</code> is appended to the command name, the results
1130 are displayed in expanded mode.
1131 If <code class="literal">+</code> is appended to the command name, each object
1132 is listed with its associated permissions and description, if any.
1133 </p></dd><dt id="APP-PSQL-META-COMMAND-DO-LC"><span class="term"><code class="literal">\do[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> [ <em class="replaceable"><code>arg_pattern</code></em> [ <em class="replaceable"><code>arg_pattern</code></em> ] ] ]</code></span> <a href="#APP-PSQL-META-COMMAND-DO-LC" class="id_link">#</a></dt><dd><p>
1134 Lists operators with their operand and result types.
1135 If <em class="replaceable"><code>pattern</code></em> is
1136 specified, only operators whose names match the pattern are listed.
1137 If one <em class="replaceable"><code>arg_pattern</code></em> is
1138 specified, only prefix operators whose right argument's type name
1139 matches that pattern are listed.
1140 If two <em class="replaceable"><code>arg_pattern</code></em>s
1141 are specified, only binary operators whose argument type names match
1142 those patterns are listed. (Alternatively, write <code class="literal">-</code>
1143 for the unused argument of a unary operator.)
1144 By default, only user-created objects are shown; supply a
1145 pattern or the <code class="literal">S</code> modifier to include system
1147 If <code class="literal">x</code> is appended to the command name, the results
1148 are displayed in expanded mode.
1149 If <code class="literal">+</code> is appended to the command name,
1150 additional information about each operator is shown, including
1151 the name of the underlying function, and whether it is leakproof.
1152 </p></dd><dt id="APP-PSQL-META-COMMAND-DO-UC"><span class="term"><code class="literal">\dO[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DO-UC" class="id_link">#</a></dt><dd><p>
1154 If <em class="replaceable"><code>pattern</code></em> is
1155 specified, only collations whose names match the pattern are
1156 listed. By default, only user-created objects are shown;
1157 supply a pattern or the <code class="literal">S</code> modifier to
1158 include system objects.
1159 If <code class="literal">x</code> is appended to the command name, the results
1160 are displayed in expanded mode.
1161 If <code class="literal">+</code> is appended
1162 to the command name, each collation is listed with its associated
1163 description, if any.
1164 Note that only collations usable with the current database's encoding
1165 are shown, so the results may vary in different databases of the
1167 </p></dd><dt id="APP-PSQL-META-COMMAND-DP-LC"><span class="term"><code class="literal">\dp[Sx] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DP-LC" class="id_link">#</a></dt><dd><p>
1168 Lists tables, views and sequences with their
1169 associated access privileges.
1170 If <em class="replaceable"><code>pattern</code></em> is
1171 specified, only tables, views and sequences whose names match the
1172 pattern are listed. By default only user-created objects are shown;
1173 supply a pattern or the <code class="literal">S</code> modifier to include
1175 If <code class="literal">x</code> is appended to the command name, the results
1176 are displayed in expanded mode.
1178 The <a class="link" href="sql-grant.html" title="GRANT"><code class="command">GRANT</code></a> and
1179 <a class="link" href="sql-revoke.html" title="REVOKE"><code class="command">REVOKE</code></a>
1180 commands are used to set access privileges. The meaning of the
1181 privilege display is explained in
1182 <a class="xref" href="ddl-priv.html" title="5.8. Privileges">Section 5.8</a>.
1183 </p></dd><dt id="APP-PSQL-META-COMMAND-DP-UC"><span class="term"><code class="literal">\dP[itnx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DP-UC" class="id_link">#</a></dt><dd><p>
1184 Lists partitioned relations.
1185 If <em class="replaceable"><code>pattern</code></em>
1186 is specified, only entries whose name matches the pattern are listed.
1187 The modifiers <code class="literal">t</code> (tables) and <code class="literal">i</code>
1188 (indexes) can be appended to the command, filtering the kind of
1189 relations to list. By default, partitioned tables and indexes are
1192 If the modifier <code class="literal">n</code> (<span class="quote">“<span class="quote">nested</span>”</span>) is used,
1193 or a pattern is specified, then non-root partitioned relations are
1194 included, and a column is shown displaying the parent of each
1195 partitioned relation.
1197 If <code class="literal">x</code> is appended to the command name, the results
1198 are displayed in expanded mode.
1199 If <code class="literal">+</code> is appended to the command name, the sum of the
1200 sizes of each relation's partitions is also displayed, along with the
1201 relation's description.
1202 If <code class="literal">n</code> is combined with <code class="literal">+</code>, two
1203 sizes are shown: one including the total size of directly-attached
1204 leaf partitions, and another showing the total size of all partitions,
1205 including indirectly attached sub-partitions.
1206 </p></dd><dt id="APP-PSQL-META-COMMAND-DRDS"><span class="term"><code class="literal">\drds[x] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>role-pattern</code></em></a> [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>database-pattern</code></em></a> ] ]</code></span> <a href="#APP-PSQL-META-COMMAND-DRDS" class="id_link">#</a></dt><dd><p>
1207 Lists defined configuration settings. These settings can be
1208 role-specific, database-specific, or both.
1209 <em class="replaceable"><code>role-pattern</code></em> and
1210 <em class="replaceable"><code>database-pattern</code></em> are used to select
1211 specific roles and databases to list, respectively. If omitted, or if
1212 <code class="literal">*</code> is specified, all settings are listed, including those
1213 not role-specific or database-specific, respectively.
1214 If <code class="literal">x</code> is appended to the command name, the results
1215 are displayed in expanded mode.
1217 The <a class="link" href="sql-alterrole.html" title="ALTER ROLE"><code class="command">ALTER ROLE</code></a> and
1218 <a class="link" href="sql-alterdatabase.html" title="ALTER DATABASE"><code class="command">ALTER DATABASE</code></a>
1219 commands are used to define per-role and per-database configuration
1221 </p></dd><dt id="APP-PSQL-META-COMMAND-DRG"><span class="term"><code class="literal">\drg[Sx] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DRG" class="id_link">#</a></dt><dd><p>
1222 Lists information about each granted role membership, including
1223 assigned options (<code class="literal">ADMIN</code>,
1224 <code class="literal">INHERIT</code> and/or <code class="literal">SET</code>) and grantor.
1225 See the <a class="link" href="sql-grant.html" title="GRANT"><code class="command">GRANT</code></a>
1226 command for information about role memberships.
1228 By default, only grants to user-created roles are shown; supply the
1229 <code class="literal">S</code> modifier to include system roles.
1230 If <em class="replaceable"><code>pattern</code></em> is specified,
1231 only grants to those roles whose names match the pattern are listed.
1232 If <code class="literal">x</code> is appended to the command name, the results
1233 are displayed in expanded mode.
1234 </p></dd><dt id="APP-PSQL-META-COMMAND-DRP"><span class="term"><code class="literal">\dRp[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DRP" class="id_link">#</a></dt><dd><p>
1235 Lists replication publications.
1236 If <em class="replaceable"><code>pattern</code></em> is
1237 specified, only those publications whose names match the pattern are
1239 If <code class="literal">x</code> is appended to the command name, the results
1240 are displayed in expanded mode.
1241 If <code class="literal">+</code> is appended to the command name, the tables and
1242 schemas associated with each publication are shown as well.
1243 </p></dd><dt id="APP-PSQL-META-COMMAND-DRS"><span class="term"><code class="literal">\dRs[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DRS" class="id_link">#</a></dt><dd><p>
1244 Lists replication subscriptions.
1245 If <em class="replaceable"><code>pattern</code></em> is
1246 specified, only those subscriptions whose names match the pattern are
1248 If <code class="literal">x</code> is appended to the command name, the results
1249 are displayed in expanded mode.
1250 If <code class="literal">+</code> is appended to the command name, additional
1251 properties of the subscriptions are shown.
1252 </p></dd><dt id="APP-PSQL-META-COMMAND-DT"><span class="term"><code class="literal">\dT[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DT" class="id_link">#</a></dt><dd><p>
1254 If <em class="replaceable"><code>pattern</code></em> is
1255 specified, only types whose names match the pattern are listed.
1256 If <code class="literal">x</code> is appended to the command name, the results
1257 are displayed in expanded mode.
1258 If <code class="literal">+</code> is appended to the command name, each type is
1259 listed with its internal name and size, its allowed values
1260 if it is an <code class="type">enum</code> type, and its associated permissions.
1261 By default, only user-created objects are shown; supply a
1262 pattern or the <code class="literal">S</code> modifier to include system
1264 </p></dd><dt id="APP-PSQL-META-COMMAND-DU"><span class="term"><code class="literal">\du[Sx+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DU" class="id_link">#</a></dt><dd><p>
1265 Lists database roles.
1266 (Since the concepts of <span class="quote">“<span class="quote">users</span>”</span> and <span class="quote">“<span class="quote">groups</span>”</span> have been
1267 unified into <span class="quote">“<span class="quote">roles</span>”</span>, this command is now equivalent to
1268 <code class="literal">\dg</code>.)
1269 By default, only user-created roles are shown; supply the
1270 <code class="literal">S</code> modifier to include system roles.
1271 If <em class="replaceable"><code>pattern</code></em> is specified,
1272 only those roles whose names match the pattern are listed.
1273 If <code class="literal">x</code> is appended to the command name, the results
1274 are displayed in expanded mode.
1275 If <code class="literal">+</code> is appended to the command name, additional information
1276 is shown about each role; currently this adds the comment for each
1278 </p></dd><dt id="APP-PSQL-META-COMMAND-DX-LC"><span class="term"><code class="literal">\dx[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DX-LC" class="id_link">#</a></dt><dd><p>
1279 Lists installed extensions.
1280 If <em class="replaceable"><code>pattern</code></em>
1281 is specified, only those extensions whose names match the pattern
1283 If <code class="literal">x</code> is appended to the command name, the results
1284 are displayed in expanded mode.
1285 If <code class="literal">+</code> is appended to the command name, all the objects belonging
1286 to each matching extension are listed.
1287 </p></dd><dt id="APP-PSQL-META-COMMAND-DX-UC"><span class="term"><code class="literal">\dX[x] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DX-UC" class="id_link">#</a></dt><dd><p>
1288 Lists extended statistics.
1289 If <em class="replaceable"><code>pattern</code></em>
1290 is specified, only those extended statistics whose names match the
1292 If <code class="literal">x</code> is appended to the command name, the results
1293 are displayed in expanded mode.
1295 The status of each kind of extended statistics is shown in a column
1296 named after its statistic kind (e.g. Ndistinct).
1297 <code class="literal">defined</code> means that it was requested when creating
1298 the statistics, and NULL means it wasn't requested.
1299 You can use <code class="structname">pg_stats_ext</code> if you'd like to
1300 know whether <a class="link" href="sql-analyze.html" title="ANALYZE"><code class="command">ANALYZE</code></a>
1301 was run and statistics are available to the planner.
1302 </p></dd><dt id="APP-PSQL-META-COMMAND-DY"><span class="term"><code class="literal">\dy[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-DY" class="id_link">#</a></dt><dd><p>
1303 Lists event triggers.
1304 If <em class="replaceable"><code>pattern</code></em>
1305 is specified, only those event triggers whose names match the pattern
1307 If <code class="literal">x</code> is appended to the command name, the results
1308 are displayed in expanded mode.
1309 If <code class="literal">+</code> is appended to the command name, each object
1310 is listed with its associated description.
1311 </p></dd><dt id="APP-PSQL-META-COMMAND-EDIT"><span class="term"><code class="literal">\e</code> or <code class="literal">\edit</code> <code class="literal"> [<span class="optional"> <em class="replaceable"><code>filename</code></em> </span>] [<span class="optional"> <em class="replaceable"><code>line_number</code></em> </span>] </code></span> <a href="#APP-PSQL-META-COMMAND-EDIT" class="id_link">#</a></dt><dd><p>
1312 If <em class="replaceable"><code>filename</code></em> is
1313 specified, the file is edited; after the editor exits, the file's
1314 content is copied into the current query buffer. If no <em class="replaceable"><code>filename</code></em> is given, the current query
1315 buffer is copied to a temporary file which is then edited in the same
1316 fashion. Or, if the current query buffer is empty, the most recently
1317 executed query is copied to a temporary file and edited in the same
1320 If you edit a file or the previous query, and you quit the editor without
1321 modifying the file, the query buffer is cleared.
1322 Otherwise, the new contents of the query buffer are re-parsed according to
1323 the normal rules of <span class="application">psql</span>, treating the
1324 whole buffer as a single line. Any complete queries are immediately
1325 executed; that is, if the query buffer contains or ends with a
1326 semicolon, everything up to that point is executed and removed from
1327 the query buffer. Whatever remains in the query buffer is
1328 redisplayed. Type semicolon or <code class="literal">\g</code> to send it,
1329 or <code class="literal">\r</code> to cancel it by clearing the query buffer.
1331 Treating the buffer as a single line primarily affects meta-commands:
1332 whatever is in the buffer after a meta-command will be taken as
1333 argument(s) to the meta-command, even if it spans multiple lines.
1334 (Thus you cannot make meta-command-using scripts this way.
1335 Use <code class="command">\i</code> for that.)
1337 If a line number is specified, <span class="application">psql</span> will
1338 position the cursor on the specified line of the file or query buffer.
1339 Note that if a single all-digits argument is given,
1340 <span class="application">psql</span> assumes it is a line number,
1342 </p><div class="tip"><h3 class="title">Tip</h3><p>
1343 See <a class="xref" href="app-psql.html#APP-PSQL-ENVIRONMENT" title="Environment">Environment</a>, below, for how to
1344 configure and customize your editor.
1345 </p></div></dd><dt id="APP-PSQL-META-COMMAND-ECHO"><span class="term"><code class="literal">\echo <em class="replaceable"><code>text</code></em> [ ... ]</code></span> <a href="#APP-PSQL-META-COMMAND-ECHO" class="id_link">#</a></dt><dd><p>
1346 Prints the evaluated arguments to standard output, separated by
1347 spaces and followed by a newline. This can be useful to
1348 intersperse information in the output of scripts. For example:
1349 </p><pre class="programlisting">
1350 => <strong class="userinput"><code>\echo `date`</code></strong>
1351 Tue Oct 26 21:40:57 CEST 1999
1353 If the first argument is an unquoted <code class="literal">-n</code> the trailing
1354 newline is not written (nor is the first argument).
1355 </p><div class="tip"><h3 class="title">Tip</h3><p>
1356 If you use the <code class="command">\o</code> command to redirect your
1357 query output you might wish to use <code class="command">\qecho</code>
1358 instead of this command. See also <code class="command">\warn</code>.
1359 </p></div></dd><dt id="APP-PSQL-META-COMMAND-EF"><span class="term"><code class="literal">\ef [<span class="optional"> <em class="replaceable"><code>function_description</code></em> [<span class="optional"> <em class="replaceable"><code>line_number</code></em> </span>] </span>] </code></span> <a href="#APP-PSQL-META-COMMAND-EF" class="id_link">#</a></dt><dd><p>
1360 This command fetches and edits the definition of the named function or procedure,
1361 in the form of a <code class="command">CREATE OR REPLACE FUNCTION</code> or
1362 <code class="command">CREATE OR REPLACE PROCEDURE</code> command.
1363 Editing is done in the same way as for <code class="literal">\edit</code>.
1364 If you quit the editor without saving, the statement is discarded.
1365 If you save and exit the editor, the updated command is executed immediately
1366 if you added a semicolon to it. Otherwise it is redisplayed;
1367 type semicolon or <code class="literal">\g</code> to send it, or <code class="literal">\r</code>
1370 The target function can be specified by name alone, or by name
1371 and arguments, for example <code class="literal">foo(integer, text)</code>.
1372 The argument types must be given if there is more
1373 than one function of the same name.
1375 If no function is specified, a blank <code class="command">CREATE FUNCTION</code>
1376 template is presented for editing.
1378 If a line number is specified, <span class="application">psql</span> will
1379 position the cursor on the specified line of the function body.
1380 (Note that the function body typically does not begin on the first
1383 Unlike most other meta-commands, the entire remainder of the line is
1384 always taken to be the argument(s) of <code class="command">\ef</code>, and neither
1385 variable interpolation nor backquote expansion are performed in the
1387 </p><div class="tip"><h3 class="title">Tip</h3><p>
1388 See <a class="xref" href="app-psql.html#APP-PSQL-ENVIRONMENT" title="Environment">Environment</a>, below, for how to
1389 configure and customize your editor.
1390 </p></div></dd><dt id="APP-PSQL-META-COMMAND-ENCODING"><span class="term"><code class="literal">\encoding [ <em class="replaceable"><code>encoding</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-ENCODING" class="id_link">#</a></dt><dd><p>
1391 Sets the client character set encoding. Without an argument, this command
1392 shows the current encoding.
1393 </p></dd><dt id="APP-PSQL-META-COMMAND-ERRVERBOSE"><span class="term"><code class="literal">\errverbose</code></span> <a href="#APP-PSQL-META-COMMAND-ERRVERBOSE" class="id_link">#</a></dt><dd><p>
1394 Repeats the most recent server error message at maximum
1395 verbosity, as though <code class="varname">VERBOSITY</code> were set
1396 to <code class="literal">verbose</code> and <code class="varname">SHOW_CONTEXT</code> were
1397 set to <code class="literal">always</code>.
1398 </p></dd><dt id="APP-PSQL-META-COMMAND-EV"><span class="term"><code class="literal">\ev [<span class="optional"> <em class="replaceable"><code>view_name</code></em> [<span class="optional"> <em class="replaceable"><code>line_number</code></em> </span>] </span>] </code></span> <a href="#APP-PSQL-META-COMMAND-EV" class="id_link">#</a></dt><dd><p>
1399 This command fetches and edits the definition of the named view,
1400 in the form of a <code class="command">CREATE OR REPLACE VIEW</code> command.
1401 Editing is done in the same way as for <code class="literal">\edit</code>.
1402 If you quit the editor without saving, the statement is discarded.
1403 If you save and exit the editor, the updated command is executed immediately
1404 if you added a semicolon to it. Otherwise it is redisplayed;
1405 type semicolon or <code class="literal">\g</code> to send it, or <code class="literal">\r</code>
1408 If no view is specified, a blank <code class="command">CREATE VIEW</code>
1409 template is presented for editing.
1411 If a line number is specified, <span class="application">psql</span> will
1412 position the cursor on the specified line of the view definition.
1414 Unlike most other meta-commands, the entire remainder of the line is
1415 always taken to be the argument(s) of <code class="command">\ev</code>, and neither
1416 variable interpolation nor backquote expansion are performed in the
1418 </p></dd><dt id="APP-PSQL-META-COMMAND-F"><span class="term"><code class="literal">\f [ <em class="replaceable"><code>string</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-F" class="id_link">#</a></dt><dd><p>
1419 Sets the field separator for unaligned query output. The default
1420 is the vertical bar (<code class="literal">|</code>). It is equivalent to
1421 <code class="command">\pset fieldsep</code>.
1422 </p></dd><dt id="APP-PSQL-META-COMMAND-G"><span class="term"><code class="literal">\g [ (<em class="replaceable"><code>option</code></em>=<em class="replaceable"><code>value</code></em> [...]) ] [ <em class="replaceable"><code>filename</code></em> ]</code><br /></span><span class="term"><code class="literal">\g [ (<em class="replaceable"><code>option</code></em>=<em class="replaceable"><code>value</code></em> [...]) ] [ |<em class="replaceable"><code>command</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-G" class="id_link">#</a></dt><dd><p>
1423 Sends the current query buffer to the server for execution.
1425 If parentheses appear after <code class="literal">\g</code>, they surround a
1426 space-separated list
1427 of <em class="replaceable"><code>option</code></em><code class="literal">=</code><em class="replaceable"><code>value</code></em>
1428 formatting-option clauses, which are interpreted in the same way
1429 as <code class="literal">\pset</code>
1430 <em class="replaceable"><code>option</code></em>
1431 <em class="replaceable"><code>value</code></em> commands, but take
1432 effect only for the duration of this query. In this list, spaces are
1433 not allowed around <code class="literal">=</code> signs, but are required
1434 between option clauses.
1435 If <code class="literal">=</code><em class="replaceable"><code>value</code></em>
1437 named <em class="replaceable"><code>option</code></em> is changed
1438 in the same way as for
1439 <code class="literal">\pset</code> <em class="replaceable"><code>option</code></em>
1440 with no explicit <em class="replaceable"><code>value</code></em>.
1442 If a <em class="replaceable"><code>filename</code></em>
1443 or <code class="literal">|</code><em class="replaceable"><code>command</code></em>
1444 argument is given, the query's output is written to the named
1445 file or piped to the given shell command, instead of displaying it as
1446 usual. The file or command is written to only if the query
1447 successfully returns zero or more tuples, not if the query fails or
1448 is a non-data-returning SQL command.
1450 If the current query buffer is empty, the most recently sent query is
1451 re-executed instead. Except for that behavior, <code class="literal">\g</code>
1452 without any arguments is essentially equivalent to a semicolon.
1453 With arguments, <code class="literal">\g</code> provides
1454 a <span class="quote">“<span class="quote">one-shot</span>”</span> alternative to the <code class="command">\o</code>
1455 command, and additionally allows one-shot adjustments of the
1456 output formatting options normally set by <code class="literal">\pset</code>.
1458 When the last argument begins with <code class="literal">|</code>, the entire
1459 remainder of the line is taken to be
1460 the <em class="replaceable"><code>command</code></em> to execute,
1461 and neither variable interpolation nor backquote expansion are
1462 performed in it. The rest of the line is simply passed literally to
1464 </p></dd><dt id="APP-PSQL-META-COMMAND-GDESC"><span class="term"><code class="literal">\gdesc</code></span> <a href="#APP-PSQL-META-COMMAND-GDESC" class="id_link">#</a></dt><dd><p>
1465 Shows the description (that is, the column names and data types)
1466 of the result of the current query buffer. The query is not
1467 actually executed; however, if it contains some type of syntax
1468 error, that error will be reported in the normal way.
1470 If the current query buffer is empty, the most recently sent query
1471 is described instead.
1472 </p></dd><dt id="APP-PSQL-META-COMMAND-GETENV"><span class="term"><code class="literal">\getenv <em class="replaceable"><code>psql_var</code></em> <em class="replaceable"><code>env_var</code></em></code></span> <a href="#APP-PSQL-META-COMMAND-GETENV" class="id_link">#</a></dt><dd><p>
1473 Gets the value of the environment
1474 variable <em class="replaceable"><code>env_var</code></em>
1475 and assigns it to the <span class="application">psql</span>
1476 variable <em class="replaceable"><code>psql_var</code></em>.
1477 If <em class="replaceable"><code>env_var</code></em> is
1478 not defined in the <span class="application">psql</span> process's
1479 environment, <em class="replaceable"><code>psql_var</code></em>
1480 is not changed. Example:
1481 </p><pre class="programlisting">
1482 => <strong class="userinput"><code>\getenv home HOME</code></strong>
1483 => <strong class="userinput"><code>\echo :home</code></strong>
1485 </pre></dd><dt id="APP-PSQL-META-COMMAND-GEXEC"><span class="term"><code class="literal">\gexec</code></span> <a href="#APP-PSQL-META-COMMAND-GEXEC" class="id_link">#</a></dt><dd><p>
1486 Sends the current query buffer to the server, then treats
1487 each column of each row of the query's output (if any) as an SQL
1488 statement to be executed. For example, to create an index on each
1489 column of <code class="structname">my_table</code>:
1490 </p><pre class="programlisting">
1491 => <strong class="userinput"><code>SELECT format('create index on my_table(%I)', attname)</code></strong>
1492 -> <strong class="userinput"><code>FROM pg_attribute</code></strong>
1493 -> <strong class="userinput"><code>WHERE attrelid = 'my_table'::regclass AND attnum > 0</code></strong>
1494 -> <strong class="userinput"><code>ORDER BY attnum</code></strong>
1495 -> <strong class="userinput"><code>\gexec</code></strong>
1502 The generated queries are executed in the order in which the rows
1503 are returned, and left-to-right within each row if there is more
1504 than one column. NULL fields are ignored. The generated queries
1505 are sent literally to the server for processing, so they cannot be
1506 <span class="application">psql</span> meta-commands nor contain <span class="application">psql</span>
1507 variable references. If any individual query fails, execution of
1508 the remaining queries continues
1509 unless <code class="varname">ON_ERROR_STOP</code> is set. Execution of each
1510 query is subject to <code class="varname">ECHO</code> processing.
1511 (Setting <code class="varname">ECHO</code> to <code class="literal">all</code>
1512 or <code class="literal">queries</code> is often advisable when
1513 using <code class="command">\gexec</code>.) Query logging, single-step mode,
1514 timing, and other query execution features apply to each generated
1517 If the current query buffer is empty, the most recently sent query
1518 is re-executed instead.
1519 </p></dd><dt id="APP-PSQL-META-COMMAND-GSET"><span class="term"><code class="literal">\gset [ <em class="replaceable"><code>prefix</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-GSET" class="id_link">#</a></dt><dd><p>
1520 Sends the current query buffer to the server and stores the
1521 query's output into <span class="application">psql</span> variables
1522 (see <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a> below).
1523 The query to be executed must return exactly one row. Each column of
1524 the row is stored into a separate variable, named the same as the
1525 column. For example:
1526 </p><pre class="programlisting">
1527 => <strong class="userinput"><code>SELECT 'hello' AS var1, 10 AS var2</code></strong>
1528 -> <strong class="userinput"><code>\gset</code></strong>
1529 => <strong class="userinput"><code>\echo :var1 :var2</code></strong>
1533 If you specify a <em class="replaceable"><code>prefix</code></em>,
1534 that string is prepended to the query's column names to create the
1535 variable names to use:
1536 </p><pre class="programlisting">
1537 => <strong class="userinput"><code>SELECT 'hello' AS var1, 10 AS var2</code></strong>
1538 -> <strong class="userinput"><code>\gset result_</code></strong>
1539 => <strong class="userinput"><code>\echo :result_var1 :result_var2</code></strong>
1543 If a column result is NULL, the corresponding variable is unset
1544 rather than being set.
1546 If the query fails or does not return one row,
1547 no variables are changed.
1549 If the current query buffer is empty, the most recently sent query
1550 is re-executed instead.
1551 </p></dd><dt id="APP-PSQL-META-COMMAND-GX"><span class="term"><code class="literal">\gx [ (<em class="replaceable"><code>option</code></em>=<em class="replaceable"><code>value</code></em> [...]) ] [ <em class="replaceable"><code>filename</code></em> ]</code><br /></span><span class="term"><code class="literal">\gx [ (<em class="replaceable"><code>option</code></em>=<em class="replaceable"><code>value</code></em> [...]) ] [ |<em class="replaceable"><code>command</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-GX" class="id_link">#</a></dt><dd><p>
1552 <code class="literal">\gx</code> is equivalent to <code class="literal">\g</code>, except
1553 that it forces expanded output mode for this query, as
1554 if <code class="literal">expanded=on</code> were included in the list of
1555 <code class="literal">\pset</code> options. See also <code class="literal">\x</code>.
1556 </p></dd><dt id="APP-PSQL-META-COMMAND-HELP"><span class="term"><code class="literal">\h</code> or <code class="literal">\help</code> <code class="literal">[ <em class="replaceable"><code>command</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-HELP" class="id_link">#</a></dt><dd><p>
1557 Gives syntax help on the specified <acronym class="acronym">SQL</acronym>
1558 command. If <em class="replaceable"><code>command</code></em>
1559 is not specified, then <span class="application">psql</span> will list
1560 all the commands for which syntax help is available. If
1561 <em class="replaceable"><code>command</code></em> is an
1562 asterisk (<code class="literal">*</code>), then syntax help on all
1563 <acronym class="acronym">SQL</acronym> commands is shown.
1565 Unlike most other meta-commands, the entire remainder of the line is
1566 always taken to be the argument(s) of <code class="command">\help</code>, and neither
1567 variable interpolation nor backquote expansion are performed in the
1569 </p><div class="note"><h3 class="title">Note</h3><p>
1570 To simplify typing, commands that consists of several words do
1571 not have to be quoted. Thus it is fine to type <strong class="userinput"><code>\help
1572 alter table</code></strong>.
1573 </p></div></dd><dt id="APP-PSQL-META-COMMAND-HTML"><span class="term"><code class="literal">\H</code> or <code class="literal">\html</code></span> <a href="#APP-PSQL-META-COMMAND-HTML" class="id_link">#</a></dt><dd><p>
1574 Turns on <acronym class="acronym">HTML</acronym> query output format. If the
1575 <acronym class="acronym">HTML</acronym> format is already on, it is switched
1576 back to the default aligned text format. This command is for
1577 compatibility and convenience, but see <code class="command">\pset</code>
1578 about setting other output options.
1579 </p></dd><dt id="APP-PSQL-META-COMMAND-INCLUDE"><span class="term"><code class="literal">\i</code> or <code class="literal">\include</code> <em class="replaceable"><code>filename</code></em></span> <a href="#APP-PSQL-META-COMMAND-INCLUDE" class="id_link">#</a></dt><dd><p>
1580 Reads input from the file <em class="replaceable"><code>filename</code></em> and executes it as
1581 though it had been typed on the keyboard.
1583 If <em class="replaceable"><code>filename</code></em> is <code class="literal">-</code>
1584 (hyphen), then standard input is read until an EOF indication
1585 or <code class="command">\q</code> meta-command. This can be used to intersperse
1586 interactive input with input from files. Note that Readline behavior
1587 will be used only if it is active at the outermost level.
1588 </p><div class="note"><h3 class="title">Note</h3><p>
1589 If you want to see the lines on the screen as they are read you
1590 must set the variable <code class="varname">ECHO</code> to
1591 <code class="literal">all</code>.
1592 </p></div></dd><dt id="PSQL-METACOMMAND-IF"><span class="term"><code class="literal">\if</code> <em class="replaceable"><code>expression</code></em><br /></span><span class="term"><code class="literal">\elif</code> <em class="replaceable"><code>expression</code></em><br /></span><span class="term"><code class="literal">\else</code><br /></span><span class="term"><code class="literal">\endif</code></span> <a href="#PSQL-METACOMMAND-IF" class="id_link">#</a></dt><dd><p>
1593 This group of commands implements nestable conditional blocks.
1594 A conditional block must begin with an <code class="command">\if</code> and end
1595 with an <code class="command">\endif</code>. In between there may be any number
1596 of <code class="command">\elif</code> clauses, which may optionally be followed
1597 by a single <code class="command">\else</code> clause. Ordinary queries and
1598 other types of backslash commands may (and usually do) appear between
1599 the commands forming a conditional block.
1601 The <code class="command">\if</code> and <code class="command">\elif</code> commands read
1602 their argument(s) and evaluate them as a Boolean expression. If the
1603 expression yields <code class="literal">true</code> then processing continues
1604 normally; otherwise, lines are skipped until a
1605 matching <code class="command">\elif</code>, <code class="command">\else</code>,
1606 or <code class="command">\endif</code> is reached. Once
1607 an <code class="command">\if</code> or <code class="command">\elif</code> test has
1608 succeeded, the arguments of later <code class="command">\elif</code> commands in
1609 the same block are not evaluated but are treated as false. Lines
1610 following an <code class="command">\else</code> are processed only if no earlier
1611 matching <code class="command">\if</code> or <code class="command">\elif</code> succeeded.
1613 The <em class="replaceable"><code>expression</code></em> argument
1614 of an <code class="command">\if</code> or <code class="command">\elif</code> command
1615 is subject to variable interpolation and backquote expansion, just
1616 like any other backslash command argument. After that it is evaluated
1617 like the value of an on/off option variable. So a valid value
1618 is any unambiguous case-insensitive match for one of:
1619 <code class="literal">true</code>, <code class="literal">false</code>, <code class="literal">1</code>,
1620 <code class="literal">0</code>, <code class="literal">on</code>, <code class="literal">off</code>,
1621 <code class="literal">yes</code>, <code class="literal">no</code>. For example,
1622 <code class="literal">t</code>, <code class="literal">T</code>, and <code class="literal">tR</code>
1623 will all be considered to be <code class="literal">true</code>.
1625 Expressions that do not properly evaluate to true or false will
1626 generate a warning and be treated as false.
1628 Lines being skipped are parsed normally to identify queries and
1629 backslash commands, but queries are not sent to the server, and
1630 backslash commands other than conditionals
1631 (<code class="command">\if</code>, <code class="command">\elif</code>,
1632 <code class="command">\else</code>, <code class="command">\endif</code>) are
1633 ignored. Conditional commands are checked only for valid nesting.
1634 Variable references in skipped lines are not expanded, and backquote
1635 expansion is not performed either.
1637 All the backslash commands of a given conditional block must appear in
1638 the same source file. If EOF is reached on the main input file or an
1639 <code class="command">\include</code>-ed file before all local
1640 <code class="command">\if</code>-blocks have been closed,
1641 then <span class="application">psql</span> will raise an error.
1644 </p><pre class="programlisting">
1645 -- check for the existence of two separate records in the database and store
1646 -- the results in separate psql variables
1648 EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer,
1649 EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee
1652 SELECT * FROM customer WHERE customer_id = 123;
1654 \echo 'is not a customer but is an employee'
1655 SELECT * FROM employee WHERE employee_id = 456;
1658 \echo 'not a customer or employee'
1660 \echo 'this will never print'
1663 </pre></dd><dt id="APP-PSQL-META-COMMAND-INCLUDE-RELATIVE"><span class="term"><code class="literal">\ir</code> or <code class="literal">\include_relative</code> <em class="replaceable"><code>filename</code></em></span> <a href="#APP-PSQL-META-COMMAND-INCLUDE-RELATIVE" class="id_link">#</a></dt><dd><p>
1664 The <code class="literal">\ir</code> command is similar to <code class="literal">\i</code>, but resolves
1665 relative file names differently. When executing in interactive mode,
1666 the two commands behave identically. However, when invoked from a
1667 script, <code class="literal">\ir</code> interprets file names relative to the
1668 directory in which the script is located, rather than the current
1670 </p></dd><dt id="APP-PSQL-META-COMMAND-LIST"><span class="term"><code class="literal">\l[x+]</code> or <code class="literal">\list[x+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-LIST" class="id_link">#</a></dt><dd><p>
1671 List the databases in the server and show their names, owners,
1672 character set encodings, and access privileges.
1673 If <em class="replaceable"><code>pattern</code></em> is specified,
1674 only databases whose names match the pattern are listed.
1675 If <code class="literal">x</code> is appended to the command name, the results
1676 are displayed in expanded mode.
1677 If <code class="literal">+</code> is appended to the command name, database
1678 sizes, default tablespaces, and descriptions are also displayed.
1679 (Size information is only available for databases that the current
1680 user can connect to.)
1681 </p></dd><dt id="APP-PSQL-META-COMMAND-LO-EXPORT"><span class="term"><code class="literal">\lo_export <em class="replaceable"><code>loid</code></em> <em class="replaceable"><code>filename</code></em></code></span> <a href="#APP-PSQL-META-COMMAND-LO-EXPORT" class="id_link">#</a></dt><dd><p>
1682 Reads the large object with <acronym class="acronym">OID</acronym> <em class="replaceable"><code>loid</code></em> from the database and
1683 writes it to <em class="replaceable"><code>filename</code></em>. Note that this is
1684 subtly different from the server function
1685 <code class="function">lo_export</code>, which acts with the permissions
1686 of the user that the database server runs as and on the server's
1688 </p><div class="tip"><h3 class="title">Tip</h3><p>
1689 Use <code class="command">\lo_list</code> to find out the large object's
1690 <acronym class="acronym">OID</acronym>.
1691 </p></div></dd><dt id="APP-PSQL-META-COMMAND-LO-IMPORT"><span class="term"><code class="literal">\lo_import <em class="replaceable"><code>filename</code></em> [ <em class="replaceable"><code>comment</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-LO-IMPORT" class="id_link">#</a></dt><dd><p>
1692 Stores the file into a <span class="productname">PostgreSQL</span>
1693 large object. Optionally, it associates the given
1694 comment with the object. Example:
1695 </p><pre class="programlisting">
1696 foo=> <strong class="userinput"><code>\lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'</code></strong>
1699 The response indicates that the large object received object
1700 ID 152801, which can be used to access the newly-created large
1701 object in the future. For the sake of readability, it is
1702 recommended to always associate a human-readable comment with
1703 every object. Both OIDs and comments can be viewed with the
1704 <code class="command">\lo_list</code> command.
1706 Note that this command is subtly different from the server-side
1707 <code class="function">lo_import</code> because it acts as the local user
1708 on the local file system, rather than the server's user and file
1710 </p></dd><dt id="APP-PSQL-META-COMMAND-LO-LIST"><span class="term"><code class="literal">\lo_list[x+]</code></span> <a href="#APP-PSQL-META-COMMAND-LO-LIST" class="id_link">#</a></dt><dd><p>
1711 Shows a list of all <span class="productname">PostgreSQL</span>
1712 large objects currently stored in the database,
1713 along with any comments provided for them.
1714 If <code class="literal">x</code> is appended to the command name, the results
1715 are displayed in expanded mode.
1716 If <code class="literal">+</code> is appended to the command name,
1717 each large object is listed with its associated permissions,
1719 </p></dd><dt id="APP-PSQL-META-COMMAND-LO-UNLINK"><span class="term"><code class="literal">\lo_unlink <em class="replaceable"><code>loid</code></em></code></span> <a href="#APP-PSQL-META-COMMAND-LO-UNLINK" class="id_link">#</a></dt><dd><p>
1720 Deletes the large object with <acronym class="acronym">OID</acronym>
1721 <em class="replaceable"><code>loid</code></em> from the
1723 </p><div class="tip"><h3 class="title">Tip</h3><p>
1724 Use <code class="command">\lo_list</code> to find out the large object's
1725 <acronym class="acronym">OID</acronym>.
1726 </p></div></dd><dt id="APP-PSQL-META-COMMAND-OUT"><span class="term"><code class="literal">\o</code> or <code class="literal">\out [ <em class="replaceable"><code>filename</code></em> ]</code><br /></span><span class="term"><code class="literal">\o</code> or <code class="literal">\out [ |<em class="replaceable"><code>command</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-OUT" class="id_link">#</a></dt><dd><p>
1727 Arranges to save future query results to the file <em class="replaceable"><code>filename</code></em> or pipe future results
1728 to the shell command <em class="replaceable"><code>command</code></em>. If no argument is
1729 specified, the query output is reset to the standard output.
1731 If the argument begins with <code class="literal">|</code>, then the entire remainder
1732 of the line is taken to be
1733 the <em class="replaceable"><code>command</code></em> to execute,
1734 and neither variable interpolation nor backquote expansion are
1735 performed in it. The rest of the line is simply passed literally to
1738 <span class="quote">“<span class="quote">Query results</span>”</span> includes all tables, command
1739 responses, and notices obtained from the database server, as
1740 well as output of various backslash commands that query the
1741 database (such as <code class="command">\d</code>); but not error
1743 </p><div class="tip"><h3 class="title">Tip</h3><p>
1744 To intersperse text output in between query results, use
1745 <code class="command">\qecho</code>.
1746 </p></div></dd><dt id="APP-PSQL-META-COMMAND-PRINT"><span class="term"><code class="literal">\p</code> or <code class="literal">\print</code></span> <a href="#APP-PSQL-META-COMMAND-PRINT" class="id_link">#</a></dt><dd><p>
1747 Print the current query buffer to the standard output.
1748 If the current query buffer is empty, the most recently executed query
1750 </p></dd><dt id="APP-PSQL-META-COMMAND-PARSE"><span class="term"><code class="literal">\parse <em class="replaceable"><code>statement_name</code></em></code></span> <a href="#APP-PSQL-META-COMMAND-PARSE" class="id_link">#</a></dt><dd><p>
1751 Creates a prepared statement from the current query buffer, based on
1752 the name of a destination prepared-statement object. An empty string
1753 denotes the unnamed prepared statement.
1756 </p><pre class="programlisting">
1757 SELECT $1 \parse stmt1
1760 This command causes the extended query protocol to be used, unlike
1761 normal <span class="application">psql</span> operation, which uses the
1762 simple query protocol. A
1763 <a class="xref" href="protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS-PARSE">Parse (F)</a>
1764 message will be issued by this command so it can be useful to
1765 test the extended query protocol from
1766 <span class="application">psql</span>. This command affects only the next
1767 query executed; all subsequent queries will use the simple query
1768 protocol by default.
1769 </p></dd><dt id="APP-PSQL-META-COMMAND-PASSWORD"><span class="term"><code class="literal">\password [ <em class="replaceable"><code>username</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-PASSWORD" class="id_link">#</a></dt><dd><p>
1770 Changes the password of the specified user (by default, the current
1771 user). This command prompts for the new password, encrypts it, and
1772 sends it to the server as an <code class="command">ALTER ROLE</code> command. This
1773 makes sure that the new password does not appear in cleartext in the
1774 command history, the server log, or elsewhere.
1775 </p></dd><dt id="APP-PSQL-META-COMMAND-PROMPT"><span class="term"><code class="literal">\prompt [ <em class="replaceable"><code>text</code></em> ] <em class="replaceable"><code>name</code></em></code></span> <a href="#APP-PSQL-META-COMMAND-PROMPT" class="id_link">#</a></dt><dd><p>
1776 Prompts the user to supply text, which is assigned to the variable
1777 <em class="replaceable"><code>name</code></em>.
1778 An optional prompt string, <em class="replaceable"><code>text</code></em>, can be specified. (For multiword
1779 prompts, surround the text with single quotes.)
1781 By default, <code class="literal">\prompt</code> uses the terminal for input and
1782 output. However, if the <code class="option">-f</code> command line switch was
1783 used, <code class="literal">\prompt</code> uses standard input and standard output.
1784 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET"><span class="term"><code class="literal">\pset [ <em class="replaceable"><code>option</code></em> [ <em class="replaceable"><code>value</code></em> ] ]</code></span> <a href="#APP-PSQL-META-COMMAND-PSET" class="id_link">#</a></dt><dd><p>
1785 This command sets options affecting the output of query result tables.
1786 <em class="replaceable"><code>option</code></em>
1787 indicates which option is to be set. The semantics of
1788 <em class="replaceable"><code>value</code></em> vary depending
1789 on the selected option. For some options, omitting <em class="replaceable"><code>value</code></em> causes the option to be toggled
1790 or unset, as described under the particular option. If no such
1791 behavior is mentioned, then omitting
1792 <em class="replaceable"><code>value</code></em> just results in
1793 the current setting being displayed.
1795 <code class="command">\pset</code> without any arguments displays the current status
1796 of all printing options.
1798 Adjustable printing options are:
1799 </p><div class="variablelist"><dl class="variablelist"><dt id="APP-PSQL-META-COMMAND-PSET-BORDER"><span class="term"><code class="literal">border</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-BORDER" class="id_link">#</a></dt><dd><p>
1800 The <em class="replaceable"><code>value</code></em> must be a
1801 number. In general, the higher
1802 the number the more borders and lines the tables will have,
1803 but details depend on the particular format.
1804 In <acronym class="acronym">HTML</acronym> format, this will translate directly
1805 into the <code class="literal">border=...</code> attribute.
1806 In most other formats only values 0 (no border), 1 (internal
1807 dividing lines), and 2 (table frame) make sense, and values above 2
1808 will be treated the same as <code class="literal">border = 2</code>.
1809 The <code class="literal">latex</code> and <code class="literal">latex-longtable</code>
1810 formats additionally allow a value of 3 to add dividing lines
1812 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-COLUMNS"><span class="term"><code class="literal">columns</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-COLUMNS" class="id_link">#</a></dt><dd><p>
1813 Sets the target width for the <code class="literal">wrapped</code> format, and also
1814 the width limit for determining whether output is wide enough to
1815 require the pager or switch to the vertical display in expanded auto
1817 Zero (the default) causes the target width to be controlled by the
1818 environment variable <code class="envar">COLUMNS</code>, or the detected screen width
1819 if <code class="envar">COLUMNS</code> is not set.
1820 In addition, if <code class="literal">columns</code> is zero then the
1821 <code class="literal">wrapped</code> format only affects screen output.
1822 If <code class="literal">columns</code> is nonzero then file and pipe output is
1823 wrapped to that width as well.
1824 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-CSV-FIELDSEP"><span class="term"><code class="literal">csv_fieldsep</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-CSV-FIELDSEP" class="id_link">#</a></dt><dd><p>
1825 Specifies the field separator to be used in
1826 <acronym class="acronym">CSV</acronym> output format. If the separator character
1827 appears in a field's value, that field is output within double
1828 quotes, following standard <acronym class="acronym">CSV</acronym> rules.
1829 The default is a comma.
1830 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-EXPANDED"><span class="term"><code class="literal">expanded</code> (or <code class="literal">x</code>)</span> <a href="#APP-PSQL-META-COMMAND-PSET-EXPANDED" class="id_link">#</a></dt><dd><p>
1831 If <em class="replaceable"><code>value</code></em> is specified it
1832 must be either <code class="literal">on</code> or <code class="literal">off</code>, which
1833 will enable or disable expanded mode, or <code class="literal">auto</code>.
1834 If <em class="replaceable"><code>value</code></em> is omitted the
1835 command toggles between the on and off settings. When expanded mode
1836 is enabled, query results are displayed in two columns, with the
1837 column name on the left and the data on the right. This mode is
1838 useful if the data wouldn't fit on the screen in the
1839 normal <span class="quote">“<span class="quote">horizontal</span>”</span> mode. In the auto setting, the
1840 expanded mode is used whenever the query output has more than one
1841 column and is wider than the screen; otherwise, the regular mode is
1842 used. The auto setting is only
1843 effective in the aligned and wrapped formats. In other formats, it
1844 always behaves as if the expanded mode is off.
1845 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-FIELDSEP"><span class="term"><code class="literal">fieldsep</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-FIELDSEP" class="id_link">#</a></dt><dd><p>
1846 Specifies the field separator to be used in unaligned output
1847 format. That way one can create, for example, tab-separated
1848 output, which other programs might prefer. To
1849 set a tab as field separator, type <code class="literal">\pset fieldsep
1850 '\t'</code>. The default field separator is
1851 <code class="literal">'|'</code> (a vertical bar).
1852 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-FIELDSEP-ZERO"><span class="term"><code class="literal">fieldsep_zero</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-FIELDSEP-ZERO" class="id_link">#</a></dt><dd><p>
1853 Sets the field separator to use in unaligned output format to a zero
1855 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-FOOTER"><span class="term"><code class="literal">footer</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-FOOTER" class="id_link">#</a></dt><dd><p>
1856 If <em class="replaceable"><code>value</code></em> is specified
1857 it must be either <code class="literal">on</code> or <code class="literal">off</code>
1858 which will enable or disable display of the table footer
1859 (the <code class="literal">(<em class="replaceable"><code>n</code></em> rows)</code> count).
1860 If <em class="replaceable"><code>value</code></em> is omitted the
1861 command toggles footer display on or off.
1862 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-FORMAT"><span class="term"><code class="literal">format</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-FORMAT" class="id_link">#</a></dt><dd><p>
1863 Sets the output format to one of <code class="literal">aligned</code>,
1864 <code class="literal">asciidoc</code>,
1865 <code class="literal">csv</code>,
1866 <code class="literal">html</code>,
1867 <code class="literal">latex</code>,
1868 <code class="literal">latex-longtable</code>, <code class="literal">troff-ms</code>,
1869 <code class="literal">unaligned</code>, or <code class="literal">wrapped</code>.
1870 Unique abbreviations are allowed.
1871 </p><p><code class="literal">aligned</code> format is the standard,
1872 human-readable, nicely formatted text output; this is the default.
1873 </p><p><code class="literal">unaligned</code> format writes all columns of a row on one
1874 line, separated by the currently active field separator. This
1875 is useful for creating output that might be intended to be read
1876 in by other programs, for example, tab-separated or comma-separated
1877 format. However, the field separator character is not treated
1878 specially if it appears in a column's value;
1879 so <acronym class="acronym">CSV</acronym> format may be better suited for such
1881 </p><p><code class="literal">csv</code> format
1882 <a id="id-1.9.4.21.8.4.11.1.81.2.3.1.8.2.4.2" class="indexterm"></a>
1883 writes column values separated by commas, applying the quoting
1885 <a class="ulink" href="https://datatracker.ietf.org/doc/html/rfc4180" target="_top">RFC 4180</a>.
1886 This output is compatible with the CSV format of the server's
1887 <code class="command">COPY</code> command.
1888 A header line with column names is generated unless
1889 the <code class="literal">tuples_only</code> parameter is
1890 <code class="literal">on</code>. Titles and footers are not printed.
1891 Each row is terminated by the system-dependent end-of-line character,
1892 which is typically a single newline (<code class="literal">\n</code>) for
1893 Unix-like systems or a carriage return and newline sequence
1894 (<code class="literal">\r\n</code>) for Microsoft Windows.
1895 Field separator characters other than comma can be selected with
1896 <code class="command">\pset csv_fieldsep</code>.
1897 </p><p><code class="literal">wrapped</code> format is like <code class="literal">aligned</code> but wraps
1898 wide data values across lines to make the output fit in the target
1899 column width. The target width is determined as described under
1900 the <code class="literal">columns</code> option. Note that <span class="application">psql</span> will
1901 not attempt to wrap column header titles; therefore,
1902 <code class="literal">wrapped</code> format behaves the same as <code class="literal">aligned</code>
1903 if the total width needed for column headers exceeds the target.
1905 The <code class="literal">asciidoc</code>, <code class="literal">html</code>,
1906 <code class="literal">latex</code>, <code class="literal">latex-longtable</code>, and
1907 <code class="literal">troff-ms</code> formats put out tables that are intended
1908 to be included in documents using the respective mark-up
1909 language. They are not complete documents! This might not be
1910 necessary in <acronym class="acronym">HTML</acronym>, but in
1911 <span class="application">LaTeX</span> you must have a complete
1913 The <code class="literal">latex</code> format
1914 uses <span class="application">LaTeX</span>'s <code class="literal">tabular</code>
1916 The <code class="literal">latex-longtable</code> format
1917 requires the <span class="application">LaTeX</span>
1918 <code class="literal">longtable</code> and <code class="literal">booktabs</code> packages.
1919 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-LINESTYLE"><span class="term"><code class="literal">linestyle</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-LINESTYLE" class="id_link">#</a></dt><dd><p>
1920 Sets the border line drawing style to one
1921 of <code class="literal">ascii</code>, <code class="literal">old-ascii</code>,
1922 or <code class="literal">unicode</code>.
1923 Unique abbreviations are allowed. (That would mean one
1925 The default setting is <code class="literal">ascii</code>.
1926 This option only affects the <code class="literal">aligned</code> and
1927 <code class="literal">wrapped</code> output formats.
1928 </p><p><code class="literal">ascii</code> style uses plain <acronym class="acronym">ASCII</acronym>
1929 characters. Newlines in data are shown using
1930 a <code class="literal">+</code> symbol in the right-hand margin.
1931 When the <code class="literal">wrapped</code> format wraps data from
1932 one line to the next without a newline character, a dot
1933 (<code class="literal">.</code>) is shown in the right-hand margin of the first line,
1934 and again in the left-hand margin of the following line.
1935 </p><p><code class="literal">old-ascii</code> style uses plain <acronym class="acronym">ASCII</acronym>
1936 characters, using the formatting style used
1937 in <span class="productname">PostgreSQL</span> 8.4 and earlier.
1938 Newlines in data are shown using a <code class="literal">:</code>
1939 symbol in place of the left-hand column separator.
1940 When the data is wrapped from one line
1941 to the next without a newline character, a <code class="literal">;</code>
1942 symbol is used in place of the left-hand column separator.
1943 </p><p><code class="literal">unicode</code> style uses Unicode box-drawing characters.
1944 Newlines in data are shown using a carriage return symbol
1945 in the right-hand margin. When the data is wrapped from one line
1946 to the next without a newline character, an ellipsis symbol
1947 is shown in the right-hand margin of the first line, and
1948 again in the left-hand margin of the following line.
1950 When the <code class="literal">border</code> setting is greater than zero,
1951 the <code class="literal">linestyle</code> option also determines the
1952 characters with which the border lines are drawn.
1953 Plain <acronym class="acronym">ASCII</acronym> characters work everywhere, but
1954 Unicode characters look nicer on displays that recognize them.
1955 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-NULL"><span class="term"><code class="literal">null</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-NULL" class="id_link">#</a></dt><dd><p>
1956 Sets the string to be printed in place of a null value.
1957 The default is to print nothing, which can easily be mistaken for
1958 an empty string. For example, one might prefer <code class="literal">\pset null
1960 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-NUMERICLOCALE"><span class="term"><code class="literal">numericlocale</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-NUMERICLOCALE" class="id_link">#</a></dt><dd><p>
1961 If <em class="replaceable"><code>value</code></em> is specified
1962 it must be either <code class="literal">on</code> or <code class="literal">off</code>
1963 which will enable or disable display of a locale-specific character
1964 to separate groups of digits to the left of the decimal marker.
1965 If <em class="replaceable"><code>value</code></em> is omitted the
1966 command toggles between regular and locale-specific numeric output.
1967 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-PAGER"><span class="term"><code class="literal">pager</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-PAGER" class="id_link">#</a></dt><dd><p>
1968 Controls use of a pager program for query and <span class="application">psql</span>
1970 When the <code class="literal">pager</code> option is <code class="literal">off</code>, the pager
1971 program is not used. When the <code class="literal">pager</code> option is
1972 <code class="literal">on</code>, the pager is used when appropriate, i.e., when the
1973 output is to a terminal and will not fit on the screen.
1974 The <code class="literal">pager</code> option can also be set to <code class="literal">always</code>,
1975 which causes the pager to be used for all terminal output regardless
1976 of whether it fits on the screen. <code class="literal">\pset pager</code>
1977 without a <em class="replaceable"><code>value</code></em>
1978 toggles pager use on and off.
1980 If the environment variable <code class="envar">PSQL_PAGER</code>
1981 or <code class="envar">PAGER</code> is set, output to be paged is piped to the
1982 specified program. Otherwise a platform-dependent default program
1983 (such as <code class="filename">more</code>) is used.
1985 When using the <code class="literal">\watch</code> command to execute a query
1986 repeatedly, the environment variable <code class="envar">PSQL_WATCH_PAGER</code>
1987 is used to find the pager program instead, on Unix systems. This is
1988 configured separately because it may confuse traditional pagers, but
1989 can be used to send output to tools that understand
1990 <span class="application">psql</span>'s output format (such as
1991 <code class="filename">pspg --stream</code>).
1992 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-PAGER-MIN-LINES"><span class="term"><code class="literal">pager_min_lines</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-PAGER-MIN-LINES" class="id_link">#</a></dt><dd><p>
1993 If <code class="literal">pager_min_lines</code> is set to a number greater than the
1994 page height, the pager program will not be called unless there are
1995 at least this many lines of output to show. The default setting
1997 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-RECORDSEP"><span class="term"><code class="literal">recordsep</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-RECORDSEP" class="id_link">#</a></dt><dd><p>
1998 Specifies the record (line) separator to use in unaligned
1999 output format. The default is a newline character.
2000 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-RECORDSEP-ZERO"><span class="term"><code class="literal">recordsep_zero</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-RECORDSEP-ZERO" class="id_link">#</a></dt><dd><p>
2001 Sets the record separator to use in unaligned output format to a zero
2003 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-TABLEATTR"><span class="term"><code class="literal">tableattr</code> (or <code class="literal">T</code>)</span> <a href="#APP-PSQL-META-COMMAND-PSET-TABLEATTR" class="id_link">#</a></dt><dd><p>
2004 In <acronym class="acronym">HTML</acronym> format, this specifies attributes
2005 to be placed inside the <code class="sgmltag-element">table</code> tag. This
2006 could for example be <code class="literal">cellpadding</code> or
2007 <code class="literal">bgcolor</code>. Note that you probably don't want
2008 to specify <code class="literal">border</code> here, as that is already
2009 taken care of by <code class="literal">\pset border</code>.
2011 <em class="replaceable"><code>value</code></em> is given,
2012 the table attributes are unset.
2014 In <code class="literal">latex-longtable</code> format, this controls
2015 the proportional width of each column containing a left-aligned
2016 data type. It is specified as a whitespace-separated list of values,
2017 e.g., <code class="literal">'0.2 0.2 0.6'</code>. Unspecified output columns
2018 use the last specified value.
2019 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-TITLE"><span class="term"><code class="literal">title</code> (or <code class="literal">C</code>)</span> <a href="#APP-PSQL-META-COMMAND-PSET-TITLE" class="id_link">#</a></dt><dd><p>
2020 Sets the table title for any subsequently printed tables. This
2021 can be used to give your output descriptive tags. If no
2022 <em class="replaceable"><code>value</code></em> is given,
2024 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-TUPLES-ONLY"><span class="term"><code class="literal">tuples_only</code> (or <code class="literal">t</code>)</span> <a href="#APP-PSQL-META-COMMAND-PSET-TUPLES-ONLY" class="id_link">#</a></dt><dd><p>
2025 If <em class="replaceable"><code>value</code></em> is specified
2026 it must be either <code class="literal">on</code> or <code class="literal">off</code>
2027 which will enable or disable tuples-only mode.
2028 If <em class="replaceable"><code>value</code></em> is omitted the
2029 command toggles between regular and tuples-only output.
2030 Regular output includes extra information such
2031 as column headers, titles, and various footers. In tuples-only
2032 mode, only actual table data is shown.
2033 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-UNICODE-BORDER-LINESTYLE"><span class="term"><code class="literal">unicode_border_linestyle</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-UNICODE-BORDER-LINESTYLE" class="id_link">#</a></dt><dd><p>
2034 Sets the border drawing style for the <code class="literal">unicode</code>
2035 line style to one of <code class="literal">single</code>
2036 or <code class="literal">double</code>.
2037 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-UNICODE-COLUMN-LINESTYLE"><span class="term"><code class="literal">unicode_column_linestyle</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-UNICODE-COLUMN-LINESTYLE" class="id_link">#</a></dt><dd><p>
2038 Sets the column drawing style for the <code class="literal">unicode</code>
2039 line style to one of <code class="literal">single</code>
2040 or <code class="literal">double</code>.
2041 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-UNICODE-HEADER-LINESTYLE"><span class="term"><code class="literal">unicode_header_linestyle</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-UNICODE-HEADER-LINESTYLE" class="id_link">#</a></dt><dd><p>
2042 Sets the header drawing style for the <code class="literal">unicode</code>
2043 line style to one of <code class="literal">single</code>
2044 or <code class="literal">double</code>.
2045 </p></dd><dt id="APP-PSQL-META-COMMAND-PSET-XHEADER-WIDTH"><span class="term"><code class="literal">xheader_width</code></span> <a href="#APP-PSQL-META-COMMAND-PSET-XHEADER-WIDTH" class="id_link">#</a></dt><dd><p>
2046 Sets the maximum width of the header for expanded output to one of
2047 <code class="literal">full</code> (the default value),
2048 <code class="literal">column</code>, <code class="literal">page</code>, or an
2049 <em class="replaceable"><code>integer value</code></em>.
2051 <code class="literal">full</code>: the expanded header is not truncated,
2052 and will be as wide as the widest output line.
2054 <code class="literal">column</code>: truncate the header line to the
2055 width of the first column.
2057 <code class="literal">page</code>: truncate the header line to the terminal
2060 <em class="replaceable"><code>integer value</code></em>: specify
2061 the exact maximum width of the header line.
2062 </p></dd></dl></div><p>
2064 Illustrations of how these different formats look can be seen in
2065 <a class="xref" href="app-psql.html#APP-PSQL-EXAMPLES" title="Examples">Examples</a>, below.
2066 </p><div class="tip"><h3 class="title">Tip</h3><p>
2067 There are various shortcut commands for <code class="command">\pset</code>. See
2068 <code class="command">\a</code>, <code class="command">\C</code>, <code class="command">\f</code>,
2069 <code class="command">\H</code>, <code class="command">\t</code>, <code class="command">\T</code>,
2070 and <code class="command">\x</code>.
2071 </p></div></dd><dt id="APP-PSQL-META-COMMAND-QUIT"><span class="term"><code class="literal">\q</code> or <code class="literal">\quit</code></span> <a href="#APP-PSQL-META-COMMAND-QUIT" class="id_link">#</a></dt><dd><p>
2072 Quits the <span class="application">psql</span> program.
2073 In a script file, only execution of that script is terminated.
2074 </p></dd><dt id="APP-PSQL-META-COMMAND-QECHO"><span class="term"><code class="literal">\qecho <em class="replaceable"><code>text</code></em> [ ... ] </code></span> <a href="#APP-PSQL-META-COMMAND-QECHO" class="id_link">#</a></dt><dd><p>
2075 This command is identical to <code class="command">\echo</code> except
2076 that the output will be written to the query output channel, as
2077 set by <code class="command">\o</code>.
2078 </p></dd><dt id="APP-PSQL-META-COMMAND-RESET"><span class="term"><code class="literal">\r</code> or <code class="literal">\reset</code></span> <a href="#APP-PSQL-META-COMMAND-RESET" class="id_link">#</a></dt><dd><p>
2079 Resets (clears) the query buffer.
2080 </p></dd><dt id="APP-PSQL-META-COMMAND-RESTRICT"><span class="term"><code class="literal">\restrict <em class="replaceable"><code>restrict_key</code></em></code></span> <a href="#APP-PSQL-META-COMMAND-RESTRICT" class="id_link">#</a></dt><dd><p>
2081 Enter "restricted" mode with the provided key. In this mode, the only
2082 allowed meta-command is <code class="command">\unrestrict</code>, to exit
2083 restricted mode. The key may contain only alphanumeric characters.
2085 This command is primarily intended for use in plain-text dumps
2086 generated by <span class="application">pg_dump</span>,
2087 <span class="application">pg_dumpall</span>, and
2088 <span class="application">pg_restore</span>, but it may be useful elsewhere.
2089 </p></dd><dt id="APP-PSQL-META-COMMAND-S"><span class="term"><code class="literal">\s [ <em class="replaceable"><code>filename</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-S" class="id_link">#</a></dt><dd><p>
2090 Print <span class="application">psql</span>'s command line history
2091 to <em class="replaceable"><code>filename</code></em>.
2092 If <em class="replaceable"><code>filename</code></em> is omitted,
2093 the history is written to the standard output (using the pager if
2094 appropriate). This command is not available
2095 if <span class="application">psql</span> was built
2096 without <span class="application">Readline</span> support.
2097 </p></dd><dt id="APP-PSQL-META-COMMAND-SET"><span class="term"><code class="literal">\set [ <em class="replaceable"><code>name</code></em> [ <em class="replaceable"><code>value</code></em> [ ... ] ] ]</code></span> <a href="#APP-PSQL-META-COMMAND-SET" class="id_link">#</a></dt><dd><p>
2098 Sets the <span class="application">psql</span> variable <em class="replaceable"><code>name</code></em> to <em class="replaceable"><code>value</code></em>, or if more than one value
2099 is given, to the concatenation of all of them. If only one
2100 argument is given, the variable is set to an empty-string value. To
2101 unset a variable, use the <code class="command">\unset</code> command.
2102 </p><p><code class="command">\set</code> without any arguments displays the names and values
2103 of all currently-set <span class="application">psql</span> variables.
2105 Valid variable names can contain letters, digits, and
2106 underscores. See <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a> below for details.
2107 Variable names are case-sensitive.
2109 Certain variables are special, in that they
2110 control <span class="application">psql</span>'s behavior or are
2111 automatically set to reflect connection state. These variables are
2112 documented in <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a>, below.
2113 </p><div class="note"><h3 class="title">Note</h3><p>
2114 This command is unrelated to the <acronym class="acronym">SQL</acronym>
2115 command <a class="link" href="sql-set.html" title="SET"><code class="command">SET</code></a>.
2116 </p></div></dd><dt id="APP-PSQL-META-COMMAND-SETENV"><span class="term"><code class="literal">\setenv <em class="replaceable"><code>name</code></em> [ <em class="replaceable"><code>value</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-SETENV" class="id_link">#</a></dt><dd><p>
2117 Sets the environment variable <em class="replaceable"><code>name</code></em> to <em class="replaceable"><code>value</code></em>, or if the
2118 <em class="replaceable"><code>value</code></em> is
2119 not supplied, unsets the environment variable. Example:
2120 </p><pre class="programlisting">
2121 testdb=> <strong class="userinput"><code>\setenv PAGER less</code></strong>
2122 testdb=> <strong class="userinput"><code>\setenv LESS -imx4F</code></strong>
2123 </pre></dd><dt id="APP-PSQL-META-COMMAND-SF"><span class="term"><code class="literal">\sf[+] <em class="replaceable"><code>function_description</code></em> </code></span> <a href="#APP-PSQL-META-COMMAND-SF" class="id_link">#</a></dt><dd><p>
2124 This command fetches and shows the definition of the named function or procedure,
2125 in the form of a <code class="command">CREATE OR REPLACE FUNCTION</code> or
2126 <code class="command">CREATE OR REPLACE PROCEDURE</code> command.
2127 The definition is printed to the current query output channel,
2128 as set by <code class="command">\o</code>.
2130 The target function can be specified by name alone, or by name
2131 and arguments, for example <code class="literal">foo(integer, text)</code>.
2132 The argument types must be given if there is more
2133 than one function of the same name.
2135 If <code class="literal">+</code> is appended to the command name, then the
2136 output lines are numbered, with the first line of the function body
2139 Unlike most other meta-commands, the entire remainder of the line is
2140 always taken to be the argument(s) of <code class="command">\sf</code>, and neither
2141 variable interpolation nor backquote expansion are performed in the
2143 </p></dd><dt id="APP-PSQL-META-COMMAND-SV"><span class="term"><code class="literal">\sv[+] <em class="replaceable"><code>view_name</code></em> </code></span> <a href="#APP-PSQL-META-COMMAND-SV" class="id_link">#</a></dt><dd><p>
2144 This command fetches and shows the definition of the named view,
2145 in the form of a <code class="command">CREATE OR REPLACE VIEW</code> command.
2146 The definition is printed to the current query output channel,
2147 as set by <code class="command">\o</code>.
2149 If <code class="literal">+</code> is appended to the command name, then the
2150 output lines are numbered from 1.
2152 Unlike most other meta-commands, the entire remainder of the line is
2153 always taken to be the argument(s) of <code class="command">\sv</code>, and neither
2154 variable interpolation nor backquote expansion are performed in the
2156 </p></dd><dt id="APP-PSQL-META-COMMAND-PIPELINE"><span class="term"><code class="literal">\startpipeline</code><br /></span><span class="term"><code class="literal">\sendpipeline</code><br /></span><span class="term"><code class="literal">\syncpipeline</code><br /></span><span class="term"><code class="literal">\endpipeline</code><br /></span><span class="term"><code class="literal">\flushrequest</code><br /></span><span class="term"><code class="literal">\flush</code><br /></span><span class="term"><code class="literal">\getresults [ <em class="replaceable"><code>number_results</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-PIPELINE" class="id_link">#</a></dt><dd><p>
2157 This group of commands implements pipelining of SQL statements.
2158 A pipeline must begin with a <code class="command">\startpipeline</code>
2159 and end with an <code class="command">\endpipeline</code>. In between there
2160 may be any number of <code class="command">\syncpipeline</code> commands,
2161 which sends a <a class="link" href="protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY" title="54.2.3. Extended Query">sync message</a>
2162 without ending the ongoing pipeline and flushing the send buffer.
2163 In pipeline mode, statements are sent to the server without waiting
2164 for the results of previous statements.
2165 See <a class="xref" href="libpq-pipeline-mode.html" title="32.5. Pipeline Mode">Section 32.5</a> for more details.
2167 All queries executed while a pipeline is ongoing use the extended
2168 query protocol. Queries are appended to the pipeline when ending with
2169 a semicolon. The meta-commands <code class="literal">\bind</code>,
2170 <code class="literal">\bind_named</code>, <code class="literal">\close_prepared</code> or
2171 <code class="literal">\parse</code> can be used in an ongoing pipeline. While
2172 a pipeline is ongoing, <code class="literal">\sendpipeline</code> will append
2173 the current query buffer to the pipeline. Other meta-commands like
2174 <code class="literal">\g</code>, <code class="literal">\gx</code> or
2175 <code class="literal">\gdesc</code> are not allowed in pipeline mode.
2177 <code class="command">\flushrequest</code> appends a flush command to the
2178 pipeline, allowing to read results with
2179 <code class="command">\getresults</code> without issuing a sync or ending the
2180 pipeline. <code class="command">\getresults</code> will automatically push
2181 unsent data to the server. <code class="command">\flush</code> can be used to
2182 manually push unsent data.
2184 <code class="command">\getresults</code> accepts an optional
2185 <em class="replaceable"><code>number_results</code></em> parameter.
2186 If provided, only the first
2187 <em class="replaceable"><code>number_results</code></em> pending
2188 results will be read. If not provided or <code class="literal">0</code>, all
2189 pending results are read.
2191 When pipeline mode is active, a dedicated prompt variable is available
2192 to report the pipeline status.
2193 See <a class="xref" href="app-psql.html#APP-PSQL-PROMPTING-P-UC"><code class="literal">%P</code></a> for more details
2195 <code class="command">COPY</code> is not supported while in pipeline mode.
2198 </p><pre class="programlisting">
2200 SELECT * FROM pg_class;
2201 SELECT 1 \bind \sendpipeline
2206 </p></dd><dt id="APP-PSQL-META-COMMAND-T-LC"><span class="term"><code class="literal">\t</code></span> <a href="#APP-PSQL-META-COMMAND-T-LC" class="id_link">#</a></dt><dd><p>
2207 Toggles the display of output column name headings and row count
2208 footer. This command is equivalent to <code class="literal">\pset
2209 tuples_only</code> and is provided for convenience.
2210 </p></dd><dt id="APP-PSQL-META-COMMAND-T-UC"><span class="term"><code class="literal">\T <em class="replaceable"><code>table_options</code></em></code></span> <a href="#APP-PSQL-META-COMMAND-T-UC" class="id_link">#</a></dt><dd><p>
2211 Specifies attributes to be placed within the
2212 <code class="sgmltag-element">table</code> tag in <acronym class="acronym">HTML</acronym>
2213 output format. This command is equivalent to <code class="literal">\pset
2214 tableattr <em class="replaceable"><code>table_options</code></em></code>.
2215 </p></dd><dt id="APP-PSQL-META-COMMAND-TIMING"><span class="term"><code class="literal">\timing [ <em class="replaceable"><code>on</code></em> | <em class="replaceable"><code>off</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-TIMING" class="id_link">#</a></dt><dd><p>
2216 With a parameter, turns displaying of how long each SQL statement
2217 takes on or off. Without a parameter, toggles the display between
2218 on and off. The display is in milliseconds; intervals longer than
2219 1 second are also shown in minutes:seconds format, with hours and
2220 days fields added if needed.
2221 </p></dd><dt id="APP-PSQL-META-COMMAND-UNRESTRICT"><span class="term"><code class="literal">\unrestrict <em class="replaceable"><code>restrict_key</code></em></code></span> <a href="#APP-PSQL-META-COMMAND-UNRESTRICT" class="id_link">#</a></dt><dd><p>
2222 Exit "restricted" mode (i.e., where all other meta-commands are
2223 blocked), provided the specified key matches the one given to
2224 <code class="command">\restrict</code> when restricted mode was entered.
2226 This command is primarily intended for use in plain-text dumps
2227 generated by <span class="application">pg_dump</span>,
2228 <span class="application">pg_dumpall</span>, and
2229 <span class="application">pg_restore</span>, but it may be useful elsewhere.
2230 </p></dd><dt id="APP-PSQL-META-COMMAND-UNSET"><span class="term"><code class="literal">\unset <em class="replaceable"><code>name</code></em></code></span> <a href="#APP-PSQL-META-COMMAND-UNSET" class="id_link">#</a></dt><dd><p>
2231 Unsets (deletes) the <span class="application">psql</span> variable <em class="replaceable"><code>name</code></em>.
2233 Most variables that control <span class="application">psql</span>'s behavior
2234 cannot be unset; instead, an <code class="literal">\unset</code> command is interpreted
2235 as setting them to their default values.
2236 See <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a> below.
2237 </p></dd><dt id="APP-PSQL-META-COMMAND-WRITE"><span class="term"><code class="literal">\w</code> or <code class="literal">\write</code> <em class="replaceable"><code>filename</code></em><br /></span><span class="term"><code class="literal">\w</code> or <code class="literal">\write</code> <code class="literal">|</code><em class="replaceable"><code>command</code></em></span> <a href="#APP-PSQL-META-COMMAND-WRITE" class="id_link">#</a></dt><dd><p>
2238 Writes the current query buffer to the file <em class="replaceable"><code>filename</code></em> or pipes it to the shell
2239 command <em class="replaceable"><code>command</code></em>.
2240 If the current query buffer is empty, the most recently executed query
2243 If the argument begins with <code class="literal">|</code>, then the entire remainder
2244 of the line is taken to be
2245 the <em class="replaceable"><code>command</code></em> to execute,
2246 and neither variable interpolation nor backquote expansion are
2247 performed in it. The rest of the line is simply passed literally to
2249 </p></dd><dt id="APP-PSQL-META-COMMAND-WARN"><span class="term"><code class="literal">\warn <em class="replaceable"><code>text</code></em> [ ... ]</code></span> <a href="#APP-PSQL-META-COMMAND-WARN" class="id_link">#</a></dt><dd><p>
2250 This command is identical to <code class="command">\echo</code> except
2251 that the output will be written to <span class="application">psql</span>'s
2252 standard error channel, rather than standard output.
2253 </p></dd><dt id="APP-PSQL-META-COMMAND-WATCH"><span class="term"><code class="literal">\watch [ i[nterval]=<em class="replaceable"><code>seconds</code></em> ] [ c[ount]=<em class="replaceable"><code>times</code></em> ] [ m[in_rows]=<em class="replaceable"><code>rows</code></em> ] [ <em class="replaceable"><code>seconds</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-WATCH" class="id_link">#</a></dt><dd><p>
2254 Repeatedly execute the current query buffer (as <code class="literal">\g</code> does)
2255 until interrupted, or the query fails, or the execution count limit
2256 (if given) is reached, or the query no longer returns the minimum number
2257 of rows. Wait the specified number of seconds (default 2) between executions.
2258 The default wait can be changed with the variable
2259 <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES-WATCH-INTERVAL"><code class="varname">WATCH_INTERVAL</code></a>.
2260 For backwards compatibility,
2261 <em class="replaceable"><code>seconds</code></em> can be specified
2262 with or without an <code class="literal">interval=</code> prefix.
2263 Each query result is
2264 displayed with a header that includes the <code class="literal">\pset title</code>
2265 string (if any), the time as of query start, and the delay interval.
2267 If the current query buffer is empty, the most recently sent query
2268 is re-executed instead.
2269 </p></dd><dt id="APP-PSQL-META-COMMAND-X"><span class="term"><code class="literal">\x [ <em class="replaceable"><code>on</code></em> | <em class="replaceable"><code>off</code></em> | <em class="replaceable"><code>auto</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-X" class="id_link">#</a></dt><dd><p>
2270 Sets or toggles expanded table formatting mode. As such it is equivalent to
2271 <code class="literal">\pset expanded</code>.
2272 </p></dd><dt id="APP-PSQL-META-COMMAND-Z"><span class="term"><code class="literal">\z[Sx] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span> <a href="#APP-PSQL-META-COMMAND-Z" class="id_link">#</a></dt><dd><p>
2273 Lists tables, views and sequences with their
2274 associated access privileges.
2275 If a <em class="replaceable"><code>pattern</code></em> is
2276 specified, only tables, views and sequences whose names match the
2277 pattern are listed. By default only user-created objects are shown;
2278 supply a pattern or the <code class="literal">S</code> modifier to include
2280 If <code class="literal">x</code> is appended to the command name, the results
2281 are displayed in expanded mode.
2283 This is an alias for <code class="command">\dp</code> (<span class="quote">“<span class="quote">display
2284 privileges</span>”</span>).
2285 </p></dd><dt id="APP-PSQL-META-COMMAND-EXCLAMATION-MARK"><span class="term"><code class="literal">\! [ <em class="replaceable"><code>command</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-EXCLAMATION-MARK" class="id_link">#</a></dt><dd><p>
2286 With no argument, escapes to a sub-shell; <span class="application">psql</span>
2287 resumes when the sub-shell exits. With an argument, executes the
2288 shell command <em class="replaceable"><code>command</code></em>.
2290 Unlike most other meta-commands, the entire remainder of the line is
2291 always taken to be the argument(s) of <code class="command">\!</code>, and neither
2292 variable interpolation nor backquote expansion are performed in the
2293 arguments. The rest of the line is simply passed literally to the
2295 </p></dd><dt id="APP-PSQL-META-COMMAND-QUESTION-MARK"><span class="term"><code class="literal">\? [ <em class="replaceable"><code>topic</code></em> ]</code></span> <a href="#APP-PSQL-META-COMMAND-QUESTION-MARK" class="id_link">#</a></dt><dd><p>
2296 Shows help information. The optional
2297 <em class="replaceable"><code>topic</code></em> parameter
2298 (defaulting to <code class="literal">commands</code>) selects which part of <span class="application">psql</span> is
2299 explained: <code class="literal">commands</code> describes <span class="application">psql</span>'s
2300 backslash commands; <code class="literal">options</code> describes the command-line
2301 options that can be passed to <span class="application">psql</span>;
2302 and <code class="literal">variables</code> shows help about <span class="application">psql</span> configuration
2304 </p></dd><dt id="APP-PSQL-META-COMMAND-SEMICOLON"><span class="term"><code class="literal">\;</code></span> <a href="#APP-PSQL-META-COMMAND-SEMICOLON" class="id_link">#</a></dt><dd><p>
2305 Backslash-semicolon is not a meta-command in the same way as the
2306 preceding commands; rather, it simply causes a semicolon to be
2307 added to the query buffer without any further processing.
2309 Normally, <span class="application">psql</span> will dispatch an SQL command to the
2310 server as soon as it reaches the command-ending semicolon, even if
2311 more input remains on the current line. Thus for example entering
2312 </p><pre class="programlisting">
2313 select 1; select 2; select 3;
2315 will result in the three SQL commands being individually sent to
2316 the server, with each one's results being displayed before
2317 continuing to the next command. However, a semicolon entered
2318 as <code class="literal">\;</code> will not trigger command processing, so that the
2319 command before it and the one after are effectively combined and
2320 sent to the server in one request. So for example
2321 </p><pre class="programlisting">
2322 select 1\; select 2\; select 3;
2324 results in sending the three SQL commands to the server in a single
2325 request, when the non-backslashed semicolon is reached.
2326 The server executes such a request as a single transaction,
2327 unless there are explicit <code class="command">BEGIN</code>/<code class="command">COMMIT</code>
2328 commands included in the string to divide it into multiple
2329 transactions. (See <a class="xref" href="protocol-flow.html#PROTOCOL-FLOW-MULTI-STATEMENT" title="54.2.2.1. Multiple Statements in a Simple Query">Section 54.2.2.1</a>
2330 for more details about how the server handles multi-query strings.)
2331 </p></dd></dl></div><p>
2332 </p><div class="refsect3" id="APP-PSQL-PATTERNS"><h4>Patterns</h4><a id="id-1.9.4.21.8.4.12.2" class="indexterm"></a><p>
2333 The various <code class="literal">\d</code> commands accept a <em class="replaceable"><code>pattern</code></em> parameter to specify the
2334 object name(s) to be displayed. In the simplest case, a pattern
2335 is just the exact name of the object. The characters within a
2336 pattern are normally folded to lower case, just as in SQL names;
2337 for example, <code class="literal">\dt FOO</code> will display the table named
2338 <code class="literal">foo</code>. As in SQL names, placing double quotes around
2339 a pattern stops folding to lower case. Should you need to include
2340 an actual double quote character in a pattern, write it as a pair
2341 of double quotes within a double-quote sequence; again this is in
2342 accord with the rules for SQL quoted identifiers. For example,
2343 <code class="literal">\dt "FOO""BAR"</code> will display the table named
2344 <code class="literal">FOO"BAR</code> (not <code class="literal">foo"bar</code>). Unlike the normal
2345 rules for SQL names, you can put double quotes around just part
2346 of a pattern, for instance <code class="literal">\dt FOO"FOO"BAR</code> will display
2347 the table named <code class="literal">fooFOObar</code>.
2349 Whenever the <em class="replaceable"><code>pattern</code></em> parameter
2350 is omitted completely, the <code class="literal">\d</code> commands display all objects
2351 that are visible in the current schema search path — this is
2352 equivalent to using <code class="literal">*</code> as the pattern.
2353 (An object is said to be <em class="firstterm">visible</em> if its
2354 containing schema is in the search path and no object of the same
2355 kind and name appears earlier in the search path. This is equivalent to the
2356 statement that the object can be referenced by name without explicit
2357 schema qualification.)
2358 To see all objects in the database regardless of visibility,
2359 use <code class="literal">*.*</code> as the pattern.
2361 Within a pattern, <code class="literal">*</code> matches any sequence of characters
2362 (including no characters) and <code class="literal">?</code> matches any single character.
2363 (This notation is comparable to Unix shell file name patterns.)
2364 For example, <code class="literal">\dt int*</code> displays tables whose names
2365 begin with <code class="literal">int</code>. But within double quotes, <code class="literal">*</code>
2366 and <code class="literal">?</code> lose these special meanings and are just matched
2369 A relation pattern that contains a dot (<code class="literal">.</code>) is interpreted as a schema
2370 name pattern followed by an object name pattern. For example,
2371 <code class="literal">\dt foo*.*bar*</code> displays all tables whose table name
2372 includes <code class="literal">bar</code> that are in schemas whose schema name
2373 starts with <code class="literal">foo</code>. When no dot appears, then the pattern
2374 matches only objects that are visible in the current schema search path.
2375 Again, a dot within double quotes loses its special meaning and is matched
2376 literally. A relation pattern that contains two dots (<code class="literal">.</code>)
2377 is interpreted as a database name followed by a schema name pattern followed
2378 by an object name pattern. The database name portion will not be treated as
2379 a pattern and must match the name of the currently connected database, else
2380 an error will be raised.
2382 A schema pattern that contains a dot (<code class="literal">.</code>) is interpreted
2383 as a database name followed by a schema name pattern. For example,
2384 <code class="literal">\dn mydb.*foo*</code> displays all schemas whose schema name
2385 includes <code class="literal">foo</code>. The database name portion will not be
2386 treated as a pattern and must match the name of the currently connected
2387 database, else an error will be raised.
2389 Advanced users can use regular-expression notations such as character
2390 classes, for example <code class="literal">[0-9]</code> to match any digit. All regular
2391 expression special characters work as specified in
2392 <a class="xref" href="functions-matching.html#FUNCTIONS-POSIX-REGEXP" title="9.7.3. POSIX Regular Expressions">Section 9.7.3</a>, except for <code class="literal">.</code> which
2393 is taken as a separator as mentioned above, <code class="literal">*</code> which is
2394 translated to the regular-expression notation <code class="literal">.*</code>,
2395 <code class="literal">?</code> which is translated to <code class="literal">.</code>, and
2396 <code class="literal">$</code> which is matched literally. You can emulate
2397 these pattern characters at need by writing
2398 <code class="literal">?</code> for <code class="literal">.</code>,
2399 <code class="literal">(<em class="replaceable"><code>R</code></em>+|)</code> for
2400 <code class="literal"><em class="replaceable"><code>R</code></em>*</code>, or
2401 <code class="literal">(<em class="replaceable"><code>R</code></em>|)</code> for
2402 <code class="literal"><em class="replaceable"><code>R</code></em>?</code>.
2403 <code class="literal">$</code> is not needed as a regular-expression character since
2404 the pattern must match the whole name, unlike the usual
2405 interpretation of regular expressions (in other words, <code class="literal">$</code>
2406 is automatically appended to your pattern). Write <code class="literal">*</code> at the
2407 beginning and/or end if you don't wish the pattern to be anchored.
2408 Note that within double quotes, all regular expression special characters
2409 lose their special meanings and are matched literally. Also, the regular
2410 expression special characters are matched literally in operator name
2411 patterns (i.e., the argument of <code class="literal">\do</code>).
2412 </p></div></div><div class="refsect2" id="id-1.9.4.21.8.5"><h3>Advanced Features</h3><div class="refsect3" id="APP-PSQL-VARIABLES"><h4>Variables</h4><p>
2413 <span class="application">psql</span> provides variable substitution
2414 features similar to common Unix command shells.
2415 Variables are simply name/value pairs, where the value
2416 can be any string of any length. The name must consist of letters
2417 (including non-Latin letters), digits, and underscores.
2419 To set a variable, use the <span class="application">psql</span> meta-command
2420 <code class="command">\set</code>. For example,
2421 </p><pre class="programlisting">
2422 testdb=> <strong class="userinput"><code>\set foo bar</code></strong>
2424 sets the variable <code class="literal">foo</code> to the value
2425 <code class="literal">bar</code>. To retrieve the content of the variable, precede
2426 the name with a colon, for example:
2427 </p><pre class="programlisting">
2428 testdb=> <strong class="userinput"><code>\echo :foo</code></strong>
2431 This works in both regular SQL commands and meta-commands; there is
2432 more detail in <a class="xref" href="app-psql.html#APP-PSQL-INTERPOLATION" title="SQL Interpolation">SQL Interpolation</a>, below.
2434 If you call <code class="command">\set</code> without a second argument, the
2435 variable is set to an empty-string value. To unset (i.e., delete)
2436 a variable, use the command <code class="command">\unset</code>. To show the
2437 values of all variables, call <code class="command">\set</code> without any argument.
2438 </p><div class="note"><h3 class="title">Note</h3><p>
2439 The arguments of <code class="command">\set</code> are subject to the same
2440 substitution rules as with other commands. Thus you can construct
2441 interesting references such as <code class="literal">\set :foo
2442 'something'</code> and get <span class="quote">“<span class="quote">soft links</span>”</span> or
2443 <span class="quote">“<span class="quote">variable variables</span>”</span> of <span class="productname">Perl</span>
2444 or <span class="productname"><acronym class="acronym">PHP</acronym></span> fame,
2445 respectively. Unfortunately (or fortunately?), there is no way to do
2446 anything useful with these constructs. On the other hand,
2447 <code class="literal">\set bar :foo</code> is a perfectly valid way to copy a
2450 A number of these variables are treated specially
2451 by <span class="application">psql</span>. They represent certain option
2452 settings that can be changed at run time by altering the value of
2453 the variable, or in some cases represent changeable state of
2454 <span class="application">psql</span>.
2455 By convention, all specially treated variables' names
2456 consist of all upper-case ASCII letters (and possibly digits and
2457 underscores). To ensure maximum compatibility in the future, avoid
2458 using such variable names for your own purposes.
2460 Variables that control <span class="application">psql</span>'s behavior
2461 generally cannot be unset or set to invalid values. An <code class="literal">\unset</code>
2462 command is allowed but is interpreted as setting the variable to its
2463 default value. A <code class="literal">\set</code> command without a second argument is
2464 interpreted as setting the variable to <code class="literal">on</code>, for control
2465 variables that accept that value, and is rejected for others. Also,
2466 control variables that accept the values <code class="literal">on</code>
2467 and <code class="literal">off</code> will also accept other common spellings of Boolean
2468 values, such as <code class="literal">true</code> and <code class="literal">false</code>.
2470 The specially treated variables are:
2471 </p><div class="variablelist"><dl class="variablelist"><dt id="APP-PSQL-VARIABLES-AUTOCOMMIT"><span class="term">
2472 <code class="varname">AUTOCOMMIT</code>
2473 <a id="id-1.9.4.21.8.5.2.9.1.1.2" class="indexterm"></a>
2474 </span> <a href="#APP-PSQL-VARIABLES-AUTOCOMMIT" class="id_link">#</a></dt><dd><p>
2475 When <code class="literal">on</code> (the default), each SQL command is automatically
2476 committed upon successful completion. To postpone commit in this
2477 mode, you must enter a <code class="command">BEGIN</code> or <code class="command">START
2478 TRANSACTION</code> SQL command. When <code class="literal">off</code> or unset, SQL
2479 commands are not committed until you explicitly issue
2480 <code class="command">COMMIT</code> or <code class="command">END</code>. The autocommit-off
2481 mode works by issuing an implicit <code class="command">BEGIN</code> for you, just
2482 before any command that is not already in a transaction block and
2483 is not itself a <code class="command">BEGIN</code> or other transaction-control
2484 command, nor a command that cannot be executed inside a transaction
2485 block (such as <code class="command">VACUUM</code>).
2486 </p><div class="note"><h3 class="title">Note</h3><p>
2487 In autocommit-off mode, you must explicitly abandon any failed
2488 transaction by entering <code class="command">ABORT</code> or <code class="command">ROLLBACK</code>.
2489 Also keep in mind that if you exit the session
2490 without committing, your work will be lost.
2491 </p></div><div class="note"><h3 class="title">Note</h3><p>
2492 The autocommit-on mode is <span class="productname">PostgreSQL</span>'s traditional
2493 behavior, but autocommit-off is closer to the SQL spec. If you
2494 prefer autocommit-off, you might wish to set it in the system-wide
2495 <code class="filename">psqlrc</code> file or your
2496 <code class="filename">~/.psqlrc</code> file.
2497 </p></div></dd><dt id="APP-PSQL-VARIABLES-COMP-KEYWORD-CASE"><span class="term"><code class="varname">COMP_KEYWORD_CASE</code></span> <a href="#APP-PSQL-VARIABLES-COMP-KEYWORD-CASE" class="id_link">#</a></dt><dd><p>
2498 Determines which letter case to use when completing an SQL key word.
2499 If set to <code class="literal">lower</code> or <code class="literal">upper</code>, the
2500 completed word will be in lower or upper case, respectively. If set
2501 to <code class="literal">preserve-lower</code>
2502 or <code class="literal">preserve-upper</code> (the default), the completed word
2503 will be in the case of the word already entered, but words being
2504 completed without anything entered will be in lower or upper case,
2506 </p></dd><dt id="APP-PSQL-VARIABLES-DBNAME"><span class="term"><code class="varname">DBNAME</code></span> <a href="#APP-PSQL-VARIABLES-DBNAME" class="id_link">#</a></dt><dd><p>
2507 The name of the database you are currently connected to. This is
2508 set every time you connect to a database (including program
2509 start-up), but can be changed or unset.
2510 </p></dd><dt id="APP-PSQL-VARIABLES-ECHO"><span class="term"><code class="varname">ECHO</code></span> <a href="#APP-PSQL-VARIABLES-ECHO" class="id_link">#</a></dt><dd><p>
2511 If set to <code class="literal">all</code>, all nonempty input lines are printed
2512 to standard output as they are read. (This does not apply to lines
2513 read interactively.) To select this behavior on program
2514 start-up, use the switch <code class="option">-a</code>. If set to
2515 <code class="literal">queries</code>,
2516 <span class="application">psql</span> prints each query to standard output
2517 as it is sent to the server. The switch to select this behavior is
2518 <code class="option">-e</code>. If set to <code class="literal">errors</code>, then only
2519 failed queries are displayed on standard error output. The switch
2520 for this behavior is <code class="option">-b</code>. If set to
2521 <code class="literal">none</code> (the default), then no queries are displayed.
2522 </p></dd><dt id="APP-PSQL-VARIABLES-ECHO-HIDDEN"><span class="term"><code class="varname">ECHO_HIDDEN</code></span> <a href="#APP-PSQL-VARIABLES-ECHO-HIDDEN" class="id_link">#</a></dt><dd><p>
2523 When this variable is set to <code class="literal">on</code> and a backslash command
2524 queries the database, the query is first shown.
2525 This feature helps you to study
2526 <span class="productname">PostgreSQL</span> internals and provide
2527 similar functionality in your own programs. (To select this behavior
2528 on program start-up, use the switch <code class="option">-E</code>.) If you set
2529 this variable to the value <code class="literal">noexec</code>, the queries are
2530 just shown but are not actually sent to the server and executed.
2531 The default value is <code class="literal">off</code>.
2532 </p></dd><dt id="APP-PSQL-VARIABLES-ENCODING"><span class="term"><code class="varname">ENCODING</code></span> <a href="#APP-PSQL-VARIABLES-ENCODING" class="id_link">#</a></dt><dd><p>
2533 The current client character set encoding.
2534 This is set every time you connect to a database (including
2535 program start-up), and when you change the encoding
2536 with <code class="literal">\encoding</code>, but it can be changed or unset.
2537 </p></dd><dt id="APP-PSQL-VARIABLES-ERROR"><span class="term"><code class="varname">ERROR</code></span> <a href="#APP-PSQL-VARIABLES-ERROR" class="id_link">#</a></dt><dd><p>
2538 <code class="literal">true</code> if the last SQL query failed, <code class="literal">false</code> if
2539 it succeeded. See also <code class="varname">SQLSTATE</code>.
2540 </p></dd><dt id="APP-PSQL-VARIABLES-FETCH-COUNT"><span class="term"><code class="varname">FETCH_COUNT</code></span> <a href="#APP-PSQL-VARIABLES-FETCH-COUNT" class="id_link">#</a></dt><dd><p>
2541 If this variable is set to an integer value greater than zero,
2542 the results of <code class="command">SELECT</code> queries are fetched
2543 and displayed in groups of that many rows, rather than the
2544 default behavior of collecting the entire result set before
2545 display. Therefore only a
2546 limited amount of memory is used, regardless of the size of
2547 the result set. Settings of 100 to 1000 are commonly used
2548 when enabling this feature.
2549 Keep in mind that when using this feature, a query might
2550 fail after having already displayed some rows.
2551 </p><div class="tip"><h3 class="title">Tip</h3><p>
2552 Although you can use any output format with this feature,
2553 the default <code class="literal">aligned</code> format tends to look bad
2554 because each group of <code class="varname">FETCH_COUNT</code> rows
2555 will be formatted separately, leading to varying column
2556 widths across the row groups. The other output formats work better.
2557 </p></div></dd><dt id="APP-PSQL-VARIABLES-HIDE-TABLEAM"><span class="term"><code class="varname">HIDE_TABLEAM</code></span> <a href="#APP-PSQL-VARIABLES-HIDE-TABLEAM" class="id_link">#</a></dt><dd><p>
2558 If this variable is set to <code class="literal">true</code>, a table's access
2559 method details are not displayed. This is mainly useful for
2561 </p></dd><dt id="APP-PSQL-VARIABLES-HIDE-TOAST-COMPRESSION"><span class="term"><code class="varname">HIDE_TOAST_COMPRESSION</code></span> <a href="#APP-PSQL-VARIABLES-HIDE-TOAST-COMPRESSION" class="id_link">#</a></dt><dd><p>
2562 If this variable is set to <code class="literal">true</code>, column
2563 compression method details are not displayed. This is mainly
2564 useful for regression tests.
2565 </p></dd><dt id="APP-PSQL-VARIABLES-HISTCONTROL"><span class="term"><code class="varname">HISTCONTROL</code></span> <a href="#APP-PSQL-VARIABLES-HISTCONTROL" class="id_link">#</a></dt><dd><p>
2566 If this variable is set to <code class="literal">ignorespace</code>,
2567 lines which begin with a space are not entered into the history
2568 list. If set to a value of <code class="literal">ignoredups</code>, lines
2569 matching the previous history line are not entered. A value of
2570 <code class="literal">ignoreboth</code> combines the two options. If
2571 set to <code class="literal">none</code> (the default), all lines
2572 read in interactive mode are saved on the history list.
2573 </p><div class="note"><h3 class="title">Note</h3><p>
2574 This feature was shamelessly plagiarized from
2575 <span class="application">Bash</span>.
2576 </p></div></dd><dt id="APP-PSQL-VARIABLES-HISTFILE"><span class="term"><code class="varname">HISTFILE</code></span> <a href="#APP-PSQL-VARIABLES-HISTFILE" class="id_link">#</a></dt><dd><p>
2577 The file name that will be used to store the history list. If unset,
2578 the file name is taken from the <code class="envar">PSQL_HISTORY</code>
2579 environment variable. If that is not set either, the default
2580 is <code class="filename">~/.psql_history</code>,
2581 or <code class="filename">%APPDATA%\postgresql\psql_history</code> on Windows.
2582 For example, putting:
2583 </p><pre class="programlisting">
2584 \set HISTFILE ~/.psql_history-:DBNAME
2586 in <code class="filename">~/.psqlrc</code> will cause
2587 <span class="application">psql</span> to maintain a separate history for
2589 </p><div class="note"><h3 class="title">Note</h3><p>
2590 This feature was shamelessly plagiarized from
2591 <span class="application">Bash</span>.
2592 </p></div></dd><dt id="APP-PSQL-VARIABLES-HISTSIZE"><span class="term"><code class="varname">HISTSIZE</code></span> <a href="#APP-PSQL-VARIABLES-HISTSIZE" class="id_link">#</a></dt><dd><p>
2593 The maximum number of commands to store in the command history
2594 (default 500). If set to a negative value, no limit is applied.
2595 </p><div class="note"><h3 class="title">Note</h3><p>
2596 This feature was shamelessly plagiarized from
2597 <span class="application">Bash</span>.
2598 </p></div></dd><dt id="APP-PSQL-VARIABLES-HOST"><span class="term"><code class="varname">HOST</code></span> <a href="#APP-PSQL-VARIABLES-HOST" class="id_link">#</a></dt><dd><p>
2599 The database server host you are currently connected to. This is
2600 set every time you connect to a database (including program
2601 start-up), but can be changed or unset.
2602 </p></dd><dt id="APP-PSQL-VARIABLES-IGNOREEOF"><span class="term"><code class="varname">IGNOREEOF</code></span> <a href="#APP-PSQL-VARIABLES-IGNOREEOF" class="id_link">#</a></dt><dd><p>
2603 If set to 1 or less, sending an <acronym class="acronym">EOF</acronym> character (usually
2604 <span class="keycap"><strong>Control</strong></span>+<span class="keycap"><strong>D</strong></span>)
2605 to an interactive session of <span class="application">psql</span>
2606 will terminate the application. If set to a larger numeric value,
2607 that many consecutive <acronym class="acronym">EOF</acronym> characters must be typed to
2608 make an interactive session terminate. If the variable is set to a
2609 non-numeric value, it is interpreted as 10. The default is 0.
2610 </p><div class="note"><h3 class="title">Note</h3><p>
2611 This feature was shamelessly plagiarized from
2612 <span class="application">Bash</span>.
2613 </p></div></dd><dt id="APP-PSQL-VARIABLES-LASTOID"><span class="term"><code class="varname">LASTOID</code></span> <a href="#APP-PSQL-VARIABLES-LASTOID" class="id_link">#</a></dt><dd><p>
2614 The value of the last affected OID, as returned from an
2615 <code class="command">INSERT</code> or <code class="command">\lo_import</code>
2616 command. This variable is only guaranteed to be valid until
2617 after the result of the next <acronym class="acronym">SQL</acronym> command has
2619 <span class="productname">PostgreSQL</span> servers since version 12 do not
2620 support OID system columns anymore, thus LASTOID will always be 0
2621 following <code class="command">INSERT</code> when targeting such servers.
2622 </p></dd><dt id="APP-PSQL-VARIABLES-LAST-ERROR-MESSAGE"><span class="term"><code class="varname">LAST_ERROR_MESSAGE</code><br /></span><span class="term"><code class="varname">LAST_ERROR_SQLSTATE</code></span> <a href="#APP-PSQL-VARIABLES-LAST-ERROR-MESSAGE" class="id_link">#</a></dt><dd><p>
2623 The primary error message and associated SQLSTATE code for the most
2624 recent failed query in the current <span class="application">psql</span> session, or
2625 an empty string and <code class="literal">00000</code> if no error has occurred in
2626 the current session.
2627 </p></dd><dt id="APP-PSQL-VARIABLES-ON-ERROR-ROLLBACK"><span class="term">
2628 <code class="varname">ON_ERROR_ROLLBACK</code>
2629 <a id="id-1.9.4.21.8.5.2.9.18.1.2" class="indexterm"></a>
2630 </span> <a href="#APP-PSQL-VARIABLES-ON-ERROR-ROLLBACK" class="id_link">#</a></dt><dd><p>
2631 When set to <code class="literal">on</code>, if a statement in a transaction block
2632 generates an error, the error is ignored and the transaction
2633 continues. When set to <code class="literal">interactive</code>, such errors are only
2634 ignored in interactive sessions, and not when reading script
2635 files. When set to <code class="literal">off</code> (the default), a statement in a
2636 transaction block that generates an error aborts the entire
2637 transaction. The error rollback mode works by issuing an
2638 implicit <code class="command">SAVEPOINT</code> for you, just before each command
2639 that is in a transaction block, and then rolling back to the
2640 savepoint if the command fails.
2641 </p></dd><dt id="APP-PSQL-VARIABLES-ON-ERROR-STOP"><span class="term"><code class="varname">ON_ERROR_STOP</code></span> <a href="#APP-PSQL-VARIABLES-ON-ERROR-STOP" class="id_link">#</a></dt><dd><p>
2642 By default, command processing continues after an error. When this
2643 variable is set to <code class="literal">on</code>, processing will instead stop
2644 immediately. In interactive mode,
2645 <span class="application">psql</span> will return to the command prompt;
2646 otherwise, <span class="application">psql</span> will exit, returning
2647 error code 3 to distinguish this case from fatal error
2648 conditions, which are reported using error code 1. In either case,
2649 any currently running scripts (the top-level script, if any, and any
2650 other scripts which it may have in invoked) will be terminated
2651 immediately. If the top-level command string contained multiple SQL
2652 commands, processing will stop with the current command.
2653 </p></dd><dt id="APP-PSQL-VARIABLES-PIPELINE-COMMAND-COUNT"><span class="term"><code class="varname">PIPELINE_COMMAND_COUNT</code></span> <a href="#APP-PSQL-VARIABLES-PIPELINE-COMMAND-COUNT" class="id_link">#</a></dt><dd><p>
2654 The number of commands queued in an ongoing pipeline.
2655 </p></dd><dt id="APP-PSQL-VARIABLES-PIPELINE-RESULT-COUNT"><span class="term"><code class="varname">PIPELINE_RESULT_COUNT</code></span> <a href="#APP-PSQL-VARIABLES-PIPELINE-RESULT-COUNT" class="id_link">#</a></dt><dd><p>
2656 The number of commands of an ongoing pipeline that were followed
2657 by either a <code class="command">\flushrequest</code> or a
2658 <code class="command">\syncpipeline</code>, forcing the server to send the
2659 results. These results can be retrieved with
2660 <code class="command">\getresults</code>.
2661 </p></dd><dt id="APP-PSQL-VARIABLES-PIPELINE-SYNC-COUNT"><span class="term"><code class="varname">PIPELINE_SYNC_COUNT</code></span> <a href="#APP-PSQL-VARIABLES-PIPELINE-SYNC-COUNT" class="id_link">#</a></dt><dd><p>
2662 The number of sync messages queued in an ongoing pipeline.
2663 </p></dd><dt id="APP-PSQL-VARIABLES-PORT"><span class="term"><code class="varname">PORT</code></span> <a href="#APP-PSQL-VARIABLES-PORT" class="id_link">#</a></dt><dd><p>
2664 The database server port to which you are currently connected.
2665 This is set every time you connect to a database (including
2666 program start-up), but can be changed or unset.
2667 </p></dd><dt id="APP-PSQL-VARIABLES-PROMPT"><span class="term"><code class="varname">PROMPT1</code><br /></span><span class="term"><code class="varname">PROMPT2</code><br /></span><span class="term"><code class="varname">PROMPT3</code></span> <a href="#APP-PSQL-VARIABLES-PROMPT" class="id_link">#</a></dt><dd><p>
2668 These specify what the prompts <span class="application">psql</span>
2669 issues should look like. See <a class="xref" href="app-psql.html#APP-PSQL-PROMPTING" title="Prompting">Prompting</a> below.
2670 </p></dd><dt id="APP-PSQL-VARIABLES-QUIET"><span class="term"><code class="varname">QUIET</code></span> <a href="#APP-PSQL-VARIABLES-QUIET" class="id_link">#</a></dt><dd><p>
2671 Setting this variable to <code class="literal">on</code> is equivalent to the command
2672 line option <code class="option">-q</code>. It is probably not too useful in
2674 </p></dd><dt id="APP-PSQL-VARIABLES-ROW-COUNT"><span class="term"><code class="varname">ROW_COUNT</code></span> <a href="#APP-PSQL-VARIABLES-ROW-COUNT" class="id_link">#</a></dt><dd><p>
2675 The number of rows returned or affected by the last SQL query, or 0
2676 if the query failed or did not report a row count.
2677 </p></dd><dt id="APP-PSQL-VARIABLES-SERVER-VERSION-NAME"><span class="term"><code class="varname">SERVER_VERSION_NAME</code><br /></span><span class="term"><code class="varname">SERVER_VERSION_NUM</code></span> <a href="#APP-PSQL-VARIABLES-SERVER-VERSION-NAME" class="id_link">#</a></dt><dd><p>
2678 The server's version number as a string, for
2679 example <code class="literal">9.6.2</code>, <code class="literal">10.1</code> or <code class="literal">11beta1</code>,
2680 and in numeric form, for
2681 example <code class="literal">90602</code> or <code class="literal">100001</code>.
2682 These are set every time you connect to a database
2683 (including program start-up), but can be changed or unset.
2684 </p></dd><dt id="APP-PSQL-VARIABLES-SERVICE"><span class="term"><code class="varname">SERVICE</code></span> <a href="#APP-PSQL-VARIABLES-SERVICE" class="id_link">#</a></dt><dd><p>
2685 The service name, if applicable.
2686 </p></dd><dt id="APP-PSQL-VARIABLES-SHELL-ERROR"><span class="term"><code class="varname">SHELL_ERROR</code></span> <a href="#APP-PSQL-VARIABLES-SHELL-ERROR" class="id_link">#</a></dt><dd><p>
2687 <code class="literal">true</code> if the last shell command
2688 failed, <code class="literal">false</code> if it succeeded.
2689 This applies to shell commands invoked via the <code class="literal">\!</code>,
2690 <code class="literal">\g</code>, <code class="literal">\o</code>, <code class="literal">\w</code>,
2691 and <code class="literal">\copy</code> meta-commands, as well as backquote
2692 (<code class="literal">`</code>) expansion. Note that
2693 for <code class="literal">\o</code>, this variable is updated when the output
2694 pipe is closed by the next <code class="literal">\o</code> command.
2695 See also <code class="varname">SHELL_EXIT_CODE</code>.
2696 </p></dd><dt id="APP-PSQL-VARIABLES-SHELL-EXIT-CODE"><span class="term"><code class="varname">SHELL_EXIT_CODE</code></span> <a href="#APP-PSQL-VARIABLES-SHELL-EXIT-CODE" class="id_link">#</a></dt><dd><p>
2697 The exit status returned by the last shell command.
2698 0–127 represent program exit codes, 128–255
2699 indicate termination by a signal, and -1 indicates failure
2700 to launch a program or to collect its exit status.
2701 This applies to shell commands invoked via the <code class="literal">\!</code>,
2702 <code class="literal">\g</code>, <code class="literal">\o</code>, <code class="literal">\w</code>,
2703 and <code class="literal">\copy</code> meta-commands, as well as backquote
2704 (<code class="literal">`</code>) expansion. Note that
2705 for <code class="literal">\o</code>, this variable is updated when the output
2706 pipe is closed by the next <code class="literal">\o</code> command.
2707 See also <code class="varname">SHELL_ERROR</code>.
2708 </p></dd><dt id="APP-PSQL-VARIABLES-SHOW-ALL-RESULTS"><span class="term"><code class="varname">SHOW_ALL_RESULTS</code></span> <a href="#APP-PSQL-VARIABLES-SHOW-ALL-RESULTS" class="id_link">#</a></dt><dd><p>
2709 When this variable is set to <code class="literal">off</code>, only the last
2710 result of a combined query (<code class="literal">\;</code>) is shown instead of
2711 all of them. The default is <code class="literal">on</code>. The off behavior
2712 is for compatibility with older versions of
2713 <span class="application">psql</span>.
2714 </p></dd><dt id="APP-PSQL-VARIABLES-SHOW-CONTEXT"><span class="term"><code class="varname">SHOW_CONTEXT</code></span> <a href="#APP-PSQL-VARIABLES-SHOW-CONTEXT" class="id_link">#</a></dt><dd><p>
2715 This variable can be set to the
2716 values <code class="literal">never</code>, <code class="literal">errors</code>, or <code class="literal">always</code>
2717 to control whether <code class="literal">CONTEXT</code> fields are displayed in
2718 messages from the server. The default is <code class="literal">errors</code> (meaning
2719 that context will be shown in error messages, but not in notice or
2720 warning messages). This setting has no effect
2721 when <code class="varname">VERBOSITY</code> is set to <code class="literal">terse</code>
2722 or <code class="literal">sqlstate</code>.
2723 (See also <code class="command">\errverbose</code>, for use when you want a verbose
2724 version of the error you just got.)
2725 </p></dd><dt id="APP-PSQL-VARIABLES-SINGLELINE"><span class="term"><code class="varname">SINGLELINE</code></span> <a href="#APP-PSQL-VARIABLES-SINGLELINE" class="id_link">#</a></dt><dd><p>
2726 Setting this variable to <code class="literal">on</code> is equivalent to the command
2727 line option <code class="option">-S</code>.
2728 </p></dd><dt id="APP-PSQL-VARIABLES-SINGLESTEP"><span class="term"><code class="varname">SINGLESTEP</code></span> <a href="#APP-PSQL-VARIABLES-SINGLESTEP" class="id_link">#</a></dt><dd><p>
2729 Setting this variable to <code class="literal">on</code> is equivalent to the command
2730 line option <code class="option">-s</code>.
2731 </p></dd><dt id="APP-PSQL-VARIABLES-SQLSTATE"><span class="term"><code class="varname">SQLSTATE</code></span> <a href="#APP-PSQL-VARIABLES-SQLSTATE" class="id_link">#</a></dt><dd><p>
2732 The error code (see <a class="xref" href="errcodes-appendix.html" title="Appendix A. PostgreSQL Error Codes">Appendix A</a>) associated
2733 with the last SQL query's failure, or <code class="literal">00000</code> if it
2735 </p></dd><dt id="APP-PSQL-VARIABLES-USER"><span class="term"><code class="varname">USER</code></span> <a href="#APP-PSQL-VARIABLES-USER" class="id_link">#</a></dt><dd><p>
2736 The database user you are currently connected as. This is set
2737 every time you connect to a database (including program
2738 start-up), but can be changed or unset.
2739 </p></dd><dt id="APP-PSQL-VARIABLES-VERBOSITY"><span class="term"><code class="varname">VERBOSITY</code></span> <a href="#APP-PSQL-VARIABLES-VERBOSITY" class="id_link">#</a></dt><dd><p>
2740 This variable can be set to the values <code class="literal">default</code>,
2741 <code class="literal">verbose</code>, <code class="literal">terse</code>,
2742 or <code class="literal">sqlstate</code> to control the verbosity of error
2744 (See also <code class="command">\errverbose</code>, for use when you want a verbose
2745 version of the error you just got.)
2746 </p></dd><dt id="APP-PSQL-VARIABLES-VERSION"><span class="term"><code class="varname">VERSION</code><br /></span><span class="term"><code class="varname">VERSION_NAME</code><br /></span><span class="term"><code class="varname">VERSION_NUM</code></span> <a href="#APP-PSQL-VARIABLES-VERSION" class="id_link">#</a></dt><dd><p>
2747 These variables are set at program start-up to reflect
2748 <span class="application">psql</span>'s version, respectively as a verbose string,
2749 a short string (e.g., <code class="literal">9.6.2</code>, <code class="literal">10.1</code>,
2750 or <code class="literal">11beta1</code>), and a number (e.g., <code class="literal">90602</code>
2751 or <code class="literal">100001</code>). They can be changed or unset.
2752 </p></dd><dt id="APP-PSQL-VARIABLES-WATCH-INTERVAL"><span class="term"><code class="varname">WATCH_INTERVAL</code></span> <a href="#APP-PSQL-VARIABLES-WATCH-INTERVAL" class="id_link">#</a></dt><dd><p>
2753 This variable sets the default interval, in seconds, which
2754 <code class="command">\watch</code> waits between executing the query. The
2755 default is 2 seconds. Specifying an interval in the command overrides
2757 </p></dd></dl></div></div><div class="refsect3" id="APP-PSQL-INTERPOLATION"><h4><acronym class="acronym">SQL</acronym> Interpolation</h4><p>
2758 A key feature of <span class="application">psql</span>
2759 variables is that you can substitute (<span class="quote">“<span class="quote">interpolate</span>”</span>)
2760 them into regular <acronym class="acronym">SQL</acronym> statements, as well as the
2761 arguments of meta-commands. Furthermore,
2762 <span class="application">psql</span> provides facilities for
2763 ensuring that variable values used as SQL literals and identifiers are
2764 properly quoted. The syntax for interpolating a value without
2765 any quoting is to prepend the variable name with a colon
2766 (<code class="literal">:</code>). For example,
2767 </p><pre class="programlisting">
2768 testdb=> <strong class="userinput"><code>\set foo 'my_table'</code></strong>
2769 testdb=> <strong class="userinput"><code>SELECT * FROM :foo;</code></strong>
2771 would query the table <code class="literal">my_table</code>. Note that this
2772 may be unsafe: the value of the variable is copied literally, so it can
2773 contain unbalanced quotes, or even backslash commands. You must make sure
2774 that it makes sense where you put it.
2776 When a value is to be used as an SQL literal or identifier, it is
2777 safest to arrange for it to be quoted. To quote the value of
2778 a variable as an SQL literal, write a colon followed by the variable
2779 name in single quotes. To quote the value as an SQL identifier, write
2780 a colon followed by the variable name in double quotes.
2781 These constructs deal correctly with quotes and other special
2782 characters embedded within the variable value.
2783 The previous example would be more safely written this way:
2784 </p><pre class="programlisting">
2785 testdb=> <strong class="userinput"><code>\set foo 'my_table'</code></strong>
2786 testdb=> <strong class="userinput"><code>SELECT * FROM :"foo";</code></strong>
2789 Variable interpolation will not be performed within quoted
2790 <acronym class="acronym">SQL</acronym> literals and identifiers. Therefore, a
2791 construction such as <code class="literal">':foo'</code> doesn't work to produce a quoted
2792 literal from a variable's value (and it would be unsafe if it did work,
2793 since it wouldn't correctly handle quotes embedded in the value).
2795 One example use of this mechanism is to
2796 copy the contents of a file into a table column.
2797 First load the file into a variable and then interpolate the variable's
2798 value as a quoted string:
2799 </p><pre class="programlisting">
2800 testdb=> <strong class="userinput"><code>\set content `cat my_file.txt`</code></strong>
2801 testdb=> <strong class="userinput"><code>INSERT INTO my_table VALUES (:'content');</code></strong>
2803 (Note that this still won't work if <code class="filename">my_file.txt</code> contains NUL bytes.
2804 <span class="application">psql</span> does not support embedded NUL bytes in variable values.)
2806 Since colons can legally appear in SQL commands, an apparent attempt
2807 at interpolation (that is, <code class="literal">:name</code>,
2808 <code class="literal">:'name'</code>, or <code class="literal">:"name"</code>) is not
2809 replaced unless the named variable is currently set. In any case, you
2810 can escape a colon with a backslash to protect it from substitution.
2812 The <code class="literal">:{?<em class="replaceable"><code>name</code></em>}</code> special syntax returns TRUE
2813 or FALSE depending on whether the variable exists or not, and is thus
2814 always substituted, unless the colon is backslash-escaped.
2816 The colon syntax for variables is standard <acronym class="acronym">SQL</acronym> for
2817 embedded query languages, such as <span class="application">ECPG</span>.
2818 The colon syntaxes for array slices and type casts are
2819 <span class="productname">PostgreSQL</span> extensions, which can sometimes
2820 conflict with the standard usage. The colon-quote syntax for escaping a
2821 variable's value as an SQL literal or identifier is a
2822 <span class="application">psql</span> extension.
2823 </p></div><div class="refsect3" id="APP-PSQL-PROMPTING"><h4>Prompting</h4><p>
2824 The prompts <span class="application">psql</span> issues can be customized
2825 to your preference. The three variables <code class="varname">PROMPT1</code>,
2826 <code class="varname">PROMPT2</code>, and <code class="varname">PROMPT3</code> contain strings
2827 and special escape sequences that describe the appearance of the
2828 prompt. Prompt 1 is the normal prompt that is issued when
2829 <span class="application">psql</span> requests a new command. Prompt 2 is
2830 issued when more input is expected during command entry, for example
2831 because the command was not terminated with a semicolon or a quote
2833 Prompt 3 is issued when you are running an <acronym class="acronym">SQL</acronym>
2834 <code class="command">COPY FROM STDIN</code> command and you need to type in
2835 a row value on the terminal.
2837 The value of the selected prompt variable is printed literally,
2838 except where a percent sign (<code class="literal">%</code>) is encountered.
2839 Depending on the next character, certain other text is substituted
2840 instead. Defined substitutions are:
2842 </p><div class="variablelist"><dl class="variablelist"><dt id="APP-PSQL-PROMPTING-M-UC"><span class="term"><code class="literal">%M</code></span> <a href="#APP-PSQL-PROMPTING-M-UC" class="id_link">#</a></dt><dd><p>
2843 The full host name (with domain name) of the database server,
2844 or <code class="literal">[local]</code> if the connection is over a Unix
2846 <code class="literal">[local:<em class="replaceable"><code>/dir/name</code></em>]</code>,
2847 if the Unix domain socket is not at the compiled in default
2849 </p></dd><dt id="APP-PSQL-PROMPTING-M-LC"><span class="term"><code class="literal">%m</code></span> <a href="#APP-PSQL-PROMPTING-M-LC" class="id_link">#</a></dt><dd><p>
2850 The host name of the database server, truncated at the
2851 first dot, or <code class="literal">[local]</code> if the connection is
2852 over a Unix domain socket.
2853 </p></dd><dt id="APP-PSQL-PROMPTING-GT"><span class="term"><code class="literal">%></code></span> <a href="#APP-PSQL-PROMPTING-GT" class="id_link">#</a></dt><dd><p>The port number at which the database server is listening.</p></dd><dt id="APP-PSQL-PROMPTING-N"><span class="term"><code class="literal">%n</code></span> <a href="#APP-PSQL-PROMPTING-N" class="id_link">#</a></dt><dd><p>
2854 The database session user name. (The expansion of this
2855 value might change during a database session as the result
2856 of the command <code class="command">SET SESSION
2857 AUTHORIZATION</code>.)
2858 </p></dd><dt id="APP-PSQL-PROMPTING-S"><span class="term"><code class="literal">%s</code></span> <a href="#APP-PSQL-PROMPTING-S" class="id_link">#</a></dt><dd><p>The name of the service.</p></dd><dt id="APP-PSQL-PROMPTING-SLASH"><span class="term"><code class="literal">%/</code></span> <a href="#APP-PSQL-PROMPTING-SLASH" class="id_link">#</a></dt><dd><p>The name of the current database.</p></dd><dt id="APP-PSQL-PROMPTING-TILDE"><span class="term"><code class="literal">%~</code></span> <a href="#APP-PSQL-PROMPTING-TILDE" class="id_link">#</a></dt><dd><p>Like <code class="literal">%/</code>, but the output is <code class="literal">~</code>
2859 (tilde) if the database is your default database.</p></dd><dt id="APP-PSQL-PROMPTING-NUMBERSIGN"><span class="term"><code class="literal">%#</code></span> <a href="#APP-PSQL-PROMPTING-NUMBERSIGN" class="id_link">#</a></dt><dd><p>
2860 If the session user is a database superuser, then a
2861 <code class="literal">#</code>, otherwise a <code class="literal">></code>.
2862 (The expansion of this value might change during a database
2863 session as the result of the command <code class="command">SET SESSION
2864 AUTHORIZATION</code>.)
2865 </p></dd><dt id="APP-PSQL-PROMPTING-P"><span class="term"><code class="literal">%p</code></span> <a href="#APP-PSQL-PROMPTING-P" class="id_link">#</a></dt><dd><p>The process ID of the backend currently connected to.</p></dd><dt id="APP-PSQL-PROMPTING-P-UC"><span class="term"><code class="literal">%P</code></span> <a href="#APP-PSQL-PROMPTING-P-UC" class="id_link">#</a></dt><dd><p>
2866 Pipeline status: <code class="literal">off</code> when not in a pipeline,
2867 <code class="literal">on</code> when in an ongoing pipeline or
2868 <code class="literal">abort</code> when in an aborted pipeline.
2869 </p></dd><dt id="APP-PSQL-PROMPTING-R"><span class="term"><code class="literal">%R</code></span> <a href="#APP-PSQL-PROMPTING-R" class="id_link">#</a></dt><dd><p>
2870 In prompt 1 normally <code class="literal">=</code>,
2871 but <code class="literal">@</code> if the session is in an inactive branch of a
2872 conditional block, or <code class="literal">^</code> if in single-line mode,
2873 or <code class="literal">!</code> if the session is disconnected from the
2874 database (which can happen if <code class="command">\connect</code> fails).
2875 In prompt 2 <code class="literal">%R</code> is replaced by a character that
2876 depends on why <span class="application">psql</span> expects more input:
2877 <code class="literal">-</code> if the command simply wasn't terminated yet,
2878 but <code class="literal">*</code> if there is an unfinished
2879 <code class="literal">/* ... */</code> comment,
2880 a single quote if there is an unfinished quoted string,
2881 a double quote if there is an unfinished quoted identifier,
2882 a dollar sign if there is an unfinished dollar-quoted string,
2883 or <code class="literal">(</code> if there is an unmatched left parenthesis.
2884 In prompt 3 <code class="literal">%R</code> doesn't produce anything.
2885 </p></dd><dt id="APP-PSQL-PROMPTING-X"><span class="term"><code class="literal">%x</code></span> <a href="#APP-PSQL-PROMPTING-X" class="id_link">#</a></dt><dd><p>
2886 Transaction status: an empty string when not in a transaction
2887 block, or <code class="literal">*</code> when in a transaction block, or
2888 <code class="literal">!</code> when in a failed transaction block, or <code class="literal">?</code>
2889 when the transaction state is indeterminate (for example, because
2890 there is no connection).
2891 </p></dd><dt id="APP-PSQL-PROMPTING-L"><span class="term"><code class="literal">%l</code></span> <a href="#APP-PSQL-PROMPTING-L" class="id_link">#</a></dt><dd><p>
2892 The line number inside the current statement, starting from <code class="literal">1</code>.
2893 </p></dd><dt id="APP-PSQL-PROMPTING-DIGITS"><span class="term"><code class="literal">%</code><em class="replaceable"><code>digits</code></em></span> <a href="#APP-PSQL-PROMPTING-DIGITS" class="id_link">#</a></dt><dd><p>
2894 The character with the indicated octal code is substituted.
2895 </p></dd><dt id="APP-PSQL-PROMPTING-NAME"><span class="term"><code class="literal">%:</code><em class="replaceable"><code>name</code></em><code class="literal">:</code></span> <a href="#APP-PSQL-PROMPTING-NAME" class="id_link">#</a></dt><dd><p>
2896 The value of the <span class="application">psql</span> variable
2897 <em class="replaceable"><code>name</code></em>. See
2898 <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a>, above, for details.
2899 </p></dd><dt id="APP-PSQL-PROMPTING-COMMAND"><span class="term"><code class="literal">%`</code><em class="replaceable"><code>command</code></em><code class="literal">`</code></span> <a href="#APP-PSQL-PROMPTING-COMMAND" class="id_link">#</a></dt><dd><p>
2900 The output of <em class="replaceable"><code>command</code></em>, similar to ordinary
2901 <span class="quote">“<span class="quote">back-tick</span>”</span> substitution.
2902 </p></dd><dt id="APP-PSQL-PROMPTING-SQUARE-BRACKETS"><span class="term"><code class="literal">%[</code> ... <code class="literal">%]</code></span> <a href="#APP-PSQL-PROMPTING-SQUARE-BRACKETS" class="id_link">#</a></dt><dd><p>
2903 Prompts can contain terminal control characters which, for
2904 example, change the color, background, or style of the prompt
2905 text, or change the title of the terminal window. In order for
2906 the line editing features of <span class="application">Readline</span> to work properly, these
2907 non-printing control characters must be designated as invisible
2908 by surrounding them with <code class="literal">%[</code> and
2909 <code class="literal">%]</code>. Multiple pairs of these can occur within
2910 the prompt. For example:
2911 </p><pre class="programlisting">
2912 testdb=> \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '
2914 results in a boldfaced (<code class="literal">1;</code>) yellow-on-black
2915 (<code class="literal">33;40</code>) prompt on VT100-compatible, color-capable
2917 </p></dd><dt id="APP-PSQL-PROMPTING-W"><span class="term"><code class="literal">%w</code></span> <a href="#APP-PSQL-PROMPTING-W" class="id_link">#</a></dt><dd><p>
2918 Whitespace of the same width as the most recent output of
2919 <code class="varname">PROMPT1</code>. This can be used as a
2920 <code class="varname">PROMPT2</code> setting, so that multi-line statements are
2921 aligned with the first line, but there is no visible secondary prompt.
2922 </p></dd></dl></div><p>
2924 To insert a percent sign into your prompt, write
2925 <code class="literal">%%</code>. The default prompts are
2926 <code class="literal">'%/%R%x%# '</code> for prompts 1 and 2, and
2927 <code class="literal">'>> '</code> for prompt 3.
2928 </p><div class="note"><h3 class="title">Note</h3><p>
2929 This feature was shamelessly plagiarized from
2930 <span class="application">tcsh</span>.
2931 </p></div></div><div class="refsect3" id="APP-PSQL-READLINE"><h4>Command-Line Editing</h4><a id="id-1.9.4.21.8.5.5.2" class="indexterm"></a><a id="id-1.9.4.21.8.5.5.3" class="indexterm"></a><p>
2932 <span class="application">psql</span> uses
2933 the <span class="application">Readline</span>
2934 or <span class="application">libedit</span> library, if available, for
2935 convenient line editing and retrieval. The command history is
2936 automatically saved when <span class="application">psql</span> exits and is
2937 reloaded when <span class="application">psql</span> starts up. Type
2938 up-arrow or control-P to retrieve previous lines.
2940 You can also use tab completion to fill in partially-typed keywords
2941 and SQL object names in many (by no means all) contexts. For example,
2942 at the start of a command, typing <code class="literal">ins</code> and pressing
2943 TAB will fill in <code class="literal">insert into </code>. Then, typing a few
2944 characters of a table or schema name and pressing <code class="literal">TAB</code>
2945 will fill in the unfinished name, or offer a menu of possible completions
2946 when there's more than one. (Depending on the library in use, you may need to
2947 press <code class="literal">TAB</code> more than once to get a menu.)
2949 Tab completion for SQL object names requires sending queries to the
2950 server to find possible matches. In some contexts this can interfere
2951 with other operations. For example, after <code class="command">BEGIN</code>
2952 it will be too late to issue <code class="command">SET TRANSACTION ISOLATION
2953 LEVEL</code> if a tab-completion query is issued in between.
2954 If you do not want tab completion at all, you
2955 can turn it off permanently by putting this in a file named
2956 <code class="filename">.inputrc</code> in your home directory:
2957 </p><pre class="programlisting">
2959 set disable-completion on
2962 (This is not a <span class="application">psql</span> but a
2963 <span class="application">Readline</span> feature. Read its documentation
2964 for further details.)
2966 The <code class="option">-n</code> (<code class="option">--no-readline</code>) command line
2967 option can also be useful to disable use
2968 of <span class="application">Readline</span> for a single run
2969 of <span class="application">psql</span>. This prevents tab completion,
2970 use or recording of command line history, and editing of multi-line
2971 commands. It is particularly useful when you need to copy-and-paste
2972 text that contains <code class="literal">TAB</code> characters.
2973 </p></div></div></div><div class="refsect1" id="APP-PSQL-ENVIRONMENT"><h2>Environment</h2><div class="variablelist"><dl class="variablelist"><dt id="APP-PSQL-ENVIRONMENT-COLUMNS"><span class="term"><code class="envar">COLUMNS</code></span> <a href="#APP-PSQL-ENVIRONMENT-COLUMNS" class="id_link">#</a></dt><dd><p>
2974 If <code class="literal">\pset columns</code> is zero, controls the
2975 width for the <code class="literal">wrapped</code> format and width for determining
2976 if wide output requires the pager or should be switched to the
2977 vertical format in expanded auto mode.
2978 </p></dd><dt id="APP-PSQL-ENVIRONMENT-PGDATABASE"><span class="term"><code class="envar">PGDATABASE</code><br /></span><span class="term"><code class="envar">PGHOST</code><br /></span><span class="term"><code class="envar">PGPORT</code><br /></span><span class="term"><code class="envar">PGUSER</code></span> <a href="#APP-PSQL-ENVIRONMENT-PGDATABASE" class="id_link">#</a></dt><dd><p>
2979 Default connection parameters (see <a class="xref" href="libpq-envars.html" title="32.15. Environment Variables">Section 32.15</a>).
2980 </p></dd><dt id="APP-PSQL-ENVIRONMENT-PG-COLOR"><span class="term"><code class="envar">PG_COLOR</code></span> <a href="#APP-PSQL-ENVIRONMENT-PG-COLOR" class="id_link">#</a></dt><dd><p>
2981 Specifies whether to use color in diagnostic messages. Possible values
2982 are <code class="literal">always</code>, <code class="literal">auto</code> and
2983 <code class="literal">never</code>.
2984 </p></dd><dt id="APP-PSQL-ENVIRONMENT-PSQL-EDITOR"><span class="term"><code class="envar">PSQL_EDITOR</code><br /></span><span class="term"><code class="envar">EDITOR</code><br /></span><span class="term"><code class="envar">VISUAL</code></span> <a href="#APP-PSQL-ENVIRONMENT-PSQL-EDITOR" class="id_link">#</a></dt><dd><p>
2985 Editor used by the <code class="command">\e</code>, <code class="command">\ef</code>,
2986 and <code class="command">\ev</code> commands.
2987 These variables are examined in the order listed;
2988 the first that is set is used.
2989 If none of them is set, the default is to use <code class="filename">vi</code>
2990 on Unix systems or <code class="filename">notepad.exe</code> on Windows systems.
2991 </p></dd><dt id="APP-PSQL-ENVIRONMENT-PSQL-EDITOR-LINENUMBER-ARG"><span class="term"><code class="envar">PSQL_EDITOR_LINENUMBER_ARG</code></span> <a href="#APP-PSQL-ENVIRONMENT-PSQL-EDITOR-LINENUMBER-ARG" class="id_link">#</a></dt><dd><p>
2992 When <code class="command">\e</code>, <code class="command">\ef</code>, or
2993 <code class="command">\ev</code> is used
2994 with a line number argument, this variable specifies the
2995 command-line argument used to pass the starting line number to
2996 the user's editor. For editors such as <span class="productname">Emacs</span> or
2997 <span class="productname">vi</span>, this is a plus sign. Include a trailing
2998 space in the value of the variable if there needs to be space
2999 between the option name and the line number. Examples:
3000 </p><pre class="programlisting">
3001 PSQL_EDITOR_LINENUMBER_ARG='+'
3002 PSQL_EDITOR_LINENUMBER_ARG='--line '
3005 The default is <code class="literal">+</code> on Unix systems
3006 (corresponding to the default editor <code class="filename">vi</code>,
3007 and useful for many other common editors); but there is no
3008 default on Windows systems.
3009 </p></dd><dt id="APP-PSQL-ENVIRONMENT-PSQL-HISTORY"><span class="term"><code class="envar">PSQL_HISTORY</code></span> <a href="#APP-PSQL-ENVIRONMENT-PSQL-HISTORY" class="id_link">#</a></dt><dd><p>
3010 Alternative location for the command history file. Tilde (<code class="literal">~</code>) expansion is performed.
3011 </p></dd><dt id="APP-PSQL-ENVIRONMENT-PAGER"><span class="term"><code class="envar">PSQL_PAGER</code><br /></span><span class="term"><code class="envar">PAGER</code></span> <a href="#APP-PSQL-ENVIRONMENT-PAGER" class="id_link">#</a></dt><dd><p>
3012 If a query's results do not fit on the screen, they are piped
3013 through this command. Typical values are <code class="literal">more</code>
3014 or <code class="literal">less</code>.
3015 Use of the pager can be disabled by setting <code class="envar">PSQL_PAGER</code>
3016 or <code class="envar">PAGER</code> to an empty string, or by adjusting the
3017 pager-related options of the <code class="command">\pset</code> command.
3018 These variables are examined in the order listed;
3019 the first that is set is used.
3020 If neither of them is set, the default is to use <code class="literal">more</code> on most
3021 platforms, but <code class="literal">less</code> on Cygwin.
3022 </p></dd><dt id="APP-PSQL-ENVIRONMENT-PSQL-WATCH-PAGER"><span class="term"><code class="envar">PSQL_WATCH_PAGER</code></span> <a href="#APP-PSQL-ENVIRONMENT-PSQL-WATCH-PAGER" class="id_link">#</a></dt><dd><p>
3023 When a query is executed repeatedly with the <code class="command">\watch</code>
3024 command, a pager is not used by default. This behavior can be changed
3025 by setting <code class="envar">PSQL_WATCH_PAGER</code> to a pager command, on Unix
3026 systems. The <code class="literal">pspg</code> pager (not part of
3027 <span class="productname">PostgreSQL</span> but available in many open source
3028 software distributions) can display the output of
3029 <code class="command">\watch</code> if started with the option
3030 <code class="literal">--stream</code>.
3031 </p></dd><dt id="APP-PSQL-ENVIRONMENT-PSQLRC"><span class="term"><code class="envar">PSQLRC</code></span> <a href="#APP-PSQL-ENVIRONMENT-PSQLRC" class="id_link">#</a></dt><dd><p>
3032 Alternative location of the user's <code class="filename">.psqlrc</code> file. Tilde (<code class="literal">~</code>) expansion is performed.
3033 </p></dd><dt id="APP-PSQL-ENVIRONMENT-SHELL"><span class="term"><code class="envar">SHELL</code></span> <a href="#APP-PSQL-ENVIRONMENT-SHELL" class="id_link">#</a></dt><dd><p>
3034 Command executed by the <code class="command">\!</code> command.
3035 </p></dd><dt id="APP-PSQL-ENVIRONMENT-TMPDIR"><span class="term"><code class="envar">TMPDIR</code></span> <a href="#APP-PSQL-ENVIRONMENT-TMPDIR" class="id_link">#</a></dt><dd><p>
3036 Directory for storing temporary files. The default is
3037 <code class="filename">/tmp</code>.
3038 </p></dd></dl></div><p>
3039 This utility, like most other <span class="productname">PostgreSQL</span> utilities,
3040 also uses the environment variables supported by <span class="application">libpq</span>
3041 (see <a class="xref" href="libpq-envars.html" title="32.15. Environment Variables">Section 32.15</a>).
3042 </p></div><div class="refsect1" id="id-1.9.4.21.10"><h2>Files</h2><div class="variablelist"><dl class="variablelist"><dt id="APP-PSQL-FILES-PSQLRC"><span class="term"><code class="filename">psqlrc</code> and <code class="filename">~/.psqlrc</code></span> <a href="#APP-PSQL-FILES-PSQLRC" class="id_link">#</a></dt><dd><p>
3043 Unless it is passed an <code class="option">-X</code> option,
3044 <span class="application">psql</span> attempts to read and execute commands
3045 from the system-wide startup file (<code class="filename">psqlrc</code>) and then
3046 the user's personal startup file (<code class="filename">~/.psqlrc</code>), after
3047 connecting to the database but before accepting normal commands.
3048 These files can be used to set up the client and/or the server to taste,
3049 typically with <code class="command">\set</code> and <code class="command">SET</code>
3052 The system-wide startup file is named <code class="filename">psqlrc</code>.
3054 sought in the installation's <span class="quote">“<span class="quote">system configuration</span>”</span> directory,
3055 which is most reliably identified by running <code class="literal">pg_config
3056 --sysconfdir</code>.
3057 Typically this directory will be <code class="filename">../etc/</code>
3058 relative to the directory containing
3059 the <span class="productname">PostgreSQL</span> executables.
3060 The directory to look in can be set explicitly via
3061 the <code class="envar">PGSYSCONFDIR</code> environment variable.
3063 The user's personal startup file is named <code class="filename">.psqlrc</code>
3064 and is sought in the invoking user's home directory.
3065 On Windows the personal startup file is instead named
3066 <code class="filename">%APPDATA%\postgresql\psqlrc.conf</code>.
3067 In either case, this default file path can be overridden by setting
3068 the <code class="envar">PSQLRC</code> environment variable.
3070 Both the system-wide startup file and the user's personal startup file
3071 can be made <span class="application">psql</span>-version-specific
3072 by appending a dash and the <span class="productname">PostgreSQL</span>
3073 major or minor release identifier to the file name,
3074 for example <code class="filename">~/.psqlrc-18</code> or
3075 <code class="filename">~/.psqlrc-18.0</code>.
3076 The most specific version-matching file will be read in preference
3077 to a non-version-specific file.
3078 These version suffixes are added after determining the file path
3080 </p></dd><dt id="APP-PSQL-FILES-PSQL-HISTORY"><span class="term"><code class="filename">.psql_history</code></span> <a href="#APP-PSQL-FILES-PSQL-HISTORY" class="id_link">#</a></dt><dd><p>
3081 The command-line history is stored in the file
3082 <code class="filename">~/.psql_history</code>, or
3083 <code class="filename">%APPDATA%\postgresql\psql_history</code> on Windows.
3085 The location of the history file can be set explicitly via
3086 the <code class="varname">HISTFILE</code> <span class="application">psql</span> variable or
3087 the <code class="envar">PSQL_HISTORY</code> environment variable.
3088 </p></dd></dl></div></div><div class="refsect1" id="id-1.9.4.21.11"><h2>Notes</h2><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><span class="application">psql</span> works best with servers of the same
3089 or an older major version. Backslash commands are particularly likely
3090 to fail if the server is of a newer version than <span class="application">psql</span>
3091 itself. However, backslash commands of the <code class="literal">\d</code> family should
3092 work with servers of versions back to 9.2, though not necessarily with
3093 servers newer than <span class="application">psql</span> itself. The general
3094 functionality of running SQL commands and displaying query results
3095 should also work with servers of a newer major version, but this cannot
3096 be guaranteed in all cases.
3098 If you want to use <span class="application">psql</span> to connect to several
3099 servers of different major versions, it is recommended that you use the
3100 newest version of <span class="application">psql</span>. Alternatively, you
3101 can keep around a copy of <span class="application">psql</span> from each
3102 major version and be sure to use the version that matches the
3103 respective server. But in practice, this additional complication should
3105 </p></li><li class="listitem"><p>
3106 Before <span class="productname">PostgreSQL</span> 9.6,
3107 the <code class="option">-c</code> option implied <code class="option">-X</code>
3108 (<code class="option">--no-psqlrc</code>); this is no longer the case.
3109 </p></li><li class="listitem"><p>
3110 Before <span class="productname">PostgreSQL</span> 8.4,
3111 <span class="application">psql</span> allowed the
3112 first argument of a single-letter backslash command to start
3113 directly after the command, without intervening whitespace.
3114 Now, some whitespace is required.
3115 </p></li></ul></div></div><div class="refsect1" id="id-1.9.4.21.12"><h2>Notes for Windows Users</h2><p>
3116 <span class="application">psql</span> is built as a <span class="quote">“<span class="quote">console
3117 application</span>”</span>. Since the Windows console windows use a different
3118 encoding than the rest of the system, you must take special care
3119 when using 8-bit characters within <span class="application">psql</span>.
3120 If <span class="application">psql</span> detects a problematic
3121 console code page, it will warn you at startup. To change the
3122 console code page, two things are necessary:
3124 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
3125 Set the code page by entering <strong class="userinput"><code>cmd.exe /c chcp
3126 1252</code></strong>. (1252 is a code page that is appropriate for
3127 German; replace it with your value.) If you are using Cygwin,
3128 you can put this command in <code class="filename">/etc/profile</code>.
3129 </p></li><li class="listitem"><p>
3130 Set the console font to <code class="literal">Lucida Console</code>, because the
3131 raster font does not work with the ANSI code page.
3132 </p></li></ul></div></div><div class="refsect1" id="APP-PSQL-EXAMPLES"><h2>Examples</h2><p>
3133 The first example shows how to spread a command over several lines of
3134 input. Notice the changing prompt:
3135 </p><pre class="programlisting">
3136 testdb=> <strong class="userinput"><code>CREATE TABLE my_table (</code></strong>
3137 testdb(> <strong class="userinput"><code> first integer not null default 0,</code></strong>
3138 testdb(> <strong class="userinput"><code> second text)</code></strong>
3139 testdb-> <strong class="userinput"><code>;</code></strong>
3142 Now look at the table definition again:
3143 </p><pre class="programlisting">
3144 testdb=> <strong class="userinput"><code>\d my_table</code></strong>
3145 Table "public.my_table"
3146 Column | Type | Collation | Nullable | Default
3147 --------+---------+-----------+----------+---------
3148 first | integer | | not null | 0
3151 Now we change the prompt to something more interesting:
3152 </p><pre class="programlisting">
3153 testdb=> <strong class="userinput"><code>\set PROMPT1 '%n@%m %~%R%# '</code></strong>
3154 peter@localhost testdb=>
3156 Let's assume you have filled the table with data and want to take a
3158 </p><pre class="programlisting">
3159 peter@localhost testdb=> SELECT * FROM my_table;
3168 You can display tables in different ways by using the
3169 <code class="command">\pset</code> command:
3170 </p><pre class="programlisting">
3171 peter@localhost testdb=> <strong class="userinput"><code>\pset border 2</code></strong>
3173 peter@localhost testdb=> <strong class="userinput"><code>SELECT * FROM my_table;</code></strong>
3184 peter@localhost testdb=> <strong class="userinput"><code>\pset border 0</code></strong>
3186 peter@localhost testdb=> <strong class="userinput"><code>SELECT * FROM my_table;</code></strong>
3195 peter@localhost testdb=> <strong class="userinput"><code>\pset border 1</code></strong>
3197 peter@localhost testdb=> <strong class="userinput"><code>\pset format csv</code></strong>
3198 Output format is csv.
3199 peter@localhost testdb=> <strong class="userinput"><code>\pset tuples_only</code></strong>
3201 peter@localhost testdb=> <strong class="userinput"><code>SELECT second, first FROM my_table;</code></strong>
3206 peter@localhost testdb=> <strong class="userinput"><code>\pset format unaligned</code></strong>
3207 Output format is unaligned.
3208 peter@localhost testdb=> <strong class="userinput"><code>\pset fieldsep '\t'</code></strong>
3209 Field separator is " ".
3210 peter@localhost testdb=> <strong class="userinput"><code>SELECT second, first FROM my_table;</code></strong>
3216 Alternatively, use the short commands:
3217 </p><pre class="programlisting">
3218 peter@localhost testdb=> <strong class="userinput"><code>\a \t \x</code></strong>
3219 Output format is aligned.
3221 Expanded display is on.
3222 peter@localhost testdb=> <strong class="userinput"><code>SELECT * FROM my_table;</code></strong>
3237 Also, these output format options can be set for just one query by using
3238 <code class="literal">\g</code>:
3239 </p><pre class="programlisting">
3240 peter@localhost testdb=> <strong class="userinput"><code>SELECT * FROM my_table</code></strong>
3241 peter@localhost testdb-> <strong class="userinput"><code>\g (format=aligned tuples_only=off expanded=on)</code></strong>
3256 Here is an example of using the <code class="command">\df</code> command to
3257 find only functions with names matching <code class="literal">int*pl</code>
3258 and whose second argument is of type <code class="type">bigint</code>:
3259 </p><pre class="programlisting">
3260 testdb=> <strong class="userinput"><code>\df int*pl * bigint</code></strong>
3262 Schema | Name | Result data type | Argument data types | Type
3263 ------------+---------+------------------+---------------------+------
3264 pg_catalog | int28pl | bigint | smallint, bigint | func
3265 pg_catalog | int48pl | bigint | integer, bigint | func
3266 pg_catalog | int8pl | bigint | bigint, bigint | func
3270 Here, the <code class="literal">+</code> option is used to display additional
3271 information about one of these functions, and <code class="literal">x</code> is used
3272 to display the results in expanded mode:
3273 </p><pre class="programlisting">
3274 testdb=> <strong class="userinput"><code>\df+x int*pl integer bigint</code></strong>
3276 -[ RECORD 1 ]-------+-----------------------------
3279 Result data type | bigint
3280 Argument data types | integer, bigint
3282 Volatility | immutable
3289 Internal name | int48pl
3290 Description | implementation of + operator
3293 When suitable, query results can be shown in a crosstab representation
3294 with the <code class="command">\crosstabview</code> command:
3295 </p><pre class="programlisting">
3296 testdb=> <strong class="userinput"><code>SELECT first, second, first > 2 AS gt2 FROM my_table;</code></strong>
3297 first | second | gt2
3298 -------+--------+-----
3305 testdb=> <strong class="userinput"><code>\crosstabview first second</code></strong>
3306 first | one | two | three | four
3307 -------+-----+-----+-------+------
3315 This second example shows a multiplication table with rows sorted in reverse
3316 numerical order and columns with an independent, ascending numerical order.
3317 </p><pre class="programlisting">
3318 testdb=> <strong class="userinput"><code>SELECT t1.first as "A", t2.first+100 AS "B", t1.first*(t2.first+100) as "AxB",</code></strong>
3319 testdb-> <strong class="userinput"><code>row_number() over(order by t2.first) AS ord</code></strong>
3320 testdb-> <strong class="userinput"><code>FROM my_table t1 CROSS JOIN my_table t2 ORDER BY 1 DESC</code></strong>
3321 testdb-> <strong class="userinput"><code>\crosstabview "A" "B" "AxB" ord</code></strong>
3322 A | 101 | 102 | 103 | 104
3323 ---+-----+-----+-----+-----
3324 4 | 404 | 408 | 412 | 416
3325 3 | 303 | 306 | 309 | 312
3326 2 | 202 | 204 | 206 | 208
3327 1 | 101 | 102 | 103 | 104
3329 </pre></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="app-pgverifybackup.html" title="pg_verifybackup">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="reference-client.html" title="PostgreSQL Client Applications">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="app-reindexdb.html" title="reindexdb">Next</a></td></tr><tr><td width="40%" align="left" valign="top"><span class="application">pg_verifybackup</span> </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> <span class="application">reindexdb</span></td></tr></table></div></body></html>