Connect
sudo -u postgres psql
psql -h 127.0.0.1 -U app_user -d app
The first line logs in as the administrative role through peer authentication. The second connects over TCP with a password, which psql asks for, as long as pg_hba.conf allows password logins for that address. Without -h, psql uses the local socket. For a server elsewhere, connect through an SSH tunnel rather than an open port; see how to allow remote connections to PostgreSQL.
Help Inside psql
| Command | What it does |
|---|---|
\? | Lists psql commands |
\h | Lists SQL commands |
\h ALTER ROLE | Shows the syntax |
\q | Quits |
Databases, Tables, and Roles
| Command | What it does |
|---|---|
\l | Lists databases |
\c app | Connects to app |
\dt | Lists tables |
\d orders | Describes a table |
\dn | Lists schemas |
\di | Lists indexes |
\dv | Lists views |
\df | Lists functions |
\du | Lists roles |
\dx | Lists extensions |
\conninfo | Shows the connection |
Add + to most of these for more detail, such as sizes: \l+, \dt+. Listing is covered at length in how to list databases and tables in PostgreSQL, and roles in how to create a user in PostgreSQL.
Readable Output
\x: switches expanded output on and off, one column per line. Useful for wide rows.\x auto: uses expanded output only when a row does not fit the screen.\timing: prints how long each query took.\pset null '(null)': makes empty values visible.
Run Files and One-Off Queries
\i /home/user/schema.sql
\i runs the SQL in a file inside the current session. From the shell, -f does the same, and -c runs one statement and exits:
psql -d app -f schema.sql
psql -d app -c "SELECT count(*) FROM orders;"
psql -d app -At -c "SELECT now();"
-A removes alignment and -t removes headers, which suits scripts. Add -v ON_ERROR_STOP=1 so a script stops at the first error instead of running on.
Edit, Repeat, and Save
\e: opens the current or last query in your editor; after you save and quit, psql runs what ends with a semicolon.\g: with an empty query buffer, runs the last query again.\o result.txt: sends output to a file until you type\oalone.\copy: imports or exports CSV through the client, with no file access needed on the server.
\copy orders TO 'orders.csv' WITH (FORMAT csv, HEADER)
Store a Password in .pgpass
A script that connects with a password should not carry it on the command line or in an exported variable. psql reads ~/.pgpass, one line per connection:
127.0.0.1:5432:app:app_user:STRONG_PASSWORD
The fields are host, port, database, user, and password; * matches any value. psql ignores the file unless only its owner can read it:
chmod 600 ~/.pgpass
The owner of that file can connect as the role, so keep it in the home folder of the account that runs the script.
Transactions in psql
psql commits each statement at once unless you open a transaction. Before a risky change, start one, check the result, and only then commit:
BEGIN;
UPDATE orders SET status = 'closed' WHERE id = 42;
SELECT status FROM orders WHERE id = 42;
COMMIT; -- or ROLLBACK;
The prompt shows an asterisk, as in app=*>, while a transaction is open, and an exclamation mark, as in app=!>, after an error inside it, which then needs ROLLBACK. For a superuser the last character is # instead of >.