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.
psql -U <user> -d <db>Connect as a user to a databasepsql -h <host> -p 5432 -U <user> <db>Connect to a remote host and portpsql "postgresql://user:pass@host/db"Connect with a connection URIpsql -d <db>Connect to a database as the current user\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\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 filepsql -f file.sqlRun a SQL script file\i file.sqlRun a SQL script from within psqlpsql -c "SELECT version();"Run a single SQL command and exitpsql -A -t -c "SELECT now()"Unaligned, tuples-only output for scripting\?Show help for psql meta-commands\h <SQL>Show SQL syntax help (e.g. \h SELECT)\qQuit psql\! <cmd>Run a shell command from psqlWhat 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.