PostgreSQL Cheat Sheet
Transform, format, and optimize your data instantly with this free client-side tool.
CLI (psql) & Access
Connect to database as user
Connect with host and port
Dump database to a file
Restore database from archive
psql Commands
List all databases
Connect to different database
List all tables
Show table schema/structure
List roles / users
List functions
List views
Quit psql
Toggle expanded display mode (vertical output)
Toggle query execution time info
Databases & Schemas
Create new database
Delete database
Create new schema
Change default schema
Tables
Create table with auto-incrementing ID
Drop table and dependent objects (e.g. views)
Empty table and reset sequences
Add column
Remove column
Rename column
Change column data type
CRUD
Retrieve all rows and columns
Insert and return generated ID
Update row
Delete row
Upsert (Insert or Update on conflict)
Queries
Select unique values
Case-insensitive pattern matching
Find records with NULL values
Return first non-null value (use 0 if null)
Paginate (limit num rows, offset start)
Joins
Inner join
Left join with USING syntax (when column names match)
Full outer join (records from both tables)
JSON & Arrays
Extract JSON object field as JSON
Extract JSON object field as text
Find document containing key/value
Access first element of array (1-indexed)
Find row where array contains value
Advanced Queries
Group and count
Concatenate values into a single string
Window function: rank items within groups
Roles & Privileges
Create a user role
Grant database privileges
Grant table privileges
Performance
Create a B-tree index
Create GIN index (for JSONB/Arrays)
Create partial index
Execute query and show actual run times
Reclaim storage and update statistics
Show active connections and queries
Advanced
Explore advanced configurations for Postgres
Performance tuning best practices for Postgres
Run security checks and validation for Postgres
🔥 Similar Tools
Explore LibraryFrequently Asked Questions
Got questions? We've got answers.