PostgreSQL37 commands
PostgreSQL Cheat Sheet
Essential PostgreSQL commands for databases, tables, queries, users, backups, and performance tuning.
Connection & psql
| Command | Description |
|---|---|
| Connect as postgres user |
| Connect to remote database |
| List databases |
| Switch database |
| List tables in current schema |
| Describe table structure |
| List roles/users |
| Quit psql |
| Show query execution time |
Database Management
| Command | Description |
|---|---|
| Create database |
| Delete database |
| Rename database |
| Create schema |
| Set schema search path |
Tables & Indexes
| Command | Description |
|---|---|
| Create table |
| Add column |
| Drop column |
| Create index |
| Create index without locking |
| Drop index |
| Empty table and reset IDs |
Users & Permissions
| Command | Description |
|---|---|
| Create user |
| Grant superuser |
| Grant database access |
| Grant table privileges |
| Revoke access |
Backup & Restore
| Command | Description |
|---|---|
| Dump database to SQL |
| Dump in custom format |
| Restore from dump |
| Restore from SQL file |
| Dump all databases |
Performance
| Command | Description |
|---|---|
| Show query plan with timing |
| Database size |
| Active connections/queries |
| Cancel running query |
| Kill connection |
| Reclaim space and update stats |