Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 58Data · PostgreSQL

How to back up and restore PostgreSQL

A database backup is only good when the restore works — and you know how long it takes. For most apps a nightly logical backup with pg_dump is the start: one file per database, restorable on any newer PostgreSQL. This how-to makes that backup (plus the roles), restores it into a test database, does it faster in parallel, and tells you when you need a physical backup with point-in-time recovery instead.

  • 5 Lessen
  • 7 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

58.1

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.

58.2

Step by step

  1. Back up the database and the roles — pg_dump saves one database, but not the roles and passwords; pg_dumpall --globals-only saves 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
  1. 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
  1. 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
  1. 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.

  1. 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
  1. 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 — pgBackRest or barman. On managed databases (RDS, Azure Database) use the provider's automated backups and test their restore too.
  2. 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.

58.3

Check that it worked

sudo -u postgres psql -d shop_restore_test -c 'SELECT count(*) FROM app.orders;'
sudo -u postgres psql -d shop -c 'SELECT count(*) FROM app.orders;'
sudo -u postgres dropdb shop_restore_test

The counts match the time of the backup, and you wrote down how long the restore took (your real RTO).

58.4

Common problems

ProblemCauseFix
pg_dump: error: aborting because of server version mismatchThe 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 restoreRoles 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 hoursSingle job, big indexes.Directory format with -j; or physical backups for big databases.
Backup file is 0 bytes and nobody noticedThe 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 inPasswords/roles from another server, or pg_hba.conf differs.Restore globals; copy the pg_hba.conf rules; check with psql -h.

58.5

Go further

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.

Commando's in deze les

Alle commando's in dit hoofdstuk