Audience: Beginners โ Intermediate PostgreSQL users, DevOps, Back-end Developers.
๐ Table of Contents
๐ Connecting to PostgreSQL
๐งญ Exploring Databases, Tables, and Schemas
๐ค User Management
๐ Schema, Role & Privilege Fixes
๐งช Query Debugging & Monitoring
๐ฆ Importing & Exporting Data
๐งฎ Disk Usage & Performance
โ Useful Shortcuts & Tips
๐ Resources
1. ๐ Connecting to PostgreSQL
Basic login:
psql -U <username> -d <database>
Common options:
| Option | Description |
| ------ | ---------------------------------------- |
| `-E` | Show the underlying SQL for `\` commands |
| `-l` | List databases and exit |
| `-h` | Show help |
| `\q` | Quit session |
2. ๐งญ Exploring Databases, Tables, and Schemas
| Command | Description |
| ----------------------------- | ------------------------------ |
| `\l` | List all databases |
| `\c db_name` | Connect to a database |
| `\dt` | List tables |
| `\dt *.*` | List all tables in all schemas |
| `\d table_name` | Show table structure |
| `\d+ table` | Show table + size + comments |
| `\dn` | List schemas |
| `\df` | List functions |
| `\dv` | List views |
| `\dx` | List installed extensions |
| `SELECT * FROM pg_extension;` | Show extension info |
3. ๐ค User Management & Roles
View users & roles:
\du
\du username
Create role:
CREATE ROLE new_user LOGIN PASSWORD 'secret';
Grant privileges:
GRANT ALL PRIVILEGES ON DATABASE your_db TO new_user;
GRANT USAGE, CREATE ON SCHEMA public TO new_user;
Role membership:
GRANT role_b TO role_a; -- role_a inherits permissions of role_b
See all grants:
SELECT table_catalog, table_schema, table_name, privilege_type
FROM information_schema.table_privileges
WHERE grantee = 'username';
4. ๐ Schema, Role & Privilege Fixes
Fixing permission denied for schema public
errors:
-- Replace with your actual DB user
ALTER SCHEMA public OWNER TO your_db_user;
GRANT ALL ON SCHEMA public TO your_db_user;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO your_db_user;
GRANT USAGE, CREATE ON SCHEMA public TO your_db_user;
Check schema owners:
SELECT schema_name, schema_owner
FROM information_schema.schemata
WHERE schema_name = 'public';
5. ๐งช Query Debugging & Monitoring
Currently running queries:
SELECT datname, pid, state, query, query_start
FROM pg_stat_activity
WHERE state <> 'idle';
Show backend processes:
SELECT pg_stat_get_backend_pid(s.backendid) AS pid,
pg_stat_get_backend_activity(s.backendid) AS query
FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
Terminate all other connections:
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'your_db' AND pid <> pg_backend_pid();
Analyze queries:
EXPLAIN ANALYZE SELECT * FROM your_table;
Verbose logging (PostgreSQL config):
Edit postgresql.conf:
log_min_messages = debug5
log_min_error_statement = debug5
log_min_duration_statement = -1
Then restart:
sudo service postgresql restart
6. ๐ฆ Importing & Exporting Data
Export to CSV:
\copy (SELECT * FROM your_table) TO 'output.csv' WITH CSV;
Import from CSV:
\copy your_table FROM 'input.csv' WITH CSV;
7.** ๐งฎ Disk Usage & Performance
**Table sizes:
SELECT
table_name,
pg_size_pretty(pg_total_relation_size(table_name)) AS total_size
FROM information_schema.tables
WHERE table_schema = 'public';
Database size:
SELECT pg_size_pretty(pg_database_size('your_db'));
8. โ Useful Shortcuts & Tips
| Shortcut | Action |
| ---------- | ------------------------------ |
| `\x` | Pretty format output |
| `\! ls` | Run shell command |
| `CTRL + R` | Reverse search command history |
| `\?` | Show command help |
9. ๐ Resources
๐ PostgreSQL Official Docs
๐ง Awesome PostgreSQL
๐ GUI tools: pgAdmin, DBeaver, TablePlus
๐งช CLI tools: pg_top, pg_activity
Top comments (0)