Quick answer
The psql commands you’ll probably reach for most are psql -U username -d database to connect, \l to list databases, \c database to switch databases, \dt to list tables, \d table_name to describe a table, \i file.sql to run a SQL file, and \q to leave psql.
psql can feel a little odd at first. Part of the time you’re writing normal SQL, and part of the time you’re using psql’s own backslash commands. This cheat sheet keeps the everyday stuff close by, so you can connect, inspect, query, import, and export without having to pull up the docs every time.
What psql is
psql is PostgreSQL’s official command-line client. You use it to connect to a PostgreSQL server, run SQL queries, manage databases, inspect schemas, execute scripts, and troubleshoot database access.
Inside psql, you’ll deal with two kinds of commands:
- SQL commands, such as
SELECT * FROM users;. These usually end with a semicolon. - psql meta-commands, such as
\dtor\l. These start with a backslash and don’t need a semicolon.
Connect to PostgreSQL from the command line
| Task | Command |
|---|---|
| Connect as a user | psql -U postgres |
| Connect to a specific database | psql -U postgres -d appdb |
| Connect to a host and port | psql -h localhost -p 5432 -U postgres -d appdb |
| Connect using a full URL | psql postgresql://user:password@localhost:5432/appdb |
Example:
psql -h localhost -p 5432 -U postgres -d appdbIf password authentication is turned on, psql asks for the password after you run the command.
psql commands you’ll use often
| Command | What it does |
|---|---|
\? | Show help for psql commands |
\h | Show help for SQL commands |
\l | List all databases |
\c database_name | Connect to another database |
\conninfo | Show current connection details |
\dt | List tables in the current schema |
\d table_name | Describe a table |
\dn | List schemas |
\du | List roles and users |
\x | Toggle expanded output |
\timing | Show query execution time |
\q | Exit psql |
Database and connection commands
These are the commands to use when you need to check where you’re connected, or move to another database.
\conninfoShows the current database, user, host, and port.
\lLists all databases on the PostgreSQL server.
\c appdbConnects to the appdb database.
\c appdb appuserConnects to appdb as the user appuser.
Inspect tables, schemas, and objects
These commands are handy when you’ve inherited a database or just need to see its structure without guessing.
| Task | Command |
|---|---|
| List tables | \dt |
| List tables in all schemas | \dt *.* |
| Describe a table | \d users |
| Show detailed table info | \d+ users |
| List indexes | \di |
| List views | \dv |
| List functions | \df |
| List schemas | \dn |
Example:
\d+ ordersThat displays columns, data types, indexes, constraints, storage details, and the table description when one is available.
Run SQL queries in psql
Normal SQL works directly inside psql. Just remember the semicolon at the end.
SELECT current_database();SELECT id, email, created_at FROM users LIMIT 10;SELECT COUNT(*) FROM orders;If your prompt changes to something like appdb-#, psql is probably waiting for you to finish a multi-line SQL statement. Add the missing semicolon, or cancel the command with Ctrl+C.
Create and manage databases
You can create databases with SQL from inside psql:
CREATE DATABASE appdb;List databases:
\lConnect to the new database:
\c appdbDrop a database only when you’re sure it’s no longer needed:
DROP DATABASE olddb;Be careful: PostgreSQL won’t let you drop the database you’re currently connected to. Connect to another database first, such as postgres, then run the drop command.
Import and run SQL files
To run a SQL file from inside psql, use \i followed by the file path.
\i /home/user/schema.sqlYou can run a SQL file from your shell too, without opening the interactive psql prompt first:
psql -U postgres -d appdb -f schema.sqlThis is a common way to handle schema setup, migrations, seed data, and database restore tasks.
Export query results
For quick exports, use \copy. It runs from the client side, which is often easier than server-side COPY.
\copy users TO 'users.csv' CSV HEADERExport selected columns:
\copy (SELECT id, email FROM users) TO 'users_export.csv' CSV HEADERImport a CSV file:
\copy users(email, name) FROM 'users.csv' CSV HEADERMake output easier to read
When a table has lots of columns, the normal output can get messy. Expanded mode helps.
\xRun your query again:
SELECT * FROM users LIMIT 1;Turn on query timing:
\timingSend output to a file:
\o results.txtSend output back to the terminal:
\oRole and permission commands
| Task | Command |
|---|---|
| List users and roles | \du |
| Create a role | CREATE ROLE appuser LOGIN PASSWORD 'strong_password'; |
| Grant database access | GRANT CONNECT ON DATABASE appdb TO appuser; |
| Grant table privileges | GRANT SELECT, INSERT, UPDATE ON users TO appuser; |
| Change a password | ALTER USER appuser WITH PASSWORD 'new_password'; |
Common psql mistakes
- Forgetting the semicolon: SQL commands need
;. psql backslash commands don’t. - Using psql commands outside psql: Commands like
\dtonly work inside the psql prompt. - Connecting to the wrong database: Run
\conninfobefore making changes. - Confusing schemas with databases: A database can contain multiple schemas. Tables are usually inside the
publicschema unless configured differently. - Dropping objects too quickly: Always verify the current connection and object name first.
Good habits in psql
- Use
\conninfobefore running destructive commands. - Use
\d+ table_namewhen checking indexes, constraints, and storage details. - Keep migration and setup scripts in version control.
- Use
\timingwhen comparing query performance. - Prefer
\copyfor local CSV imports and exports. - Don’t put passwords directly in shell history when you’re working on shared systems.
A basic psql workflow
psql -U postgres -d appdb
\conninfo
\dt
\d users
SELECT COUNT(*) FROM users;
\timing
\x
SELECT * FROM users LIMIT 1;
\qThis little workflow covers the basics: connect, confirm the database, list tables, inspect a table, run a query, adjust output, and exit safely.
