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.dump with a custom-format dump.
All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: PostgreSQL basics

More on PostgreSQL