Backing up PostgreSQL without downtime
Copying the data directory of a running PostgreSQL server gives you a backup that may not start. Here's how to do it properly — and what our agent does behind the scenes.
PostgreSQL writes to many files at once, and a regular file-level backup reads those files one after another. If the database changes while you copy — and on a production server it always does — you end up with files from different points in time. Sometimes PostgreSQL can recover from that. Sometimes it can't, and you find out during a restore.
There are three reliable ways to back up a running PostgreSQL server. Each has its place.
1. Logical dumps with pg_dump
pg_dump connects like a normal client and exports the database as SQL or in its custom format. It uses a single transaction snapshot, so the dump is consistent even while writes continue.
- Good for: small and medium databases, migrations between major versions, restoring a single table.
- Limitations: restore time grows with database size because indexes are rebuilt; you can only restore to the moment the dump started.
2. Physical base backups
pg_basebackup copies the data directory through the replication protocol while the server keeps running, together with the write-ahead log (WAL) needed to make the copy consistent. Restoring is fast — it's essentially starting PostgreSQL on the copied files.
- Good for: large databases where restore time matters.
- Limitations: restores the whole cluster, same major version only.
3. Continuous WAL archiving
Every change in PostgreSQL is first written to the WAL. If you keep a base backup plus every WAL segment written since, you can replay the database to any moment — for example, 14:31:05, ten seconds before someone ran a DELETE without a WHERE clause.
- Good for: production databases where losing an hour of data is not acceptable.
- Limitations: needs careful setup and monitoring — a gap in the WAL archive silently breaks point-in-time recovery after that point.
| pg_dump | Base backup | Base + WAL | |
|---|---|---|---|
| Backup impact | Medium (CPU, I/O) | Medium (I/O) | Low after first backup |
| Restore speed | Slow for large DBs | Fast | Fast + replay |
| Restore granularity | Dump time | Backup time | Any second |
| Single table restore | Yes | No | No |
What the BackupSafe agent does
When you add a PostgreSQL source to a backup plan, the agent combines methods 2 and 3:
- It connects as a replication user and runs a base backup on the schedule you choose — weekly by default.
- Between base backups it streams WAL continuously through a replication slot, so no segment is lost even if the agent restarts.
- Base backups and WAL are chunked, deduplicated and encrypted on the server before upload, like any other data.
- Every night, the agent verifies the WAL chain and raises an alert in the console if there is a gap.
Setup takes a minute. Create a role with replication privileges, allow it in pg_hba.conf, and add the source:
-- in psql
CREATE ROLE backupsafe_backup WITH REPLICATION LOGIN PASSWORD '...';
# on the database server
$ sudo backupsafe plans add pg-prod --type postgresql \
--dsn "host=/var/run/postgresql user=backupsafe_backup" \
--base-backup weekly --wal stream --retention compliance
✓ plan pg-prod created — first base backup starts now
Restoring to a point in time
To restore, pick a target time in the console or on the command line. The agent downloads the closest earlier base backup, writes the recovery configuration and replays WAL up to that moment.
$ sudo backupsafe restore pg-prod --to "2026-03-10 14:31:05" --target /var/lib/postgresql/16/restore
base backup 2026-03-08 03:00 (48.2 GB)
wal replay 1,904 segments
✓ cluster restored to 2026-03-10 14:31:05 UTC in 11m27s
We recommend restoring to a separate directory or a separate server, checking the data, and only then switching applications over.
Should you still run pg_dump?
Often, yes. A weekly logical dump is a cheap extra layer: it's readable by any PostgreSQL version and lets you restore one table without spinning up a whole cluster. Add it as a pre-hook in the same plan, and the dump file is backed up together with everything else — see the databases section of the docs.
PostgreSQL 12 through 17 are supported. MySQL and MariaDB use the same approach with binary logs instead of WAL; Microsoft SQL Server uses its native full and log backups through VSS.