psql meta-commands cheat sheet
The backslash commands you use every day in psql: connecting, listing databases, tables and roles, describing objects, timing queries, expanded output and running SQL files.
Not checked by a trusted editor yet Last edited Outdated?
Besides SQL, psql understands meta-commands: lines that start with a backslash and are handled by psql itself, not by the server. They do not need a semicolon. Most listing commands accept a pattern (\dt sales.*), a + suffix for more detail and an S modifier to include system objects.
Starting psql
psql -h db1.example.com -p 5432 -U appuser -d appdb
psql "host=db1.example.com dbname=appdb user=appuser sslmode=require"
psql -d appdb -c 'SELECT count(*) FROM orders'
psql -d appdb -f migrate.sql
psql -X -v ON_ERROR_STOP=1 -d appdb -f migrate.sql
-hhost (a value starting with/is a socket directory),-pport,-Uuser,-ddatabase or a full connection string.-cruns one command and exits;-fruns a file.-Xskips your~/.psqlrc, which makes scripts behave the same everywhere.-v ON_ERROR_STOP=1stops a script at the first error; psql then exits with status 3.
Connection and help
\c dbname [user]: connect to another database (and optionally as another user).\conninfo: show the current connection, including SSL details.\?: list all meta-commands;\? variableslists psql variables.\h CREATE INDEX: SQL syntax help for a command.\q: quit.
Listing and describing objects
\l: databases, with owner, encoding and privileges.\dn: schemas.\dt,\dv,\di,\ds: tables, views, indexes, sequences.\d name: columns, types, indexes and constraints of one table or view.\d+ nameadds storage and size information.\df: functions.\du(same as\dg): roles and their attributes.\dp(alias\z): privileges on tables, views and sequences.\dx: installed extensions.
\dt public.*
\d+ orders
\di *orders*
\du
\dp orders
Output and timing
\xtoggles expanded output (one column per line), useful for wide rows.\x autoswitches only when a row does not fit.\timing onprints how long each statement takes.\gxinstead of;runs the query once with expanded output.\watch 5re-runs the last query every 5 seconds until you press Ctrl+C (the default is 2 seconds).\o file.txtsends query results to a file;\oalone switches back to the screen.
\timing on
SELECT pid, state, query FROM pg_stat_activity \gx
SELECT count(*) FROM pg_stat_activity WHERE state = 'active' \watch 5
Files, editing and variables
\i file.sql: run commands from a file.\e: open the current query in your editor ($EDITOR), then run it when you save and close.\copy: client-side COPY. It reads or writes files on your machine, so it needs no server file privileges.\set name valuesets a psql variable; reference it as:nameor:'name'(quoted literal) in SQL.\echoprints text, handy in scripts.\gexecruns each value of the result as a new SQL statement.\password appuserchanges a password without sending it in clear text in an SQL statement.\sshows your command history.
\copy orders TO 'orders.csv' WITH (FORMAT csv, HEADER)
\set customer_id 42
SELECT * FROM orders WHERE customer_id = :customer_id;
SELECT format('ANALYZE %I.%I', schemaname, tablename) FROM pg_tables WHERE schemaname = 'public' \gexec
Passwords without typing them
For scripts, put credentials in ~/.pgpass (one line per server, format hostname:port:database:username:password, * as a wildcard). libpq ignores the file unless only the owner can read it:
echo 'db1.example.com:5432:appdb:appuser:CHANGE_ME' >> ~/.pgpass
chmod 0600 ~/.pgpass
On Windows the file is %APPDATA%\postgresql\pgpass.conf. The PGPASSFILE environment variable points to a different file.
Official documentation
See also
- 0198 Install PostgreSQL and connect with psql on Debian, Ubuntu and RHEL Install the PostgreSQL server from your distribution or the official PGDG repositories, start it, and open your first psql session as the… Beginner · How-to · Linux Article Install & connect 15 Oct 2026
- 0200 PostgreSQL roles, privileges and default privileges Create login and group roles, grant access to databases, schemas and tables, and use ALTER DEFAULT PRIVILEGES so tables created later get… Intermediate · How-to · Linux Article Roles & administration · Security & access 15 Oct 2026
- 0201 pg_hba.conf and PostgreSQL authentication methods pg_hba.conf decides who may connect from where and how they must prove who they are. Rules are checked top to bottom and… Intermediate · Concept · Linux Article Install & connect · Security & access 15 Oct 2026
- 0204 VACUUM, autovacuum and table bloat in PostgreSQL Updates and deletes leave dead row versions behind. VACUUM makes that space reusable, keeps statistics fresh and prevents transaction ID wraparound; autovacuum… Intermediate · Concept · Linux Article Performance & tuning · Roles & administration 15 Oct 2026
- 0207 Find long-running PostgreSQL queries and slow statements A psql-based script that lists sessions whose current query or open transaction is older than a threshold, plus pg_stat_statements queries to find… Intermediate · Reference · Linux Script Performance & tuning · Roles & administration 15 Oct 2026
- 0209 Fix: FATAL: password authentication failed for user PostgreSQL reached the server and found a matching pg_hba.conf rule, but the password check failed. Usually a wrong password, a role without… Intermediate · Fix · Any system Article Roles & administration · Security & access 15 Oct 2026
Repos in the wild
- postgres/postgres: PostgreSQL source code mirror postgres/postgres The official read-only GitHub mirror of the PostgreSQL source repository. Browse the code and history here; patches go through the project's mailing lists, not pull requests. GitHub
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Commitpedia +3,657 chars