Psql Cheat Sheet
Psql Cheat Sheet

Basic Navigation Commands
List all databases
\l
Connect to another database
\c dbname
Show current connection
\conninfo
Listing Objects Quickly
List all schemas
\dn
List all tables in current database
\dt
List tables across all schemas
\dt *.*
List all functions
\df
List all views
\dv
List everything (tables, views, sequences)
\d
Inspecting Tables & Structures
Describe a table
\d table_name
Describe a table with storage info
\d+ table_name
List indexes of a table
\di table_name
Show sequences
\ds
Size & Storage Tricks
Size of a single database
SELECT
pg_size_pretty(pg_database_size('mydb'));
Size of all databases
SELECT
datname,
pg_size_pretty(pg_database_size(datname))
FROM
pg_database;
Table size
SELECT
pg_size_pretty(pg_total_relation_size('table_name'));
User & Permission Tools
List all users and roles
\du
Show grants for a role
\dg+ username;
Useful psql Quality-of-Life Options
Show SQL timing
\timing
Turn expanded display on/off (better formatting)
\x
Enable/disable auto-commit
\set AUTOCOMMIT on
\set AUTOCOMMIT off
View all psql settings
\set
Command History & Editing
Show command history
\history
Repeat last query
\g
Save query output to file
\o output.txt
SELECT * FROM test;
\o
Final Thoughts
psql is much more powerful than most people realize. With a few meta-commands, you can navigate databases, inspect structures, and analyze size or performance with almost no SQL at all. This cheatsheet is designed to be a compact, daily-use reference that keeps your workflow fast and efficient.
← PostgreSQL Blog