|
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <!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 V1.79.1" /><link rel="prev" href="app-pgrestore.html" title="pg_restore" /><link rel="next" href="app-reindexdb.html" title="reindexdb" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center"><span xmlns="http://www.w3.org/1999/xhtml" class="application">psql</span></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="app-pgrestore.html" title="pg_restore">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 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="app-reindexdb.html" title="reindexdb">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="APP-PSQL"><div class="titlepage"></div><a id="id-1.9.4.18.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle"><span class="application">psql</span></span></h2><p><span class="application">psql</span> —
- <span class="productname">PostgreSQL</span> interactive terminal
- </p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p id="id-1.9.4.18.4.1"><code class="command">psql</code> [<em class="replaceable"><code>option</code></em>...] [<em class="replaceable"><code>dbname</code></em>
- [<em class="replaceable"><code>username</code></em>]]</p></div></div><div class="refsect1" id="id-1.9.4.18.5"><h2>Description</h2><p>
- <span class="application">psql</span> is a terminal-based front-end to
- <span class="productname">PostgreSQL</span>. It enables you to type in
- queries interactively, issue them to
- <span class="productname">PostgreSQL</span>, and see the query results.
- Alternatively, input can be from a file or from command line
- arguments. In addition, <span class="application">psql</span> provides a
- number of meta-commands and various shell-like features to
- facilitate writing scripts and automating a wide variety of tasks.
- </p></div><div class="refsect1" id="R1-APP-PSQL-3"><h2>Options</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-a</code><br /></span><span class="term"><code class="option">--echo-all</code></span></dt><dd><p>
- Print all nonempty input lines to standard output as they are read.
- (This does not apply to lines read interactively.) This is
- equivalent to setting the variable <code class="varname">ECHO</code> to
- <code class="literal">all</code>.
- </p></dd><dt><span class="term"><code class="option">-A</code><br /></span><span class="term"><code class="option">--no-align</code></span></dt><dd><p>
- Switches to unaligned output mode. (The default output mode is
- <code class="literal">aligned</code>.) This is equivalent to
- <code class="command">\pset format unaligned</code>.
- </p></dd><dt><span class="term"><code class="option">-b</code><br /></span><span class="term"><code class="option">--echo-errors</code></span></dt><dd><p>
- Print failed SQL commands to standard error output. This is
- equivalent to setting the variable <code class="varname">ECHO</code> to
- <code class="literal">errors</code>.
- </p></dd><dt><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></dt><dd><p>
- Specifies that <span class="application">psql</span> is to execute the given
- command string, <em class="replaceable"><code>command</code></em>.
- This option can be repeated and combined in any order with
- the <code class="option">-f</code> option. When either <code class="option">-c</code>
- or <code class="option">-f</code> is specified, <span class="application">psql</span>
- does not read commands from standard input; instead it terminates
- after processing all the <code class="option">-c</code> and <code class="option">-f</code>
- options in sequence.
- </p><p>
- <em class="replaceable"><code>command</code></em> must be either
- a command string that is completely parsable by the server (i.e.,
- it contains no <span class="application">psql</span>-specific features),
- or a single backslash command. Thus you cannot mix
- <acronym class="acronym">SQL</acronym> and <span class="application">psql</span>
- meta-commands within a <code class="option">-c</code> option. To achieve that,
- you could use repeated <code class="option">-c</code> options or pipe the string
- into <span class="application">psql</span>, for example:
- </p><pre class="programlisting">
- psql -c '\x' -c 'SELECT * FROM foo;'
- </pre><p>
- or
- </p><pre class="programlisting">
- echo '\x \\ SELECT * FROM foo;' | psql
- </pre><p>
- (<code class="literal">\\</code> is the separator meta-command.)
- </p><p>
- Each <acronym class="acronym">SQL</acronym> command string passed
- to <code class="option">-c</code> is sent to the server as a single request.
- Because of this, the server executes it as a single transaction even
- if the string contains multiple <acronym class="acronym">SQL</acronym> commands,
- unless there are explicit <code class="command">BEGIN</code>/<code class="command">COMMIT</code>
- commands included in the string to divide it into multiple
- transactions. (See <a class="xref" href="protocol-flow.html#PROTOCOL-FLOW-MULTI-STATEMENT" title="52.2.2.1. Multiple Statements in a Simple Query">Section 52.2.2.1</a>
- for more details about how the server handles multi-query strings.)
- Also, <span class="application">psql</span> only prints the
- result of the last <acronym class="acronym">SQL</acronym> command in the string.
- This is different from the behavior when the same string is read from
- a file or fed to <span class="application">psql</span>'s standard input,
- because then <span class="application">psql</span> sends
- each <acronym class="acronym">SQL</acronym> command separately.
- </p><p>
- Because of this behavior, putting more than one SQL command in a
- single <code class="option">-c</code> string often has unexpected results.
- It's better to use repeated <code class="option">-c</code> commands or feed
- multiple commands to <span class="application">psql</span>'s standard input,
- either using <span class="application">echo</span> as illustrated above, or
- via a shell here-document, for example:
- </p><pre class="programlisting">
- psql <<EOF
- \x
- SELECT * FROM foo;
- EOF
- </pre><p>
- </p></dd><dt><span class="term"><code class="option">--csv</code></span></dt><dd><p>
- Switches to <acronym class="acronym">CSV</acronym> (Comma-Separated Values) output
- mode. This is equivalent to <code class="command">\pset format csv</code>.
- </p></dd><dt><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></dt><dd><p>
- Specifies the name of the database to connect to. This is
- equivalent to specifying <em class="replaceable"><code>dbname</code></em> as the first non-option
- argument on the command line.
- </p><p>
- If this parameter contains an <code class="symbol">=</code> sign or starts
- with a valid <acronym class="acronym">URI</acronym> prefix
- (<code class="literal">postgresql://</code>
- or <code class="literal">postgres://</code>), it is treated as a
- <em class="parameter"><code>conninfo</code></em> string. See <a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="33.1.1. Connection Strings">Section 33.1.1</a> for more information.
- </p></dd><dt><span class="term"><code class="option">-e</code><br /></span><span class="term"><code class="option">--echo-queries</code></span></dt><dd><p>
- Copy all SQL commands sent to the server to standard output as well.
- This is equivalent
- to setting the variable <code class="varname">ECHO</code> to
- <code class="literal">queries</code>.
- </p></dd><dt><span class="term"><code class="option">-E</code><br /></span><span class="term"><code class="option">--echo-hidden</code></span></dt><dd><p>
- Echo the actual queries generated by <code class="command">\d</code> and other backslash
- commands. You can use this to study <span class="application">psql</span>'s
- internal operations. This is equivalent to
- setting the variable <code class="varname">ECHO_HIDDEN</code> to <code class="literal">on</code>.
- </p></dd><dt><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></dt><dd><p>
- Read commands from the
- file <em class="replaceable"><code>filename</code></em>,
- rather than standard input.
- This option can be repeated and combined in any order with
- the <code class="option">-c</code> option. When either <code class="option">-c</code>
- or <code class="option">-f</code> is specified, <span class="application">psql</span>
- does not read commands from standard input; instead it terminates
- after processing all the <code class="option">-c</code> and <code class="option">-f</code>
- options in sequence.
- Except for that, this option is largely equivalent to the
- meta-command <code class="command">\i</code>.
- </p><p>
- If <em class="replaceable"><code>filename</code></em> is <code class="literal">-</code>
- (hyphen), then standard input is read until an EOF indication
- or <code class="command">\q</code> meta-command. This can be used to intersperse
- interactive input with input from files. Note however that Readline
- is not used in this case (much as if <code class="option">-n</code> had been
- specified).
- </p><p>
- Using this option is subtly different from writing <code class="literal">psql
- < <em class="replaceable"><code>filename</code></em></code>. In general,
- both will do what you expect, but using <code class="literal">-f</code>
- enables some nice features such as error messages with line
- numbers. There is also a slight chance that using this option will
- reduce the start-up overhead. On the other hand, the variant using
- the shell's input redirection is (in theory) guaranteed to yield
- exactly the same output you would have received had you entered
- everything by hand.
- </p></dd><dt><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></dt><dd><p>
- Use <em class="replaceable"><code>separator</code></em> as the
- field separator for unaligned output. This is equivalent to
- <code class="command">\pset fieldsep</code> or <code class="command">\f</code>.
- </p></dd><dt><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></dt><dd><p>
- Specifies the host name of the machine on which the
- server is running. If the value begins
- with a slash, it is used as the directory for the Unix-domain
- socket.
- </p></dd><dt><span class="term"><code class="option">-H</code><br /></span><span class="term"><code class="option">--html</code></span></dt><dd><p>
- Switches to <acronym class="acronym">HTML</acronym> output mode. This is
- equivalent to <code class="command">\pset format html</code> or the
- <code class="command">\H</code> command.
- </p></dd><dt><span class="term"><code class="option">-l</code><br /></span><span class="term"><code class="option">--list</code></span></dt><dd><p>
- List all available databases, then exit. Other non-connection
- options are ignored. This is similar to the meta-command
- <code class="command">\list</code>.
- </p><p>
- When this option is used, <span class="application">psql</span> will connect
- to the database <code class="literal">postgres</code>, unless a different database
- is named on the command line (option <code class="option">-d</code> or non-option
- argument, possibly via a service entry, but not via an environment
- variable).
- </p></dd><dt><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></dt><dd><p>
- Write all query output into file <em class="replaceable"><code>filename</code></em>, in addition to the
- normal output destination.
- </p></dd><dt><span class="term"><code class="option">-n</code><br /></span><span class="term"><code class="option">--no-readline</code></span></dt><dd><p>
- Do not use <span class="application">Readline</span> for line editing and do
- not use the command history.
- This can be useful to turn off tab expansion when cutting and pasting.
- </p></dd><dt><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></dt><dd><p>
- Put all query output into file <em class="replaceable"><code>filename</code></em>. This is equivalent to
- the command <code class="command">\o</code>.
- </p></dd><dt><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></dt><dd><p>
- Specifies the TCP port or the local Unix-domain
- socket file extension on which the server is listening for
- connections. Defaults to the value of the <code class="envar">PGPORT</code>
- environment variable or, if not set, to the port specified at
- compile time, usually 5432.
- </p></dd><dt><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></dt><dd><p>
- Specifies printing options, in the style of
- <code class="command">\pset</code>. Note that here you
- have to separate name and value with an equal sign instead of a
- space. For example, to set the output format to <span class="application">LaTeX</span>, you could write
- <code class="literal">-P format=latex</code>.
- </p></dd><dt><span class="term"><code class="option">-q</code><br /></span><span class="term"><code class="option">--quiet</code></span></dt><dd><p>
- Specifies that <span class="application">psql</span> should do its work
- quietly. By default, it prints welcome messages and various
- informational output. If this option is used, none of this
- happens. This is useful with the <code class="option">-c</code> option.
- This is equivalent to setting the variable <code class="varname">QUIET</code>
- to <code class="literal">on</code>.
- </p></dd><dt><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></dt><dd><p>
- Use <em class="replaceable"><code>separator</code></em> as the
- record separator for unaligned output. This is equivalent to
- <code class="command">\pset recordsep</code>.
- </p></dd><dt><span class="term"><code class="option">-s</code><br /></span><span class="term"><code class="option">--single-step</code></span></dt><dd><p>
- Run in single-step mode. That means the user is prompted before
- each command is sent to the server, with the option to cancel
- execution as well. Use this to debug scripts.
- </p></dd><dt><span class="term"><code class="option">-S</code><br /></span><span class="term"><code class="option">--single-line</code></span></dt><dd><p>
- Runs in single-line mode where a newline terminates an SQL command, as a
- semicolon does.
- </p><div class="note"><h3 class="title">Note</h3><p>
- This mode is provided for those who insist on it, but you are not
- necessarily encouraged to use it. In particular, if you mix
- <acronym class="acronym">SQL</acronym> and meta-commands on a line the order of
- execution might not always be clear to the inexperienced user.
- </p></div></dd><dt><span class="term"><code class="option">-t</code><br /></span><span class="term"><code class="option">--tuples-only</code></span></dt><dd><p>
- Turn off printing of column names and result row count footers,
- etc. This is equivalent to <code class="command">\t</code> or
- <code class="command">\pset tuples_only</code>.
- </p></dd><dt><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></dt><dd><p>
- Specifies options to be placed within the
- <acronym class="acronym">HTML</acronym> <code class="sgmltag-element">table</code> tag. See
- <code class="command">\pset tableattr</code> for details.
- </p></dd><dt><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></dt><dd><p>
- Connect to the database as the user <em class="replaceable"><code>username</code></em> instead of the default.
- (You must have permission to do so, of course.)
- </p></dd><dt><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></dt><dd><p>
- Perform a variable assignment, like the <code class="command">\set</code>
- meta-command. Note that you must separate name and value, if
- any, by an equal sign on the command line. To unset a variable,
- leave off the equal sign. To set a variable with an empty value,
- use the equal sign but leave off the value. These assignments are
- done during command line processing, so variables that reflect
- connection state will get overwritten later.
- </p></dd><dt><span class="term"><code class="option">-V</code><br /></span><span class="term"><code class="option">--version</code></span></dt><dd><p>
- Print the <span class="application">psql</span> version and exit.
- </p></dd><dt><span class="term"><code class="option">-w</code><br /></span><span class="term"><code class="option">--no-password</code></span></dt><dd><p>
- Never issue a password prompt. If the server requires password
- authentication and a password is not available by other means
- such as a <code class="filename">.pgpass</code> file, the connection
- attempt will fail. This option can be useful in batch jobs and
- scripts where no user is present to enter a password.
- </p><p>
- Note that this option will remain set for the entire session,
- and so it affects uses of the meta-command
- <code class="command">\connect</code> as well as the initial connection attempt.
- </p></dd><dt><span class="term"><code class="option">-W</code><br /></span><span class="term"><code class="option">--password</code></span></dt><dd><p>
- Force <span class="application">psql</span> to prompt for a
- password before connecting to a database.
- </p><p>
- This option is never essential, since <span class="application">psql</span>
- will automatically prompt for a password if the server demands
- password authentication. However, <span class="application">psql</span>
- will waste a connection attempt finding out that the server wants a
- password. In some cases it is worth typing <code class="option">-W</code> to avoid
- the extra connection attempt.
- </p><p>
- Note that this option will remain set for the entire session,
- and so it affects uses of the meta-command
- <code class="command">\connect</code> as well as the initial connection attempt.
- </p></dd><dt><span class="term"><code class="option">-x</code><br /></span><span class="term"><code class="option">--expanded</code></span></dt><dd><p>
- Turn on the expanded table formatting mode. This is equivalent to
- <code class="command">\x</code> or <code class="command">\pset expanded</code>.
- </p></dd><dt><span class="term"><code class="option">-X,</code><br /></span><span class="term"><code class="option">--no-psqlrc</code></span></dt><dd><p>
- Do not read the start-up file (neither the system-wide
- <code class="filename">psqlrc</code> file nor the user's
- <code class="filename">~/.psqlrc</code> file).
- </p></dd><dt><span class="term"><code class="option">-z</code><br /></span><span class="term"><code class="option">--field-separator-zero</code></span></dt><dd><p>
- Set the field separator for unaligned output to a zero byte. This is
- equivalent to <code class="command">\pset fieldsep_zero</code>.
- </p></dd><dt><span class="term"><code class="option">-0</code><br /></span><span class="term"><code class="option">--record-separator-zero</code></span></dt><dd><p>
- Set the record separator for unaligned output to a zero byte. This is
- useful for interfacing, for example, with <code class="literal">xargs -0</code>.
- This is equivalent to <code class="command">\pset recordsep_zero</code>.
- </p></dd><dt><span class="term"><code class="option">-1</code><br /></span><span class="term"><code class="option">--single-transaction</code></span></dt><dd><p>
- This option can only be used in combination with one or more
- <code class="option">-c</code> and/or <code class="option">-f</code> options. It causes
- <span class="application">psql</span> to issue a <code class="command">BEGIN</code> command
- before the first such option and a <code class="command">COMMIT</code> command after
- the last one, thereby wrapping all the commands into a single
- transaction. This ensures that either all the commands complete
- successfully, or no changes are applied.
- </p><p>
- If the commands themselves
- contain <code class="command">BEGIN</code>, <code class="command">COMMIT</code>,
- or <code class="command">ROLLBACK</code>, this option will not have the desired
- effects. Also, if an individual command cannot be executed inside a
- transaction block, specifying this option will cause the whole
- transaction to fail.
- </p></dd><dt><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></dt><dd><p>
- Show help about <span class="application">psql</span> and exit. The optional
- <em class="replaceable"><code>topic</code></em> parameter (defaulting
- to <code class="literal">options</code>) selects which part of <span class="application">psql</span> is
- explained: <code class="literal">commands</code> describes <span class="application">psql</span>'s
- backslash commands; <code class="literal">options</code> describes the command-line
- options that can be passed to <span class="application">psql</span>;
- and <code class="literal">variables</code> shows help about <span class="application">psql</span> configuration
- variables.
- </p></dd></dl></div></div><div class="refsect1" id="id-1.9.4.18.7"><h2>Exit Status</h2><p>
- <span class="application">psql</span> returns 0 to the shell if it
- finished normally, 1 if a fatal error of its own occurs (e.g. out of memory,
- file not found), 2 if the connection to the server went bad
- and the session was not interactive, and 3 if an error occurred in a
- script and the variable <code class="varname">ON_ERROR_STOP</code> was set.
- </p></div><div class="refsect1" id="id-1.9.4.18.8"><h2>Usage</h2><div class="refsect2" id="R2-APP-PSQL-CONNECTING"><h3>Connecting to a Database</h3><p>
- <span class="application">psql</span> is a regular
- <span class="productname">PostgreSQL</span> client application. In order
- to connect to a database you need to know the name of your target
- database, the host name and port number of the server, and what user
- name you want to connect as. <span class="application">psql</span> can be
- told about those parameters via command line options, namely
- <code class="option">-d</code>, <code class="option">-h</code>, <code class="option">-p</code>, and
- <code class="option">-U</code> respectively. If an argument is found that does
- not belong to any option it will be interpreted as the database name
- (or the user name, if the database name is already given). Not all
- of these options are required; there are useful defaults. If you omit the host
- name, <span class="application">psql</span> will connect via a Unix-domain socket
- to a server on the local host, or via TCP/IP to <code class="literal">localhost</code> on
- machines that don't have Unix-domain sockets. The default port number is
- determined at compile time.
- Since the database server uses the same default, you will not have
- to specify the port in most cases. The default user name is your
- operating-system user name, as is the default database name.
- Note that you cannot
- just connect to any database under any user name. Your database
- administrator should have informed you about your access rights.
- </p><p>
- When the defaults aren't quite right, you can save yourself
- some typing by setting the environment variables
- <code class="envar">PGDATABASE</code>, <code class="envar">PGHOST</code>,
- <code class="envar">PGPORT</code> and/or <code class="envar">PGUSER</code> to appropriate
- values. (For additional environment variables, see <a class="xref" href="libpq-envars.html" title="33.14. Environment Variables">Section 33.14</a>.) It is also convenient to have a
- <code class="filename">~/.pgpass</code> file to avoid regularly having to type in
- passwords. See <a class="xref" href="libpq-pgpass.html" title="33.15. The Password File">Section 33.15</a> for more information.
- </p><p>
- An alternative way to specify connection parameters is in a
- <em class="parameter"><code>conninfo</code></em> string or
- a <acronym class="acronym">URI</acronym>, which is used instead of a database
- name. This mechanism give you very wide control over the
- connection. For example:
- </p><pre class="programlisting">
- $ <strong class="userinput"><code>psql "service=myservice sslmode=require"</code></strong>
- $ <strong class="userinput"><code>psql postgresql://dbmaster:5433/mydb?sslmode=require</code></strong>
- </pre><p>
- This way you can also use <acronym class="acronym">LDAP</acronym> for connection
- parameter lookup as described in <a class="xref" href="libpq-ldap.html" title="33.17. LDAP Lookup of Connection Parameters">Section 33.17</a>.
- See <a class="xref" href="libpq-connect.html#LIBPQ-PARAMKEYWORDS" title="33.1.2. Parameter Key Words">Section 33.1.2</a> for more information on all the
- available connection options.
- </p><p>
- If the connection could not be made for any reason (e.g., insufficient
- privileges, server is not running on the targeted host, etc.),
- <span class="application">psql</span> will return an error and terminate.
- </p><p>
- If both standard input and standard output are a
- terminal, then <span class="application">psql</span> sets the client
- encoding to <span class="quote">“<span class="quote">auto</span>”</span>, which will detect the
- appropriate client encoding from the locale settings
- (<code class="envar">LC_CTYPE</code> environment variable on Unix systems).
- If this doesn't work out as expected, the client encoding can be
- overridden using the environment
- variable <code class="envar">PGCLIENTENCODING</code>.
- </p></div><div class="refsect2" id="R2-APP-PSQL-4"><h3>Entering SQL Commands</h3><p>
- In normal operation, <span class="application">psql</span> provides a
- prompt with the name of the database to which
- <span class="application">psql</span> is currently connected, followed by
- the string <code class="literal">=></code>. For example:
- </p><pre class="programlisting">
- $ <strong class="userinput"><code>psql testdb</code></strong>
- psql (12.4)
- Type "help" for help.
-
- testdb=>
- </pre><p>
- </p><p>
- At the prompt, the user can type in <acronym class="acronym">SQL</acronym> commands.
- Ordinarily, input lines are sent to the server when a
- command-terminating semicolon is reached. An end of line does not
- terminate a command. Thus commands can be spread over several lines for
- clarity. If the command was sent and executed without error, the results
- of the command are displayed on the screen.
- </p><p>
- If untrusted users have access to a database that has not adopted a
- <a class="link" href="ddl-schemas.html#DDL-SCHEMAS-PATTERNS" title="5.9.6. Usage Patterns">secure schema usage pattern</a>,
- begin your session by removing publicly-writable schemas
- from <code class="varname">search_path</code>. One can
- add <code class="literal">options=-csearch_path=</code> to the connection string or
- issue <code class="literal">SELECT pg_catalog.set_config('search_path', '',
- false)</code> before other SQL commands. This consideration is not
- specific to <span class="application">psql</span>; it applies to every interface
- for executing arbitrary SQL commands.
- </p><p>
- Whenever a command is executed, <span class="application">psql</span> also polls
- for asynchronous notification events generated by
- <a class="xref" href="sql-listen.html" title="LISTEN"><span class="refentrytitle">LISTEN</span></a> and
- <a class="xref" href="sql-notify.html" title="NOTIFY"><span class="refentrytitle">NOTIFY</span></a>.
- </p><p>
- While C-style block comments are passed to the server for
- processing and removal, SQL-standard comments are removed by
- <span class="application">psql</span>.
- </p></div><div class="refsect2" id="APP-PSQL-META-COMMANDS"><h3>Meta-Commands</h3><p>
- Anything you enter in <span class="application">psql</span> that begins
- with an unquoted backslash is a <span class="application">psql</span>
- meta-command that is processed by <span class="application">psql</span>
- itself. These commands make
- <span class="application">psql</span> more useful for administration or
- scripting. Meta-commands are often called slash or backslash commands.
- </p><p>
- The format of a <span class="application">psql</span> command is the backslash,
- followed immediately by a command verb, then any arguments. The arguments
- are separated from the command verb and each other by any number of
- whitespace characters.
- </p><p>
- To include whitespace in an argument you can quote it with
- single quotes. To include a single quote in an argument,
- write two single quotes within single-quoted text.
- Anything contained in single quotes is
- furthermore subject to C-like substitutions for
- <code class="literal">\n</code> (new line), <code class="literal">\t</code> (tab),
- <code class="literal">\b</code> (backspace), <code class="literal">\r</code> (carriage return),
- <code class="literal">\f</code> (form feed),
- <code class="literal">\</code><em class="replaceable"><code>digits</code></em> (octal), and
- <code class="literal">\x</code><em class="replaceable"><code>digits</code></em> (hexadecimal).
- A backslash preceding any other character within single-quoted text
- quotes that single character, whatever it is.
- </p><p>
- If an unquoted colon (<code class="literal">:</code>) followed by a
- <span class="application">psql</span> variable name appears within an argument, it is
- replaced by the variable's value, as described in <a class="xref" href="app-psql.html#APP-PSQL-INTERPOLATION" title="SQL Interpolation"><acronym class="acronym">SQL</acronym> Interpolation</a>.
- The forms <code class="literal">:'<em class="replaceable"><code>variable_name</code></em>'</code> and
- <code class="literal">:"<em class="replaceable"><code>variable_name</code></em>"</code> described there
- work as well.
- The <code class="literal">:{?<em class="replaceable"><code>variable_name</code></em>}</code> syntax allows
- testing whether a variable is defined. It is substituted by
- TRUE or FALSE.
- Escaping the colon with a backslash protects it from substitution.
- </p><p>
- Within an argument, text that is enclosed in backquotes
- (<code class="literal">`</code>) is taken as a command line that is passed to the
- shell. The output of the command (with any trailing newline removed)
- replaces the backquoted text. Within the text enclosed in backquotes,
- no special quoting or other processing occurs, except that appearances
- of <code class="literal">:<em class="replaceable"><code>variable_name</code></em></code> where
- <em class="replaceable"><code>variable_name</code></em> is a <span class="application">psql</span> variable name
- are replaced by the variable's value. Also, appearances of
- <code class="literal">:'<em class="replaceable"><code>variable_name</code></em>'</code> are replaced by the
- variable's value suitably quoted to become a single shell command
- argument. (The latter form is almost always preferable, unless you are
- very sure of what is in the variable.) Because carriage return and line
- feed characters cannot be safely quoted on all platforms, the
- <code class="literal">:'<em class="replaceable"><code>variable_name</code></em>'</code> form prints an
- error message and does not substitute the variable value when such
- characters appear in the value.
- </p><p>
- Some commands take an <acronym class="acronym">SQL</acronym> identifier (such as a
- table name) as argument. These arguments follow the syntax rules
- of <acronym class="acronym">SQL</acronym>: Unquoted letters are forced to
- lowercase, while double quotes (<code class="literal">"</code>) protect letters
- from case conversion and allow incorporation of whitespace into
- the identifier. Within double quotes, paired double quotes reduce
- to a single double quote in the resulting name. For example,
- <code class="literal">FOO"BAR"BAZ</code> is interpreted as <code class="literal">fooBARbaz</code>,
- and <code class="literal">"A weird"" name"</code> becomes <code class="literal">A weird"
- name</code>.
- </p><p>
- Parsing for arguments stops at the end of the line, or when another
- unquoted backslash is found. An unquoted backslash
- is taken as the beginning of a new meta-command. The special
- sequence <code class="literal">\\</code> (two backslashes) marks the end of
- arguments and continues parsing <acronym class="acronym">SQL</acronym> commands, if
- any. That way <acronym class="acronym">SQL</acronym> and
- <span class="application">psql</span> commands can be freely mixed on a
- line. But in any case, the arguments of a meta-command cannot
- continue beyond the end of the line.
- </p><p>
- Many of the meta-commands act on the <em class="firstterm">current query buffer</em>.
- This is simply a buffer holding whatever SQL command text has been typed
- but not yet sent to the server for execution. This will include previous
- input lines as well as any text appearing before the meta-command on the
- same line.
- </p><p>
- The following meta-commands are defined:
-
- </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">\a</code></span></dt><dd><p>
- If the current table output format is unaligned, it is switched to aligned.
- If it is not unaligned, it is set to unaligned. This command is
- kept for backwards compatibility. See <code class="command">\pset</code> for a
- more general solution.
- </p></dd><dt><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></dt><dd><p>
- Establishes a new connection to a <span class="productname">PostgreSQL</span>
- server. The connection parameters to use can be specified either
- using a positional syntax, or using <em class="replaceable"><code>conninfo</code></em> connection
- strings as detailed in <a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="33.1.1. Connection Strings">Section 33.1.1</a>.
- </p><p>
- Where the command omits database name, user, host, or port, the new
- connection can reuse values from the previous connection. By default,
- values from the previous connection are reused except when processing
- a <em class="replaceable"><code>conninfo</code></em> string. Passing a first argument
- of <code class="literal">-reuse-previous=on</code>
- or <code class="literal">-reuse-previous=off</code> overrides that default.
- When the command neither specifies nor reuses a particular parameter,
- the <span class="application">libpq</span> default is used. Specifying any
- of <em class="replaceable"><code>dbname</code></em>,
- <em class="replaceable"><code>username</code></em>,
- <em class="replaceable"><code>host</code></em> or
- <em class="replaceable"><code>port</code></em>
- as <code class="literal">-</code> is equivalent to omitting that parameter.
- If <code class="literal">hostaddr</code> was specified in the original
- connection's <code class="structname">conninfo</code>, that address is reused
- for the new connection (disregarding any other host specification).
- </p><p>
- If the new connection is successfully made, the previous
- connection is closed.
- If the connection attempt failed (wrong user name, access
- denied, etc.), the previous connection will only be kept if
- <span class="application">psql</span> is in interactive mode. When
- executing a non-interactive script, processing will
- immediately stop with an error. This distinction was chosen as
- a user convenience against typos on the one hand, and a safety
- mechanism that scripts are not accidentally acting on the
- wrong database on the other hand.
- </p><p>
- Examples:
- </p><pre class="programlisting">
- => \c mydb myuser host.dom 6432
- => \c service=foo
- => \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
- => \c postgresql://tom@localhost/mydb?application_name=myapp
- </pre></dd><dt><span class="term"><code class="literal">\C [ <em class="replaceable"><code>title</code></em> ]</code></span></dt><dd><p>
- Sets the title of any tables being printed as the result of a
- query or unset any such title. This command is equivalent to
- <code class="literal">\pset title <em class="replaceable"><code>title</code></em></code>. (The name of
- this command derives from <span class="quote">“<span class="quote">caption</span>”</span>, as it was
- previously only used to set the caption in an
- <acronym class="acronym">HTML</acronym> table.)
- </p></dd><dt><span class="term"><code class="literal">\cd [ <em class="replaceable"><code>directory</code></em> ]</code></span></dt><dd><p>
- Changes the current working directory to
- <em class="replaceable"><code>directory</code></em>. Without argument, changes
- to the current user's home directory.
- </p><div class="tip"><h3 class="title">Tip</h3><p>
- To print your current working directory, use <code class="literal">\! pwd</code>.
- </p></div></dd><dt><span class="term"><code class="literal">\conninfo</code></span></dt><dd><p>
- Outputs information about the current database connection.
- </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> ) ] }
- <code class="literal">from</code>
- { <em class="replaceable"><code>'filename'</code></em> | program <em class="replaceable"><code>'command'</code></em> | stdin | pstdin }
- [ [ with ] ( <em class="replaceable"><code>option</code></em> [, ...] ) ]
- [ 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> ) }
- <code class="literal">to</code>
- { <em class="replaceable"><code>'filename'</code></em> | program <em class="replaceable"><code>'command'</code></em> | stdout | pstdout }
- [ [ with ] ( <em class="replaceable"><code>option</code></em> [, ...] ) ]</code></span></dt><dd><p>
- Performs a frontend (client) copy. This is an operation that
- runs an <acronym class="acronym">SQL</acronym> <a class="xref" href="sql-copy.html" title="COPY"><span class="refentrytitle">COPY</span></a>
- command, but instead of the server
- reading or writing the specified file,
- <span class="application">psql</span> reads or writes the file and
- routes the data between the server and the local file system.
- This means that file accessibility and privileges are those of
- the local user, not the server, and no SQL superuser
- privileges are required.
- </p><p>
- When <code class="literal">program</code> is specified,
- <em class="replaceable"><code>command</code></em> is
- executed by <span class="application">psql</span> and the data passed from
- or to <em class="replaceable"><code>command</code></em> is
- routed between the server and the client.
- Again, the execution privileges are those of
- the local user, not the server, and no SQL superuser
- privileges are required.
- </p><p>
- For <code class="literal">\copy ... from stdin</code>, data rows are read from the same
- source that issued the command, continuing until <code class="literal">\.</code>
- is read or the stream reaches <acronym class="acronym">EOF</acronym>. This option is useful
- for populating tables in-line within a SQL script file.
- For <code class="literal">\copy ... to stdout</code>, output is sent to the same place
- as <span class="application">psql</span> command output, and
- the <code class="literal">COPY <em class="replaceable"><code>count</code></em></code> command status is
- not printed (since it might be confused with a data row).
- To read/write <span class="application">psql</span>'s standard input or
- output regardless of the current command source or <code class="literal">\o</code>
- option, write <code class="literal">from pstdin</code> or <code class="literal">to pstdout</code>.
- </p><p>
- The syntax of this command is similar to that of the
- <acronym class="acronym">SQL</acronym> <a class="xref" href="sql-copy.html" title="COPY"><span class="refentrytitle">COPY</span></a>
- command. All options other than the data source/destination are
- as specified for <a class="xref" href="sql-copy.html" title="COPY"><span class="refentrytitle">COPY</span></a>.
- Because of this, special parsing rules apply to the <code class="command">\copy</code>
- meta-command. Unlike most other meta-commands, the entire remainder
- of the line is always taken to be the arguments of <code class="command">\copy</code>,
- and neither variable interpolation nor backquote expansion are
- performed in the arguments.
- </p><div class="tip"><h3 class="title">Tip</h3><p>
- Another way to obtain the same result as <code class="literal">\copy
- ... to</code> is to use the <acronym class="acronym">SQL</acronym> <code class="literal">COPY
- ... TO STDOUT</code> command and terminate it
- with <code class="literal">\g <em class="replaceable"><code>filename</code></em></code>
- or <code class="literal">\g |<em class="replaceable"><code>program</code></em></code>.
- Unlike <code class="literal">\copy</code>, this method allows the command to
- span multiple lines; also, variable interpolation and backquote
- expansion can be used.
- </p></div><div class="tip"><h3 class="title">Tip</h3><p>
- These operations are not as efficient as the <acronym class="acronym">SQL</acronym>
- <code class="command">COPY</code> command with a file or program data source or
- destination, because all data must pass through the client/server
- connection. For large amounts of data the <acronym class="acronym">SQL</acronym>
- command might be preferable.
- </p></div></dd><dt><span class="term"><code class="literal">\copyright</code></span></dt><dd><p>
- Shows the copyright and distribution terms of
- <span class="productname">PostgreSQL</span>.
- </p></dd><dt id="APP-PSQL-META-COMMANDS-CROSSTABVIEW"><span class="term"><code class="literal">\crosstabview [
- <em class="replaceable"><code>colV</code></em>
- [ <em class="replaceable"><code>colH</code></em>
- [ <em class="replaceable"><code>colD</code></em>
- [ <em class="replaceable"><code>sortcolH</code></em>
- ] ] ] ] </code></span></dt><dd><p>
- Executes the current query buffer (like <code class="literal">\g</code>) and
- shows the results in a crosstab grid.
- The query must return at least three columns.
- The output column identified by <em class="replaceable"><code>colV</code></em>
- becomes a vertical header and the output column identified by
- <em class="replaceable"><code>colH</code></em>
- becomes a horizontal header.
- <em class="replaceable"><code>colD</code></em> identifies
- the output column to display within the grid.
- <em class="replaceable"><code>sortcolH</code></em> identifies
- an optional sort column for the horizontal header.
- </p><p>
- Each column specification can be a column number (starting at 1) or
- a column name. The usual SQL case folding and quoting rules apply to
- column names. If omitted,
- <em class="replaceable"><code>colV</code></em> is taken as column 1
- and <em class="replaceable"><code>colH</code></em> as column 2.
- <em class="replaceable"><code>colH</code></em> must differ from
- <em class="replaceable"><code>colV</code></em>.
- If <em class="replaceable"><code>colD</code></em> is not
- specified, then there must be exactly three columns in the query
- result, and the column that is neither
- <em class="replaceable"><code>colV</code></em> nor
- <em class="replaceable"><code>colH</code></em>
- is taken to be <em class="replaceable"><code>colD</code></em>.
- </p><p>
- The vertical header, displayed as the leftmost column, contains the
- values found in column <em class="replaceable"><code>colV</code></em>, in the
- same order as in the query results, but with duplicates removed.
- </p><p>
- The horizontal header, displayed as the first row, contains the values
- found in column <em class="replaceable"><code>colH</code></em>,
- with duplicates removed. By default, these appear in the same order
- as in the query results. But if the
- optional <em class="replaceable"><code>sortcolH</code></em> argument is given,
- it identifies a column whose values must be integer numbers, and the
- values from <em class="replaceable"><code>colH</code></em> will
- appear in the horizontal header sorted according to the
- corresponding <em class="replaceable"><code>sortcolH</code></em> values.
- </p><p>
- Inside the crosstab grid, for each distinct value <code class="literal">x</code>
- of <em class="replaceable"><code>colH</code></em> and each distinct
- value <code class="literal">y</code>
- of <em class="replaceable"><code>colV</code></em>, the cell located
- at the intersection <code class="literal">(x,y)</code> contains the value of
- the <code class="literal">colD</code> column in the query result row for which
- the value of <em class="replaceable"><code>colH</code></em>
- is <code class="literal">x</code> and the value
- of <em class="replaceable"><code>colV</code></em>
- is <code class="literal">y</code>. If there is no such row, the cell is empty. If
- there are multiple such rows, an error is reported.
- </p></dd><dt><span class="term"><code class="literal">\d[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- For each relation (table, view, materialized view, index, sequence,
- or foreign table)
- or composite type matching the
- <em class="replaceable"><code>pattern</code></em>, show all
- columns, their types, the tablespace (if not the default) and any
- special attributes such as <code class="literal">NOT NULL</code> or defaults.
- Associated indexes, constraints, rules, and triggers are
- also shown. For foreign tables, the associated foreign
- server is shown as well.
- (<span class="quote">“<span class="quote">Matching the pattern</span>”</span> is defined in
- <a class="xref" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns">Patterns</a>
- below.)
- </p><p>
- For some types of relation, <code class="literal">\d</code> shows additional information
- for each column: column values for sequences, indexed expressions for
- indexes, and foreign data wrapper options for foreign tables.
- </p><p>
- The command form <code class="literal">\d+</code> is identical, except that
- more information is displayed: any comments associated with the
- columns of the table are shown, as is the presence of OIDs in the
- table, the view definition if the relation is a view, a non-default
- <a class="link" href="sql-altertable.html#SQL-CREATETABLE-REPLICA-IDENTITY">replica
- identity</a> setting.
- </p><p>
- By default, only user-created objects are shown; supply a
- pattern or the <code class="literal">S</code> modifier to include system
- objects.
- </p><div class="note"><h3 class="title">Note</h3><p>
- If <code class="command">\d</code> is used without a
- <em class="replaceable"><code>pattern</code></em> argument, it is
- equivalent to <code class="command">\dtvmsE</code> which will show a list of
- all visible tables, views, materialized views, sequences and
- foreign tables.
- This is purely a convenience measure.
- </p></div></dd><dt><span class="term"><code class="literal">\da[S] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists aggregate functions, together with their
- return type and the data types they operate on. If <em class="replaceable"><code>pattern</code></em>
- is specified, only aggregates whose names match the pattern are shown.
- By default, only user-created objects are shown; supply a
- pattern or the <code class="literal">S</code> modifier to include system
- objects.
- </p></dd><dt><span class="term"><code class="literal">\dA[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists access methods. If <em class="replaceable"><code>pattern</code></em> is specified, only access
- methods whose names match the pattern are shown. If
- <code class="literal">+</code> is appended to the command name, each access
- method is listed with its associated handler function and description.
- </p></dd><dt><span class="term"><code class="literal">\db[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists tablespaces. If <em class="replaceable"><code>pattern</code></em>
- is specified, only tablespaces whose names match the pattern are shown.
- If <code class="literal">+</code> is appended to the command name, each tablespace
- is listed with its associated options, on-disk size, permissions and
- description.
- </p></dd><dt><span class="term"><code class="literal">\dc[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists conversions between character-set encodings.
- If <em class="replaceable"><code>pattern</code></em>
- is specified, only conversions whose names match the pattern are
- listed.
- By default, only user-created objects are shown; supply a
- pattern or the <code class="literal">S</code> modifier to include system
- objects.
- If <code class="literal">+</code> is appended to the command name, each object
- is listed with its associated description.
- </p></dd><dt><span class="term"><code class="literal">\dC[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists type casts.
- If <em class="replaceable"><code>pattern</code></em>
- is specified, only casts whose source or target types match the
- pattern are listed.
- If <code class="literal">+</code> is appended to the command name, each object
- is listed with its associated description.
- </p></dd><dt><span class="term"><code class="literal">\dd[S] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Shows the descriptions of objects of type <code class="literal">constraint</code>,
- <code class="literal">operator class</code>, <code class="literal">operator family</code>,
- <code class="literal">rule</code>, and <code class="literal">trigger</code>. All
- other comments may be viewed by the respective backslash commands for
- those object types.
- </p><p><code class="literal">\dd</code> displays descriptions for objects matching the
- <em class="replaceable"><code>pattern</code></em>, or of visible
- objects of the appropriate type if no argument is given. But in either
- case, only objects that have a description are listed.
- By default, only user-created objects are shown; supply a
- pattern or the <code class="literal">S</code> modifier to include system
- objects.
- </p><p>
- Descriptions for objects can be created with the <a class="xref" href="sql-comment.html" title="COMMENT"><span class="refentrytitle">COMMENT</span></a>
- <acronym class="acronym">SQL</acronym> command.
- </p></dd><dt><span class="term"><code class="literal">\dD[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists domains. If <em class="replaceable"><code>pattern</code></em>
- is specified, only domains whose names match the pattern are shown.
- By default, only user-created objects are shown; supply a
- pattern or the <code class="literal">S</code> modifier to include system
- objects.
- If <code class="literal">+</code> is appended to the command name, each object
- is listed with its associated permissions and description.
- </p></dd><dt><span class="term"><code class="literal">\ddp [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists default access privilege settings. An entry is shown for
- each role (and schema, if applicable) for which the default
- privilege settings have been changed from the built-in defaults.
- If <em class="replaceable"><code>pattern</code></em> is
- specified, only entries whose role name or schema name matches
- the pattern are listed.
- </p><p>
- The <a class="xref" href="sql-alterdefaultprivileges.html" title="ALTER DEFAULT PRIVILEGES"><span class="refentrytitle">ALTER DEFAULT PRIVILEGES</span></a> command is used to set
- default access privileges. The meaning of the
- privilege display is explained in
- <a class="xref" href="ddl-priv.html" title="5.7. Privileges">Section 5.7</a>.
- </p></dd><dt><span class="term"><code class="literal">\dE[S+] [ <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[S+] [ <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[S+] [ <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[S+] [ <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[S+] [ <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[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- In this group of commands, the letters <code class="literal">E</code>,
- <code class="literal">i</code>, <code class="literal">m</code>, <code class="literal">s</code>,
- <code class="literal">t</code>, and <code class="literal">v</code>
- stand for foreign table, index, materialized view, sequence, table, and view,
- respectively.
- You can specify any or all of
- these letters, in any order, to obtain a listing of objects
- of these types. For example, <code class="literal">\dit</code> lists indexes
- and tables. If <code class="literal">+</code> is
- appended to the command name, each object is listed with its
- physical size on disk and its associated description, if any.
- If <em class="replaceable"><code>pattern</code></em> is
- specified, only objects whose names match the pattern are listed.
- By default, only user-created objects are shown; supply a
- pattern or the <code class="literal">S</code> modifier to include system
- objects.
- </p></dd><dt><span class="term"><code class="literal">\des[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists foreign servers (mnemonic: <span class="quote">“<span class="quote">external
- servers</span>”</span>).
- If <em class="replaceable"><code>pattern</code></em> is
- specified, only those servers whose name matches the pattern
- are listed. If the form <code class="literal">\des+</code> is used, a
- full description of each server is shown, including the
- server's access privileges, type, version, options, and description.
- </p></dd><dt><span class="term"><code class="literal">\det[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists foreign tables (mnemonic: <span class="quote">“<span class="quote">external tables</span>”</span>).
- If <em class="replaceable"><code>pattern</code></em> is
- specified, only entries whose table name or schema name matches
- the pattern are listed. If the form <code class="literal">\det+</code>
- is used, generic options and the foreign table description
- are also displayed.
- </p></dd><dt><span class="term"><code class="literal">\deu[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists user mappings (mnemonic: <span class="quote">“<span class="quote">external
- users</span>”</span>).
- If <em class="replaceable"><code>pattern</code></em> is
- specified, only those mappings whose user names match the
- pattern are listed. If the form <code class="literal">\deu+</code> is
- used, additional information about each mapping is shown.
- </p><div class="caution"><h3 class="title">Caution</h3><p>
- <code class="literal">\deu+</code> might also display the user name and
- password of the remote user, so care should be taken not to
- disclose them.
- </p></div></dd><dt><span class="term"><code class="literal">\dew[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists foreign-data wrappers (mnemonic: <span class="quote">“<span class="quote">external
- wrappers</span>”</span>).
- If <em class="replaceable"><code>pattern</code></em> is
- specified, only those foreign-data wrappers whose name matches
- the pattern are listed. If the form <code class="literal">\dew+</code>
- is used, the access privileges, options, and description of the
- foreign-data wrapper are also shown.
- </p></dd><dt><span class="term"><code class="literal">\df[anptwS+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists functions, together with their result data types, argument data
- types, and function types, which are classified as <span class="quote">“<span class="quote">agg</span>”</span>
- (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>.
- To display only functions
- of specific type(s), add the corresponding letters <code class="literal">a</code>,
- <code class="literal">n</code>, <code class="literal">p</code>, <code class="literal">t</code>, or <code class="literal">w</code> to the command.
- If <em class="replaceable"><code>pattern</code></em> is specified, only
- functions whose names match the pattern are shown.
- By default, only user-created
- objects are shown; supply a pattern or the <code class="literal">S</code>
- modifier to include system objects.
- If the form <code class="literal">\df+</code> is used, additional information
- about each function is shown, including volatility,
- parallel safety, owner, security classification, access privileges,
- language, source code and description.
- </p><div class="tip"><h3 class="title">Tip</h3><p>
- To look up functions taking arguments or returning values of a specific
- data type, use your pager's search capability to scroll through the
- <code class="literal">\df</code> output.
- </p></div></dd><dt><span class="term"><code class="literal">\dF[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists text search configurations.
- If <em class="replaceable"><code>pattern</code></em> is specified,
- only configurations whose names match the pattern are shown.
- If the form <code class="literal">\dF+</code> is used, a full description of
- each configuration is shown, including the underlying text search
- parser and the dictionary list for each parser token type.
- </p></dd><dt><span class="term"><code class="literal">\dFd[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists text search dictionaries.
- If <em class="replaceable"><code>pattern</code></em> is specified,
- only dictionaries whose names match the pattern are shown.
- If the form <code class="literal">\dFd+</code> is used, additional information
- is shown about each selected dictionary, including the underlying
- text search template and the option values.
- </p></dd><dt><span class="term"><code class="literal">\dFp[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists text search parsers.
- If <em class="replaceable"><code>pattern</code></em> is specified,
- only parsers whose names match the pattern are shown.
- If the form <code class="literal">\dFp+</code> is used, a full description of
- each parser is shown, including the underlying functions and the
- list of recognized token types.
- </p></dd><dt><span class="term"><code class="literal">\dFt[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists text search templates.
- If <em class="replaceable"><code>pattern</code></em> is specified,
- only templates whose names match the pattern are shown.
- If the form <code class="literal">\dFt+</code> is used, additional information
- is shown about each template, including the underlying function names.
- </p></dd><dt><span class="term"><code class="literal">\dg[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists database roles.
- (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
- unified into <span class="quote">“<span class="quote">roles</span>”</span>, this command is now equivalent to
- <code class="literal">\du</code>.)
- By default, only user-created roles are shown; supply the
- <code class="literal">S</code> modifier to include system roles.
- If <em class="replaceable"><code>pattern</code></em> is specified,
- only those roles whose names match the pattern are listed.
- If the form <code class="literal">\dg+</code> is used, additional information
- is shown about each role; currently this adds the comment for each
- role.
- </p></dd><dt><span class="term"><code class="literal">\dl</code></span></dt><dd><p>
- This is an alias for <code class="command">\lo_list</code>, which shows a
- list of large objects.
- </p></dd><dt><span class="term"><code class="literal">\dL[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists procedural languages. If <em class="replaceable"><code>pattern</code></em>
- is specified, only languages whose names match the pattern are listed.
- By default, only user-created languages
- are shown; supply the <code class="literal">S</code> modifier to include system
- objects. If <code class="literal">+</code> is appended to the command name, each
- language is listed with its call handler, validator, access privileges,
- and whether it is a system object.
- </p></dd><dt><span class="term"><code class="literal">\dn[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists schemas (namespaces). If <em class="replaceable"><code>pattern</code></em>
- is specified, only schemas whose names match the pattern are listed.
- By default, only user-created objects are shown; supply a
- pattern or the <code class="literal">S</code> modifier to include system objects.
- If <code class="literal">+</code> is appended to the command name, each object
- is listed with its associated permissions and description, if any.
- </p></dd><dt><span class="term"><code class="literal">\do[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists operators with their operand and result types.
- If <em class="replaceable"><code>pattern</code></em> is
- specified, only operators whose names match the pattern are listed.
- By default, only user-created objects are shown; supply a
- pattern or the <code class="literal">S</code> modifier to include system
- objects.
- If <code class="literal">+</code> is appended to the command name,
- additional information about each operator is shown, currently just
- the name of the underlying function.
- </p></dd><dt><span class="term"><code class="literal">\dO[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists collations.
- If <em class="replaceable"><code>pattern</code></em> is
- specified, only collations whose names match the pattern are
- listed. By default, only user-created objects are shown;
- supply a pattern or the <code class="literal">S</code> modifier to
- include system objects. If <code class="literal">+</code> is appended
- to the command name, each collation is listed with its associated
- description, if any.
- Note that only collations usable with the current database's encoding
- are shown, so the results may vary in different databases of the
- same installation.
- </p></dd><dt><span class="term"><code class="literal">\dp [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists tables, views and sequences with their
- associated access privileges.
- If <em class="replaceable"><code>pattern</code></em> is
- specified, only tables, views and sequences whose names match the
- pattern are listed.
- </p><p>
- The <a class="xref" href="sql-grant.html" title="GRANT"><span class="refentrytitle">GRANT</span></a> and
- <a class="xref" href="sql-revoke.html" title="REVOKE"><span class="refentrytitle">REVOKE</span></a>
- commands are used to set access privileges. The meaning of the
- privilege display is explained in
- <a class="xref" href="ddl-priv.html" title="5.7. Privileges">Section 5.7</a>.
- </p></dd><dt><span class="term"><code class="literal">\dP[itn+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists partitioned relations.
- If <em class="replaceable"><code>pattern</code></em>
- is specified, only entries whose name matches the pattern are listed.
- The modifiers <code class="literal">t</code> (tables) and <code class="literal">i</code>
- (indexes) can be appended to the command, filtering the kind of
- relations to list. By default, partitioned tables and indexes are
- listed.
- </p><p>
- If the modifier <code class="literal">n</code> (<span class="quote">“<span class="quote">nested</span>”</span>) is used,
- or a pattern is specified, then non-root partitioned relations are
- included, and a column is shown displaying the parent of each
- partitioned relation.
- </p><p>
- If <code class="literal">+</code> is appended to the command name, the sum of the
- sizes of each relation's partitions is also displayed, along with the
- relation's description.
- If <code class="literal">n</code> is combined with <code class="literal">+</code>, two
- sizes are shown: one including the total size of directly-attached
- leaf partitions, and another showing the total size of all partitions,
- including indirectly attached sub-partitions.
- </p></dd><dt><span class="term"><code class="literal">\drds [ <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></dt><dd><p>
- Lists defined configuration settings. These settings can be
- role-specific, database-specific, or both.
- <em class="replaceable"><code>role-pattern</code></em> and
- <em class="replaceable"><code>database-pattern</code></em> are used to select
- specific roles and databases to list, respectively. If omitted, or if
- <code class="literal">*</code> is specified, all settings are listed, including those
- not role-specific or database-specific, respectively.
- </p><p>
- The <a class="xref" href="sql-alterrole.html" title="ALTER ROLE"><span class="refentrytitle">ALTER ROLE</span></a> and
- <a class="xref" href="sql-alterdatabase.html" title="ALTER DATABASE"><span class="refentrytitle">ALTER DATABASE</span></a>
- commands are used to define per-role and per-database configuration
- settings.
- </p></dd><dt><span class="term"><code class="literal">\dRp[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists replication publications.
- If <em class="replaceable"><code>pattern</code></em> is
- specified, only those publications whose names match the pattern are
- listed.
- If <code class="literal">+</code> is appended to the command name, the tables
- associated with each publication are shown as well.
- </p></dd><dt><span class="term"><code class="literal">\dRs[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists replication subscriptions.
- If <em class="replaceable"><code>pattern</code></em> is
- specified, only those subscriptions whose names match the pattern are
- listed.
- If <code class="literal">+</code> is appended to the command name, additional
- properties of the subscriptions are shown.
- </p></dd><dt><span class="term"><code class="literal">\dT[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists data types.
- If <em class="replaceable"><code>pattern</code></em> is
- specified, only types whose names match the pattern are listed.
- If <code class="literal">+</code> is appended to the command name, each type is
- listed with its internal name and size, its allowed values
- if it is an <code class="type">enum</code> type, and its associated permissions.
- By default, only user-created objects are shown; supply a
- pattern or the <code class="literal">S</code> modifier to include system
- objects.
- </p></dd><dt><span class="term"><code class="literal">\du[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists database roles.
- (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
- unified into <span class="quote">“<span class="quote">roles</span>”</span>, this command is now equivalent to
- <code class="literal">\dg</code>.)
- By default, only user-created roles are shown; supply the
- <code class="literal">S</code> modifier to include system roles.
- If <em class="replaceable"><code>pattern</code></em> is specified,
- only those roles whose names match the pattern are listed.
- If the form <code class="literal">\du+</code> is used, additional information
- is shown about each role; currently this adds the comment for each
- role.
- </p></dd><dt><span class="term"><code class="literal">\dx[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists installed extensions.
- If <em class="replaceable"><code>pattern</code></em>
- is specified, only those extensions whose names match the pattern
- are listed.
- If the form <code class="literal">\dx+</code> is used, all the objects belonging
- to each matching extension are listed.
- </p></dd><dt><span class="term"><code class="literal">\dy[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists event triggers.
- If <em class="replaceable"><code>pattern</code></em>
- is specified, only those event triggers whose names match the pattern
- are listed.
- If <code class="literal">+</code> is appended to the command name, each object
- is listed with its associated description.
- </p></dd><dt><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></dt><dd><p>
- If <em class="replaceable"><code>filename</code></em> is
- specified, the file is edited; after the editor exits, the file's
- content is copied into the current query buffer. If no <em class="replaceable"><code>filename</code></em> is given, the current query
- buffer is copied to a temporary file which is then edited in the same
- fashion. Or, if the current query buffer is empty, the most recently
- executed query is copied to a temporary file and edited in the same
- fashion.
- </p><p>
- The new contents of the query buffer are then re-parsed according to
- the normal rules of <span class="application">psql</span>, treating the
- whole buffer as a single line. Any complete queries are immediately
- executed; that is, if the query buffer contains or ends with a
- semicolon, everything up to that point is executed. Whatever remains
- will wait in the query buffer; type semicolon or <code class="literal">\g</code> to
- send it, or <code class="literal">\r</code> to cancel it by clearing the query buffer.
- Treating the buffer as a single line primarily affects meta-commands:
- whatever is in the buffer after a meta-command will be taken as
- argument(s) to the meta-command, even if it spans multiple lines.
- (Thus you cannot make meta-command-using scripts this way.
- Use <code class="command">\i</code> for that.)
- </p><p>
- If a line number is specified, <span class="application">psql</span> will
- position the cursor on the specified line of the file or query buffer.
- Note that if a single all-digits argument is given,
- <span class="application">psql</span> assumes it is a line number,
- not a file name.
- </p><div class="tip"><h3 class="title">Tip</h3><p>
- See under <a class="xref" href="app-psql.html#APP-PSQL-ENVIRONMENT" title="Environment">Environment</a> for how to configure and
- customize your editor.
- </p></div></dd><dt><span class="term"><code class="literal">\echo <em class="replaceable"><code>text</code></em> [ ... ]</code></span></dt><dd><p>
- Prints the arguments to the standard output, separated by one
- space and followed by a newline. This can be useful to
- intersperse information in the output of scripts. For example:
- </p><pre class="programlisting">
- => <strong class="userinput"><code>\echo `date`</code></strong>
- Tue Oct 26 21:40:57 CEST 1999
- </pre><p>
- If the first argument is an unquoted <code class="literal">-n</code> the trailing
- newline is not written.
- </p><div class="tip"><h3 class="title">Tip</h3><p>
- If you use the <code class="command">\o</code> command to redirect your
- query output you might wish to use <code class="command">\qecho</code>
- instead of this command.
- </p></div></dd><dt><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></dt><dd><p>
- This command fetches and edits the definition of the named function or procedure,
- in the form of a <code class="command">CREATE OR REPLACE FUNCTION</code> or
- <code class="command">CREATE OR REPLACE PROCEDURE</code> command.
- Editing is done in the same way as for <code class="literal">\edit</code>.
- After the editor exits, the updated command waits in the query buffer;
- type semicolon or <code class="literal">\g</code> to send it, or <code class="literal">\r</code>
- to cancel.
- </p><p>
- The target function can be specified by name alone, or by name
- and arguments, for example <code class="literal">foo(integer, text)</code>.
- The argument types must be given if there is more
- than one function of the same name.
- </p><p>
- If no function is specified, a blank <code class="command">CREATE FUNCTION</code>
- template is presented for editing.
- </p><p>
- If a line number is specified, <span class="application">psql</span> will
- position the cursor on the specified line of the function body.
- (Note that the function body typically does not begin on the first
- line of the file.)
- </p><p>
- Unlike most other meta-commands, the entire remainder of the line is
- always taken to be the argument(s) of <code class="command">\ef</code>, and neither
- variable interpolation nor backquote expansion are performed in the
- arguments.
- </p><div class="tip"><h3 class="title">Tip</h3><p>
- See under <a class="xref" href="app-psql.html#APP-PSQL-ENVIRONMENT" title="Environment">Environment</a> for how to configure and
- customize your editor.
- </p></div></dd><dt><span class="term"><code class="literal">\encoding [ <em class="replaceable"><code>encoding</code></em> ]</code></span></dt><dd><p>
- Sets the client character set encoding. Without an argument, this command
- shows the current encoding.
- </p></dd><dt><span class="term"><code class="literal">\errverbose</code></span></dt><dd><p>
- Repeats the most recent server error message at maximum
- verbosity, as though <code class="varname">VERBOSITY</code> were set
- to <code class="literal">verbose</code> and <code class="varname">SHOW_CONTEXT</code> were
- set to <code class="literal">always</code>.
- </p></dd><dt><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></dt><dd><p>
- This command fetches and edits the definition of the named view,
- in the form of a <code class="command">CREATE OR REPLACE VIEW</code> command.
- Editing is done in the same way as for <code class="literal">\edit</code>.
- After the editor exits, the updated command waits in the query buffer;
- type semicolon or <code class="literal">\g</code> to send it, or <code class="literal">\r</code>
- to cancel.
- </p><p>
- If no view is specified, a blank <code class="command">CREATE VIEW</code>
- template is presented for editing.
- </p><p>
- If a line number is specified, <span class="application">psql</span> will
- position the cursor on the specified line of the view definition.
- </p><p>
- Unlike most other meta-commands, the entire remainder of the line is
- always taken to be the argument(s) of <code class="command">\ev</code>, and neither
- variable interpolation nor backquote expansion are performed in the
- arguments.
- </p></dd><dt><span class="term"><code class="literal">\f [ <em class="replaceable"><code>string</code></em> ]</code></span></dt><dd><p>
- Sets the field separator for unaligned query output. The default
- is the vertical bar (<code class="literal">|</code>). It is equivalent to
- <code class="command">\pset fieldsep</code>.
- </p></dd><dt><span class="term"><code class="literal">\g [ <em class="replaceable"><code>filename</code></em> ]</code><br /></span><span class="term"><code class="literal">\g [ |<em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
- Sends the current query buffer to the server for execution.
- If an argument is given, the query's output is written to the named
- file or piped to the given shell command, instead of displaying it as
- usual. The file or command is written to only if the query
- successfully returns zero or more tuples, not if the query fails or
- is a non-data-returning SQL command.
- </p><p>
- If the current query buffer is empty, the most recently sent query is
- re-executed instead. Except for that behavior, <code class="literal">\g</code>
- without an argument is essentially equivalent to a semicolon.
- A <code class="literal">\g</code> with argument is a <span class="quote">“<span class="quote">one-shot</span>”</span>
- alternative to the <code class="command">\o</code> command.
- </p><p>
- If the argument begins with <code class="literal">|</code>, then the entire remainder
- of the line is taken to be
- the <em class="replaceable"><code>command</code></em> to execute,
- and neither variable interpolation nor backquote expansion are
- performed in it. The rest of the line is simply passed literally to
- the shell.
- </p></dd><dt><span class="term"><code class="literal">\gdesc</code></span></dt><dd><p>
- Shows the description (that is, the column names and data types)
- of the result of the current query buffer. The query is not
- actually executed; however, if it contains some type of syntax
- error, that error will be reported in the normal way.
- </p><p>
- If the current query buffer is empty, the most recently sent query
- is described instead.
- </p></dd><dt><span class="term"><code class="literal">\gexec</code></span></dt><dd><p>
- Sends the current query buffer to the server, then treats
- each column of each row of the query's output (if any) as a SQL
- statement to be executed. For example, to create an index on each
- column of <code class="structname">my_table</code>:
- </p><pre class="programlisting">
- => <strong class="userinput"><code>SELECT format('create index on my_table(%I)', attname)</code></strong>
- -> <strong class="userinput"><code>FROM pg_attribute</code></strong>
- -> <strong class="userinput"><code>WHERE attrelid = 'my_table'::regclass AND attnum > 0</code></strong>
- -> <strong class="userinput"><code>ORDER BY attnum</code></strong>
- -> <strong class="userinput"><code>\gexec</code></strong>
- CREATE INDEX
- CREATE INDEX
- CREATE INDEX
- CREATE INDEX
- </pre><p>
- </p><p>
- The generated queries are executed in the order in which the rows
- are returned, and left-to-right within each row if there is more
- than one column. NULL fields are ignored. The generated queries
- are sent literally to the server for processing, so they cannot be
- <span class="application">psql</span> meta-commands nor contain <span class="application">psql</span>
- variable references. If any individual query fails, execution of
- the remaining queries continues
- unless <code class="varname">ON_ERROR_STOP</code> is set. Execution of each
- query is subject to <code class="varname">ECHO</code> processing.
- (Setting <code class="varname">ECHO</code> to <code class="literal">all</code>
- or <code class="literal">queries</code> is often advisable when
- using <code class="command">\gexec</code>.) Query logging, single-step mode,
- timing, and other query execution features apply to each generated
- query as well.
- </p><p>
- If the current query buffer is empty, the most recently sent query
- is re-executed instead.
- </p></dd><dt><span class="term"><code class="literal">\gset [ <em class="replaceable"><code>prefix</code></em> ]</code></span></dt><dd><p>
- Sends the current query buffer to the server and stores the
- query's output into <span class="application">psql</span> variables (see <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a>).
- The query to be executed must return exactly one row. Each column of
- the row is stored into a separate variable, named the same as the
- column. For example:
- </p><pre class="programlisting">
- => <strong class="userinput"><code>SELECT 'hello' AS var1, 10 AS var2</code></strong>
- -> <strong class="userinput"><code>\gset</code></strong>
- => <strong class="userinput"><code>\echo :var1 :var2</code></strong>
- hello 10
- </pre><p>
- </p><p>
- If you specify a <em class="replaceable"><code>prefix</code></em>,
- that string is prepended to the query's column names to create the
- variable names to use:
- </p><pre class="programlisting">
- => <strong class="userinput"><code>SELECT 'hello' AS var1, 10 AS var2</code></strong>
- -> <strong class="userinput"><code>\gset result_</code></strong>
- => <strong class="userinput"><code>\echo :result_var1 :result_var2</code></strong>
- hello 10
- </pre><p>
- </p><p>
- If a column result is NULL, the corresponding variable is unset
- rather than being set.
- </p><p>
- If the query fails or does not return one row,
- no variables are changed.
- </p><p>
- If the current query buffer is empty, the most recently sent query
- is re-executed instead.
- </p></dd><dt><span class="term"><code class="literal">\gx [ <em class="replaceable"><code>filename</code></em> ]</code><br /></span><span class="term"><code class="literal">\gx [ |<em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
- <code class="literal">\gx</code> is equivalent to <code class="literal">\g</code>, but
- forces expanded output mode for this query. See <code class="literal">\x</code>.
- </p></dd><dt><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></dt><dd><p>
- Gives syntax help on the specified <acronym class="acronym">SQL</acronym>
- command. If <em class="replaceable"><code>command</code></em>
- is not specified, then <span class="application">psql</span> will list
- all the commands for which syntax help is available. If
- <em class="replaceable"><code>command</code></em> is an
- asterisk (<code class="literal">*</code>), then syntax help on all
- <acronym class="acronym">SQL</acronym> commands is shown.
- </p><p>
- Unlike most other meta-commands, the entire remainder of the line is
- always taken to be the argument(s) of <code class="command">\help</code>, and neither
- variable interpolation nor backquote expansion are performed in the
- arguments.
- </p><div class="note"><h3 class="title">Note</h3><p>
- To simplify typing, commands that consists of several words do
- not have to be quoted. Thus it is fine to type <strong class="userinput"><code>\help
- alter table</code></strong>.
- </p></div></dd><dt><span class="term"><code class="literal">\H</code> or <code class="literal">\html</code></span></dt><dd><p>
- Turns on <acronym class="acronym">HTML</acronym> query output format. If the
- <acronym class="acronym">HTML</acronym> format is already on, it is switched
- back to the default aligned text format. This command is for
- compatibility and convenience, but see <code class="command">\pset</code>
- about setting other output options.
- </p></dd><dt><span class="term"><code class="literal">\i</code> or <code class="literal">\include</code> <em class="replaceable"><code>filename</code></em></span></dt><dd><p>
- Reads input from the file <em class="replaceable"><code>filename</code></em> and executes it as
- though it had been typed on the keyboard.
- </p><p>
- If <em class="replaceable"><code>filename</code></em> is <code class="literal">-</code>
- (hyphen), then standard input is read until an EOF indication
- or <code class="command">\q</code> meta-command. This can be used to intersperse
- interactive input with input from files. Note that Readline behavior
- will be used only if it is active at the outermost level.
- </p><div class="note"><h3 class="title">Note</h3><p>
- If you want to see the lines on the screen as they are read you
- must set the variable <code class="varname">ECHO</code> to
- <code class="literal">all</code>.
- </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></dt><dd><p>
- This group of commands implements nestable conditional blocks.
- A conditional block must begin with an <code class="command">\if</code> and end
- with an <code class="command">\endif</code>. In between there may be any number
- of <code class="command">\elif</code> clauses, which may optionally be followed
- by a single <code class="command">\else</code> clause. Ordinary queries and
- other types of backslash commands may (and usually do) appear between
- the commands forming a conditional block.
- </p><p>
- The <code class="command">\if</code> and <code class="command">\elif</code> commands read
- their argument(s) and evaluate them as a boolean expression. If the
- expression yields <code class="literal">true</code> then processing continues
- normally; otherwise, lines are skipped until a
- matching <code class="command">\elif</code>, <code class="command">\else</code>,
- or <code class="command">\endif</code> is reached. Once
- an <code class="command">\if</code> or <code class="command">\elif</code> test has
- succeeded, the arguments of later <code class="command">\elif</code> commands in
- the same block are not evaluated but are treated as false. Lines
- following an <code class="command">\else</code> are processed only if no earlier
- matching <code class="command">\if</code> or <code class="command">\elif</code> succeeded.
- </p><p>
- The <em class="replaceable"><code>expression</code></em> argument
- of an <code class="command">\if</code> or <code class="command">\elif</code> command
- is subject to variable interpolation and backquote expansion, just
- like any other backslash command argument. After that it is evaluated
- like the value of an on/off option variable. So a valid value
- is any unambiguous case-insensitive match for one of:
- <code class="literal">true</code>, <code class="literal">false</code>, <code class="literal">1</code>,
- <code class="literal">0</code>, <code class="literal">on</code>, <code class="literal">off</code>,
- <code class="literal">yes</code>, <code class="literal">no</code>. For example,
- <code class="literal">t</code>, <code class="literal">T</code>, and <code class="literal">tR</code>
- will all be considered to be <code class="literal">true</code>.
- </p><p>
- Expressions that do not properly evaluate to true or false will
- generate a warning and be treated as false.
- </p><p>
- Lines being skipped are parsed normally to identify queries and
- backslash commands, but queries are not sent to the server, and
- backslash commands other than conditionals
- (<code class="command">\if</code>, <code class="command">\elif</code>,
- <code class="command">\else</code>, <code class="command">\endif</code>) are
- ignored. Conditional commands are checked only for valid nesting.
- Variable references in skipped lines are not expanded, and backquote
- expansion is not performed either.
- </p><p>
- All the backslash commands of a given conditional block must appear in
- the same source file. If EOF is reached on the main input file or an
- <code class="command">\include</code>-ed file before all local
- <code class="command">\if</code>-blocks have been closed,
- then <span class="application">psql</span> will raise an error.
- </p><p>
- Here is an example:
- </p><pre class="programlisting">
- -- check for the existence of two separate records in the database and store
- -- the results in separate psql variables
- SELECT
- EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer,
- EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee
- \gset
- \if :is_customer
- SELECT * FROM customer WHERE customer_id = 123;
- \elif :is_employee
- \echo 'is not a customer but is an employee'
- SELECT * FROM employee WHERE employee_id = 456;
- \else
- \if yes
- \echo 'not a customer or employee'
- \else
- \echo 'this will never print'
- \endif
- \endif
- </pre></dd><dt><span class="term"><code class="literal">\ir</code> or <code class="literal">\include_relative</code> <em class="replaceable"><code>filename</code></em></span></dt><dd><p>
- The <code class="literal">\ir</code> command is similar to <code class="literal">\i</code>, but resolves
- relative file names differently. When executing in interactive mode,
- the two commands behave identically. However, when invoked from a
- script, <code class="literal">\ir</code> interprets file names relative to the
- directory in which the script is located, rather than the current
- working directory.
- </p></dd><dt><span class="term"><code class="literal">\l[+]</code> or <code class="literal">\list[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- List the databases in the server and show their names, owners,
- character set encodings, and access privileges.
- If <em class="replaceable"><code>pattern</code></em> is specified,
- only databases whose names match the pattern are listed.
- If <code class="literal">+</code> is appended to the command name, database
- sizes, default tablespaces, and descriptions are also displayed.
- (Size information is only available for databases that the current
- user can connect to.)
- </p></dd><dt><span class="term"><code class="literal">\lo_export <em class="replaceable"><code>loid</code></em> <em class="replaceable"><code>filename</code></em></code></span></dt><dd><p>
- Reads the large object with <acronym class="acronym">OID</acronym> <em class="replaceable"><code>loid</code></em> from the database and
- writes it to <em class="replaceable"><code>filename</code></em>. Note that this is
- subtly different from the server function
- <code class="function">lo_export</code>, which acts with the permissions
- of the user that the database server runs as and on the server's
- file system.
- </p><div class="tip"><h3 class="title">Tip</h3><p>
- Use <code class="command">\lo_list</code> to find out the large object's
- <acronym class="acronym">OID</acronym>.
- </p></div></dd><dt><span class="term"><code class="literal">\lo_import <em class="replaceable"><code>filename</code></em> [ <em class="replaceable"><code>comment</code></em> ]</code></span></dt><dd><p>
- Stores the file into a <span class="productname">PostgreSQL</span>
- large object. Optionally, it associates the given
- comment with the object. Example:
- </p><pre class="programlisting">
- foo=> <strong class="userinput"><code>\lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'</code></strong>
- lo_import 152801
- </pre><p>
- The response indicates that the large object received object
- ID 152801, which can be used to access the newly-created large
- object in the future. For the sake of readability, it is
- recommended to always associate a human-readable comment with
- every object. Both OIDs and comments can be viewed with the
- <code class="command">\lo_list</code> command.
- </p><p>
- Note that this command is subtly different from the server-side
- <code class="function">lo_import</code> because it acts as the local user
- on the local file system, rather than the server's user and file
- system.
- </p></dd><dt><span class="term"><code class="literal">\lo_list</code></span></dt><dd><p>
- Shows a list of all <span class="productname">PostgreSQL</span>
- large objects currently stored in the database,
- along with any comments provided for them.
- </p></dd><dt><span class="term"><code class="literal">\lo_unlink <em class="replaceable"><code>loid</code></em></code></span></dt><dd><p>
- Deletes the large object with <acronym class="acronym">OID</acronym>
- <em class="replaceable"><code>loid</code></em> from the
- database.
- </p><div class="tip"><h3 class="title">Tip</h3><p>
- Use <code class="command">\lo_list</code> to find out the large object's
- <acronym class="acronym">OID</acronym>.
- </p></div></dd><dt><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></dt><dd><p>
- Arranges to save future query results to the file <em class="replaceable"><code>filename</code></em> or pipe future results
- to the shell command <em class="replaceable"><code>command</code></em>. If no argument is
- specified, the query output is reset to the standard output.
- </p><p>
- If the argument begins with <code class="literal">|</code>, then the entire remainder
- of the line is taken to be
- the <em class="replaceable"><code>command</code></em> to execute,
- and neither variable interpolation nor backquote expansion are
- performed in it. The rest of the line is simply passed literally to
- the shell.
- </p><p>
- <span class="quote">“<span class="quote">Query results</span>”</span> includes all tables, command
- responses, and notices obtained from the database server, as
- well as output of various backslash commands that query the
- database (such as <code class="command">\d</code>); but not error
- messages.
- </p><div class="tip"><h3 class="title">Tip</h3><p>
- To intersperse text output in between query results, use
- <code class="command">\qecho</code>.
- </p></div></dd><dt><span class="term"><code class="literal">\p</code> or <code class="literal">\print</code></span></dt><dd><p>
- Print the current query buffer to the standard output.
- If the current query buffer is empty, the most recently executed query
- is printed instead.
- </p></dd><dt><span class="term"><code class="literal">\password [ <em class="replaceable"><code>username</code></em> ]</code></span></dt><dd><p>
- Changes the password of the specified user (by default, the current
- user). This command prompts for the new password, encrypts it, and
- sends it to the server as an <code class="command">ALTER ROLE</code> command. This
- makes sure that the new password does not appear in cleartext in the
- command history, the server log, or elsewhere.
- </p></dd><dt><span class="term"><code class="literal">\prompt [ <em class="replaceable"><code>text</code></em> ] <em class="replaceable"><code>name</code></em></code></span></dt><dd><p>
- Prompts the user to supply text, which is assigned to the variable
- <em class="replaceable"><code>name</code></em>.
- An optional prompt string, <em class="replaceable"><code>text</code></em>, can be specified. (For multiword
- prompts, surround the text with single quotes.)
- </p><p>
- By default, <code class="literal">\prompt</code> uses the terminal for input and
- output. However, if the <code class="option">-f</code> command line switch was
- used, <code class="literal">\prompt</code> uses standard input and standard output.
- </p></dd><dt><span class="term"><code class="literal">\pset [ <em class="replaceable"><code>option</code></em> [ <em class="replaceable"><code>value</code></em> ] ]</code></span></dt><dd><p>
- This command sets options affecting the output of query result tables.
- <em class="replaceable"><code>option</code></em>
- indicates which option is to be set. The semantics of
- <em class="replaceable"><code>value</code></em> vary depending
- on the selected option. For some options, omitting <em class="replaceable"><code>value</code></em> causes the option to be toggled
- or unset, as described under the particular option. If no such
- behavior is mentioned, then omitting
- <em class="replaceable"><code>value</code></em> just results in
- the current setting being displayed.
- </p><p>
- <code class="command">\pset</code> without any arguments displays the current status
- of all printing options.
- </p><p>
- Adjustable printing options are:
- </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">border</code></span></dt><dd><p>
- The <em class="replaceable"><code>value</code></em> must be a
- number. In general, the higher
- the number the more borders and lines the tables will have,
- but details depend on the particular format.
- In <acronym class="acronym">HTML</acronym> format, this will translate directly
- into the <code class="literal">border=...</code> attribute.
- In most other formats only values 0 (no border), 1 (internal
- dividing lines), and 2 (table frame) make sense, and values above 2
- will be treated the same as <code class="literal">border = 2</code>.
- The <code class="literal">latex</code> and <code class="literal">latex-longtable</code>
- formats additionally allow a value of 3 to add dividing lines
- between data rows.
- </p></dd><dt><span class="term"><code class="literal">columns</code></span></dt><dd><p>
- Sets the target width for the <code class="literal">wrapped</code> format, and also
- the width limit for determining whether output is wide enough to
- require the pager or switch to the vertical display in expanded auto
- mode.
- Zero (the default) causes the target width to be controlled by the
- environment variable <code class="envar">COLUMNS</code>, or the detected screen width
- if <code class="envar">COLUMNS</code> is not set.
- In addition, if <code class="literal">columns</code> is zero then the
- <code class="literal">wrapped</code> format only affects screen output.
- If <code class="literal">columns</code> is nonzero then file and pipe output is
- wrapped to that width as well.
- </p></dd><dt><span class="term"><code class="literal">csv_fieldsep</code></span></dt><dd><p>
- Specifies the field separator to be used in
- <acronym class="acronym">CSV</acronym> output format. If the separator character
- appears in a field's value, that field is output within double
- quotes, following standard <acronym class="acronym">CSV</acronym> rules.
- The default is a comma.
- </p></dd><dt><span class="term"><code class="literal">expanded</code> (or <code class="literal">x</code>)</span></dt><dd><p>
- If <em class="replaceable"><code>value</code></em> is specified it
- must be either <code class="literal">on</code> or <code class="literal">off</code>, which
- will enable or disable expanded mode, or <code class="literal">auto</code>.
- If <em class="replaceable"><code>value</code></em> is omitted the
- command toggles between the on and off settings. When expanded mode
- is enabled, query results are displayed in two columns, with the
- column name on the left and the data on the right. This mode is
- useful if the data wouldn't fit on the screen in the
- normal <span class="quote">“<span class="quote">horizontal</span>”</span> mode. In the auto setting, the
- expanded mode is used whenever the query output has more than one
- column and is wider than the screen; otherwise, the regular mode is
- used. The auto setting is only
- effective in the aligned and wrapped formats. In other formats, it
- always behaves as if the expanded mode is off.
- </p></dd><dt><span class="term"><code class="literal">fieldsep</code></span></dt><dd><p>
- Specifies the field separator to be used in unaligned output
- format. That way one can create, for example, tab-separated
- output, which other programs might prefer. To
- set a tab as field separator, type <code class="literal">\pset fieldsep
- '\t'</code>. The default field separator is
- <code class="literal">'|'</code> (a vertical bar).
- </p></dd><dt><span class="term"><code class="literal">fieldsep_zero</code></span></dt><dd><p>
- Sets the field separator to use in unaligned output format to a zero
- byte.
- </p></dd><dt><span class="term"><code class="literal">footer</code></span></dt><dd><p>
- If <em class="replaceable"><code>value</code></em> is specified
- it must be either <code class="literal">on</code> or <code class="literal">off</code>
- which will enable or disable display of the table footer
- (the <code class="literal">(<em class="replaceable"><code>n</code></em> rows)</code> count).
- If <em class="replaceable"><code>value</code></em> is omitted the
- command toggles footer display on or off.
- </p></dd><dt><span class="term"><code class="literal">format</code></span></dt><dd><p>
- Sets the output format to one of <code class="literal">aligned</code>,
- <code class="literal">asciidoc</code>,
- <code class="literal">csv</code>,
- <code class="literal">html</code>,
- <code class="literal">latex</code>,
- <code class="literal">latex-longtable</code>, <code class="literal">troff-ms</code>,
- <code class="literal">unaligned</code>, or <code class="literal">wrapped</code>.
- Unique abbreviations are allowed.
- </p><p><code class="literal">aligned</code> format is the standard,
- human-readable, nicely formatted text output; this is the default.
- </p><p><code class="literal">unaligned</code> format writes all columns of a row on one
- line, separated by the currently active field separator. This
- is useful for creating output that might be intended to be read
- in by other programs, for example, tab-separated or comma-separated
- format. However, the field separator character is not treated
- specially if it appears in a column's value;
- so <acronym class="acronym">CSV</acronym> format may be better suited for such
- purposes.
- </p><p><code class="literal">csv</code> format
- <a id="id-1.9.4.18.8.4.10.1.69.2.3.1.8.2.4.2" class="indexterm"></a>
- writes column values separated by commas, applying the quoting
- rules described in
- <a class="ulink" href="https://tools.ietf.org/html/rfc4180" target="_top">RFC 4180</a>.
- This output is compatible with the CSV format of the server's
- <code class="command">COPY</code> command.
- A header line with column names is generated unless
- the <code class="literal">tuples_only</code> parameter is
- <code class="literal">on</code>. Titles and footers are not printed.
- Each row is terminated by the system-dependent end-of-line character,
- which is typically a single newline (<code class="literal">\n</code>) for
- Unix-like systems or a carriage return and newline sequence
- (<code class="literal">\r\n</code>) for Microsoft Windows.
- Field separator characters other than comma can be selected with
- <code class="command">\pset csv_fieldsep</code>.
- </p><p><code class="literal">wrapped</code> format is like <code class="literal">aligned</code> but wraps
- wide data values across lines to make the output fit in the target
- column width. The target width is determined as described under
- the <code class="literal">columns</code> option. Note that <span class="application">psql</span> will
- not attempt to wrap column header titles; therefore,
- <code class="literal">wrapped</code> format behaves the same as <code class="literal">aligned</code>
- if the total width needed for column headers exceeds the target.
- </p><p>
- The <code class="literal">asciidoc</code>, <code class="literal">html</code>,
- <code class="literal">latex</code>, <code class="literal">latex-longtable</code>, and
- <code class="literal">troff-ms</code> formats put out tables that are intended
- to be included in documents using the respective mark-up
- language. They are not complete documents! This might not be
- necessary in <acronym class="acronym">HTML</acronym>, but in
- <span class="application">LaTeX</span> you must have a complete
- document wrapper.
- The <code class="literal">latex</code> format
- uses <span class="application">LaTeX</span>'s <code class="literal">tabular</code>
- environment.
- The <code class="literal">latex-longtable</code> format
- requires the <span class="application">LaTeX</span>
- <code class="literal">longtable</code> and <code class="literal">booktabs</code> packages.
- </p></dd><dt><span class="term"><code class="literal">linestyle</code></span></dt><dd><p>
- Sets the border line drawing style to one
- of <code class="literal">ascii</code>, <code class="literal">old-ascii</code>,
- or <code class="literal">unicode</code>.
- Unique abbreviations are allowed. (That would mean one
- letter is enough.)
- The default setting is <code class="literal">ascii</code>.
- This option only affects the <code class="literal">aligned</code> and
- <code class="literal">wrapped</code> output formats.
- </p><p><code class="literal">ascii</code> style uses plain <acronym class="acronym">ASCII</acronym>
- characters. Newlines in data are shown using
- a <code class="literal">+</code> symbol in the right-hand margin.
- When the <code class="literal">wrapped</code> format wraps data from
- one line to the next without a newline character, a dot
- (<code class="literal">.</code>) is shown in the right-hand margin of the first line,
- and again in the left-hand margin of the following line.
- </p><p><code class="literal">old-ascii</code> style uses plain <acronym class="acronym">ASCII</acronym>
- characters, using the formatting style used
- in <span class="productname">PostgreSQL</span> 8.4 and earlier.
- Newlines in data are shown using a <code class="literal">:</code>
- symbol in place of the left-hand column separator.
- When the data is wrapped from one line
- to the next without a newline character, a <code class="literal">;</code>
- symbol is used in place of the left-hand column separator.
- </p><p><code class="literal">unicode</code> style uses Unicode box-drawing characters.
- Newlines in data are shown using a carriage return symbol
- in the right-hand margin. When the data is wrapped from one line
- to the next without a newline character, an ellipsis symbol
- is shown in the right-hand margin of the first line, and
- again in the left-hand margin of the following line.
- </p><p>
- When the <code class="literal">border</code> setting is greater than zero,
- the <code class="literal">linestyle</code> option also determines the
- characters with which the border lines are drawn.
- Plain <acronym class="acronym">ASCII</acronym> characters work everywhere, but
- Unicode characters look nicer on displays that recognize them.
- </p></dd><dt><span class="term"><code class="literal">null</code></span></dt><dd><p>
- Sets the string to be printed in place of a null value.
- The default is to print nothing, which can easily be mistaken for
- an empty string. For example, one might prefer <code class="literal">\pset null
- '(null)'</code>.
- </p></dd><dt><span class="term"><code class="literal">numericlocale</code></span></dt><dd><p>
- If <em class="replaceable"><code>value</code></em> is specified
- it must be either <code class="literal">on</code> or <code class="literal">off</code>
- which will enable or disable display of a locale-specific character
- to separate groups of digits to the left of the decimal marker.
- If <em class="replaceable"><code>value</code></em> is omitted the
- command toggles between regular and locale-specific numeric output.
- </p></dd><dt><span class="term"><code class="literal">pager</code></span></dt><dd><p>
- Controls use of a pager program for query and <span class="application">psql</span>
- help output. If the environment variable <code class="envar">PSQL_PAGER</code>
- or <code class="envar">PAGER</code> is set, the output is piped to the
- specified program. Otherwise a platform-dependent default program
- (such as <code class="filename">more</code>) is used.
- </p><p>
- When the <code class="literal">pager</code> option is <code class="literal">off</code>, the pager
- program is not used. When the <code class="literal">pager</code> option is
- <code class="literal">on</code>, the pager is used when appropriate, i.e., when the
- output is to a terminal and will not fit on the screen.
- The <code class="literal">pager</code> option can also be set to <code class="literal">always</code>,
- which causes the pager to be used for all terminal output regardless
- of whether it fits on the screen. <code class="literal">\pset pager</code>
- without a <em class="replaceable"><code>value</code></em>
- toggles pager use on and off.
- </p></dd><dt><span class="term"><code class="literal">pager_min_lines</code></span></dt><dd><p>
- If <code class="literal">pager_min_lines</code> is set to a number greater than the
- page height, the pager program will not be called unless there are
- at least this many lines of output to show. The default setting
- is 0.
- </p></dd><dt><span class="term"><code class="literal">recordsep</code></span></dt><dd><p>
- Specifies the record (line) separator to use in unaligned
- output format. The default is a newline character.
- </p></dd><dt><span class="term"><code class="literal">recordsep_zero</code></span></dt><dd><p>
- Sets the record separator to use in unaligned output format to a zero
- byte.
- </p></dd><dt><span class="term"><code class="literal">tableattr</code> (or <code class="literal">T</code>)</span></dt><dd><p>
- In <acronym class="acronym">HTML</acronym> format, this specifies attributes
- to be placed inside the <code class="sgmltag-element">table</code> tag. This
- could for example be <code class="literal">cellpadding</code> or
- <code class="literal">bgcolor</code>. Note that you probably don't want
- to specify <code class="literal">border</code> here, as that is already
- taken care of by <code class="literal">\pset border</code>.
- If no
- <em class="replaceable"><code>value</code></em> is given,
- the table attributes are unset.
- </p><p>
- In <code class="literal">latex-longtable</code> format, this controls
- the proportional width of each column containing a left-aligned
- data type. It is specified as a whitespace-separated list of values,
- e.g. <code class="literal">'0.2 0.2 0.6'</code>. Unspecified output columns
- use the last specified value.
- </p></dd><dt><span class="term"><code class="literal">title</code> (or <code class="literal">C</code>)</span></dt><dd><p>
- Sets the table title for any subsequently printed tables. This
- can be used to give your output descriptive tags. If no
- <em class="replaceable"><code>value</code></em> is given,
- the title is unset.
- </p></dd><dt><span class="term"><code class="literal">tuples_only</code> (or <code class="literal">t</code>)</span></dt><dd><p>
- If <em class="replaceable"><code>value</code></em> is specified
- it must be either <code class="literal">on</code> or <code class="literal">off</code>
- which will enable or disable tuples-only mode.
- If <em class="replaceable"><code>value</code></em> is omitted the
- command toggles between regular and tuples-only output.
- Regular output includes extra information such
- as column headers, titles, and various footers. In tuples-only
- mode, only actual table data is shown.
- </p></dd><dt><span class="term"><code class="literal">unicode_border_linestyle</code></span></dt><dd><p>
- Sets the border drawing style for the <code class="literal">unicode</code>
- line style to one of <code class="literal">single</code>
- or <code class="literal">double</code>.
- </p></dd><dt><span class="term"><code class="literal">unicode_column_linestyle</code></span></dt><dd><p>
- Sets the column drawing style for the <code class="literal">unicode</code>
- line style to one of <code class="literal">single</code>
- or <code class="literal">double</code>.
- </p></dd><dt><span class="term"><code class="literal">unicode_header_linestyle</code></span></dt><dd><p>
- Sets the header drawing style for the <code class="literal">unicode</code>
- line style to one of <code class="literal">single</code>
- or <code class="literal">double</code>.
- </p></dd></dl></div><p>
- </p><p>
- Illustrations of how these different formats look can be seen in
- the <a class="xref" href="app-psql.html#APP-PSQL-EXAMPLES" title="Examples">Examples</a> section.
- </p><div class="tip"><h3 class="title">Tip</h3><p>
- There are various shortcut commands for <code class="command">\pset</code>. See
- <code class="command">\a</code>, <code class="command">\C</code>, <code class="command">\f</code>,
- <code class="command">\H</code>, <code class="command">\t</code>, <code class="command">\T</code>,
- and <code class="command">\x</code>.
- </p></div></dd><dt><span class="term"><code class="literal">\q</code> or <code class="literal">\quit</code></span></dt><dd><p>
- Quits the <span class="application">psql</span> program.
- In a script file, only execution of that script is terminated.
- </p></dd><dt><span class="term"><code class="literal">\qecho <em class="replaceable"><code>text</code></em> [ ... ] </code></span></dt><dd><p>
- This command is identical to <code class="command">\echo</code> except
- that the output will be written to the query output channel, as
- set by <code class="command">\o</code>.
- </p></dd><dt><span class="term"><code class="literal">\r</code> or <code class="literal">\reset</code></span></dt><dd><p>
- Resets (clears) the query buffer.
- </p></dd><dt><span class="term"><code class="literal">\s [ <em class="replaceable"><code>filename</code></em> ]</code></span></dt><dd><p>
- Print <span class="application">psql</span>'s command line history
- to <em class="replaceable"><code>filename</code></em>.
- If <em class="replaceable"><code>filename</code></em> is omitted,
- the history is written to the standard output (using the pager if
- appropriate). This command is not available
- if <span class="application">psql</span> was built
- without <span class="application">Readline</span> support.
- </p></dd><dt><span class="term"><code class="literal">\set [ <em class="replaceable"><code>name</code></em> [ <em class="replaceable"><code>value</code></em> [ ... ] ] ]</code></span></dt><dd><p>
- 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
- is given, to the concatenation of all of them. If only one
- argument is given, the variable is set to an empty-string value. To
- unset a variable, use the <code class="command">\unset</code> command.
- </p><p><code class="command">\set</code> without any arguments displays the names and values
- of all currently-set <span class="application">psql</span> variables.
- </p><p>
- Valid variable names can contain letters, digits, and
- underscores. See the section <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a> below for details.
- Variable names are case-sensitive.
- </p><p>
- Certain variables are special, in that they
- control <span class="application">psql</span>'s behavior or are
- automatically set to reflect connection state. These variables are
- documented in <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a>, below.
- </p><div class="note"><h3 class="title">Note</h3><p>
- This command is unrelated to the <acronym class="acronym">SQL</acronym>
- command <a class="xref" href="sql-set.html" title="SET"><span class="refentrytitle">SET</span></a>.
- </p></div></dd><dt><span class="term"><code class="literal">\setenv <em class="replaceable"><code>name</code></em> [ <em class="replaceable"><code>value</code></em> ]</code></span></dt><dd><p>
- Sets the environment variable <em class="replaceable"><code>name</code></em> to <em class="replaceable"><code>value</code></em>, or if the
- <em class="replaceable"><code>value</code></em> is
- not supplied, unsets the environment variable. Example:
- </p><pre class="programlisting">
- testdb=> <strong class="userinput"><code>\setenv PAGER less</code></strong>
- testdb=> <strong class="userinput"><code>\setenv LESS -imx4F</code></strong>
- </pre></dd><dt><span class="term"><code class="literal">\sf[+] <em class="replaceable"><code>function_description</code></em> </code></span></dt><dd><p>
- This command fetches and shows the definition of the named function or procedure,
- in the form of a <code class="command">CREATE OR REPLACE FUNCTION</code> or
- <code class="command">CREATE OR REPLACE PROCEDURE</code> command.
- The definition is printed to the current query output channel,
- as set by <code class="command">\o</code>.
- </p><p>
- The target function can be specified by name alone, or by name
- and arguments, for example <code class="literal">foo(integer, text)</code>.
- The argument types must be given if there is more
- than one function of the same name.
- </p><p>
- If <code class="literal">+</code> is appended to the command name, then the
- output lines are numbered, with the first line of the function body
- being line 1.
- </p><p>
- Unlike most other meta-commands, the entire remainder of the line is
- always taken to be the argument(s) of <code class="command">\sf</code>, and neither
- variable interpolation nor backquote expansion are performed in the
- arguments.
- </p></dd><dt><span class="term"><code class="literal">\sv[+] <em class="replaceable"><code>view_name</code></em> </code></span></dt><dd><p>
- This command fetches and shows the definition of the named view,
- in the form of a <code class="command">CREATE OR REPLACE VIEW</code> command.
- The definition is printed to the current query output channel,
- as set by <code class="command">\o</code>.
- </p><p>
- If <code class="literal">+</code> is appended to the command name, then the
- output lines are numbered from 1.
- </p><p>
- Unlike most other meta-commands, the entire remainder of the line is
- always taken to be the argument(s) of <code class="command">\sv</code>, and neither
- variable interpolation nor backquote expansion are performed in the
- arguments.
- </p></dd><dt><span class="term"><code class="literal">\t</code></span></dt><dd><p>
- Toggles the display of output column name headings and row count
- footer. This command is equivalent to <code class="literal">\pset
- tuples_only</code> and is provided for convenience.
- </p></dd><dt><span class="term"><code class="literal">\T <em class="replaceable"><code>table_options</code></em></code></span></dt><dd><p>
- Specifies attributes to be placed within the
- <code class="sgmltag-element">table</code> tag in <acronym class="acronym">HTML</acronym>
- output format. This command is equivalent to <code class="literal">\pset
- tableattr <em class="replaceable"><code>table_options</code></em></code>.
- </p></dd><dt><span class="term"><code class="literal">\timing [ <em class="replaceable"><code>on</code></em> | <em class="replaceable"><code>off</code></em> ]</code></span></dt><dd><p>
- With a parameter, turns displaying of how long each SQL statement
- takes on or off. Without a parameter, toggles the display between
- on and off. The display is in milliseconds; intervals longer than
- 1 second are also shown in minutes:seconds format, with hours and
- days fields added if needed.
- </p></dd><dt><span class="term"><code class="literal">\unset <em class="replaceable"><code>name</code></em></code></span></dt><dd><p>
- Unsets (deletes) the <span class="application">psql</span> variable <em class="replaceable"><code>name</code></em>.
- </p><p>
- Most variables that control <span class="application">psql</span>'s behavior
- cannot be unset; instead, an <code class="literal">\unset</code> command is interpreted
- as setting them to their default values.
- See <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a>, below.
- </p></dd><dt><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></dt><dd><p>
- Writes the current query buffer to the file <em class="replaceable"><code>filename</code></em> or pipes it to the shell
- command <em class="replaceable"><code>command</code></em>.
- If the current query buffer is empty, the most recently executed query
- is written instead.
- </p><p>
- If the argument begins with <code class="literal">|</code>, then the entire remainder
- of the line is taken to be
- the <em class="replaceable"><code>command</code></em> to execute,
- and neither variable interpolation nor backquote expansion are
- performed in it. The rest of the line is simply passed literally to
- the shell.
- </p></dd><dt><span class="term"><code class="literal">\watch [ <em class="replaceable"><code>seconds</code></em> ]</code></span></dt><dd><p>
- Repeatedly execute the current query buffer (as <code class="literal">\g</code> does)
- until interrupted or the query fails. Wait the specified number of
- seconds (default 2) between executions. Each query result is
- displayed with a header that includes the <code class="literal">\pset title</code>
- string (if any), the time as of query start, and the delay interval.
- </p><p>
- If the current query buffer is empty, the most recently sent query
- is re-executed instead.
- </p></dd><dt><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></dt><dd><p>
- Sets or toggles expanded table formatting mode. As such it is equivalent to
- <code class="literal">\pset expanded</code>.
- </p></dd><dt><span class="term"><code class="literal">\z [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
- Lists tables, views and sequences with their
- associated access privileges.
- If a <em class="replaceable"><code>pattern</code></em> is
- specified, only tables, views and sequences whose names match the
- pattern are listed.
- </p><p>
- This is an alias for <code class="command">\dp</code> (<span class="quote">“<span class="quote">display
- privileges</span>”</span>).
- </p></dd><dt><span class="term"><code class="literal">\! [ <em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
- With no argument, escapes to a sub-shell; <span class="application">psql</span>
- resumes when the sub-shell exits. With an argument, executes the
- shell command <em class="replaceable"><code>command</code></em>.
- </p><p>
- Unlike most other meta-commands, the entire remainder of the line is
- always taken to be the argument(s) of <code class="command">\!</code>, and neither
- variable interpolation nor backquote expansion are performed in the
- arguments. The rest of the line is simply passed literally to the
- shell.
- </p></dd><dt><span class="term"><code class="literal">\? [ <em class="replaceable"><code>topic</code></em> ]</code></span></dt><dd><p>
- Shows help information. The optional
- <em class="replaceable"><code>topic</code></em> parameter
- (defaulting to <code class="literal">commands</code>) selects which part of <span class="application">psql</span> is
- explained: <code class="literal">commands</code> describes <span class="application">psql</span>'s
- backslash commands; <code class="literal">options</code> describes the command-line
- options that can be passed to <span class="application">psql</span>;
- and <code class="literal">variables</code> shows help about <span class="application">psql</span> configuration
- variables.
- </p></dd><dt><span class="term"><code class="literal">\;</code></span></dt><dd><p>
- Backslash-semicolon is not a meta-command in the same way as the
- preceding commands; rather, it simply causes a semicolon to be
- added to the query buffer without any further processing.
- </p><p>
- Normally, <span class="application">psql</span> will dispatch a SQL command to the
- server as soon as it reaches the command-ending semicolon, even if
- more input remains on the current line. Thus for example entering
- </p><pre class="programlisting">
- select 1; select 2; select 3;
- </pre><p>
- will result in the three SQL commands being individually sent to
- the server, with each one's results being displayed before
- continuing to the next command. However, a semicolon entered
- as <code class="literal">\;</code> will not trigger command processing, so that the
- command before it and the one after are effectively combined and
- sent to the server in one request. So for example
- </p><pre class="programlisting">
- select 1\; select 2\; select 3;
- </pre><p>
- results in sending the three SQL commands to the server in a single
- request, when the non-backslashed semicolon is reached.
- The server executes such a request as a single transaction,
- unless there are explicit <code class="command">BEGIN</code>/<code class="command">COMMIT</code>
- commands included in the string to divide it into multiple
- transactions. (See <a class="xref" href="protocol-flow.html#PROTOCOL-FLOW-MULTI-STATEMENT" title="52.2.2.1. Multiple Statements in a Simple Query">Section 52.2.2.1</a>
- for more details about how the server handles multi-query strings.)
- <span class="application">psql</span> prints only the last query result
- it receives for each request; in this example, although all
- three <code class="command">SELECT</code>s are indeed executed, <span class="application">psql</span>
- only prints the <code class="literal">3</code>.
- </p></dd></dl></div><p>
- </p><div class="refsect3" id="APP-PSQL-PATTERNS"><h4>Patterns</h4><a id="id-1.9.4.18.8.4.11.2" class="indexterm"></a><p>
- The various <code class="literal">\d</code> commands accept a <em class="replaceable"><code>pattern</code></em> parameter to specify the
- object name(s) to be displayed. In the simplest case, a pattern
- is just the exact name of the object. The characters within a
- pattern are normally folded to lower case, just as in SQL names;
- for example, <code class="literal">\dt FOO</code> will display the table named
- <code class="literal">foo</code>. As in SQL names, placing double quotes around
- a pattern stops folding to lower case. Should you need to include
- an actual double quote character in a pattern, write it as a pair
- of double quotes within a double-quote sequence; again this is in
- accord with the rules for SQL quoted identifiers. For example,
- <code class="literal">\dt "FOO""BAR"</code> will display the table named
- <code class="literal">FOO"BAR</code> (not <code class="literal">foo"bar</code>). Unlike the normal
- rules for SQL names, you can put double quotes around just part
- of a pattern, for instance <code class="literal">\dt FOO"FOO"BAR</code> will display
- the table named <code class="literal">fooFOObar</code>.
- </p><p>
- Whenever the <em class="replaceable"><code>pattern</code></em> parameter
- is omitted completely, the <code class="literal">\d</code> commands display all objects
- that are visible in the current schema search path — this is
- equivalent to using <code class="literal">*</code> as the pattern.
- (An object is said to be <em class="firstterm">visible</em> if its
- containing schema is in the search path and no object of the same
- kind and name appears earlier in the search path. This is equivalent to the
- statement that the object can be referenced by name without explicit
- schema qualification.)
- To see all objects in the database regardless of visibility,
- use <code class="literal">*.*</code> as the pattern.
- </p><p>
- Within a pattern, <code class="literal">*</code> matches any sequence of characters
- (including no characters) and <code class="literal">?</code> matches any single character.
- (This notation is comparable to Unix shell file name patterns.)
- For example, <code class="literal">\dt int*</code> displays tables whose names
- begin with <code class="literal">int</code>. But within double quotes, <code class="literal">*</code>
- and <code class="literal">?</code> lose these special meanings and are just matched
- literally.
- </p><p>
- A pattern that contains a dot (<code class="literal">.</code>) is interpreted as a schema
- name pattern followed by an object name pattern. For example,
- <code class="literal">\dt foo*.*bar*</code> displays all tables whose table name
- includes <code class="literal">bar</code> that are in schemas whose schema name
- starts with <code class="literal">foo</code>. When no dot appears, then the pattern
- matches only objects that are visible in the current schema search path.
- Again, a dot within double quotes loses its special meaning and is matched
- literally.
- </p><p>
- Advanced users can use regular-expression notations such as character
- classes, for example <code class="literal">[0-9]</code> to match any digit. All regular
- expression special characters work as specified in
- <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
- is taken as a separator as mentioned above, <code class="literal">*</code> which is
- translated to the regular-expression notation <code class="literal">.*</code>,
- <code class="literal">?</code> which is translated to <code class="literal">.</code>, and
- <code class="literal">$</code> which is matched literally. You can emulate
- these pattern characters at need by writing
- <code class="literal">?</code> for <code class="literal">.</code>,
- <code class="literal">(<em class="replaceable"><code>R</code></em>+|)</code> for
- <code class="literal"><em class="replaceable"><code>R</code></em>*</code>, or
- <code class="literal">(<em class="replaceable"><code>R</code></em>|)</code> for
- <code class="literal"><em class="replaceable"><code>R</code></em>?</code>.
- <code class="literal">$</code> is not needed as a regular-expression character since
- the pattern must match the whole name, unlike the usual
- interpretation of regular expressions (in other words, <code class="literal">$</code>
- is automatically appended to your pattern). Write <code class="literal">*</code> at the
- beginning and/or end if you don't wish the pattern to be anchored.
- Note that within double quotes, all regular expression special characters
- lose their special meanings and are matched literally. Also, the regular
- expression special characters are matched literally in operator name
- patterns (i.e., the argument of <code class="literal">\do</code>).
- </p></div></div><div class="refsect2" id="id-1.9.4.18.8.5"><h3>Advanced Features</h3><div class="refsect3" id="APP-PSQL-VARIABLES"><h4>Variables</h4><p>
- <span class="application">psql</span> provides variable substitution
- features similar to common Unix command shells.
- Variables are simply name/value pairs, where the value
- can be any string of any length. The name must consist of letters
- (including non-Latin letters), digits, and underscores.
- </p><p>
- To set a variable, use the <span class="application">psql</span> meta-command
- <code class="command">\set</code>. For example,
- </p><pre class="programlisting">
- testdb=> <strong class="userinput"><code>\set foo bar</code></strong>
- </pre><p>
- sets the variable <code class="literal">foo</code> to the value
- <code class="literal">bar</code>. To retrieve the content of the variable, precede
- the name with a colon, for example:
- </p><pre class="programlisting">
- testdb=> <strong class="userinput"><code>\echo :foo</code></strong>
- bar
- </pre><p>
- This works in both regular SQL commands and meta-commands; there is
- more detail in <a class="xref" href="app-psql.html#APP-PSQL-INTERPOLATION" title="SQL Interpolation"><acronym class="acronym">SQL</acronym> Interpolation</a>, below.
- </p><p>
- If you call <code class="command">\set</code> without a second argument, the
- variable is set to an empty-string value. To unset (i.e., delete)
- a variable, use the command <code class="command">\unset</code>. To show the
- values of all variables, call <code class="command">\set</code> without any argument.
- </p><div class="note"><h3 class="title">Note</h3><p>
- The arguments of <code class="command">\set</code> are subject to the same
- substitution rules as with other commands. Thus you can construct
- interesting references such as <code class="literal">\set :foo
- 'something'</code> and get <span class="quote">“<span class="quote">soft links</span>”</span> or
- <span class="quote">“<span class="quote">variable variables</span>”</span> of <span class="productname">Perl</span>
- or <span class="productname"><acronym class="acronym">PHP</acronym></span> fame,
- respectively. Unfortunately (or fortunately?), there is no way to do
- anything useful with these constructs. On the other hand,
- <code class="literal">\set bar :foo</code> is a perfectly valid way to copy a
- variable.
- </p></div><p>
- A number of these variables are treated specially
- by <span class="application">psql</span>. They represent certain option
- settings that can be changed at run time by altering the value of
- the variable, or in some cases represent changeable state of
- <span class="application">psql</span>.
- By convention, all specially treated variables' names
- consist of all upper-case ASCII letters (and possibly digits and
- underscores). To ensure maximum compatibility in the future, avoid
- using such variable names for your own purposes.
- </p><p>
- Variables that control <span class="application">psql</span>'s behavior
- generally cannot be unset or set to invalid values. An <code class="literal">\unset</code>
- command is allowed but is interpreted as setting the variable to its
- default value. A <code class="literal">\set</code> command without a second argument is
- interpreted as setting the variable to <code class="literal">on</code>, for control
- variables that accept that value, and is rejected for others. Also,
- control variables that accept the values <code class="literal">on</code>
- and <code class="literal">off</code> will also accept other common spellings of Boolean
- values, such as <code class="literal">true</code> and <code class="literal">false</code>.
- </p><p>
- The specially treated variables are:
- </p><div class="variablelist"><dl class="variablelist"><dt><span class="term">
- <code class="varname">AUTOCOMMIT</code>
- <a id="id-1.9.4.18.8.5.2.9.1.1.2" class="indexterm"></a>
- </span></dt><dd><p>
- When <code class="literal">on</code> (the default), each SQL command is automatically
- committed upon successful completion. To postpone commit in this
- mode, you must enter a <code class="command">BEGIN</code> or <code class="command">START
- TRANSACTION</code> SQL command. When <code class="literal">off</code> or unset, SQL
- commands are not committed until you explicitly issue
- <code class="command">COMMIT</code> or <code class="command">END</code>. The autocommit-off
- mode works by issuing an implicit <code class="command">BEGIN</code> for you, just
- before any command that is not already in a transaction block and
- is not itself a <code class="command">BEGIN</code> or other transaction-control
- command, nor a command that cannot be executed inside a transaction
- block (such as <code class="command">VACUUM</code>).
- </p><div class="note"><h3 class="title">Note</h3><p>
- In autocommit-off mode, you must explicitly abandon any failed
- transaction by entering <code class="command">ABORT</code> or <code class="command">ROLLBACK</code>.
- Also keep in mind that if you exit the session
- without committing, your work will be lost.
- </p></div><div class="note"><h3 class="title">Note</h3><p>
- The autocommit-on mode is <span class="productname">PostgreSQL</span>'s traditional
- behavior, but autocommit-off is closer to the SQL spec. If you
- prefer autocommit-off, you might wish to set it in the system-wide
- <code class="filename">psqlrc</code> file or your
- <code class="filename">~/.psqlrc</code> file.
- </p></div></dd><dt><span class="term"><code class="varname">COMP_KEYWORD_CASE</code></span></dt><dd><p>
- Determines which letter case to use when completing an SQL key word.
- If set to <code class="literal">lower</code> or <code class="literal">upper</code>, the
- completed word will be in lower or upper case, respectively. If set
- to <code class="literal">preserve-lower</code>
- or <code class="literal">preserve-upper</code> (the default), the completed word
- will be in the case of the word already entered, but words being
- completed without anything entered will be in lower or upper case,
- respectively.
- </p></dd><dt><span class="term"><code class="varname">DBNAME</code></span></dt><dd><p>
- The name of the database you are currently connected to. This is
- set every time you connect to a database (including program
- start-up), but can be changed or unset.
- </p></dd><dt><span class="term"><code class="varname">ECHO</code></span></dt><dd><p>
- If set to <code class="literal">all</code>, all nonempty input lines are printed
- to standard output as they are read. (This does not apply to lines
- read interactively.) To select this behavior on program
- start-up, use the switch <code class="option">-a</code>. If set to
- <code class="literal">queries</code>,
- <span class="application">psql</span> prints each query to standard output
- as it is sent to the server. The switch to select this behavior is
- <code class="option">-e</code>. If set to <code class="literal">errors</code>, then only
- failed queries are displayed on standard error output. The switch
- for this behavior is <code class="option">-b</code>. If set to
- <code class="literal">none</code> (the default), then no queries are displayed.
- </p></dd><dt><span class="term"><code class="varname">ECHO_HIDDEN</code></span></dt><dd><p>
- When this variable is set to <code class="literal">on</code> and a backslash command
- queries the database, the query is first shown.
- This feature helps you to study
- <span class="productname">PostgreSQL</span> internals and provide
- similar functionality in your own programs. (To select this behavior
- on program start-up, use the switch <code class="option">-E</code>.) If you set
- this variable to the value <code class="literal">noexec</code>, the queries are
- just shown but are not actually sent to the server and executed.
- The default value is <code class="literal">off</code>.
- </p></dd><dt><span class="term"><code class="varname">ENCODING</code></span></dt><dd><p>
- The current client character set encoding.
- This is set every time you connect to a database (including
- program start-up), and when you change the encoding
- with <code class="literal">\encoding</code>, but it can be changed or unset.
- </p></dd><dt><span class="term"><code class="varname">ERROR</code></span></dt><dd><p>
- <code class="literal">true</code> if the last SQL query failed, <code class="literal">false</code> if
- it succeeded. See also <code class="varname">SQLSTATE</code>.
- </p></dd><dt><span class="term"><code class="varname">FETCH_COUNT</code></span></dt><dd><p>
- If this variable is set to an integer value greater than zero,
- the results of <code class="command">SELECT</code> queries are fetched
- and displayed in groups of that many rows, rather than the
- default behavior of collecting the entire result set before
- display. Therefore only a
- limited amount of memory is used, regardless of the size of
- the result set. Settings of 100 to 1000 are commonly used
- when enabling this feature.
- Keep in mind that when using this feature, a query might
- fail after having already displayed some rows.
- </p><div class="tip"><h3 class="title">Tip</h3><p>
- Although you can use any output format with this feature,
- the default <code class="literal">aligned</code> format tends to look bad
- because each group of <code class="varname">FETCH_COUNT</code> rows
- will be formatted separately, leading to varying column
- widths across the row groups. The other output formats work better.
- </p></div></dd><dt><span class="term"><code class="varname">HIDE_TABLEAM</code></span></dt><dd><p>
- If this variable is set to <code class="literal">true</code>, a table's access
- method details are not displayed. This is mainly useful for
- regression tests.
- </p></dd><dt><span class="term"><code class="varname">HISTCONTROL</code></span></dt><dd><p>
- If this variable is set to <code class="literal">ignorespace</code>,
- lines which begin with a space are not entered into the history
- list. If set to a value of <code class="literal">ignoredups</code>, lines
- matching the previous history line are not entered. A value of
- <code class="literal">ignoreboth</code> combines the two options. If
- set to <code class="literal">none</code> (the default), all lines
- read in interactive mode are saved on the history list.
- </p><div class="note"><h3 class="title">Note</h3><p>
- This feature was shamelessly plagiarized from
- <span class="application">Bash</span>.
- </p></div></dd><dt><span class="term"><code class="varname">HISTFILE</code></span></dt><dd><p>
- The file name that will be used to store the history list. If unset,
- the file name is taken from the <code class="envar">PSQL_HISTORY</code>
- environment variable. If that is not set either, the default
- is <code class="filename">~/.psql_history</code>,
- or <code class="filename">%APPDATA%\postgresql\psql_history</code> on Windows.
- For example, putting:
- </p><pre class="programlisting">
- \set HISTFILE ~/.psql_history- :DBNAME
- </pre><p>
- in <code class="filename">~/.psqlrc</code> will cause
- <span class="application">psql</span> to maintain a separate history for
- each database.
- </p><div class="note"><h3 class="title">Note</h3><p>
- This feature was shamelessly plagiarized from
- <span class="application">Bash</span>.
- </p></div></dd><dt><span class="term"><code class="varname">HISTSIZE</code></span></dt><dd><p>
- The maximum number of commands to store in the command history
- (default 500). If set to a negative value, no limit is applied.
- </p><div class="note"><h3 class="title">Note</h3><p>
- This feature was shamelessly plagiarized from
- <span class="application">Bash</span>.
- </p></div></dd><dt><span class="term"><code class="varname">HOST</code></span></dt><dd><p>
- The database server host you are currently connected to. This is
- set every time you connect to a database (including program
- start-up), but can be changed or unset.
- </p></dd><dt><span class="term"><code class="varname">IGNOREEOF</code></span></dt><dd><p>
- If set to 1 or less, sending an <acronym class="acronym">EOF</acronym> character (usually
- <span class="keycap"><strong>Control</strong></span>+<span class="keycap"><strong>D</strong></span>)
- to an interactive session of <span class="application">psql</span>
- will terminate the application. If set to a larger numeric value,
- that many consecutive <acronym class="acronym">EOF</acronym> characters must be typed to
- make an interactive session terminate. If the variable is set to a
- non-numeric value, it is interpreted as 10. The default is 0.
- </p><div class="note"><h3 class="title">Note</h3><p>
- This feature was shamelessly plagiarized from
- <span class="application">Bash</span>.
- </p></div></dd><dt><span class="term"><code class="varname">LASTOID</code></span></dt><dd><p>
- The value of the last affected OID, as returned from an
- <code class="command">INSERT</code> or <code class="command">\lo_import</code>
- command. This variable is only guaranteed to be valid until
- after the result of the next <acronym class="acronym">SQL</acronym> command has
- been displayed.
- <span class="productname">PostgreSQL</span> servers since version 12 do not
- support OID system columns anymore, thus LASTOID will always be 0
- following <code class="command">INSERT</code> when targeting such servers.
- </p></dd><dt><span class="term"><code class="varname">LAST_ERROR_MESSAGE</code><br /></span><span class="term"><code class="varname">LAST_ERROR_SQLSTATE</code></span></dt><dd><p>
- The primary error message and associated SQLSTATE code for the most
- recent failed query in the current <span class="application">psql</span> session, or
- an empty string and <code class="literal">00000</code> if no error has occurred in
- the current session.
- </p></dd><dt><span class="term">
- <code class="varname">ON_ERROR_ROLLBACK</code>
- <a id="id-1.9.4.18.8.5.2.9.17.1.2" class="indexterm"></a>
- </span></dt><dd><p>
- When set to <code class="literal">on</code>, if a statement in a transaction block
- generates an error, the error is ignored and the transaction
- continues. When set to <code class="literal">interactive</code>, such errors are only
- ignored in interactive sessions, and not when reading script
- files. When set to <code class="literal">off</code> (the default), a statement in a
- transaction block that generates an error aborts the entire
- transaction. The error rollback mode works by issuing an
- implicit <code class="command">SAVEPOINT</code> for you, just before each command
- that is in a transaction block, and then rolling back to the
- savepoint if the command fails.
- </p></dd><dt><span class="term"><code class="varname">ON_ERROR_STOP</code></span></dt><dd><p>
- By default, command processing continues after an error. When this
- variable is set to <code class="literal">on</code>, processing will instead stop
- immediately. In interactive mode,
- <span class="application">psql</span> will return to the command prompt;
- otherwise, <span class="application">psql</span> will exit, returning
- error code 3 to distinguish this case from fatal error
- conditions, which are reported using error code 1. In either case,
- any currently running scripts (the top-level script, if any, and any
- other scripts which it may have in invoked) will be terminated
- immediately. If the top-level command string contained multiple SQL
- commands, processing will stop with the current command.
- </p></dd><dt><span class="term"><code class="varname">PORT</code></span></dt><dd><p>
- The database server port to which you are currently connected.
- This is set every time you connect to a database (including
- program start-up), but can be changed or unset.
- </p></dd><dt><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></dt><dd><p>
- These specify what the prompts <span class="application">psql</span>
- issues should look like. See <a class="xref" href="app-psql.html#APP-PSQL-PROMPTING" title="Prompting">Prompting</a> below.
- </p></dd><dt><span class="term"><code class="varname">QUIET</code></span></dt><dd><p>
- Setting this variable to <code class="literal">on</code> is equivalent to the command
- line option <code class="option">-q</code>. It is probably not too useful in
- interactive mode.
- </p></dd><dt><span class="term"><code class="varname">ROW_COUNT</code></span></dt><dd><p>
- The number of rows returned or affected by the last SQL query, or 0
- if the query failed or did not report a row count.
- </p></dd><dt><span class="term"><code class="varname">SERVER_VERSION_NAME</code><br /></span><span class="term"><code class="varname">SERVER_VERSION_NUM</code></span></dt><dd><p>
- The server's version number as a string, for
- example <code class="literal">9.6.2</code>, <code class="literal">10.1</code> or <code class="literal">11beta1</code>,
- and in numeric form, for
- example <code class="literal">90602</code> or <code class="literal">100001</code>.
- These are set every time you connect to a database
- (including program start-up), but can be changed or unset.
- </p></dd><dt><span class="term"><code class="varname">SHOW_CONTEXT</code></span></dt><dd><p>
- This variable can be set to the
- values <code class="literal">never</code>, <code class="literal">errors</code>, or <code class="literal">always</code>
- to control whether <code class="literal">CONTEXT</code> fields are displayed in
- messages from the server. The default is <code class="literal">errors</code> (meaning
- that context will be shown in error messages, but not in notice or
- warning messages). This setting has no effect
- when <code class="varname">VERBOSITY</code> is set to <code class="literal">terse</code>
- or <code class="literal">sqlstate</code>.
- (See also <code class="command">\errverbose</code>, for use when you want a verbose
- version of the error you just got.)
- </p></dd><dt><span class="term"><code class="varname">SINGLELINE</code></span></dt><dd><p>
- Setting this variable to <code class="literal">on</code> is equivalent to the command
- line option <code class="option">-S</code>.
- </p></dd><dt><span class="term"><code class="varname">SINGLESTEP</code></span></dt><dd><p>
- Setting this variable to <code class="literal">on</code> is equivalent to the command
- line option <code class="option">-s</code>.
- </p></dd><dt><span class="term"><code class="varname">SQLSTATE</code></span></dt><dd><p>
- The error code (see <a class="xref" href="errcodes-appendix.html" title="Appendix A. PostgreSQL Error Codes">Appendix A</a>) associated
- with the last SQL query's failure, or <code class="literal">00000</code> if it
- succeeded.
- </p></dd><dt><span class="term"><code class="varname">USER</code></span></dt><dd><p>
- The database user you are currently connected as. This is set
- every time you connect to a database (including program
- start-up), but can be changed or unset.
- </p></dd><dt><span class="term"><code class="varname">VERBOSITY</code></span></dt><dd><p>
- This variable can be set to the values <code class="literal">default</code>,
- <code class="literal">verbose</code>, <code class="literal">terse</code>,
- or <code class="literal">sqlstate</code> to control the verbosity of error
- reports.
- (See also <code class="command">\errverbose</code>, for use when you want a verbose
- version of the error you just got.)
- </p></dd><dt><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></dt><dd><p>
- These variables are set at program start-up to reflect
- <span class="application">psql</span>'s version, respectively as a verbose string,
- a short string (e.g., <code class="literal">9.6.2</code>, <code class="literal">10.1</code>,
- or <code class="literal">11beta1</code>), and a number (e.g., <code class="literal">90602</code>
- or <code class="literal">100001</code>). They can be changed or unset.
- </p></dd></dl></div></div><div class="refsect3" id="APP-PSQL-INTERPOLATION"><h4><acronym class="acronym">SQL</acronym> Interpolation</h4><p>
- A key feature of <span class="application">psql</span>
- variables is that you can substitute (<span class="quote">“<span class="quote">interpolate</span>”</span>)
- them into regular <acronym class="acronym">SQL</acronym> statements, as well as the
- arguments of meta-commands. Furthermore,
- <span class="application">psql</span> provides facilities for
- ensuring that variable values used as SQL literals and identifiers are
- properly quoted. The syntax for interpolating a value without
- any quoting is to prepend the variable name with a colon
- (<code class="literal">:</code>). For example,
- </p><pre class="programlisting">
- testdb=> <strong class="userinput"><code>\set foo 'my_table'</code></strong>
- testdb=> <strong class="userinput"><code>SELECT * FROM :foo;</code></strong>
- </pre><p>
- would query the table <code class="literal">my_table</code>. Note that this
- may be unsafe: the value of the variable is copied literally, so it can
- contain unbalanced quotes, or even backslash commands. You must make sure
- that it makes sense where you put it.
- </p><p>
- When a value is to be used as an SQL literal or identifier, it is
- safest to arrange for it to be quoted. To quote the value of
- a variable as an SQL literal, write a colon followed by the variable
- name in single quotes. To quote the value as an SQL identifier, write
- a colon followed by the variable name in double quotes.
- These constructs deal correctly with quotes and other special
- characters embedded within the variable value.
- The previous example would be more safely written this way:
- </p><pre class="programlisting">
- testdb=> <strong class="userinput"><code>\set foo 'my_table'</code></strong>
- testdb=> <strong class="userinput"><code>SELECT * FROM :"foo";</code></strong>
- </pre><p>
- </p><p>
- Variable interpolation will not be performed within quoted
- <acronym class="acronym">SQL</acronym> literals and identifiers. Therefore, a
- construction such as <code class="literal">':foo'</code> doesn't work to produce a quoted
- literal from a variable's value (and it would be unsafe if it did work,
- since it wouldn't correctly handle quotes embedded in the value).
- </p><p>
- One example use of this mechanism is to
- copy the contents of a file into a table column.
- First load the file into a variable and then interpolate the variable's
- value as a quoted string:
- </p><pre class="programlisting">
- testdb=> <strong class="userinput"><code>\set content `cat my_file.txt`</code></strong>
- testdb=> <strong class="userinput"><code>INSERT INTO my_table VALUES (:'content');</code></strong>
- </pre><p>
- (Note that this still won't work if <code class="filename">my_file.txt</code> contains NUL bytes.
- <span class="application">psql</span> does not support embedded NUL bytes in variable values.)
- </p><p>
- Since colons can legally appear in SQL commands, an apparent attempt
- at interpolation (that is, <code class="literal">:name</code>,
- <code class="literal">:'name'</code>, or <code class="literal">:"name"</code>) is not
- replaced unless the named variable is currently set. In any case, you
- can escape a colon with a backslash to protect it from substitution.
- </p><p>
- The <code class="literal">:{?<em class="replaceable"><code>name</code></em>}</code> special syntax returns TRUE
- or FALSE depending on whether the variable exists or not, and is thus
- always substituted, unless the colon is backslash-escaped.
- </p><p>
- The colon syntax for variables is standard <acronym class="acronym">SQL</acronym> for
- embedded query languages, such as <span class="application">ECPG</span>.
- The colon syntaxes for array slices and type casts are
- <span class="productname">PostgreSQL</span> extensions, which can sometimes
- conflict with the standard usage. The colon-quote syntax for escaping a
- variable's value as an SQL literal or identifier is a
- <span class="application">psql</span> extension.
- </p></div><div class="refsect3" id="APP-PSQL-PROMPTING"><h4>Prompting</h4><p>
- The prompts <span class="application">psql</span> issues can be customized
- to your preference. The three variables <code class="varname">PROMPT1</code>,
- <code class="varname">PROMPT2</code>, and <code class="varname">PROMPT3</code> contain strings
- and special escape sequences that describe the appearance of the
- prompt. Prompt 1 is the normal prompt that is issued when
- <span class="application">psql</span> requests a new command. Prompt 2 is
- issued when more input is expected during command entry, for example
- because the command was not terminated with a semicolon or a quote
- was not closed.
- Prompt 3 is issued when you are running an <acronym class="acronym">SQL</acronym>
- <code class="command">COPY FROM STDIN</code> command and you need to type in
- a row value on the terminal.
- </p><p>
- The value of the selected prompt variable is printed literally,
- except where a percent sign (<code class="literal">%</code>) is encountered.
- Depending on the next character, certain other text is substituted
- instead. Defined substitutions are:
-
- </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">%M</code></span></dt><dd><p>
- The full host name (with domain name) of the database server,
- or <code class="literal">[local]</code> if the connection is over a Unix
- domain socket, or
- <code class="literal">[local:<em class="replaceable"><code>/dir/name</code></em>]</code>,
- if the Unix domain socket is not at the compiled in default
- location.
- </p></dd><dt><span class="term"><code class="literal">%m</code></span></dt><dd><p>
- The host name of the database server, truncated at the
- first dot, or <code class="literal">[local]</code> if the connection is
- over a Unix domain socket.
- </p></dd><dt><span class="term"><code class="literal">%></code></span></dt><dd><p>The port number at which the database server is listening.</p></dd><dt><span class="term"><code class="literal">%n</code></span></dt><dd><p>
- The database session user name. (The expansion of this
- value might change during a database session as the result
- of the command <code class="command">SET SESSION
- AUTHORIZATION</code>.)
- </p></dd><dt><span class="term"><code class="literal">%/</code></span></dt><dd><p>The name of the current database.</p></dd><dt><span class="term"><code class="literal">%~</code></span></dt><dd><p>Like <code class="literal">%/</code>, but the output is <code class="literal">~</code>
- (tilde) if the database is your default database.</p></dd><dt><span class="term"><code class="literal">%#</code></span></dt><dd><p>
- If the session user is a database superuser, then a
- <code class="literal">#</code>, otherwise a <code class="literal">></code>.
- (The expansion of this value might change during a database
- session as the result of the command <code class="command">SET SESSION
- AUTHORIZATION</code>.)
- </p></dd><dt><span class="term"><code class="literal">%p</code></span></dt><dd><p>The process ID of the backend currently connected to.</p></dd><dt><span class="term"><code class="literal">%R</code></span></dt><dd><p>
- In prompt 1 normally <code class="literal">=</code>,
- but <code class="literal">@</code> if the session is in an inactive branch of a
- conditional block, or <code class="literal">^</code> if in single-line mode,
- or <code class="literal">!</code> if the session is disconnected from the
- database (which can happen if <code class="command">\connect</code> fails).
- In prompt 2 <code class="literal">%R</code> is replaced by a character that
- depends on why <span class="application">psql</span> expects more input:
- <code class="literal">-</code> if the command simply wasn't terminated yet,
- but <code class="literal">*</code> if there is an unfinished
- <code class="literal">/* ... */</code> comment,
- a single quote if there is an unfinished quoted string,
- a double quote if there is an unfinished quoted identifier,
- a dollar sign if there is an unfinished dollar-quoted string,
- or <code class="literal">(</code> if there is an unmatched left parenthesis.
- In prompt 3 <code class="literal">%R</code> doesn't produce anything.
- </p></dd><dt><span class="term"><code class="literal">%x</code></span></dt><dd><p>
- Transaction status: an empty string when not in a transaction
- block, or <code class="literal">*</code> when in a transaction block, or
- <code class="literal">!</code> when in a failed transaction block, or <code class="literal">?</code>
- when the transaction state is indeterminate (for example, because
- there is no connection).
- </p></dd><dt><span class="term"><code class="literal">%l</code></span></dt><dd><p>
- The line number inside the current statement, starting from <code class="literal">1</code>.
- </p></dd><dt><span class="term"><code class="literal">%</code><em class="replaceable"><code>digits</code></em></span></dt><dd><p>
- The character with the indicated octal code is substituted.
- </p></dd><dt><span class="term"><code class="literal">%:</code><em class="replaceable"><code>name</code></em><code class="literal">:</code></span></dt><dd><p>
- The value of the <span class="application">psql</span> variable
- <em class="replaceable"><code>name</code></em>. See the
- section <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a> for details.
- </p></dd><dt><span class="term"><code class="literal">%`</code><em class="replaceable"><code>command</code></em><code class="literal">`</code></span></dt><dd><p>
- The output of <em class="replaceable"><code>command</code></em>, similar to ordinary
- <span class="quote">“<span class="quote">back-tick</span>”</span> substitution.
- </p></dd><dt><span class="term"><code class="literal">%[</code> ... <code class="literal">%]</code></span></dt><dd><p>
- Prompts can contain terminal control characters which, for
- example, change the color, background, or style of the prompt
- text, or change the title of the terminal window. In order for
- the line editing features of <span class="application">Readline</span> to work properly, these
- non-printing control characters must be designated as invisible
- by surrounding them with <code class="literal">%[</code> and
- <code class="literal">%]</code>. Multiple pairs of these can occur within
- the prompt. For example:
- </p><pre class="programlisting">
- testdb=> \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '
- </pre><p>
- results in a boldfaced (<code class="literal">1;</code>) yellow-on-black
- (<code class="literal">33;40</code>) prompt on VT100-compatible, color-capable
- terminals.
- </p></dd></dl></div><p>
-
- To insert a percent sign into your prompt, write
- <code class="literal">%%</code>. The default prompts are
- <code class="literal">'%/%R%# '</code> for prompts 1 and 2, and
- <code class="literal">'>> '</code> for prompt 3.
- </p><div class="note"><h3 class="title">Note</h3><p>
- This feature was shamelessly plagiarized from
- <span class="application">tcsh</span>.
- </p></div></div><div class="refsect3" id="id-1.9.4.18.8.5.5"><h4>Command-Line Editing</h4><p>
- <span class="application">psql</span> supports the <span class="application">Readline</span>
- library for convenient line editing and retrieval. The command
- history is automatically saved when <span class="application">psql</span>
- exits and is reloaded when
- <span class="application">psql</span> starts up. Tab-completion is also
- supported, although the completion logic makes no claim to be an
- <acronym class="acronym">SQL</acronym> parser. The queries generated by tab-completion
- can also interfere with other SQL commands, e.g. <code class="literal">SET
- TRANSACTION ISOLATION LEVEL</code>.
- If for some reason you do not like the tab completion, you
- can turn it off by putting this in a file named
- <code class="filename">.inputrc</code> in your home directory:
- </p><pre class="programlisting">
- $if psql
- set disable-completion on
- $endif
- </pre><p>
- (This is not a <span class="application">psql</span> but a
- <span class="application">Readline</span> feature. Read its documentation
- for further details.)
- </p></div></div></div><div class="refsect1" id="APP-PSQL-ENVIRONMENT"><h2>Environment</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="envar">COLUMNS</code></span></dt><dd><p>
- If <code class="literal">\pset columns</code> is zero, controls the
- width for the <code class="literal">wrapped</code> format and width for determining
- if wide output requires the pager or should be switched to the
- vertical format in expanded auto mode.
- </p></dd><dt><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></dt><dd><p>
- Default connection parameters (see <a class="xref" href="libpq-envars.html" title="33.14. Environment Variables">Section 33.14</a>).
- </p></dd><dt><span class="term"><code class="envar">PG_COLOR</code></span></dt><dd><p>
- Specifies whether to use color in diagnostic messages. Possible values
- are <code class="literal">always</code>, <code class="literal">auto</code> and
- <code class="literal">never</code>.
- </p></dd><dt><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></dt><dd><p>
- Editor used by the <code class="command">\e</code>, <code class="command">\ef</code>,
- and <code class="command">\ev</code> commands.
- These variables are examined in the order listed;
- the first that is set is used.
- If none of them is set, the default is to use <code class="filename">vi</code>
- on Unix systems or <code class="filename">notepad.exe</code> on Windows systems.
- </p></dd><dt><span class="term"><code class="envar">PSQL_EDITOR_LINENUMBER_ARG</code></span></dt><dd><p>
- When <code class="command">\e</code>, <code class="command">\ef</code>, or
- <code class="command">\ev</code> is used
- with a line number argument, this variable specifies the
- command-line argument used to pass the starting line number to
- the user's editor. For editors such as <span class="productname">Emacs</span> or
- <span class="productname">vi</span>, this is a plus sign. Include a trailing
- space in the value of the variable if there needs to be space
- between the option name and the line number. Examples:
- </p><pre class="programlisting">
- PSQL_EDITOR_LINENUMBER_ARG='+'
- PSQL_EDITOR_LINENUMBER_ARG='--line '
- </pre><p>
- </p><p>
- The default is <code class="literal">+</code> on Unix systems
- (corresponding to the default editor <code class="filename">vi</code>,
- and useful for many other common editors); but there is no
- default on Windows systems.
- </p></dd><dt><span class="term"><code class="envar">PSQL_HISTORY</code></span></dt><dd><p>
- Alternative location for the command history file. Tilde (<code class="literal">~</code>) expansion is performed.
- </p></dd><dt><span class="term"><code class="envar">PSQL_PAGER</code><br /></span><span class="term"><code class="envar">PAGER</code></span></dt><dd><p>
- If a query's results do not fit on the screen, they are piped
- through this command. Typical values are <code class="literal">more</code>
- or <code class="literal">less</code>.
- Use of the pager can be disabled by setting <code class="envar">PSQL_PAGER</code>
- or <code class="envar">PAGER</code> to an empty string, or by adjusting the
- pager-related options of the <code class="command">\pset</code> command.
- These variables are examined in the order listed;
- the first that is set is used.
- If none of them is set, the default is to use <code class="literal">more</code> on most
- platforms, but <code class="literal">less</code> on Cygwin.
- </p></dd><dt><span class="term"><code class="envar">PSQLRC</code></span></dt><dd><p>
- Alternative location of the user's <code class="filename">.psqlrc</code> file. Tilde (<code class="literal">~</code>) expansion is performed.
- </p></dd><dt><span class="term"><code class="envar">SHELL</code></span></dt><dd><p>
- Command executed by the <code class="command">\!</code> command.
- </p></dd><dt><span class="term"><code class="envar">TMPDIR</code></span></dt><dd><p>
- Directory for storing temporary files. The default is
- <code class="filename">/tmp</code>.
- </p></dd></dl></div><p>
- This utility, like most other <span class="productname">PostgreSQL</span> utilities,
- also uses the environment variables supported by <span class="application">libpq</span>
- (see <a class="xref" href="libpq-envars.html" title="33.14. Environment Variables">Section 33.14</a>).
- </p></div><div class="refsect1" id="id-1.9.4.18.10"><h2>Files</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="filename">psqlrc</code> and <code class="filename">~/.psqlrc</code></span></dt><dd><p>
- Unless it is passed an <code class="option">-X</code> option,
- <span class="application">psql</span> attempts to read and execute commands
- from the system-wide startup file (<code class="filename">psqlrc</code>) and then
- the user's personal startup file (<code class="filename">~/.psqlrc</code>), after
- connecting to the database but before accepting normal commands.
- These files can be used to set up the client and/or the server to taste,
- typically with <code class="command">\set</code> and <code class="command">SET</code>
- commands.
- </p><p>
- The system-wide startup file is named <code class="filename">psqlrc</code> and is
- sought in the installation's <span class="quote">“<span class="quote">system configuration</span>”</span> directory,
- which is most reliably identified by running <code class="literal">pg_config
- --sysconfdir</code>. By default this directory will be <code class="filename">../etc/</code>
- relative to the directory containing
- the <span class="productname">PostgreSQL</span> executables. The name of this
- directory can be set explicitly via the <code class="envar">PGSYSCONFDIR</code>
- environment variable.
- </p><p>
- The user's personal startup file is named <code class="filename">.psqlrc</code>
- and is sought in the invoking user's home directory. On Windows, which
- lacks such a concept, the personal startup file is named
- <code class="filename">%APPDATA%\postgresql\psqlrc.conf</code>.
- The location of the user's startup file can be set explicitly via
- the <code class="envar">PSQLRC</code> environment variable.
- </p><p>
- Both the system-wide startup file and the user's personal startup file
- can be made <span class="application">psql</span>-version-specific
- by appending a dash and the <span class="productname">PostgreSQL</span>
- major or minor release number to the file name,
- for example <code class="filename">~/.psqlrc-9.2</code> or
- <code class="filename">~/.psqlrc-9.2.5</code>. The most specific
- version-matching file will be read in preference to a
- non-version-specific file.
- </p></dd><dt><span class="term"><code class="filename">.psql_history</code></span></dt><dd><p>
- The command-line history is stored in the file
- <code class="filename">~/.psql_history</code>, or
- <code class="filename">%APPDATA%\postgresql\psql_history</code> on Windows.
- </p><p>
- The location of the history file can be set explicitly via
- the <code class="varname">HISTFILE</code> <span class="application">psql</span> variable or
- the <code class="envar">PSQL_HISTORY</code> environment variable.
- </p></dd></dl></div></div><div class="refsect1" id="id-1.9.4.18.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
- or an older major version. Backslash commands are particularly likely
- to fail if the server is of a newer version than <span class="application">psql</span>
- itself. However, backslash commands of the <code class="literal">\d</code> family should
- work with servers of versions back to 7.4, though not necessarily with
- servers newer than <span class="application">psql</span> itself. The general
- functionality of running SQL commands and displaying query results
- should also work with servers of a newer major version, but this cannot
- be guaranteed in all cases.
- </p><p>
- If you want to use <span class="application">psql</span> to connect to several
- servers of different major versions, it is recommended that you use the
- newest version of <span class="application">psql</span>. Alternatively, you
- can keep around a copy of <span class="application">psql</span> from each
- major version and be sure to use the version that matches the
- respective server. But in practice, this additional complication should
- not be necessary.
- </p></li><li class="listitem"><p>
- Before <span class="productname">PostgreSQL</span> 9.6,
- the <code class="option">-c</code> option implied <code class="option">-X</code>
- (<code class="option">--no-psqlrc</code>); this is no longer the case.
- </p></li><li class="listitem"><p>
- Before <span class="productname">PostgreSQL</span> 8.4,
- <span class="application">psql</span> allowed the
- first argument of a single-letter backslash command to start
- directly after the command, without intervening whitespace.
- Now, some whitespace is required.
- </p></li></ul></div></div><div class="refsect1" id="id-1.9.4.18.12"><h2>Notes for Windows Users</h2><p>
- <span class="application">psql</span> is built as a <span class="quote">“<span class="quote">console
- application</span>”</span>. Since the Windows console windows use a different
- encoding than the rest of the system, you must take special care
- when using 8-bit characters within <span class="application">psql</span>.
- If <span class="application">psql</span> detects a problematic
- console code page, it will warn you at startup. To change the
- console code page, two things are necessary:
-
- </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
- Set the code page by entering <strong class="userinput"><code>cmd.exe /c chcp
- 1252</code></strong>. (1252 is a code page that is appropriate for
- German; replace it with your value.) If you are using Cygwin,
- you can put this command in <code class="filename">/etc/profile</code>.
- </p></li><li class="listitem"><p>
- Set the console font to <code class="literal">Lucida Console</code>, because the
- raster font does not work with the ANSI code page.
- </p></li></ul></div></div><div class="refsect1" id="APP-PSQL-EXAMPLES"><h2>Examples</h2><p>
- The first example shows how to spread a command over several lines of
- input. Notice the changing prompt:
- </p><pre class="programlisting">
- testdb=> <strong class="userinput"><code>CREATE TABLE my_table (</code></strong>
- testdb(> <strong class="userinput"><code> first integer not null default 0,</code></strong>
- testdb(> <strong class="userinput"><code> second text)</code></strong>
- testdb-> <strong class="userinput"><code>;</code></strong>
- CREATE TABLE
- </pre><p>
- Now look at the table definition again:
- </p><pre class="programlisting">
- testdb=> <strong class="userinput"><code>\d my_table</code></strong>
- Table "public.my_table"
- Column | Type | Collation | Nullable | Default
- --------+---------+-----------+----------+---------
- first | integer | | not null | 0
- second | text | | |
- </pre><p>
- Now we change the prompt to something more interesting:
- </p><pre class="programlisting">
- testdb=> <strong class="userinput"><code>\set PROMPT1 '%n@%m %~%R%# '</code></strong>
- peter@localhost testdb=>
- </pre><p>
- Let's assume you have filled the table with data and want to take a
- look at it:
- </p><pre class="programlisting">
- peter@localhost testdb=> SELECT * FROM my_table;
- first | second
- -------+--------
- 1 | one
- 2 | two
- 3 | three
- 4 | four
- (4 rows)
-
- </pre><p>
- You can display tables in different ways by using the
- <code class="command">\pset</code> command:
- </p><pre class="programlisting">
- peter@localhost testdb=> <strong class="userinput"><code>\pset border 2</code></strong>
- Border style is 2.
- peter@localhost testdb=> <strong class="userinput"><code>SELECT * FROM my_table;</code></strong>
- +-------+--------+
- | first | second |
- +-------+--------+
- | 1 | one |
- | 2 | two |
- | 3 | three |
- | 4 | four |
- +-------+--------+
- (4 rows)
-
- peter@localhost testdb=> <strong class="userinput"><code>\pset border 0</code></strong>
- Border style is 0.
- peter@localhost testdb=> <strong class="userinput"><code>SELECT * FROM my_table;</code></strong>
- first second
- ----- ------
- 1 one
- 2 two
- 3 three
- 4 four
- (4 rows)
-
- peter@localhost testdb=> <strong class="userinput"><code>\pset border 1</code></strong>
- Border style is 1.
- peter@localhost testdb=> <strong class="userinput"><code>\pset format csv</code></strong>
- Output format is csv.
- peter@localhost testdb=> <strong class="userinput"><code>\pset tuples_only</code></strong>
- Tuples only is on.
- peter@localhost testdb=> <strong class="userinput"><code>SELECT second, first FROM my_table;</code></strong>
- one,1
- two,2
- three,3
- four,4
- peter@localhost testdb=> <strong class="userinput"><code>\pset format unaligned</code></strong>
- Output format is unaligned.
- peter@localhost testdb=> <strong class="userinput"><code>\pset fieldsep '\t'</code></strong>
- Field separator is " ".
- peter@localhost testdb=> <strong class="userinput"><code>SELECT second, first FROM my_table;</code></strong>
- one 1
- two 2
- three 3
- four 4
- </pre><p>
- Alternatively, use the short commands:
- </p><pre class="programlisting">
- peter@localhost testdb=> <strong class="userinput"><code>\a \t \x</code></strong>
- Output format is aligned.
- Tuples only is off.
- Expanded display is on.
- peter@localhost testdb=> <strong class="userinput"><code>SELECT * FROM my_table;</code></strong>
- -[ RECORD 1 ]-
- first | 1
- second | one
- -[ RECORD 2 ]-
- first | 2
- second | two
- -[ RECORD 3 ]-
- first | 3
- second | three
- -[ RECORD 4 ]-
- first | 4
- second | four
- </pre><p>
- When suitable, query results can be shown in a crosstab representation
- with the <code class="command">\crosstabview</code> command:
- </p><pre class="programlisting">
- testdb=> <strong class="userinput"><code>SELECT first, second, first > 2 AS gt2 FROM my_table;</code></strong>
- first | second | gt2
- -------+--------+-----
- 1 | one | f
- 2 | two | f
- 3 | three | t
- 4 | four | t
- (4 rows)
-
- testdb=> <strong class="userinput"><code>\crosstabview first second</code></strong>
- first | one | two | three | four
- -------+-----+-----+-------+------
- 1 | f | | |
- 2 | | f | |
- 3 | | | t |
- 4 | | | | t
- (4 rows)
- </pre><p>
-
- This second example shows a multiplication table with rows sorted in reverse
- numerical order and columns with an independent, ascending numerical order.
- </p><pre class="programlisting">
- testdb=> <strong class="userinput"><code>SELECT t1.first as "A", t2.first+100 AS "B", t1.first*(t2.first+100) as "AxB",</code></strong>
- testdb(> <strong class="userinput"><code>row_number() over(order by t2.first) AS ord</code></strong>
- testdb(> <strong class="userinput"><code>FROM my_table t1 CROSS JOIN my_table t2 ORDER BY 1 DESC</code></strong>
- testdb(> <strong class="userinput"><code>\crosstabview "A" "B" "AxB" ord</code></strong>
- A | 101 | 102 | 103 | 104
- ---+-----+-----+-----+-----
- 4 | 404 | 408 | 412 | 416
- 3 | 303 | 306 | 309 | 312
- 2 | 202 | 204 | 206 | 208
- 1 | 101 | 102 | 103 | 104
- (4 rows)
- </pre><p>
-
- </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="app-pgrestore.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="reference-client.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="app-reindexdb.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">pg_restore </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> <span class="application">reindexdb</span></td></tr></table></div></body></html>
|