The psql Client: Advanced Topics

Basic information about the psql client is included in Chapter 4; this section documents more advanced information about the psql client. The topics covered include a complete list of command line options, and an explanation of each psql slash command. This section also contains information on how to load SQL input from external files, use the psql history, and substitute variables dynamically into SQL statements within psql.

Command Line Options

Here is the complete syntax to start psql:

psql [  options  ] [  dbname  [  username  ] ]

The optional dbname value specifies the database to initially connect to. The optional username specifies the PostgreSQL user to connect as. If either value is unspecified, psql will default to a database and username with the same name as the operating system user starting the program.

Additionally, several run-time options can be set by command-line flags. By default, psql understands both standard Unix short options (e.g., -c), and GNU-style long options (e.g., --command). The latter are not available on all systems. In the following list, the Unix short options (which are always one letter) are shown first, followed by the equivalent long option.

-a, - -echo-all

Turns on the ‘echo all’ option, which displays all lines as they are read by psql. This option can be useful for scripting, and is equivalent to issuing the command: \set ECHO all from within psql.

-A, - -no-align

Starts psql in unaligned output formatting mode. If ...

Get Practical PostgreSQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.