How do you back up and restore a PostgreSQL database?
BeginnerHands-onPostgreSQL
Answer
Short answer
Use pg_dump to back up one database and pg_restore (or psql) to restore it. For everything on the server, including users, use pg_dumpall.
Step by step
# Custom format (compressed, flexible restore) — recommended
pg_dump -h db.internal -U postgres -Fc orders > orders-$(date +%F).dump
# Plain SQL format
pg_dump -U postgres orders > orders.sql
# Restore a custom-format dump into a new database
createdb -U postgres orders_restore
pg_restore -U postgres -d orders_restore --no-owner orders-2026-11-26.dump
# Restore a plain SQL dump
psql -U postgres -d orders_restore -f orders.sql
# Everything, including roles
pg_dumpall -U postgres > full-cluster.sql
| Option | Meaning |
|---|---|
-Fc |
Custom format: compressed, and lets you restore single tables |
--no-owner |
Don't try to set original owners — useful when restoring into a different server |
-j 4 |
(pg_restore) restore with 4 parallel jobs — faster |
What to say in the interview
"I take logical backups with pg_dump in custom format, which is compressed and allows selective restores with pg_restore. pg_dumpall covers roles and every database. Backups are scheduled and copied off the server, for example to S3, and on RDS we also use automated snapshots with point-in-time recovery. We test restores regularly."
Follow-up questions you may get
- Does pg_dump lock the database? No — it takes a consistent snapshot without blocking normal reads and writes.
- How do you restore just one table?
pg_restore -t orders -d target orders.dumpwith a custom-format dump.
More on PostgreSQL
How do you set up PostgreSQL and log in with psql?BeginnerHow do you create a database, tables, users and query data in PostgreSQL?BeginnerWhat is PostgreSQL and why is it so popular?BeginnerHow is PostgreSQL organised: cluster, database, schema and table?BeginnerHow do you connect to PostgreSQL from the command line?BeginnerWhat are the most useful psql meta-commands?Beginner