|
Server : Apache/2.2.2 (Fedora) System : Linux App1.pathumtani.go.th 2.6.20-1.2320.fc5smp #1 SMP Tue Jun 12 19:40:16 EDT 2007 i686 User : apache ( 48) PHP Version : 5.2.9 Disable Function : NONE Directory : /proc/self/root/usr/share/doc/postgresql-8.1.9/html/ |
Upload File : |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>psql</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 8.1.9 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="PostgreSQL Client Applications"
HREF="reference-client.html"><LINK
REL="PREVIOUS"
TITLE="pg_restore"
HREF="app-pgrestore.html"><LINK
REL="NEXT"
TITLE="reindexdb"
HREF="app-reindexdb.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2007-04-20T04:40:08"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
>PostgreSQL 8.1.9 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="app-pgrestore.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="app-pgrestore.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="app-reindexdb.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="app-reindexdb.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="APP-PSQL"
></A
><SPAN
CLASS="APPLICATION"
>psql</SPAN
></H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN52056"
></A
><H2
>Name</H2
><SPAN
CLASS="APPLICATION"
>psql</SPAN
> -- <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> interactive terminal
</DIV
><A
NAME="AEN52061"
></A
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN52063"
></A
><H2
>Synopsis</H2
><P
><TT
CLASS="COMMAND"
>psql</TT
> [<TT
CLASS="REPLACEABLE"
><I
>option</I
></TT
>...] [<TT
CLASS="REPLACEABLE"
><I
>dbname</I
></TT
>
[<TT
CLASS="REPLACEABLE"
><I
>username</I
></TT
>]]</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN52072"
></A
><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. In addition, it 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"
><A
NAME="R1-APP-PSQL-3"
></A
><H2
>Options</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="OPTION"
>-a</TT
><BR><TT
CLASS="OPTION"
>--echo-all</TT
></DT
><DD
><P
> Print all input lines to standard output as they are read. This is more
useful for script processing rather than interactive mode. This is
equivalent to setting the variable <TT
CLASS="VARNAME"
>ECHO</TT
> to
<TT
CLASS="LITERAL"
>all</TT
>.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-A</TT
><BR><TT
CLASS="OPTION"
>--no-align</TT
></DT
><DD
><P
> Switches to unaligned output mode. (The default output mode is
otherwise aligned.)
</P
></DD
><DT
><TT
CLASS="OPTION"
>-c <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--command <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
></TT
></DT
><DD
><P
> Specifies that <SPAN
CLASS="APPLICATION"
>psql</SPAN
> is to execute one
command string, <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>,
and then exit. This is useful in shell scripts.
</P
><P
> <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
> 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. To achieve that, you could pipe the string into
<SPAN
CLASS="APPLICATION"
>psql</SPAN
>, like this: <TT
CLASS="LITERAL"
>echo "\x \\
select * from foo;" | psql</TT
>.
</P
><P
> If the command string contains multiple SQL commands, they are
processed in a single transaction, unless there are explicit
BEGIN/COMMIT commands included in the string to divide it into
multiple transactions. This is different from the behavior when
the same string is fed to <SPAN
CLASS="APPLICATION"
>psql</SPAN
>'s standard input.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-d <TT
CLASS="REPLACEABLE"
><I
>dbname</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--dbname <TT
CLASS="REPLACEABLE"
><I
>dbname</I
></TT
></TT
></DT
><DD
><P
> Specifies the name of the database to connect to. This is
equivalent to specifying <TT
CLASS="REPLACEABLE"
><I
>dbname</I
></TT
> as the first non-option
argument on the command line.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-e</TT
><BR><TT
CLASS="OPTION"
>--echo-queries</TT
></DT
><DD
><P
> Copy all SQL commands sent to the server to standard output as well.
This is equivalent
to setting the variable <TT
CLASS="VARNAME"
>ECHO</TT
> to
<TT
CLASS="LITERAL"
>queries</TT
>.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-E</TT
><BR><TT
CLASS="OPTION"
>--echo-hidden</TT
></DT
><DD
><P
> Echo the actual queries generated by <TT
CLASS="COMMAND"
>\d</TT
> 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 <TT
CLASS="VARNAME"
>ECHO_HIDDEN</TT
> from within
<SPAN
CLASS="APPLICATION"
>psql</SPAN
>.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-f <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--file <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></TT
></DT
><DD
><P
> Use the file <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
>
as the source of commands instead of reading commands interactively.
After the file is processed, <SPAN
CLASS="APPLICATION"
>psql</SPAN
>
terminates. This is in many ways equivalent to the internal
command <TT
CLASS="COMMAND"
>\i</TT
>.
</P
><P
> If <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> is <TT
CLASS="LITERAL"
>-</TT
>
(hyphen), then standard input is read.
</P
><P
> Using this option is subtly different from writing <TT
CLASS="LITERAL"
>psql
< <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></TT
>. In general,
both will do what you expect, but using <TT
CLASS="LITERAL"
>-f</TT
>
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 that you would have gotten had you entered
everything by hand.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-F <TT
CLASS="REPLACEABLE"
><I
>separator</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--field-separator <TT
CLASS="REPLACEABLE"
><I
>separator</I
></TT
></TT
></DT
><DD
><P
> Use <TT
CLASS="REPLACEABLE"
><I
>separator</I
></TT
> as the
field separator for unaligned output. This is equivalent to
<TT
CLASS="COMMAND"
>\pset fieldsep</TT
> or <TT
CLASS="COMMAND"
>\f</TT
>.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-h <TT
CLASS="REPLACEABLE"
><I
>hostname</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--host <TT
CLASS="REPLACEABLE"
><I
>hostname</I
></TT
></TT
></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
><TT
CLASS="OPTION"
>-H</TT
><BR><TT
CLASS="OPTION"
>--html</TT
></DT
><DD
><P
> Turn on <ACRONYM
CLASS="ACRONYM"
>HTML</ACRONYM
> tabular output. This is
equivalent to <TT
CLASS="LITERAL"
>\pset format html</TT
> or the
<TT
CLASS="COMMAND"
>\H</TT
> command.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-l</TT
><BR><TT
CLASS="OPTION"
>--list</TT
></DT
><DD
><P
> List all available databases, then exit. Other non-connection
options are ignored. This is similar to the internal command
<TT
CLASS="COMMAND"
>\list</TT
>.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-L <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--log-file <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></TT
></DT
><DD
><P
> Write all query output into file <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
>, in addition to the
normal output destination.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-o <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--output <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></TT
></DT
><DD
><P
> Put all query output into file <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
>. This is equivalent to
the command <TT
CLASS="COMMAND"
>\o</TT
>.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-p <TT
CLASS="REPLACEABLE"
><I
>port</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--port <TT
CLASS="REPLACEABLE"
><I
>port</I
></TT
></TT
></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 <TT
CLASS="ENVAR"
>PGPORT</TT
>
environment variable or, if not set, to the port specified at
compile time, usually 5432.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-P <TT
CLASS="REPLACEABLE"
><I
>assignment</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--pset <TT
CLASS="REPLACEABLE"
><I
>assignment</I
></TT
></TT
></DT
><DD
><P
> Allows you to specify printing options in the style of
<TT
CLASS="COMMAND"
>\pset</TT
> on the command line. Note that here you
have to separate name and value with an equal sign instead of a
space. Thus to set the output format to LaTeX, you could write
<TT
CLASS="LITERAL"
>-P format=latex</TT
>.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-q</TT
><BR><TT
CLASS="OPTION"
>--quiet</TT
></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 <TT
CLASS="OPTION"
>-c</TT
> option.
Within <SPAN
CLASS="APPLICATION"
>psql</SPAN
> you can also set the
<TT
CLASS="VARNAME"
>QUIET</TT
> variable to achieve the same effect.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-R <TT
CLASS="REPLACEABLE"
><I
>separator</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--record-separator <TT
CLASS="REPLACEABLE"
><I
>separator</I
></TT
></TT
></DT
><DD
><P
> Use <TT
CLASS="REPLACEABLE"
><I
>separator</I
></TT
> as the
record separator for unaligned output. This is equivalent to the
<TT
CLASS="COMMAND"
>\pset recordsep</TT
> command.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-s</TT
><BR><TT
CLASS="OPTION"
>--single-step</TT
></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
><TT
CLASS="OPTION"
>-S</TT
><BR><TT
CLASS="OPTION"
>--single-line</TT
></DT
><DD
><P
> Runs in single-line mode where a newline terminates an SQL command, as a
semicolon does.
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> 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
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="OPTION"
>-t</TT
><BR><TT
CLASS="OPTION"
>--tuples-only</TT
></DT
><DD
><P
> Turn off printing of column names and result row count footers,
etc. This is equivalent to the <TT
CLASS="COMMAND"
>\t</TT
> command.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-T <TT
CLASS="REPLACEABLE"
><I
>table_options</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--table-attr <TT
CLASS="REPLACEABLE"
><I
>table_options</I
></TT
></TT
></DT
><DD
><P
> Allows you to specify options to be placed within the
<ACRONYM
CLASS="ACRONYM"
>HTML</ACRONYM
> <CODE
CLASS="SGMLTAG"
>table</CODE
> tag. See
<TT
CLASS="COMMAND"
>\pset</TT
> for details.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-u</TT
></DT
><DD
><P
> Forces <SPAN
CLASS="APPLICATION"
>psql</SPAN
> to prompt for the user name and
password before connecting to the database.
</P
><P
> This option is deprecated, as it is conceptually flawed.
(Prompting for a non-default user name and prompting for a
password because the server requires it are really two different
things.) You are encouraged to look at the <TT
CLASS="OPTION"
>-U</TT
> and
<TT
CLASS="OPTION"
>-W</TT
> options instead.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-U <TT
CLASS="REPLACEABLE"
><I
>username</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--username <TT
CLASS="REPLACEABLE"
><I
>username</I
></TT
></TT
></DT
><DD
><P
> Connect to the database as the user <TT
CLASS="REPLACEABLE"
><I
>username</I
></TT
> instead of the default.
(You must have permission to do so, of course.)
</P
></DD
><DT
><TT
CLASS="OPTION"
>-v <TT
CLASS="REPLACEABLE"
><I
>assignment</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--set <TT
CLASS="REPLACEABLE"
><I
>assignment</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--variable <TT
CLASS="REPLACEABLE"
><I
>assignment</I
></TT
></TT
></DT
><DD
><P
> Perform a variable assignment, like the <TT
CLASS="COMMAND"
>\set</TT
>
internal 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 just set a variable without a value,
use the equal sign but leave off the value. These assignments are
done during a very early stage of start-up, so variables reserved
for internal purposes might get overwritten later.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-V</TT
><BR><TT
CLASS="OPTION"
>--version</TT
></DT
><DD
><P
> Print the <SPAN
CLASS="APPLICATION"
>psql</SPAN
> version and exit.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-W</TT
><BR><TT
CLASS="OPTION"
>--password</TT
></DT
><DD
><P
> Forces <SPAN
CLASS="APPLICATION"
>psql</SPAN
> to prompt for a
password before connecting to a database.
</P
><P
> <SPAN
CLASS="APPLICATION"
>psql</SPAN
> should automatically prompt for a
password whenever the server requests password authentication.
However, currently password request detection is not totally
reliable, hence this option to force a prompt. If no password
prompt is issued and the server requires password authentication,
the connection attempt will fail.
</P
><P
> This option will remain set for the entire session, even if you
change the database connection with the meta-command
<TT
CLASS="COMMAND"
>\connect</TT
>.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-x</TT
><BR><TT
CLASS="OPTION"
>--expanded</TT
></DT
><DD
><P
> Turn on the expanded table formatting mode. This is equivalent to the
<TT
CLASS="COMMAND"
>\x</TT
> command.
</P
></DD
><DT
><TT
CLASS="OPTION"
>-X,</TT
><BR><TT
CLASS="OPTION"
>--no-psqlrc</TT
></DT
><DD
><P
> Do not read the start-up file (neither the system-wide
<TT
CLASS="FILENAME"
>psqlrc</TT
> file nor the user's
<TT
CLASS="FILENAME"
>~/.psqlrc</TT
> file).
</P
></DD
><DT
><TT
CLASS="OPTION"
>-?</TT
><BR><TT
CLASS="OPTION"
>--help</TT
></DT
><DD
><P
> Show help about <SPAN
CLASS="APPLICATION"
>psql</SPAN
> command line
arguments, and exit.
</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN52383"
></A
><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 (out of memory,
file not found) occurs, 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 <TT
CLASS="VARNAME"
>ON_ERROR_STOP</TT
> was set.
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN52388"
></A
><H2
>Usage</H2
><DIV
CLASS="REFSECT2"
><A
NAME="R2-APP-PSQL-CONNECTING"
></A
><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
<TT
CLASS="OPTION"
>-d</TT
>, <TT
CLASS="OPTION"
>-h</TT
>, <TT
CLASS="OPTION"
>-p</TT
>, and
<TT
CLASS="OPTION"
>-U</TT
> 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
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 <TT
CLASS="LITERAL"
>localhost</TT
> 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
Unix user name, as is the default database name. Note that you can't
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
<TT
CLASS="ENVAR"
>PGDATABASE</TT
>, <TT
CLASS="ENVAR"
>PGHOST</TT
>,
<TT
CLASS="ENVAR"
>PGPORT</TT
> and/or <TT
CLASS="ENVAR"
>PGUSER</TT
> to appropriate
values. (For additional environment variables, see <A
HREF="libpq-envars.html"
>Section 28.11</A
>.) It is also convenient to have a
<TT
CLASS="FILENAME"
>~/.pgpass</TT
> file to avoid regularly having to type in
passwords. See <A
HREF="libpq-pgpass.html"
>Section 28.12</A
> for more information.
</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
></DIV
><DIV
CLASS="REFSECT2"
><A
NAME="R2-APP-PSQL-4"
></A
><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 <TT
CLASS="LITERAL"
>=></TT
>. For example,
</P><PRE
CLASS="PROGRAMLISTING"
>$ <KBD
CLASS="USERINPUT"
>psql testdb</KBD
>
Welcome to psql 8.1.9, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
testdb=></PRE
><P>
</P
><P
> At the prompt, the user may 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
> Whenever a command is executed, <SPAN
CLASS="APPLICATION"
>psql</SPAN
> also polls
for asynchronous notification events generated by
<A
HREF="sql-listen.html"
><I
>LISTEN</I
></A
> and
<A
HREF="sql-notify.html"
><I
>NOTIFY</I
></A
>.
</P
></DIV
><DIV
CLASS="REFSECT2"
><A
NAME="AEN52426"
></A
><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 help make
<SPAN
CLASS="APPLICATION"
>psql</SPAN
> more useful for administration or
scripting. Meta-commands are more commonly 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 into an argument you may quote it with a
single quote. To include a single quote into such an argument,
precede it by a backslash. Anything contained in single quotes is
furthermore subject to C-like substitutions for
<TT
CLASS="LITERAL"
>\n</TT
> (new line), <TT
CLASS="LITERAL"
>\t</TT
> (tab),
<TT
CLASS="LITERAL"
>\</TT
><TT
CLASS="REPLACEABLE"
><I
>digits</I
></TT
> (octal), and
<TT
CLASS="LITERAL"
>\x</TT
><TT
CLASS="REPLACEABLE"
><I
>digits</I
></TT
> (hexadecimal).
</P
><P
> If an unquoted argument begins with a colon (<TT
CLASS="LITERAL"
>:</TT
>),
it is taken as a <SPAN
CLASS="APPLICATION"
>psql</SPAN
> variable and the value of the
variable is used as the argument instead.
</P
><P
> Arguments that are enclosed in backquotes (<TT
CLASS="LITERAL"
>`</TT
>)
are taken as a command line that is passed to the shell. The
output of the command (with any trailing newline removed) is taken
as the argument value. The above escape sequences also apply in
backquotes.
</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 (<TT
CLASS="LITERAL"
>"</TT
>) 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,
<TT
CLASS="LITERAL"
>FOO"BAR"BAZ</TT
> is interpreted as <TT
CLASS="LITERAL"
>fooBARbaz</TT
>,
and <TT
CLASS="LITERAL"
>"A weird"" name"</TT
> becomes <TT
CLASS="LITERAL"
>A weird"
name</TT
>.
</P
><P
> Parsing for arguments stops when another unquoted backslash occurs.
This is taken as the beginning of a new meta-command. The special
sequence <TT
CLASS="LITERAL"
>\\</TT
> (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
> The following meta-commands are defined:
<P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>\a</TT
></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 <TT
CLASS="COMMAND"
>\pset</TT
> for a
more general solution.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\cd [ <TT
CLASS="REPLACEABLE"
><I
>directory</I
></TT
> ]</TT
></DT
><DD
><P
> Changes the current working directory to
<TT
CLASS="REPLACEABLE"
><I
>directory</I
></TT
>. Without argument, changes
to the current user's home directory.
</P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
> To print your current working directory, use <TT
CLASS="LITERAL"
>\!pwd</TT
>.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\C [ <TT
CLASS="REPLACEABLE"
><I
>title</I
></TT
> ]</TT
></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
<TT
CLASS="LITERAL"
>\pset title <TT
CLASS="REPLACEABLE"
><I
>title</I
></TT
></TT
>. (The name of
this command derives from <SPAN
CLASS="QUOTE"
>"caption"</SPAN
>, as it was
previously only used to set the caption in an
<ACRONYM
CLASS="ACRONYM"
>HTML</ACRONYM
> table.)
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\connect</TT
> (or <TT
CLASS="LITERAL"
>\c</TT
>) <TT
CLASS="LITERAL"
>[ <TT
CLASS="REPLACEABLE"
><I
>dbname</I
></TT
> [ <TT
CLASS="REPLACEABLE"
><I
>username</I
></TT
> ] ]</TT
></DT
><DD
><P
> Establishes a connection to a new database and/or under a user
name. The previous connection is closed. If <TT
CLASS="REPLACEABLE"
><I
>dbname</I
></TT
> is <TT
CLASS="LITERAL"
>-</TT
>
the current database name is assumed.
</P
><P
> If <TT
CLASS="REPLACEABLE"
><I
>username</I
></TT
> is
omitted the current user name is assumed. </P
><P
> As a special rule, <TT
CLASS="COMMAND"
>\connect</TT
> without any
arguments will connect to the default database as the default
user (as you would have gotten by starting
<SPAN
CLASS="APPLICATION"
>psql</SPAN
> without any arguments).
</P
><P
> If the connection attempt failed (wrong user name, access
denied, etc.), the previous connection will be kept if and only
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
></DD
><DT
><TT
CLASS="LITERAL"
>\copy <TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
>
[ ( <TT
CLASS="REPLACEABLE"
><I
>column_list</I
></TT
> ) ]
{ <TT
CLASS="LITERAL"
>from</TT
> | <TT
CLASS="LITERAL"
>to</TT
> }
{ <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> | stdin | stdout | pstdin | pstdout }
[ with ]
[ oids ]
[ delimiter [ as ] '<TT
CLASS="REPLACEABLE"
><I
>character</I
></TT
>' ]
[ null [ as ] '<TT
CLASS="REPLACEABLE"
><I
>string</I
></TT
>' ]
[ csv [ quote [ as ] '<TT
CLASS="REPLACEABLE"
><I
>character</I
></TT
>' ]
[ escape [ as ] '<TT
CLASS="REPLACEABLE"
><I
>character</I
></TT
>' ]
[ force quote <TT
CLASS="REPLACEABLE"
><I
>column_list</I
></TT
> ]
[ force not null <TT
CLASS="REPLACEABLE"
><I
>column_list</I
></TT
> ] ]</TT
></DT
><DD
><P
> Performs a frontend (client) copy. This is an operation that
runs an <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> <A
HREF="sql-copy.html"
><I
>COPY</I
></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
> The syntax of the command is similar to that of the
<ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> <A
HREF="sql-copy.html"
><I
>COPY</I
></A
> command. Note that, because of this,
special parsing rules apply to the <TT
CLASS="COMMAND"
>\copy</TT
>
command. In particular, the variable substitution rules and
backslash escapes do not apply.
</P
><P
> <TT
CLASS="LITERAL"
>\copy <TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
> from <TT
CLASS="REPLACEABLE"
><I
>stdin | stdout</I
></TT
></TT
>
reads/writes based on the command input and output respectively.
All rows are read from the same source that issued the command,
continuing until <TT
CLASS="LITERAL"
>\.</TT
> is read or the stream
reaches <ACRONYM
CLASS="ACRONYM"
>EOF</ACRONYM
>. Output is sent to the same place as
command output. To read/write from
<SPAN
CLASS="APPLICATION"
>psql</SPAN
>'s standard input or output, use
<TT
CLASS="LITERAL"
>pstdin</TT
> or <TT
CLASS="LITERAL"
>pstdout</TT
>. This option is useful
for populating tables in-line within a SQL script file.
</P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
> This operation is not as efficient as the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
>
<TT
CLASS="COMMAND"
>COPY</TT
> command because all data must pass
through the client/server connection. For large
amounts of data the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> command may be preferable.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\copyright</TT
></DT
><DD
><P
> Shows the copyright and distribution terms of
<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\d [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
><BR><TT
CLASS="LITERAL"
>\d+ [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> For each relation (table, view, index, or sequence) matching the
<TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
>, show all
columns, their types, the tablespace (if not the default) and any special
attributes such as <TT
CLASS="LITERAL"
>NOT NULL</TT
> or defaults, if
any. Associated indexes, constraints, rules, and triggers are
also shown, as is the view definition if the relation is a view.
(<SPAN
CLASS="QUOTE"
>"Matching the pattern"</SPAN
> is defined below.)
</P
><P
> The command form <TT
CLASS="LITERAL"
>\d+</TT
> 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.
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> If <TT
CLASS="COMMAND"
>\d</TT
> is used without a
<TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> argument, it is
equivalent to <TT
CLASS="COMMAND"
>\dtvs</TT
> which will show a list of
all tables, views, and sequences. This is purely a convenience
measure.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\da [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Lists all available aggregate functions, together with the data
type they operate on. If <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
>
is specified, only aggregates whose names match the pattern are shown.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\db [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
><BR><TT
CLASS="LITERAL"
>\db+ [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Lists all available tablespaces. If <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
>
is specified, only tablespaces whose names match the pattern are shown.
If <TT
CLASS="LITERAL"
>+</TT
> is appended to the command name, each object
is listed with its associated permissions.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\dc [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Lists all available conversions between character-set encodings.
If <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
>
is specified, only conversions whose names match the pattern are
listed.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\dC</TT
></DT
><DD
><P
> Lists all available type casts.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\dd [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Shows the descriptions of objects matching the <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
>, or of all visible objects if
no argument is given. But in either case, only objects that have
a description are listed.
(<SPAN
CLASS="QUOTE"
>"Object"</SPAN
> covers aggregates, functions, operators,
types, relations (tables, views, indexes, sequences, large
objects), rules, and triggers.) For example:
</P><PRE
CLASS="PROGRAMLISTING"
>=> <KBD
CLASS="USERINPUT"
>\dd version</KBD
>
Object descriptions
Schema | Name | Object | Description
------------+---------+----------+---------------------------
pg_catalog | version | function | PostgreSQL version string
(1 row)</PRE
><P>
</P
><P
> Descriptions for objects can be created with the <A
HREF="sql-comment.html"
><I
>COMMENT</I
></A
>
<ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> command.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\dD [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Lists all available domains. If <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
>
is specified, only matching domains are shown.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\df [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
><BR><TT
CLASS="LITERAL"
>\df+ [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Lists available functions, together with their argument and
return types. If <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
>
is specified, only functions whose names match the pattern are shown.
If the form <TT
CLASS="LITERAL"
>\df+</TT
> is used, additional information about
each function, including language and description, is shown.
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> To look up functions taking argument or returning values of a specific
type, use your pager's search capability to scroll through the <TT
CLASS="LITERAL"
>\df</TT
>
output.
</P
><P
> To reduce clutter, <TT
CLASS="LITERAL"
>\df</TT
> does not show data type I/O
functions. This is implemented by ignoring functions that accept
or return type <TT
CLASS="TYPE"
>cstring</TT
>.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\dg [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Lists all database roles. If <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> is specified, only
those roles whose names match the pattern are listed.
(This command is now effectively the same as <TT
CLASS="LITERAL"
>\du</TT
>.)
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\distvS [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> This is not the actual command name: the letters
<TT
CLASS="LITERAL"
>i</TT
>, <TT
CLASS="LITERAL"
>s</TT
>, <TT
CLASS="LITERAL"
>t</TT
>,
<TT
CLASS="LITERAL"
>v</TT
>, <TT
CLASS="LITERAL"
>S</TT
> stand for index,
sequence, table, view, and system table, respectively. You can
specify any or all of these letters, in any order, to obtain a
listing of all the matching objects. The letter S restricts the
listing to system objects; without <TT
CLASS="LITERAL"
>S</TT
>, only
non-system objects are shown. If <TT
CLASS="LITERAL"
>+</TT
> is appended
to the command name, each object is listed with its associated
description, if any.
</P
><P
> If <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> is
specified, only objects whose names match the pattern are listed.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\dl</TT
></DT
><DD
><P
> This is an alias for <TT
CLASS="COMMAND"
>\lo_list</TT
>, which shows a
list of large objects.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\dn [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
><BR><TT
CLASS="LITERAL"
>\dn+ [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Lists all available schemas (namespaces). If <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> (a regular expression)
is specified, only schemas whose names match the pattern are listed.
Non-local temporary schemas are suppressed. If <TT
CLASS="LITERAL"
>+</TT
>
is appended to the command name, each object is listed with its associated
permissions and description, if any.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\do [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Lists available operators with their operand and return types.
If <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> is
specified, only operators whose names match the pattern are listed.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\dp [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Produces a list of all available tables, views and sequences with their
associated access privileges.
If <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> is
specified, only tables, views and sequences whose names match the pattern are listed.
</P
><P
> The commands <TT
CLASS="COMMAND"
>GRANT</TT
> and
<TT
CLASS="COMMAND"
>REVOKE</TT
> are used to set access privileges.
See <A
HREF="sql-grant.html"
><I
>GRANT</I
></A
>
for more information.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\dT [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
><BR><TT
CLASS="LITERAL"
>\dT+ [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Lists all data types or only those that match <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
>. The command form
<TT
CLASS="LITERAL"
>\dT+</TT
> shows extra information.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\du [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Lists all database roles, or only those that match <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
>.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\edit</TT
> (or <TT
CLASS="LITERAL"
>\e</TT
>) <TT
CLASS="LITERAL"
>[ <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> ]</TT
></DT
><DD
><P
> If <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> is
specified, the file is edited; after the editor exits, its
content is copied back to the query buffer. If no argument is
given, the current query buffer is copied to a temporary file
which is then edited in the same fashion.
</P
><P
> The new query buffer is then re-parsed according to the normal
rules of <SPAN
CLASS="APPLICATION"
>psql</SPAN
>, where the whole buffer
is treated as a single line. (Thus you cannot make scripts this
way. Use <TT
CLASS="COMMAND"
>\i</TT
> for that.) This means also that
if the query ends with (or rather contains) a semicolon, it is
immediately executed. In other cases it will merely wait in the
query buffer.
</P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
> <SPAN
CLASS="APPLICATION"
>psql</SPAN
> searches the environment
variables <TT
CLASS="ENVAR"
>PSQL_EDITOR</TT
>, <TT
CLASS="ENVAR"
>EDITOR</TT
>, and
<TT
CLASS="ENVAR"
>VISUAL</TT
> (in that order) for an editor to use. If
all of them are unset, <TT
CLASS="FILENAME"
>vi</TT
> is used on Unix
systems, <TT
CLASS="FILENAME"
>notepad.exe</TT
> on Windows systems.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\echo <TT
CLASS="REPLACEABLE"
><I
>text</I
></TT
> [ ... ]</TT
></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"
>=> <KBD
CLASS="USERINPUT"
>\echo `date`</KBD
>
Tue Oct 26 21:40:57 CEST 1999</PRE
><P>
If the first argument is an unquoted <TT
CLASS="LITERAL"
>-n</TT
> the trailing
newline is not written.
</P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
> If you use the <TT
CLASS="COMMAND"
>\o</TT
> command to redirect your
query output you may wish to use <TT
CLASS="COMMAND"
>\qecho</TT
>
instead of this command.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\encoding [ <TT
CLASS="REPLACEABLE"
><I
>encoding</I
></TT
> ]</TT
></DT
><DD
><P
> Sets the client character set encoding. Without an argument, this command
shows the current encoding.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\f [ <TT
CLASS="REPLACEABLE"
><I
>string</I
></TT
> ]</TT
></DT
><DD
><P
> Sets the field separator for unaligned query output. The default
is the vertical bar (<TT
CLASS="LITERAL"
>|</TT
>). See also
<TT
CLASS="COMMAND"
>\pset</TT
> for a generic way of setting output
options.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\g</TT
> [ { <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> | <TT
CLASS="LITERAL"
>|</TT
><TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
> } ]</DT
><DD
><P
> Sends the current query input buffer to the server and
optionally stores the query's output in <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> or pipes the output
into a separate Unix shell executing <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>. A bare
<TT
CLASS="LITERAL"
>\g</TT
> is virtually equivalent to a semicolon. A
<TT
CLASS="LITERAL"
>\g</TT
> with argument is a <SPAN
CLASS="QUOTE"
>"one-shot"</SPAN
>
alternative to the <TT
CLASS="COMMAND"
>\o</TT
> command.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\help</TT
> (or <TT
CLASS="LITERAL"
>\h</TT
>) <TT
CLASS="LITERAL"
>[ <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
> ]</TT
></DT
><DD
><P
> Gives syntax help on the specified <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
>
command. If <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>
is not specified, then <SPAN
CLASS="APPLICATION"
>psql</SPAN
> will list
all the commands for which syntax help is available. If
<TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
> is an
asterisk (<TT
CLASS="LITERAL"
>*</TT
>), then syntax help on all
<ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> commands is shown.
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> To simplify typing, commands that consists of several words do
not have to be quoted. Thus it is fine to type <KBD
CLASS="USERINPUT"
>\help
alter table</KBD
>.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\H</TT
></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 <TT
CLASS="COMMAND"
>\pset</TT
>
about setting other output options.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\i <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></TT
></DT
><DD
><P
> Reads input from the file <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> and executes it as
though it had been typed on the keyboard.
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> If you want to see the lines on the screen as they are read you
must set the variable <TT
CLASS="VARNAME"
>ECHO</TT
> to
<TT
CLASS="LITERAL"
>all</TT
>.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\l</TT
> (or <TT
CLASS="LITERAL"
>\list</TT
>)<BR><TT
CLASS="LITERAL"
>\l+</TT
> (or <TT
CLASS="LITERAL"
>\list+</TT
>)</DT
><DD
><P
> List the names, owners, and character set encodings of all the databases in
the server. If <TT
CLASS="LITERAL"
>+</TT
> is appended to the command
name, database descriptions are also displayed.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\lo_export <TT
CLASS="REPLACEABLE"
><I
>loid</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></TT
></DT
><DD
><P
> Reads the large object with <ACRONYM
CLASS="ACRONYM"
>OID</ACRONYM
> <TT
CLASS="REPLACEABLE"
><I
>loid</I
></TT
> from the database and
writes it to <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
>. 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"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
> Use <TT
CLASS="COMMAND"
>\lo_list</TT
> to find out the large object's
<ACRONYM
CLASS="ACRONYM"
>OID</ACRONYM
>.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\lo_import <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> [ <TT
CLASS="REPLACEABLE"
><I
>comment</I
></TT
> ]</TT
></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=> <KBD
CLASS="USERINPUT"
>\lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'</KBD
>
lo_import 152801</PRE
><P>
The response indicates that the large object received object ID
152801 which one ought to remember if one wants to access the
object ever again. For that reason it is recommended to always
associate a human-readable comment with every object. Those can
then be seen with the <TT
CLASS="COMMAND"
>\lo_list</TT
> 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
><TT
CLASS="LITERAL"
>\lo_list</TT
></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
><TT
CLASS="LITERAL"
>\lo_unlink <TT
CLASS="REPLACEABLE"
><I
>loid</I
></TT
></TT
></DT
><DD
><P
> Deletes the large object with <ACRONYM
CLASS="ACRONYM"
>OID</ACRONYM
>
<TT
CLASS="REPLACEABLE"
><I
>loid</I
></TT
> from the
database.
</P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
> Use <TT
CLASS="COMMAND"
>\lo_list</TT
> to find out the large object's
<ACRONYM
CLASS="ACRONYM"
>OID</ACRONYM
>.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\o</TT
> [ {<TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> | <TT
CLASS="LITERAL"
>|</TT
><TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>} ]</DT
><DD
><P
> Saves future query results to the file <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> or pipes future results
into a separate Unix shell to execute <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>. If no arguments are
specified, the query output will be reset to the standard output.
</P
><P
> <SPAN
CLASS="QUOTE"
>"Query results"</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 <TT
CLASS="COMMAND"
>\d</TT
>), but not error
messages.
</P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
> To intersperse text output in between query results, use
<TT
CLASS="COMMAND"
>\qecho</TT
>.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\p</TT
></DT
><DD
><P
> Print the current query buffer to the standard output.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\pset <TT
CLASS="REPLACEABLE"
><I
>parameter</I
></TT
> [ <TT
CLASS="REPLACEABLE"
><I
>value</I
></TT
> ]</TT
></DT
><DD
><P
> This command sets options affecting the output of query result
tables. <TT
CLASS="REPLACEABLE"
><I
>parameter</I
></TT
>
describes which option is to be set. The semantics of
<TT
CLASS="REPLACEABLE"
><I
>value</I
></TT
> depend
thereon.
</P
><P
> Adjustable printing options are:
<P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>format</TT
></DT
><DD
><P
> Sets the output format to one of <TT
CLASS="LITERAL"
>unaligned</TT
>,
<TT
CLASS="LITERAL"
>aligned</TT
>, <TT
CLASS="LITERAL"
>html</TT
>,
<TT
CLASS="LITERAL"
>latex</TT
>, or <TT
CLASS="LITERAL"
>troff-ms</TT
>.
Unique abbreviations are allowed. (That would mean one letter
is enough.)
</P
><P
> <SPAN
CLASS="QUOTE"
>"Unaligned"</SPAN
> writes all columns of a row on a
line, separated by the currently active field separator. This
is intended to create output that might be intended to be read
in by other programs (tab-separated, comma-separated).
<SPAN
CLASS="QUOTE"
>"Aligned"</SPAN
> mode is the standard, human-readable,
nicely formatted text output that is default. The
<SPAN
CLASS="QUOTE"
>"<ACRONYM
CLASS="ACRONYM"
>HTML</ACRONYM
>"</SPAN
> and
<SPAN
CLASS="QUOTE"
>"LaTeX"</SPAN
> modes 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
so dramatic in <ACRONYM
CLASS="ACRONYM"
>HTML</ACRONYM
>, but in LaTeX you must
have a complete document wrapper.)
</P
></DD
><DT
><TT
CLASS="LITERAL"
>border</TT
></DT
><DD
><P
> The second argument must be a number. In general, the higher
the number the more borders and lines the tables will have,
but this depends on the particular format. In
<ACRONYM
CLASS="ACRONYM"
>HTML</ACRONYM
> mode, this will translate directly
into the <TT
CLASS="LITERAL"
>border=...</TT
> attribute, in the
others only values 0 (no border), 1 (internal dividing lines),
and 2 (table frame) make sense.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>expanded</TT
> (or <TT
CLASS="LITERAL"
>x</TT
>)</DT
><DD
><P
> Toggles between regular and expanded format. When expanded
format 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"
>"horizontal"</SPAN
> mode.
</P
><P
> Expanded mode is supported by all four output formats.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>null</TT
></DT
><DD
><P
> The second argument is a string that should be printed
whenever a column is null. The default is not to print
anything, which can easily be mistaken for, say, an empty
string. Thus, one might choose to write <TT
CLASS="LITERAL"
>\pset null
'(null)'</TT
>.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>fieldsep</TT
></DT
><DD
><P
> Specifies the field separator to be used in unaligned output
mode. That way one can create, for example, tab- or
comma-separated output, which other programs might prefer. To
set a tab as field separator, type <TT
CLASS="LITERAL"
>\pset fieldsep
'\t'</TT
>. The default field separator is
<TT
CLASS="LITERAL"
>'|'</TT
> (a vertical bar).
</P
></DD
><DT
><TT
CLASS="LITERAL"
>footer</TT
></DT
><DD
><P
> Toggles the display of the default footer <TT
CLASS="LITERAL"
>(x
rows)</TT
>.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>numericlocale</TT
></DT
><DD
><P
> Toggles the display of a locale-aware character to separate groups
of digits to the left of the decimal marker. It also enables
a locale-aware decimal marker.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>recordsep</TT
></DT
><DD
><P
> Specifies the record (line) separator to use in unaligned
output mode. The default is a newline character.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>tuples_only</TT
> (or <TT
CLASS="LITERAL"
>t</TT
>)</DT
><DD
><P
> Toggles between tuples only and full display. Full display may
show extra information such as column headers, titles, and
various footers. In tuples only mode, only actual table data
is shown.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>title [ <TT
CLASS="REPLACEABLE"
><I
>text</I
></TT
> ]</TT
></DT
><DD
><P
> Sets the table title for any subsequently printed tables. This
can be used to give your output descriptive tags. If no
argument is given, the title is unset.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>tableattr</TT
> (or <TT
CLASS="LITERAL"
>T</TT
>) <TT
CLASS="LITERAL"
>[ <TT
CLASS="REPLACEABLE"
><I
>text</I
></TT
> ]</TT
></DT
><DD
><P
> Allows you to specify any attributes to be placed inside the
<ACRONYM
CLASS="ACRONYM"
>HTML</ACRONYM
> <CODE
CLASS="SGMLTAG"
>table</CODE
> tag. This
could for example be <TT
CLASS="LITERAL"
>cellpadding</TT
> or
<TT
CLASS="LITERAL"
>bgcolor</TT
>. Note that you probably don't want
to specify <TT
CLASS="LITERAL"
>border</TT
> here, as that is already
taken care of by <TT
CLASS="LITERAL"
>\pset border</TT
>.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>pager</TT
></DT
><DD
><P
> Controls use of a pager for query and <SPAN
CLASS="APPLICATION"
>psql</SPAN
>
help output. If the environment variable <TT
CLASS="ENVAR"
>PAGER</TT
>
is set, the output is piped to the specified program.
Otherwise a platform-dependent default (such as
<TT
CLASS="FILENAME"
>more</TT
>) is used.
</P
><P
> When the pager is off, the pager is not used. When the pager
is on, the pager is used only when appropriate, i.e. the
output is to a terminal and will not fit on the screen.
(<SPAN
CLASS="APPLICATION"
>psql</SPAN
> does not do a perfect job of estimating
when to use the pager.) <TT
CLASS="LITERAL"
>\pset pager</TT
> turns the
pager on and off. Pager can also be set to <TT
CLASS="LITERAL"
>always</TT
>,
which causes the pager to be always used.
</P
></DD
></DL
></DIV
><P>
</P
><P
> Illustrations on how these different formats look can be seen in
the <A
HREF="app-psql.html#APP-PSQL-EXAMPLES"
><I
>Examples</I
></A
> section.
</P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
> There are various shortcut commands for <TT
CLASS="COMMAND"
>\pset</TT
>. See
<TT
CLASS="COMMAND"
>\a</TT
>, <TT
CLASS="COMMAND"
>\C</TT
>, <TT
CLASS="COMMAND"
>\H</TT
>,
<TT
CLASS="COMMAND"
>\t</TT
>, <TT
CLASS="COMMAND"
>\T</TT
>, and <TT
CLASS="COMMAND"
>\x</TT
>.
</P
></BLOCKQUOTE
></DIV
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> It is an error to call <TT
CLASS="COMMAND"
>\pset</TT
> without
arguments. In the future this call might show the current status
of all printing options.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\q</TT
></DT
><DD
><P
> Quits the <SPAN
CLASS="APPLICATION"
>psql</SPAN
> program.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\qecho <TT
CLASS="REPLACEABLE"
><I
>text</I
></TT
> [ ... ] </TT
></DT
><DD
><P
> This command is identical to <TT
CLASS="COMMAND"
>\echo</TT
> except
that the output will be written to the query output channel, as
set by <TT
CLASS="COMMAND"
>\o</TT
>.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\r</TT
></DT
><DD
><P
> Resets (clears) the query buffer.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\s [ <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> ]</TT
></DT
><DD
><P
> Print or save the command line history to <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
>. If <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> is omitted, the history
is written to the standard output. This option is only available
if <SPAN
CLASS="APPLICATION"
>psql</SPAN
> is configured to use the
<ACRONYM
CLASS="ACRONYM"
>GNU</ACRONYM
> <SPAN
CLASS="APPLICATION"
>Readline</SPAN
> library.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\set [ <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> [ <TT
CLASS="REPLACEABLE"
><I
>value</I
></TT
> [ ... ] ] ]</TT
></DT
><DD
><P
> Sets the internal variable <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> to <TT
CLASS="REPLACEABLE"
><I
>value</I
></TT
> or, if more than one value
is given, to the concatenation of all of them. If no second
argument is given, the variable is just set with no value. To
unset a variable, use the <TT
CLASS="COMMAND"
>\unset</TT
> command.
</P
><P
> Valid variable names can contain characters, digits, and
underscores. See the section <A
HREF="app-psql.html#APP-PSQL-VARIABLES"
><I
>Variables</I
></A
> below for details.
Variable names are case-sensitive.
</P
><P
> Although you are welcome to set any variable to anything you
want, <SPAN
CLASS="APPLICATION"
>psql</SPAN
> treats several variables
as special. They are documented in the section about variables.
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> This command is totally separate from the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
>
command <A
HREF="sql-set.html"
><I
>SET</I
></A
>.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>\t</TT
></DT
><DD
><P
> Toggles the display of output column name headings and row count
footer. This command is equivalent to <TT
CLASS="LITERAL"
>\pset
tuples_only</TT
> and is provided for convenience.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\T <TT
CLASS="REPLACEABLE"
><I
>table_options</I
></TT
></TT
></DT
><DD
><P
> Allows you to specify attributes to be placed within the
<CODE
CLASS="SGMLTAG"
>table</CODE
> tag in <ACRONYM
CLASS="ACRONYM"
>HTML</ACRONYM
> tabular
output mode. This command is equivalent to <TT
CLASS="LITERAL"
>\pset
tableattr <TT
CLASS="REPLACEABLE"
><I
>table_options</I
></TT
></TT
>.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\timing</TT
></DT
><DD
><P
> Toggles a display of how long each SQL statement takes, in milliseconds.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\w</TT
> {<TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> | <TT
CLASS="REPLACEABLE"
><I
>|command</I
></TT
>}</DT
><DD
><P
> Outputs the current query buffer to the file <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
> or pipes it to the Unix
command <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\x</TT
></DT
><DD
><P
> Toggles expanded table formatting mode. As such it is equivalent to
<TT
CLASS="LITERAL"
>\pset expanded</TT
>.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\z [ <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> ]</TT
></DT
><DD
><P
> Produces a list of all available tables, views and sequences with their
associated access privileges.
If a <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> is
specified, only tables,views and sequences whose names match the pattern are listed.
</P
><P
> The commands <TT
CLASS="COMMAND"
>GRANT</TT
> and
<TT
CLASS="COMMAND"
>REVOKE</TT
> are used to set access privileges.
See <A
HREF="sql-grant.html"
><I
>GRANT</I
></A
> for
more information.
</P
><P
> This is an alias for <TT
CLASS="COMMAND"
>\dp</TT
> (<SPAN
CLASS="QUOTE"
>"display
privileges"</SPAN
>).
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\! [ <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
> ]</TT
></DT
><DD
><P
> Escapes to a separate Unix shell or executes the Unix command
<TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>. The
arguments are not further interpreted, the shell will see them
as is.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>\?</TT
></DT
><DD
><P
> Shows help information about the backslash commands.
</P
></DD
></DL
></DIV
><P>
</P
><P
> The various <TT
CLASS="LITERAL"
>\d</TT
> commands accept a <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> parameter to specify the
object name(s) to be displayed. <TT
CLASS="LITERAL"
>*</TT
> means <SPAN
CLASS="QUOTE"
>"any
sequence of characters"</SPAN
> and <TT
CLASS="LITERAL"
>?</TT
> means <SPAN
CLASS="QUOTE"
>"any single
character"</SPAN
>. (This notation is comparable to Unix shell file name
patterns.) Advanced users can also use regular-expression
notations such as character classes, for example <TT
CLASS="LITERAL"
>[0-9]</TT
>
to match <SPAN
CLASS="QUOTE"
>"any digit"</SPAN
>. To make any of these
pattern-matching characters be interpreted literally, surround it
with double quotes.
</P
><P
> A pattern that contains an (unquoted) dot is interpreted as a schema
name pattern followed by an object name pattern. For example,
<TT
CLASS="LITERAL"
>\dt foo*.bar*</TT
> displays all tables in schemas whose name
starts with <TT
CLASS="LITERAL"
>foo</TT
> and whose table name
starts with <TT
CLASS="LITERAL"
>bar</TT
>. If no dot appears, then the pattern
matches only objects that are visible in the current schema search path.
</P
><P
> Whenever the <TT
CLASS="REPLACEABLE"
><I
>pattern</I
></TT
> parameter
is omitted completely, the <TT
CLASS="LITERAL"
>\d</TT
> commands display all objects
that are visible in the current schema search path. To see all objects
in the database, use the pattern <TT
CLASS="LITERAL"
>*.*</TT
>.
</P
></DIV
><DIV
CLASS="REFSECT2"
><A
NAME="AEN53136"
></A
><H3
>Advanced features</H3
><DIV
CLASS="REFSECT3"
><A
NAME="APP-PSQL-VARIABLES"
></A
><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. To set variables, use the
<SPAN
CLASS="APPLICATION"
>psql</SPAN
> meta-command
<TT
CLASS="COMMAND"
>\set</TT
>:
</P><PRE
CLASS="PROGRAMLISTING"
>testdb=> <KBD
CLASS="USERINPUT"
>\set foo bar</KBD
></PRE
><P>
sets the variable <TT
CLASS="LITERAL"
>foo</TT
> to the value
<TT
CLASS="LITERAL"
>bar</TT
>. To retrieve the content of the variable, precede
the name with a colon and use it as the argument of any slash
command:
</P><PRE
CLASS="PROGRAMLISTING"
>testdb=> <KBD
CLASS="USERINPUT"
>\echo :foo</KBD
>
bar</PRE
><P>
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> The arguments of <TT
CLASS="COMMAND"
>\set</TT
> are subject to the same
substitution rules as with other commands. Thus you can construct
interesting references such as <TT
CLASS="LITERAL"
>\set :foo
'something'</TT
> and get <SPAN
CLASS="QUOTE"
>"soft links"</SPAN
> or
<SPAN
CLASS="QUOTE"
>"variable variables"</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,
<TT
CLASS="LITERAL"
>\set bar :foo</TT
> is a perfectly valid way to copy a
variable.
</P
></BLOCKQUOTE
></DIV
><P
> If you call <TT
CLASS="COMMAND"
>\set</TT
> without a second argument, the
variable is set, with an empty string as value. To unset (or delete) a
variable, use the command <TT
CLASS="COMMAND"
>\unset</TT
>.
</P
><P
> <SPAN
CLASS="APPLICATION"
>psql</SPAN
>'s internal variable names can
consist of letters, numbers, and underscores in any order and any
number of them. A number of these variables are treated specially
by <SPAN
CLASS="APPLICATION"
>psql</SPAN
>. They indicate certain option
settings that can be changed at run time by altering the value of
the variable or represent some state of the application. Although
you can use these variables for any other purpose, this is not
recommended, as the program behavior might grow really strange
really quickly. By convention, all specially treated variables
consist of all upper-case letters (and possibly numbers and
underscores). To ensure maximum compatibility in the future, avoid
using such variable names for your own purposes. A list of all specially
treated variables follows.
</P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="VARNAME"
>AUTOCOMMIT</TT
></DT
><DD
><P
> When <TT
CLASS="LITERAL"
>on</TT
> (the default), each SQL command is automatically
committed upon successful completion. To postpone commit in this
mode, you must enter a <TT
CLASS="COMMAND"
>BEGIN</TT
> or <TT
CLASS="COMMAND"
>START
TRANSACTION</TT
> SQL command. When <TT
CLASS="LITERAL"
>off</TT
> or unset, SQL
commands are not committed until you explicitly issue
<TT
CLASS="COMMAND"
>COMMIT</TT
> or <TT
CLASS="COMMAND"
>END</TT
>. The autocommit-off
mode works by issuing an implicit <TT
CLASS="COMMAND"
>BEGIN</TT
> for you, just
before any command that is not already in a transaction block and
is not itself a <TT
CLASS="COMMAND"
>BEGIN</TT
> or other transaction-control
command, nor a command that cannot be executed inside a transaction
block (such as <TT
CLASS="COMMAND"
>VACUUM</TT
>).
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> In autocommit-off mode, you must explicitly abandon any failed
transaction by entering <TT
CLASS="COMMAND"
>ABORT</TT
> or <TT
CLASS="COMMAND"
>ROLLBACK</TT
>.
Also keep in mind that if you exit the session
without committing, your work will be lost.
</P
></BLOCKQUOTE
></DIV
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> 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 may wish to set it in the system-wide
<TT
CLASS="FILENAME"
>psqlrc</TT
> file or your
<TT
CLASS="FILENAME"
>~/.psqlrc</TT
> file.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="VARNAME"
>DBNAME</TT
></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 unset.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>ECHO</TT
></DT
><DD
><P
> If set to <TT
CLASS="LITERAL"
>all</TT
>, all lines
entered from the keyboard or from a script are written to the standard output
before they are parsed or executed. To select this behavior on program
start-up, use the switch <TT
CLASS="OPTION"
>-a</TT
>. If set to
<TT
CLASS="LITERAL"
>queries</TT
>,
<SPAN
CLASS="APPLICATION"
>psql</SPAN
> merely prints all queries as
they are sent to the server. The switch for this is
<TT
CLASS="OPTION"
>-e</TT
>.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>ECHO_HIDDEN</TT
></DT
><DD
><P
> When this variable is set and a backslash command queries the
database, the query is first shown. This way you can study the
<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 <TT
CLASS="OPTION"
>-E</TT
>.) If you set
the variable to the value <TT
CLASS="LITERAL"
>noexec</TT
>, the queries are
just shown but are not actually sent to the server and executed.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>ENCODING</TT
></DT
><DD
><P
> The current client character set encoding.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>HISTCONTROL</TT
></DT
><DD
><P
> If this variable is set to <TT
CLASS="LITERAL"
>ignorespace</TT
>,
lines which begin with a space are not entered into the history
list. If set to a value of <TT
CLASS="LITERAL"
>ignoredups</TT
>, lines
matching the previous history line are not entered. A value of
<TT
CLASS="LITERAL"
>ignoreboth</TT
> combines the two options. If
unset, or if set to any other value than those above, all lines
read in interactive mode are saved on the history list.
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> This feature was shamelessly plagiarized from
<SPAN
CLASS="APPLICATION"
>Bash</SPAN
>.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="VARNAME"
>HISTFILE</TT
></DT
><DD
><P
> The file name that will be used to store the history list. The default
value is <TT
CLASS="FILENAME"
>~/.psql_history</TT
>. For example, putting
</P><PRE
CLASS="PROGRAMLISTING"
>\set HISTFILE ~/.psql_history- :DBNAME</PRE
><P>
in <TT
CLASS="FILENAME"
>~/.psqlrc</TT
> will cause
<SPAN
CLASS="APPLICATION"
>psql</SPAN
> to maintain a separate history for
each database.
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> This feature was shamelessly plagiarized from
<SPAN
CLASS="APPLICATION"
>Bash</SPAN
>.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="VARNAME"
>HISTSIZE</TT
></DT
><DD
><P
> The number of commands to store in the command history. The
default value is 500.
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> This feature was shamelessly plagiarized from
<SPAN
CLASS="APPLICATION"
>Bash</SPAN
>.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="VARNAME"
>HOST</TT
></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 unset.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>IGNOREEOF</TT
></DT
><DD
><P
> If unset, sending an <ACRONYM
CLASS="ACRONYM"
>EOF</ACRONYM
> character (usually
<B
CLASS="KEYCAP"
>Control</B
>+<B
CLASS="KEYCAP"
>D</B
>)
to an interactive session of <SPAN
CLASS="APPLICATION"
>psql</SPAN
>
will terminate the application. If set to a numeric value,
that many <ACRONYM
CLASS="ACRONYM"
>EOF</ACRONYM
> characters are ignored before the
application terminates. If the variable is set but has no
numeric value, the default is 10.
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> This feature was shamelessly plagiarized from
<SPAN
CLASS="APPLICATION"
>Bash</SPAN
>.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="VARNAME"
>LASTOID</TT
></DT
><DD
><P
> The value of the last affected OID, as returned from an
<TT
CLASS="COMMAND"
>INSERT</TT
> or <TT
CLASS="COMMAND"
>lo_insert</TT
>
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.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>ON_ERROR_ROLLBACK</TT
></DT
><DD
><P
> When <TT
CLASS="LITERAL"
>on</TT
>, if a statement in a transaction block
generates an error, the error is ignored and the transaction
continues. When <TT
CLASS="LITERAL"
>interactive</TT
>, such errors are only
ignored in interactive sessions, and not when reading script
files. When <TT
CLASS="LITERAL"
>off</TT
> (the default), a statement in a
transaction block that generates an error aborts the entire
transaction. The on_error_rollback-on mode works by issuing an
implicit <TT
CLASS="COMMAND"
>SAVEPOINT</TT
> for you, just before each command
that is in a transaction block, and rolls back to the savepoint
on error.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>ON_ERROR_STOP</TT
></DT
><DD
><P
> By default, if non-interactive scripts encounter an error, such
as a malformed <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> command or internal
meta-command, processing continues. This has been the
traditional behavior of <SPAN
CLASS="APPLICATION"
>psql</SPAN
> but it
is sometimes not desirable. If this variable is set, script
processing will immediately terminate. If the script was called
from another script it will terminate in the same fashion. If
the outermost script was not called from an interactive
<SPAN
CLASS="APPLICATION"
>psql</SPAN
> session but rather using the
<TT
CLASS="OPTION"
>-f</TT
> option, <SPAN
CLASS="APPLICATION"
>psql</SPAN
> will
return error code 3, to distinguish this case from fatal error
conditions (error code 1).
</P
></DD
><DT
><TT
CLASS="VARNAME"
>PORT</TT
></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 unset.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>PROMPT1</TT
><BR><TT
CLASS="VARNAME"
>PROMPT2</TT
><BR><TT
CLASS="VARNAME"
>PROMPT3</TT
></DT
><DD
><P
> These specify what the prompts <SPAN
CLASS="APPLICATION"
>psql</SPAN
>
issues should look like. See <A
HREF="app-psql.html#APP-PSQL-PROMPTING"
><I
>Prompting</I
></A
> below.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>QUIET</TT
></DT
><DD
><P
> This variable is equivalent to the command line option
<TT
CLASS="OPTION"
>-q</TT
>. It is probably not too useful in
interactive mode.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>SINGLELINE</TT
></DT
><DD
><P
> This variable is equivalent to the command line option
<TT
CLASS="OPTION"
>-S</TT
>.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>SINGLESTEP</TT
></DT
><DD
><P
> This variable is equivalent to the command line option
<TT
CLASS="OPTION"
>-s</TT
>.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>USER</TT
></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 unset.
</P
></DD
><DT
><TT
CLASS="VARNAME"
>VERBOSITY</TT
></DT
><DD
><P
> This variable can be set to the values <TT
CLASS="LITERAL"
>default</TT
>,
<TT
CLASS="LITERAL"
>verbose</TT
>, or <TT
CLASS="LITERAL"
>terse</TT
> to control the verbosity
of error reports.
</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT3"
><A
NAME="AEN53348"
></A
><H4
><ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> Interpolation</H4
><P
> An additional useful feature of <SPAN
CLASS="APPLICATION"
>psql</SPAN
>
variables is that you can substitute (<SPAN
CLASS="QUOTE"
>"interpolate"</SPAN
>)
them into regular <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> statements. The syntax for
this is again to prepend the variable name with a colon
(<TT
CLASS="LITERAL"
>:</TT
>).
</P><PRE
CLASS="PROGRAMLISTING"
>testdb=> <KBD
CLASS="USERINPUT"
>\set foo 'my_table'</KBD
>
testdb=> <KBD
CLASS="USERINPUT"
>SELECT * FROM :foo;</KBD
></PRE
><P>
would then query the table <TT
CLASS="LITERAL"
>my_table</TT
>. The value of
the variable is copied literally, so it can even contain unbalanced
quotes or backslash commands. You must make sure that it makes sense
where you put it. Variable interpolation will not be performed into
quoted <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> entities.
</P
><P
> A popular application of this facility is to refer to the last
inserted <ACRONYM
CLASS="ACRONYM"
>OID</ACRONYM
> in subsequent statements to build a
foreign key scenario. Another possible 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 proceed as above.
</P><PRE
CLASS="PROGRAMLISTING"
>testdb=> <KBD
CLASS="USERINPUT"
>\set content '\'' `cat my_file.txt` '\''</KBD
>
testdb=> <KBD
CLASS="USERINPUT"
>INSERT INTO my_table VALUES (:content);</KBD
></PRE
><P>
One possible problem with this approach is that <TT
CLASS="FILENAME"
>my_file.txt</TT
>
might contain single quotes. These need to be escaped so that
they don't cause a syntax error when the second line is processed. This
could be done with the program <TT
CLASS="COMMAND"
>sed</TT
>:
</P><PRE
CLASS="PROGRAMLISTING"
>testdb=> <KBD
CLASS="USERINPUT"
>\set content '\'' `sed -e "s/'/\\\\\\'/g" < my_file.txt` '\''</KBD
></PRE
><P>
Observe the correct number of backslashes (6)! It works
this way: After <SPAN
CLASS="APPLICATION"
>psql</SPAN
> has parsed this
line, it passes <TT
CLASS="LITERAL"
>sed -e "s/'/\\\'/g" < my_file.txt</TT
>
to the shell. The shell will do its own thing inside the double
quotes and execute <TT
CLASS="COMMAND"
>sed</TT
> with the arguments
<TT
CLASS="LITERAL"
>-e</TT
> and <TT
CLASS="LITERAL"
>s/'/\\'/g</TT
>. When
<TT
CLASS="COMMAND"
>sed</TT
> parses this it will replace the two
backslashes with a single one and then do the substitution. Perhaps
at one point you thought it was great that all Unix commands use the
same escape character. And this is ignoring the fact that you might
have to escape all backslashes as well because
<ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> text constants are also subject to certain
interpretations. In that case you might be better off preparing the
file externally.
</P
><P
> Since colons may legally appear in SQL commands, the following rule
applies: the character sequence
<SPAN
CLASS="QUOTE"
>":name"</SPAN
> is not changed unless <SPAN
CLASS="QUOTE"
>"name"</SPAN
> is the name
of a variable that is currently set. In any case you can escape
a colon with a backslash to protect it from substitution. (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 syntax for array slices and type casts are
<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> extensions, hence the
conflict.)
</P
></DIV
><DIV
CLASS="REFSECT3"
><A
NAME="APP-PSQL-PROMPTING"
></A
><H4
>Prompting</H4
><P
> The prompts <SPAN
CLASS="APPLICATION"
>psql</SPAN
> issues can be customized
to your preference. The three variables <TT
CLASS="VARNAME"
>PROMPT1</TT
>,
<TT
CLASS="VARNAME"
>PROMPT2</TT
>, and <TT
CLASS="VARNAME"
>PROMPT3</TT
> 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 input because the
command was not terminated with a semicolon or a quote was not closed.
Prompt 3 is issued when you run an <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
>
<TT
CLASS="COMMAND"
>COPY</TT
> command and you are expected to type in the
row values on the terminal.
</P
><P
> The value of the selected prompt variable is printed literally,
except where a percent sign (<TT
CLASS="LITERAL"
>%</TT
>) is encountered.
Depending on the next character, certain other text is substituted
instead. Defined substitutions are:
<P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>%M</TT
></DT
><DD
><P
> The full host name (with domain name) of the database server,
or <TT
CLASS="LITERAL"
>[local]</TT
> if the connection is over a Unix
domain socket, or
<TT
CLASS="LITERAL"
>[local:<TT
CLASS="REPLACEABLE"
><I
>/dir/name</I
></TT
>]</TT
>,
if the Unix domain socket is not at the compiled in default
location.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>%m</TT
></DT
><DD
><P
> The host name of the database server, truncated at the
first dot, or <TT
CLASS="LITERAL"
>[local]</TT
> if the connection is
over a Unix domain socket.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>%></TT
></DT
><DD
><P
>The port number at which the database server is listening.</P
></DD
><DT
><TT
CLASS="LITERAL"
>%n</TT
></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 <TT
CLASS="COMMAND"
>SET SESSION
AUTHORIZATION</TT
>.)
</P
></DD
><DT
><TT
CLASS="LITERAL"
>%/</TT
></DT
><DD
><P
>The name of the current database.</P
></DD
><DT
><TT
CLASS="LITERAL"
>%~</TT
></DT
><DD
><P
>Like <TT
CLASS="LITERAL"
>%/</TT
>, but the output is <TT
CLASS="LITERAL"
>~</TT
>
(tilde) if the database is your default database.</P
></DD
><DT
><TT
CLASS="LITERAL"
>%#</TT
></DT
><DD
><P
> If the session user is a database superuser, then a
<TT
CLASS="LITERAL"
>#</TT
>, otherwise a <TT
CLASS="LITERAL"
>></TT
>.
(The expansion of this value might change during a database
session as the result of the command <TT
CLASS="COMMAND"
>SET SESSION
AUTHORIZATION</TT
>.)
</P
></DD
><DT
><TT
CLASS="LITERAL"
>%R</TT
></DT
><DD
><P
> In prompt 1 normally <TT
CLASS="LITERAL"
>=</TT
>, but <TT
CLASS="LITERAL"
>^</TT
> if
in single-line mode, and <TT
CLASS="LITERAL"
>!</TT
> if the session is
disconnected from the database (which can happen if
<TT
CLASS="COMMAND"
>\connect</TT
> fails). In prompt 2 the sequence is
replaced by <TT
CLASS="LITERAL"
>-</TT
>, <TT
CLASS="LITERAL"
>*</TT
>, a single quote,
a double quote, or a dollar sign, depending on whether
<SPAN
CLASS="APPLICATION"
>psql</SPAN
> expects more input because the
command wasn't terminated yet, because you are inside a
<TT
CLASS="LITERAL"
>/* ... */</TT
> comment, or because you are inside
a quoted or dollar-escaped string. In prompt 3 the sequence doesn't
produce anything.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>%x</TT
></DT
><DD
><P
> Transaction status: an empty string when not in a transaction
block, or <TT
CLASS="LITERAL"
>*</TT
> when in a transaction block, or
<TT
CLASS="LITERAL"
>!</TT
> when in a failed transaction block, or <TT
CLASS="LITERAL"
>?</TT
>
when the transaction state is indeterminate (for example, because
there is no connection).
</P
></DD
><DT
><TT
CLASS="LITERAL"
>%</TT
><TT
CLASS="REPLACEABLE"
><I
>digits</I
></TT
></DT
><DD
><P
> The character with the indicated octal code is substituted.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>%:</TT
><TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
><TT
CLASS="LITERAL"
>:</TT
></DT
><DD
><P
> The value of the <SPAN
CLASS="APPLICATION"
>psql</SPAN
> variable
<TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
>. See the
section <A
HREF="app-psql.html#APP-PSQL-VARIABLES"
><I
>Variables</I
></A
> for details.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>%`</TT
><TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
><TT
CLASS="LITERAL"
>`</TT
></DT
><DD
><P
> The output of <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>, similar to ordinary
<SPAN
CLASS="QUOTE"
>"back-tick"</SPAN
> substitution.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>%[</TT
> ... <TT
CLASS="LITERAL"
>%]</TT
></DT
><DD
><P
> Prompts may 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 <TT
CLASS="LITERAL"
>%[</TT
> and
<TT
CLASS="LITERAL"
>%]</TT
>. Multiple pairs of these may 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 (<TT
CLASS="LITERAL"
>1;</TT
>) yellow-on-black
(<TT
CLASS="LITERAL"
>33;40</TT
>) prompt on VT100-compatible, color-capable
terminals.
</P
></DD
></DL
></DIV
><P>
To insert a percent sign into your prompt, write
<TT
CLASS="LITERAL"
>%%</TT
>. The default prompts are
<TT
CLASS="LITERAL"
>'%/%R%# '</TT
> for prompts 1 and 2, and
<TT
CLASS="LITERAL"
>'>> '</TT
> for prompt 3.
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> This feature was shamelessly plagiarized from
<SPAN
CLASS="APPLICATION"
>tcsh</SPAN
>.
</P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="REFSECT3"
><A
NAME="AEN53505"
></A
><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. If for some reason you do not like the tab completion, you
can turn it off by putting this in a file named
<TT
CLASS="FILENAME"
>.inputrc</TT
> 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"
><A
NAME="AEN53517"
></A
><H2
>Environment</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="ENVAR"
>PAGER</TT
></DT
><DD
><P
> If the query results do not fit on the screen, they are piped
through this command. Typical values are
<TT
CLASS="LITERAL"
>more</TT
> or <TT
CLASS="LITERAL"
>less</TT
>. The default
is platform-dependent. The use of the pager can be disabled by
using the <TT
CLASS="COMMAND"
>\pset</TT
> command.
</P
></DD
><DT
><TT
CLASS="ENVAR"
>PGDATABASE</TT
></DT
><DD
><P
> Default connection database
</P
></DD
><DT
><TT
CLASS="ENVAR"
>PGHOST</TT
><BR><TT
CLASS="ENVAR"
>PGPORT</TT
><BR><TT
CLASS="ENVAR"
>PGUSER</TT
></DT
><DD
><P
> Default connection parameters
</P
></DD
><DT
><TT
CLASS="ENVAR"
>PSQL_EDITOR</TT
><BR><TT
CLASS="ENVAR"
>EDITOR</TT
><BR><TT
CLASS="ENVAR"
>VISUAL</TT
></DT
><DD
><P
> Editor used by the <TT
CLASS="COMMAND"
>\e</TT
> command. The variables
are examined in the order listed; the first that is set is used.
</P
></DD
><DT
><TT
CLASS="ENVAR"
>SHELL</TT
></DT
><DD
><P
> Command executed by the <TT
CLASS="COMMAND"
>\!</TT
> command.
</P
></DD
><DT
><TT
CLASS="ENVAR"
>TMPDIR</TT
></DT
><DD
><P
> Directory for storing temporary files. The default is
<TT
CLASS="FILENAME"
>/tmp</TT
>.
</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN53564"
></A
><H2
>Files</H2
><P
></P
><UL
><LI
><P
> Before starting up, <SPAN
CLASS="APPLICATION"
>psql</SPAN
> attempts to
read and execute commands from the system-wide
<TT
CLASS="FILENAME"
>psqlrc</TT
> file and the user's
<TT
CLASS="FILENAME"
>~/.psqlrc</TT
> file.
(On Windows, the user's startup file is named
<TT
CLASS="FILENAME"
>%APPDATA%\postgresql\psqlrc.conf</TT
>.)
See <TT
CLASS="FILENAME"
><TT
CLASS="REPLACEABLE"
><I
>PREFIX</I
></TT
>/share/psqlrc.sample</TT
>
for information on setting up the system-wide file. It could be used
to set up the client or the server to taste (using the <TT
CLASS="COMMAND"
>\set
</TT
> and <TT
CLASS="COMMAND"
>SET</TT
> commands).
</P
></LI
><LI
><P
> Both the system-wide <TT
CLASS="FILENAME"
>psqlrc</TT
> file and the user's
<TT
CLASS="FILENAME"
>~/.psqlrc</TT
> file can be made version-specific
by appending a dash and the <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
release number, for example <TT
CLASS="FILENAME"
>~/.psqlrc-8.1.9</TT
>.
A matching version-specific file will be read in preference to a
non-version-specific file.
</P
></LI
><LI
><P
> The command-line history is stored in the file
<TT
CLASS="FILENAME"
>~/.psql_history</TT
>, or
<TT
CLASS="FILENAME"
>%APPDATA%\postgresql\psql_history</TT
> on Windows.
</P
></LI
></UL
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN53587"
></A
><H2
>Notes</H2
><P
></P
><UL
><LI
><P
> In an earlier life <SPAN
CLASS="APPLICATION"
>psql</SPAN
> allowed the
first argument of a single-letter backslash command to start
directly after the command, without intervening whitespace. For
compatibility this is still supported to some extent,
but we are not going to explain the details here as this use is
discouraged. If you get strange messages, keep this in mind.
For example
</P><PRE
CLASS="PROGRAMLISTING"
>testdb=> <KBD
CLASS="USERINPUT"
>\foo</KBD
>
Field separator is "oo".</PRE
><P>
which is perhaps not what one would expect.
</P
></LI
><LI
><P
> <SPAN
CLASS="APPLICATION"
>psql</SPAN
> only works smoothly with servers
of the same version. That does not mean other combinations will
fail outright, but subtle and not-so-subtle problems might come
up. Backslash commands are particularly likely to fail if the
server is of a different version.
</P
></LI
></UL
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN53598"
></A
><H2
>Notes for Windows users</H2
><P
> <SPAN
CLASS="APPLICATION"
>psql</SPAN
> is built as a <SPAN
CLASS="QUOTE"
>"console
application"</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
></P
></P><UL
><LI
><P
> Set the code page by entering <KBD
CLASS="USERINPUT"
>cmd.exe /c chcp
1252</KBD
>. (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 <TT
CLASS="FILENAME"
>/etc/profile</TT
>.
</P
></LI
><LI
><P
> Set the console font to <SPAN
CLASS="QUOTE"
>"Lucida Console"</SPAN
>, because the
raster font does not work with the ANSI code page.
</P
></LI
></UL
><P>
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="APP-PSQL-EXAMPLES"
></A
><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=> <KBD
CLASS="USERINPUT"
>CREATE TABLE my_table (</KBD
>
testdb(> <KBD
CLASS="USERINPUT"
> first integer not null default 0,</KBD
>
testdb(> <KBD
CLASS="USERINPUT"
> second text)</KBD
>
testdb-> <KBD
CLASS="USERINPUT"
>;</KBD
>
CREATE TABLE</PRE
><P>
Now look at the table definition again:
</P><PRE
CLASS="PROGRAMLISTING"
>testdb=> <KBD
CLASS="USERINPUT"
>\d my_table</KBD
>
Table "my_table"
Attribute | Type | Modifier
-----------+---------+--------------------
first | integer | not null default 0
second | text | </PRE
><P>
Now we change the prompt to something more interesting:
</P><PRE
CLASS="PROGRAMLISTING"
>testdb=> <KBD
CLASS="USERINPUT"
>\set PROMPT1 '%n@%m %~%R%# '</KBD
>
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
<TT
CLASS="COMMAND"
>\pset</TT
> command:
</P><PRE
CLASS="PROGRAMLISTING"
>peter@localhost testdb=> <KBD
CLASS="USERINPUT"
>\pset border 2</KBD
>
Border style is 2.
peter@localhost testdb=> <KBD
CLASS="USERINPUT"
>SELECT * FROM my_table;</KBD
>
+-------+--------+
| first | second |
+-------+--------+
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
+-------+--------+
(4 rows)
peter@localhost testdb=> <KBD
CLASS="USERINPUT"
>\pset border 0</KBD
>
Border style is 0.
peter@localhost testdb=> <KBD
CLASS="USERINPUT"
>SELECT * FROM my_table;</KBD
>
first second
----- ------
1 one
2 two
3 three
4 four
(4 rows)
peter@localhost testdb=> <KBD
CLASS="USERINPUT"
>\pset border 1</KBD
>
Border style is 1.
peter@localhost testdb=> <KBD
CLASS="USERINPUT"
>\pset format unaligned</KBD
>
Output format is unaligned.
peter@localhost testdb=> <KBD
CLASS="USERINPUT"
>\pset fieldsep ","</KBD
>
Field separator is ",".
peter@localhost testdb=> <KBD
CLASS="USERINPUT"
>\pset tuples_only</KBD
>
Showing only tuples.
peter@localhost testdb=> <KBD
CLASS="USERINPUT"
>SELECT second, first FROM my_table;</KBD
>
one,1
two,2
three,3
four,4</PRE
><P>
Alternatively, use the short commands:
</P><PRE
CLASS="PROGRAMLISTING"
>peter@localhost testdb=> <KBD
CLASS="USERINPUT"
>\a \t \x</KBD
>
Output format is aligned.
Tuples only is off.
Expanded display is on.
peter@localhost testdb=> <KBD
CLASS="USERINPUT"
>SELECT * FROM my_table;</KBD
>
-[ 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>
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN53640"
></A
><H2
>See Also</H2
>Environment Variables (<A
HREF="libpq-envars.html"
>Section 28.11</A
>)</DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="app-pgrestore.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="app-reindexdb.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>pg_restore</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="reference-client.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><SPAN
CLASS="APPLICATION"
>reindexdb</SPAN
></TD
></TR
></TABLE
></DIV
></BODY
></HTML
>