What are the most useful psql meta-commands?
BeginnerHands-onPostgreSQL
Answer
Short answer
psql's backslash commands let you explore without writing SQL: \l (databases), \c (connect), \dt (tables), \d table (describe), \du (roles), \dn (schemas), \di (indexes), \x (expanded output), \conninfo, \timing, \q (quit).
Cheat sheet
| Command | Shows |
|---|---|
\l |
All databases with owner and encoding |
\c shop |
Switch to database shop |
\dt / \dt+ |
Tables (+ sizes) |
\d orders |
Columns, types, indexes, constraints of a table |
\di |
Indexes |
\dn |
Schemas |
\du |
Roles and attributes (Superuser, Create DB…) |
\dx |
Installed extensions |
\conninfo |
Current database, user, host, port |
\x |
Toggle vertical output for wide rows |
\timing |
Show how long each query takes |
\e |
Edit the last query in your editor |
\? |
Help for all meta-commands |
Tip
Run psql with -E to see the SQL behind meta-commands — handy for learning the system catalogs.
What to say in the interview
"\l, \c, \dt, \d, \du and \dn cover most exploration; \x for wide rows, \timing for performance, \conninfo to confirm where I'm connected — especially important before running anything on production."
More on PostgreSQL
How do you set up PostgreSQL and log in with psql?BeginnerHow do you create a database, tables, users and query data in PostgreSQL?BeginnerHow do you back up and restore a PostgreSQL database?BeginnerWhat is PostgreSQL and why is it so popular?BeginnerHow is PostgreSQL organised: cluster, database, schema and table?BeginnerHow do you connect to PostgreSQL from the command line?Beginner