Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
sudo -u postgres pg_dump -Fc -d shop -f /backup/shop-$(date +%F).dump
sudo -u postgres pg_dumpall --globals-only > /backup/globals-$(date +%F).sql
sudo -u postgres createdb shop_restore_test
sudo -u postgres pg_restore -d shop_restore_test /backup/shop-2026-11-19.dump-Fc = the compressed custom format: small, and pg_restore can pick single tables from it.
Step by step
- Back up the database and the roles —
pg_dumpsaves one database, but not the roles and passwords;pg_dumpall --globals-onlysaves those:
sudo -u postgres pg_dump -Fc -d shop -f /backup/shop-2026-11-19.dump
sudo -u postgres pg_dumpall --globals-only > /backup/globals-2026-11-19.sql
chmod 600 /backup/*.dump /backup/*.sql- Big databases: directory format in parallel (
-j= jobs):
sudo -u postgres pg_dump -Fd -j 4 -d shop -f /backup/shop-2026-11-19.dir
sudo -u postgres pg_restore -j 4 -d shop_restore_test /backup/shop-2026-11-19.dir- Look inside a backup and restore only what you need:
pg_restore -l /backup/shop-2026-11-19.dump | head
sudo -u postgres pg_restore -d shop_restore_test -t orders /backup/shop-2026-11-19.dump- Restore for real (after an incident) — roles first, then the database:
sudo -u postgres psql -f /backup/globals-2026-11-19.sql
sudo -u postgres pg_restore --clean --if-exists --no-owner --role=shop -d shop /backup/shop-2026-11-19.dump--clean --if-exists drops the objects before it creates them again. Stop the app first, so nobody writes during the restore.
- Database in a container:
docker compose exec -T db pg_dump -U shop -Fc shop > shop-2026-11-19.dump
docker compose exec -T db pg_restore -U shop -d shop --clean --if-exists < shop-2026-11-19.dump- When pg_dump is not enough — you need to restore to "10:42 this morning" (point-in-time) or the database is hundreds of GB: use a physical backup with WAL archiving —
pgBackRestorbarman. On managed databases (RDS, Azure Database) use the provider's automated backups and test their restore too. - Automate and copy away: a nightly systemd timer, keep e.g. 14 days, copy each file to object storage in another place, and a monthly restore test with the time it took.
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
pg_dump: error: aborting because of server version mismatch | The pg_dump client is older than the server. | Use the client of the same or a newer major version. |
role "shop" does not exist during restore | Roles were not restored first. | Restore globals.sql first, or use --no-owner --role=NAME. |
input file appears to be a text format dump. Please use psql. | A plain SQL dump (pg_dump without -Fc). | Restore it with psql -f FILE. |
| Restore takes hours | Single job, big indexes. | Directory format with -j; or physical backups for big databases. |
| Backup file is 0 bytes and nobody noticed | The job failed silently. | set -euo pipefail in the script; alert if the file is missing or too small. |
| Restore works but the app can not log in | Passwords/roles from another server, or pg_hba.conf differs. | Restore globals; copy the pg_hba.conf rules; check with psql -h. |
Go further
- Command pages: psql.
- Next how-tos: Create a PostgreSQL user and database, Back up and restore MySQL, Create a systemd timer, Copy files to and from S3, Back up a server with restic, Restore PostgreSQL to a point in time, Run a database as a StatefulSet.
- Full guide: PostgreSQL backups. Versioned backups: restic. At work: Backups and the 3-2-1 rule.
- Practise: Lab: prove the backup restores — a timed restore drill.
1Which roles and passwords are in a pg_dump file?
None — save them with pg_dumpall --globals-only.
2When do you need a physical backup (pgBackRest, barman) instead of pg_dump?
For point-in-time recovery and for very big databases.