Top 12 psql Commands You’ll Actually Use
If you're new to PostgreSQL, the psql shell can feel intimidating — but it shouldn’t. You only need a handful of commands to navigate, inspect objects, run queries, and troubleshoot. These are the 12 psql commands you will actually use all the time, whether you're just starting out or already comfortable at the terminal.
1. Connect to a Database
Use -U to specify the user and -d to specify the database. If your system defaults are set, you can often connect by just giving the database name.
psql -U postgres -d mydb
psql mydb
2. List All Databases
Shows every database the server knows about, along with owners and access settings.
\l
3. Switch to Another Database
Changes your current connection without leaving the psql shell.
\c database_name
4. List All Tables
Displays all tables visible in your search path. Add a schema prefix to narrow it down.
\dt
\dt public.*
5. Describe a Table
Shows columns, data types, constraints, and other table details at a glance.
\d table_name
6. Show Indexes on a Table
Use \d+ to see a table’s storage info, including all indexes and constraints.
\d+ table_name
7. View Connection Info
Displays who you're logged in as, what database you're connected to, and from where.
\conninfo
8. Run a SQL File
Executes all the SQL inside a file — perfect for schema setup or bulk operations.
\i path/to/file.sql
9. Save Query Output to a File
Sends query results to a file instead of the terminal. Run \o again with no filename to go back to normal output.
\o output.txt
SELECT * FROM customer LIMIT 10;
\o
10. Show Query Timing
Turns on execution time reporting for every query — great for quick performance checks.
\timing
11. Quit psql
Cleanly exits the psql shell.
\q
12. Clear the Terminal Screen
Runs your system’s clear command without leaving psql.
\! clear
Bonus: The Everyday Starter Combo
\l
\c dbname
\dt
These really are the day-to-day psql essentials. If you can connect to a database, inspect objects, view structure, run SQL, and get yourself unstuck, you can do real work in PostgreSQL. Everything else builds on these fundamentals!