Home  Postgress   Cheatsheet ...

Cheatsheet for postgress DBA

🧩 1️⃣ Top SQL Queries for DBAs

This section contains the 10 most essential SQL queries a DBA uses to monitor, troubleshoot, and optimize PostgreSQL databases.

PurposeExample QueryWhy it’s important
Active QueriesSELECT pid, usename, datname, state, query FROM pg_stat_activity WHERE state != 'idle';Shows currently running queries — useful to detect long-running or blocking operations.
Blocking QueriesJoins pg_locks on transaction IDsIdentifies which sessions are blocking others (deadlocks, waiting transactions).
Table SizeUses pg_total_relation_size()Helps find largest tables for tuning and partitioning.
Database Sizepg_database_size()Lets you quickly see which database consumes most space.
Unused IndexesSELECT relname, indexrelname, idx_scan FROM pg_stat_user_indexes WHERE idx_scan = 0;Detects indexes that consume space but are never used — candidates for removal.
Slow Queriespg_stat_statementsFinds top slow SQL queries by execution time — crucial for performance tuning.
Autovacuum Statspg_stat_all_tablesShows if autovacuum is cleaning up dead tuples effectively.
Roles & PrivilegesSELECT rolname, rolsuper, rolcreatedb, rolcanlogin FROM pg_roles;Helps audit user roles and permissions.
Connections per DBSELECT datname, count(*) FROM pg_stat_activity GROUP BY datname;Ensures no single DB is hitting connection limits.
Table BloatChecks n_dead_tupIdentifies bloat — tables with too many dead tuples slowing down performance.

These queries are the backbone of any DBA’s daily health check or monitoring dashboard.


🧮 2️⃣ Common PostgreSQL Data Types

Knowing PostgreSQL data types is key for designing efficient schemas. The cheat sheet includes:

CategoryExamplesNotes
Numericinteger, bigint, decimal, numeric, serialUsed for numbers and auto-incrementing fields.
Characterchar(n), varchar(n), textUsed for text and strings.
BooleanbooleanTrue/False values.
Date/Timedate, timestamp, timestamptz, intervalFor time zones and scheduling.
UUIDuuidGreat for unique IDs instead of integers (distributed systems).
Arrayinteger[], text[]Stores multiple values in one column — handy but use with care.
JSON / JSONBjson, jsonbStore structured data; jsonb supports indexing and search.
Rangeint4range, numrange, daterangeRepresents value ranges (e.g., date intervals).
Networkinet, cidr, macaddrFor IP and network data — useful in DevOps and firewall logs.
Full Text Searchtsvector, tsqueryFor advanced text searching (search bars, document search).

💻 3️⃣ Useful psql Commands

These are shortcuts used inside the PostgreSQL CLI (psql) to explore and manage databases:

CommandDescription
\lList all databases.
\dtList all tables in the current database.
\duShow all roles and users.
\xToggle expanded output — great for wide columns.
\timingShow execution time for queries (performance testing).
\conninfoShow details of current connection.
\qQuit psql.

These commands are like shortcuts that make your DBA workflow much faster.


💾 4️⃣ Backup & Restore Commands

A DBA must always be ready for disaster recovery. These commands show how to handle that safely.

Backup

pg_dump -U username -F c -f backup.dump dbname

Restore

pg_restore -U username -d dbname backup.dump

Dump All Databases

pg_dumpall -U username > alldb_backup.sql

🔐 Bonus Tips for PostgreSQL DBAs

  1. Enable logging of slow queries (log_min_duration_statement) to identify performance issues.
  2. Monitor autovacuum — stale tuples slow down reads.
  3. Use pg_stat_statements for query performance insights.
  4. Regularly reindex to avoid index bloat.
  5. Set proper max_connections and work_mem in postgresql.conf for scaling.
  6. Use pgAdmin or DBeaver for GUI-based monitoring.
  7. Learn EXPLAIN ANALYZE to optimize slow queries.
  8. Keep PostgreSQL updated — many versions bring huge performance and security improvements.
Published on: Oct 07, 2025, 05:19 AM  
 

Comments

Add your comment