psql Commands

psql commands cheatsheet.

Connect, list, describe and export — the PostgreSQL psql client commands and backslash meta-commands you reach for every day. Tap to copy.

Connect
psql -U <user> -d <db>Connect as a user to a database
psql -h <host> -p 5432 -U <user> <db>Connect to a remote host and port
psql "postgresql://user:pass@host/db"Connect with a connection URI
psql -d <db>Connect to a database as the current user
Databases & tables
\lList all databases with owners and encoding
\c <db>Connect to another database
\dtList tables in the current schema
\d <table>Describe a table's columns, indexes and constraints
\dnList schemas
\diList indexes
\dvList views
\dfList functions
\duList roles and their attributes
Query & output
\xToggle expanded (one column per line) display
\timingToggle timing of how long each query takes
\eOpen the query buffer in your editor
\gRe-run the previous query buffer
\watchRe-run the last query every 2 seconds
\copy <table> to 'file.csv' csvExport a table to CSV (client-side)
\copy <table> from 'file.csv' csvImport a CSV file into a table
\o <file>Send query results to a file
Import & run
psql -f file.sqlRun a SQL script file
\i file.sqlRun a SQL script from within psql
psql -c "SELECT version();"Run a single SQL command and exit
psql -A -t -c "SELECT now()"Unaligned, tuples-only output for scripting
Help & exit
\?Show help for psql meta-commands
\h <SQL>Show SQL syntax help (e.g. \h SELECT)
\qQuit psql
\! <cmd>Run a shell command from psql

What psql actually is

psql is the interactive terminal client that ships with PostgreSQL. It is not the database itself and not the SQL language — it is the program you run to open a session against a server, type queries, and get results back formatted in your terminal. Its superpower is a set of backslash meta-commands (all starting with a \) that the client interprets itself rather than sending to the server. That is why \dt can list your tables in one keystroke while a plain SQL client would need a query against the system catalogs.

Connecting

Every session starts with a connection. psql -U <user> -d <db> connects locally, while psql -h <host> -p 5432 -U <user> <db> reaches a remote server on its port. If you already have a connection string, psql "postgresql://user:pass@host/db" parses the whole URI for you. Once inside, \c <db> switches to a different database without leaving psql, and \l lists everything the server hosts.

Exploring and describing

The describe family is where psql shines. \dt lists tables, \dn lists schemas, and \d <table> prints a single object's columns, types, indexes and constraints — the fastest way to remember a table's shape. Siblings like \di, \dv, \df and \du do the same for indexes, views, functions and roles. Toggle \x when a wide row is unreadable and psql will flip to an expanded, one-column-per-line layout.

Running and exporting

You can drive whole scripts from psql: psql -f file.sql runs a file from the shell, and \i file.sql does the same from inside a session. For one-liners in automation, psql -c "SELECT ..." runs a single statement and exits, and adding -A -t strips the formatting for clean, scriptable output. To move data, \copy reads and writes CSV files on the client side. These pair naturally with the SQL commands you actually run and the Docker commands when your database lives in a container.

FAQ

How do I list tables in psql?
Run the backslash meta-command \dt to list tables in the current schema, or \dt *.* to include every schema. Use \d <table> to see a single table's columns, indexes and constraints.
What's the difference between \dt and a SELECT query?
\dt is a psql client meta-command that asks psql to format a listing of tables for you; a SELECT is SQL sent to the server. Meta-commands like \dt, \l and \d only work inside the psql client, while SELECT works from any PostgreSQL client.

More cheatsheets