Cheat Sheet
The commands you keep forgetting, with a one-line description and a real example you can copy - 629 across 38 tools. Pick a tool in the sidebar, or search across all of them.
psql (Postgres)
The PostgreSQL command-line client - connect, inspect, and run SQL.| Command | What it does | Example |
|---|---|---|
psql connect | Connect to a database. | psql -h localhost -U postgres -d mydb |
psql (URL) | Connect with a connection string. | psql postgresql://ana:secret@localhost:5432/mydb |
psql -c | Run one query and exit. | psql -U postgres -d mydb -c "SELECT count(*) FROM users;" |
\l | List databases. | \l |
\c | Switch to another database. | \c mydb |
\dt | List tables. | \dt |
\d | Describe a table (columns, indexes). | \d users |
\dn | List schemas. | \dn |
\du | List roles / users. | \du |
\x | Toggle expanded (row-per-column) output. | \x |
\timing | Show how long each query takes. | \timing |
\i | Run SQL from a file. | \i seed.sql |
\copy | Import/export a table as CSV. | \copy users TO 'users.csv' CSV HEADER |
pg_dump | Back up a database to a plain SQL file. | pg_dump -U postgres mydb > backup.sql |
pg_dump -Fc | Compressed custom-format backup (for pg_restore). | pg_dump -Fc -U postgres mydb > mydb.dump |
pg_restore | Restore a custom-format backup. | pg_restore -U postgres -d mydb mydb.dump |
restore (plain SQL) | Replay a plain SQL backup with psql itself. | psql -U postgres -d mydb < backup.sql |
\e | Edit the current query in your $EDITOR. | \e |
\q | Quit psql. | \q |