This section summarize the capabilities of psql.
The psql manual has detailed information about each item.
See Chapter for an introduction
to psql.
Table shows the commands used to control
psql's query buffer.
|
Table lists psql's general
commands.
|
The \pset command controls the output format
used by psql. Table lists the
available formatting commands, and Figure
shows examples of their use.
In the figure, \pset tuples_only causes psql to show only data rows, suppressing table headings and row counts. The tuples_only does not take a second argument, as it is an on/off parameter. The first \pset tuples_only turns it on, and the second one turns it off. The second \pset in the figure causes psql to display NULL as (null).
In addition to using \pset, some output format
options have shortcuts, as shown in Table .
|
The \set command sets a variable, and \unset removes a variable. To access a variable you precede its name with a colon. The \set command used alone lists all defined variables.
Figure shows the use of psql variables.
test=> \set num_var 4
test=> SELECT :num_var;
?column?
----------
4
(1 row)
test=> \set operation SELECT
test=> :operation :num_var;
?column?
----------
4
(1 row)
test=> \set str_var '\'My long string\''
test=> \echo :str_var
'My long string'
test=> SELECT :str_var;
?column?
----------------
My long string
(1 row)
test=> \set date_var `date`
test=> \echo :date_var
Thu Aug 11 20:54:21 EDT 1994
test=> \set date_var2 '\''`date`'\''
test=> \echo :date_var2
'Thu Aug 11 20:54:24 EDT 1994'
test=> SELECT :date_var2;
?column?
------------------------------
Thu Aug 11 20:54:24 EDT 1994
(1 row)
The first variable assigned, num_var, is accessed in the SELECT query by preceding the variable name with a colon. The second \set command places the word SELECT into a variable, then uses that variable to perform a SELECT query. The next example uses backslash-quotes (\') to create a string that contains single quotes. This variable then replaces a quoted string in a query. With date_var, grave accents (`) allow a command to be run and the result placed into a variable. In this case, the output of the Unix date command is captured and placed into a variable. The assignment to date_var2 combines the use of backslash-quotes and grave accents to run the date command and surround it with single quotes. The final SELECT shows that date_var2 holds a quoted date string that can be used in queries.
Psql includes a number of predefined variables, as listed
in Table .
|
You can find a great deal of information about the current database
by using psql's listing commands, as shown in Table .
|
Most listing commands take an optional name parameter, which
can be specified as a regular expression. For example, \dt
sec displays all table names beginning with sec, and \dt
.*x.* shows all table names containing an x. Regular expressions
were covered in Section .
When you are using listing commands, the descriptions of the various data types and functions are called comments. POSTGRESQL predefines many comments, and the COMMENT command allows users to define their own as well. The \dd command and others then display these comments. See the COMMENT manual page for more information.
Many commands support the use of an optional plus sign, which shows additional information. For example, \dT lists all data types, \dT+ includes the size of each type, and \df+ shows additional information about functions. With the other commands, a plus sign causes the comments for the object to be displayed.
Psql has a local large object interface that supports large
object operations using files local to the computer running psql,
rather than local to the computer running the database server. Table
shows the local large object commands supported.
|
You can change the behavior of psql when starting a new session.
Psql is normally started from the command line by typing
psql followed by the database name. However, psql
accepts extra arguments between psql and the database name,
which modify psql's behavior. For example, psql -f
file test will read commands from file, rather than from
the keyboard. Table summarizes
psql's command-line options. Consult the psql manual
page for more detailed information.
|
Another way to change the behavior of psql on start-up is to create a file called .psqlrc in your home directory. Each time psql starts, it executes any backslash or SQL commands in that file.