PostgreSQL Interview Questions for DBAs

PostgreSQL Interview Questions for DBAs – 25 Beginner Q&A | Bright DBA
Interview Preparation Guide

PostgreSQL Interview Questions
for Beginner DBAs

25 essential questions every beginner PostgreSQL DBA must know — with clear, practical answers. Ideal for Oracle DBAs transitioning to PostgreSQL.

25Questions
5Categories
Beginner Level
Architecture MVCC & WAL Backup & Recovery Replication Administration Performance
🏗️
Architecture & Basics
Core concepts every PostgreSQL DBA must understand
Q1 – Q7
Q1 What is PostgreSQL, and how is it different from other databases like Oracle or MySQL?
Architecture +

PostgreSQL is a free, open-source object-relational database management system (ORDBMS) that has been actively developed for over 35 years. It is known for its reliability, standards compliance (SQL:2016), and powerful extensibility.

  • Open Source & Free — No licensing cost, unlike Oracle which requires expensive licenses.
  • ACID Compliant — Full support for Atomicity, Consistency, Isolation, and Durability.
  • Extensible — You can define custom data types, operators, functions, and index methods.
  • JSON Support — Supports both relational (SQL) and non-relational (JSONB) querying.
  • Cross-Platform — Runs on Linux, Windows, and macOS.
💡
Oracle DBA Note
PostgreSQL is the closest open-source equivalent to Oracle. Features like sequences, schemas, tablespaces, WAL (similar to redo logs), and MVCC are all present in PostgreSQL.
Q2 Explain the architecture of PostgreSQL.
Architecture +

PostgreSQL follows a client-server, multi-process architecture. The main components are:

  • Postmaster Process — The master daemon that listens on port 5432, accepts connections, and spawns backend processes.
  • Backend Process — A dedicated server process is spawned for each client connection to handle queries.
  • Shared Buffers — Shared memory pool for caching data pages (like Oracle's SGA Buffer Cache).
  • WAL Writer — Writes transaction logs (WAL) to disk for durability.
  • Checkpointer — Flushes dirty pages from shared buffers to data files at regular intervals.
  • Autovacuum Daemon — Cleans dead tuples and updates statistics automatically.
  • Background Writer — Proactively writes dirty buffers to disk to reduce I/O spikes at checkpoints.
  • WAL Archiver — Archives WAL files when continuous archiving is enabled.
  • Stats Collector — Gathers activity statistics stored in pg_stat_* views.
💡
Oracle DBA Note
The Postmaster ≈ Oracle Listener + PMON. Shared Buffers ≈ SGA Buffer Cache. WAL ≈ Online Redo Logs. Checkpointer ≈ CKPT process. Autovacuum ≈ SMON cleanup.
Q3 What is MVCC (Multi-Version Concurrency Control) in PostgreSQL?
Architecture +

MVCC is the mechanism PostgreSQL uses to allow multiple transactions to read and write data concurrently without blocking each other. Instead of locking rows, PostgreSQL creates new versions of rows for each update or delete.

  • When a row is updated, the old version is marked as dead and a new version is created.
  • When a row is deleted, it is just marked as dead — not physically removed immediately.
  • Each transaction sees a consistent snapshot of the data as of the transaction start time.
  • Dead row versions are later cleaned up by the VACUUM process.
  • This eliminates read-write conflicts — readers never block writers and writers never block readers.
💡
Oracle DBA Note
Oracle also uses MVCC, but stores old row versions in the UNDO tablespace. PostgreSQL stores them in the table itself (heap), which is why VACUUM is needed to reclaim space.
Q4 What is WAL (Write-Ahead Logging) in PostgreSQL?
Architecture +

WAL (Write-Ahead Logging) is PostgreSQL's mechanism for ensuring data durability and crash recovery. The core principle is: changes are written to the WAL log file first, before being applied to actual data files.

  • WAL files are stored in the $PGDATA/pg_wal directory.
  • Each WAL file is 16 MB by default and has a name like 000000010000000000000001.
  • WAL is used for crash recovery, point-in-time recovery (PITR), and streaming replication.
  • WAL level is configured via wal_level parameter: minimal, replica, or logical.
-- Check WAL level SHOW wal_level;-- Check current WAL location SELECT pg_current_wal_lsn();-- Manually switch to next WAL file SELECT pg_switch_wal();
💡
Oracle DBA Note
WAL in PostgreSQL ≈ Online Redo Logs in Oracle. WAL archiving ≈ Oracle archivelog mode. pg_wal directory ≈ Oracle's redo log location.
Q5 What is the difference between a Role and a User in PostgreSQL?
Admin +

In PostgreSQL, there is no difference between a role and a user at the database level. A USER is simply a ROLE with the LOGIN privilege.

-- These two are equivalent: CREATE USER raj WITH PASSWORD 'mypassword'; CREATE ROLE raj LOGIN PASSWORD 'mypassword';-- A role WITHOUT login (cannot connect to DB) CREATE ROLE readonly_role;-- Grant a role to a user GRANT readonly_role TO raj;-- List all roles \du
  • Roles can be granted to other roles, enabling role inheritance.
  • SUPERUSER roles have unrestricted access — equivalent to Oracle's SYSDBA.
  • Use NOLOGIN for group roles that are never used to connect directly.
Q6 What is pg_hba.conf and what is it used for?
Admin +

pg_hba.conf is the Host-Based Authentication configuration file in PostgreSQL. It controls which hosts are allowed to connect, which databases they can access, which users can connect, and what authentication method is used.

# TYPE DATABASE USER ADDRESS METHOD local all postgres peer local all all md5 host all all 127.0.0.1/32 scram-sha-256 host all all 192.168.1.0/24 scram-sha-256 host replication all 192.168.1.52/32 scram-sha-256
  • local — Unix socket connections.
  • host — TCP/IP connections (both SSL and non-SSL).
  • hostssl — SSL-only connections.
  • Authentication methods: trust, md5, scram-sha-256, peer, ldap.
  • After editing, reload with: SELECT pg_reload_conf();
💡
Oracle DBA Note
pg_hba.conf ≈ Oracle sqlnet.ora + listener.ora combined for controlling who can connect and how they authenticate.
Q7 What is a tablespace in PostgreSQL and how do you create one?
Admin +

A tablespace in PostgreSQL is a named location on disk where database objects (tables, indexes) are stored. It allows DBAs to control where data is physically stored — for example, placing large tables on a faster disk.

-- Step 1: Create directory (as OS user) mkdir -p /pgdata/tbs/app_tbs01 chown postgres:postgres /pgdata/tbs/app_tbs01-- Step 2: Create tablespace in PostgreSQL CREATE TABLESPACE app_tbs01 OWNER postgres LOCATION '/pgdata/tbs/app_tbs01';-- Step 3: Create a table in that tablespace CREATE TABLE orders (id SERIAL, amount NUMERIC) TABLESPACE app_tbs01;-- List all tablespaces \db
💾
Backup & Recovery
pg_dump, pg_basebackup, PITR, and WAL archiving
Q8 – Q13
Q8 What are the types of backups available in PostgreSQL?
Backup +

PostgreSQL supports two main types of backups:

  • Logical Backup — Uses pg_dump or pg_dumpall. Exports SQL statements. Human-readable, portable across versions. Cannot be used for PITR. Best for schema/table level recovery.
  • Physical Backup — Uses pg_basebackup. Copies the actual data files. Can be combined with WAL archives for Point-in-Time Recovery (PITR). Used for full cluster recovery and standby server setup.
💡
Oracle DBA Note
pg_dump ≈ Oracle Data Pump Export (expdp). pg_basebackup ≈ Oracle RMAN full backup. WAL archiving ≈ Oracle archivelog mode for PITR.
Q9 What is the difference between pg_dump and pg_dumpall?
Backup +
-- pg_dump: backup a SINGLE database pg_dump -U postgres -d mydb -Fc -f /backup/mydb.dmp-- pg_dumpall: backup ALL databases + global objects (roles, tablespaces) pg_dumpall -U postgres -f /backup/all_databases.sql-- pg_dumpall globals only (roles and tablespaces) pg_dumpall -U postgres --globals-only -f /backup/globals.sql-- Restore pg_dump backup pg_restore -U postgres -d mydb -v /backup/mydb.dmp-- Restore pg_dumpall backup psql -U postgres -f /backup/all_databases.sql
  • pg_dump backs up one database at a time. Supports custom (-Fc), plain (-Fp), directory (-Fd) formats.
  • pg_dumpall backs up all databases AND global objects (roles, tablespaces). Output is always plain SQL.
  • Neither supports PITR — for that, use pg_basebackup + WAL archiving.
Q10 What is pg_basebackup and when do you use it?
Backup +

pg_basebackup takes a physical binary backup of the entire PostgreSQL cluster while the database is running (online backup). It copies all data files, WAL files, and configuration files.

-- Basic pg_basebackup (tar format, includes WAL) pg_basebackup -U postgres \ -D /backup/base_bkp \ -Ft -Xs -P -v-- Options explained: -- -Ft : tar format -- -Xs : stream WAL during backup -- -P : show progress -- -v : verbose
  • Used for setting up standby (replica) servers for streaming replication.
  • Used as the base for Point-in-Time Recovery (PITR) when combined with WAL archives.
  • Requires wal_level = replica and replication privilege on the user.
Q11 What is Point-in-Time Recovery (PITR) in PostgreSQL?
Backup +

PITR allows you to restore a PostgreSQL database to any specific point in time — for example, just before an accidental DROP TABLE. It requires a base backup + continuous WAL archives.

  • Enable archiving: set archive_mode = on and archive_command in postgresql.conf.
  • Take a base backup using pg_basebackup.
  • During recovery, set restore_command and recovery_target_time in postgresql.conf.
  • Create an empty recovery.signal file in PGDATA to trigger recovery mode.
  • After recovery, run SELECT pg_wal_replay_resume(); to promote the DB.
-- postgresql.conf settings for PITR recovery restore_command = 'cp /backup/archive/%f %p' recovery_target_time = '2025-10-12 22:05:00'-- Create recovery signal file touch $PGDATA/recovery.signal-- After DB starts in recovery, promote it SELECT pg_wal_replay_resume();
💡
Oracle DBA Note
PostgreSQL PITR ≈ Oracle RMAN Point-in-Time Recovery. recovery.signal ≈ Oracle's incomplete recovery trigger. WAL archives ≈ Oracle archived redo logs.
Q12 How do you enable WAL archiving in PostgreSQL?
Backup +
-- Edit postgresql.conf wal_level = replica archive_mode = on archive_command = 'cp %p /pgArchive/arch/%f' max_wal_senders = 10-- Reload configuration SELECT pg_reload_conf();-- Verify archiving is active SELECT * FROM pg_stat_archiver;-- Manually force a WAL switch to test archive_command SELECT pg_switch_wal();
  • %p in archive_command = full path of the WAL file to be archived.
  • %f = filename only.
  • The archive directory must be owned by the postgres OS user.
  • Check pg_stat_archiver.last_failed_wal to diagnose archiving failures.
Q13 How do you restore a single table using pg_dump and pg_restore?
Backup +
-- Step 1: Dump a specific table (custom format) pg_dump -U postgres -d mydb \ -t schema_name.table_name \ -Fc -v \ -f /backup/table_backup.dmp-- Step 2: Verify contents of the dump pg_restore -l /backup/table_backup.dmp-- Step 3: Restore the table to the database pg_restore -U postgres -d mydb \ -v /backup/table_backup.dmp
  • Use -Fc (custom format) for table-level restores — plain SQL format does not support selective restore.
  • If the table already exists, add --clean flag to drop it before restoring.
  • Use -n schema_name to specify schema if needed.
Performance & Maintenance
VACUUM, ANALYZE, indexes, and query tuning basics
Q14 – Q18
Q14 What is VACUUM in PostgreSQL and why is it needed?
Performance +

Due to MVCC, PostgreSQL does not immediately delete updated or deleted rows — it marks them as "dead tuples". Over time, these accumulate and cause table bloat, slowing down queries. VACUUM reclaims this dead space.

-- Standard VACUUM: marks space as reusable (does NOT shrink file) VACUUM mytable;-- VACUUM ANALYZE: reclaim space + update statistics VACUUM ANALYZE mytable;-- VACUUM FULL: rewrites entire table, returns space to OS (LOCKS table!) VACUUM FULL mytable;-- Check dead tuples per table SELECT relname, n_dead_tup, last_autovacuum FROM pg_stat_user_tables ORDER BY n_dead_tup DESC;
  • Autovacuum runs VACUUM automatically in the background — it is enabled by default.
  • VACUUM does NOT lock the table. VACUUM FULL holds an exclusive lock.
  • VACUUM also prevents Transaction ID (XID) wraparound — a critical PostgreSQL risk.
Q15 What is autovacuum and how does it work?
Performance +

Autovacuum is a background PostgreSQL daemon that automatically runs VACUUM and ANALYZE on tables when they accumulate enough dead tuples or changed rows, without DBA intervention.

  • It is triggered when: dead tuples > autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor × total_rows
  • Key parameters: autovacuum_max_workers (default: 3), autovacuum_naptime (default: 1 min).
  • Never disable autovacuum on production — it prevents bloat and XID wraparound.
-- Check autovacuum settings SHOW autovacuum; SHOW autovacuum_vacuum_scale_factor;-- Monitor autovacuum activity SELECT pid, query FROM pg_stat_activity WHERE query LIKE 'autovacuum%';
💡
Oracle DBA Note
Autovacuum ≈ Oracle SMON (cleaning temporary segments and managing undo). However, PostgreSQL autovacuum must also prevent XID wraparound, which has no direct Oracle equivalent.
Q16 What types of indexes are available in PostgreSQL?
Performance +
  • B-Tree (default) — Best for equality and range queries. Used for most cases.
  • Hash — Only for equality comparisons (=). Faster than B-Tree for pure equality.
  • GIN (Generalized Inverted Index) — Best for full-text search, arrays, and JSONB data.
  • GiST (Generalized Search Tree) — Used for geometric data, full-text search, and range types.
  • BRIN (Block Range Index) — Very small index for large tables with naturally ordered data (e.g., timestamps).
  • SP-GiST — Space-partitioned trees for non-balanced data structures.
-- Create a standard B-Tree index CREATE INDEX idx_emp_name ON employees(last_name);-- Create index without locking table (CONCURRENTLY) CREATE INDEX CONCURRENTLY idx_emp_email ON employees(email);-- GIN index for JSONB CREATE INDEX idx_data_gin ON logs USING GIN (payload);
Q17 How do you generate and read an execution plan in PostgreSQL?
Performance +
-- EXPLAIN: shows plan without executing EXPLAIN SELECT * FROM employees WHERE dept_id = 10;-- EXPLAIN ANALYZE: executes query and shows actual timings EXPLAIN ANALYZE SELECT * FROM employees WHERE dept_id = 10;-- EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) SELECT * FROM employees WHERE dept_id = 10;
  • Seq Scan — Full table scan (no index used).
  • Index Scan — Index used to fetch specific rows.
  • cost= — Estimated startup cost .. total cost.
  • actual time= — Actual execution time in milliseconds.
  • rows= — Estimated vs actual row count. Large difference = stale statistics → run ANALYZE.
💡
Oracle DBA Note
EXPLAIN ANALYZE ≈ Oracle's EXPLAIN PLAN + AUTOTRACE. The output is similar to Oracle's execution plan with cost estimates and actual row counts.
Q18 What are the key parameters in postgresql.conf that affect performance?
Performance +
-- Memory Parameters shared_buffers = 4GB -- 25% of total RAM (like Oracle SGA) work_mem = 64MB -- Per-sort operation memory maintenance_work_mem = 512MB -- For VACUUM, CREATE INDEX effective_cache_size = 12GB -- Estimated OS + DB cache (for planner)-- WAL / Checkpoint Parameters checkpoint_timeout = 15min max_wal_size = 2GB wal_compression = on-- Connection Parameters max_connections = 200-- Parallel Query max_parallel_workers_per_gather = 4
  • shared_buffers — Most impactful single parameter. Set to 25% of RAM.
  • work_mem — Be careful: each sort/hash in each query can use this amount.
  • effective_cache_size — Hint to the planner only; does not allocate memory.
🔁
Replication & High Availability
Streaming replication, failover, and standby concepts
Q19 – Q22
Q19 What is streaming replication in PostgreSQL?
High Availability +

Streaming replication allows a standby (replica) server to stay in sync with the primary by continuously receiving and applying WAL records over a TCP connection — in near real time.

  • The standby connects to the primary using the replication protocol.
  • By default it is asynchronous — primary does not wait for standby confirmation before committing.
  • Synchronous replication is possible via synchronous_standby_names — zero data loss but adds latency.
  • The standby can serve read-only queries (Hot Standby).
  • Monitor replication lag via pg_stat_replication on primary.
💡
Oracle DBA Note
PostgreSQL streaming replication ≈ Oracle Data Guard Physical Standby. Synchronous mode ≈ Oracle Data Guard SYNC protection mode. pg_stat_replication ≈ Oracle v$managed_standby.
Q20 What is the difference between asynchronous and synchronous replication?
High Availability +
  • Asynchronous (default) — Primary commits immediately without waiting for standby confirmation. Better performance, but risk of small data loss if primary crashes before WAL is sent to standby.
  • Synchronous — Primary waits for at least one standby to confirm WAL receipt before committing. Zero data loss, but adds latency to every write transaction.
-- Enable synchronous replication on PRIMARY -- (postgresql.conf) synchronous_standby_names = 'standby1'-- Check replication status SELECT application_name, sync_state, sent_lsn, replay_lsn FROM pg_stat_replication;
Q21 How do you promote a standby to primary (manual failover)?
High Availability +
-- Method 1: Using pg_promote() function (PostgreSQL 12+) SELECT pg_promote();-- Method 2: Using pg_ctl promote command pg_ctl promote -D /pgdata/pgsql17/data-- Verify promotion was successful SELECT pg_is_in_recovery(); -- Returns: f (false) = now a primary-- Check timeline (should increase by 1 after promotion) SELECT timeline_id FROM pg_control_checkpoint();
  • After promotion, the standby becomes a new primary on a new timeline.
  • Old primary should NOT be brought back up without reinitializing as a new standby.
  • Tools like Patroni or repmgr automate this process.
Q22 How do you check replication lag in PostgreSQL?
High Availability +
-- On PRIMARY: check all connected standbys and their lag SELECT application_name, client_addr, state, sent_lsn, replay_lsn, (sent_lsn - replay_lsn) AS replication_lag_bytes, sync_state FROM pg_stat_replication;-- On STANDBY: check how far behind this standby is SELECT now() - pg_last_xact_replay_timestamp() AS replication_delay;
🛠️
Day-to-Day Administration
Common DBA commands and monitoring queries
Q23 – Q25
Q23 How do you find and kill long-running queries in PostgreSQL?
Admin +
-- Find all active queries running more than 5 minutes SELECT pid, now() - pg_stat_activity.query_start AS duration, query, state, usename, client_addr FROM pg_stat_activity WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes' AND state != 'idle' ORDER BY duration DESC;-- Gracefully cancel a query (sends SIGINT) SELECT pg_cancel_backend(12345);-- Forcefully terminate a session (sends SIGTERM) SELECT pg_terminate_backend(12345);
💡
Oracle DBA Note
pg_stat_activity ≈ Oracle v$session. pg_cancel_backend ≈ ALTER SYSTEM CANCEL SQL. pg_terminate_backend ≈ ALTER SYSTEM KILL SESSION.
Q24 How do you find blocking sessions in PostgreSQL?
Admin +
-- Find blocking and blocked sessions SELECT blocked.pid AS blocked_pid, blocked.query AS blocked_query, blocking.pid AS blocking_pid, blocking.query AS blocking_query, blocked.usename AS blocked_user, blocking.usename AS blocking_user FROM pg_stat_activity AS blocked JOIN pg_stat_activity AS blocking ON blocking.pid = ANY(pg_blocking_pids(blocked.pid)) WHERE cardinality(pg_blocking_pids(blocked.pid)) > 0;-- Kill the blocking session SELECT pg_terminate_backend(blocking_pid);
Q25 What are the most important monitoring views every PostgreSQL DBA must know?
Admin +
  • pg_stat_activity — Active sessions, queries, wait events. (≈ Oracle v$session)
  • pg_stat_user_tables — Table-level stats: dead tuples, seq scans, last vacuum. (≈ Oracle dba_tab_modifications)
  • pg_stat_user_indexes — Index usage statistics. Identify unused indexes.
  • pg_stat_replication — Standby connection status and replication lag. (≈ Oracle v$managed_standby)
  • pg_stat_archiver — WAL archiving status and failures.
  • pg_stat_statements — Top SQL by execution count, total time. (≈ Oracle v$sql — requires extension)
  • pg_locks — Active lock information. (≈ Oracle v$lock)
  • pg_database — Database list with sizes. (≈ Oracle dba_data_files)
-- Top 10 queries by total execution time SELECT query, calls, total_exec_time, mean_exec_time FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10;-- Enable pg_stat_statements (in postgresql.conf) shared_preload_libraries = 'pg_stat_statements'