Setting Up pgBackRest for PostgreSQL 17 Backups With Point-in-Time Recovery

A nightly pg_dump is a logical export, not a backup strategy. It gives you last night’s data and nothing since — no way to roll a busy database forward to the moment before someone ran the wrong DELETE.

What you actually want is a physical backup with continuous write-ahead log archiving, so you can restore to any second between backups. That is point-in-time recovery, and pgBackRest is the tool most teams reach for. This walks through a working setup: pgBackRest 2.53 against PostgreSQL 17, backups pushed to an encrypted S3 repository, a full/differential/incremental schedule, and a restore to an arbitrary timestamp that you verify rather than assume. It is the operational companion to our baseline pgBackRest setup — this one goes further into the S3 repository, retention and the test restore.

The repository configuration

pgBackRest is driven entirely by /etc/pgbackrest/pgbackrest.conf. The [global] section defines the repository — where backups live, how they are compressed and encrypted, and how long they are kept. A named stanza section (here [main]) points at the cluster it protects. Below is a repository on S3 with client-side encryption, Zstandard compression and block incremental enabled.

# /etc/pgbackrest/pgbackrest.conf

[global]
repo1-path=/pgbackrest
repo1-type=s3
repo1-s3-bucket=acme-pg-backups
repo1-s3-endpoint=s3.eu-west-1.amazonaws.com
repo1-s3-region=eu-west-1
repo1-s3-key=AKIAEXAMPLE
repo1-s3-key-secret=EXAMPLEsecretkey

# Client-side encryption: pgBackRest encrypts before upload
repo1-cipher-type=aes-256-cbc
repo1-cipher-pass=CHANGE_ME_long_random_passphrase

# Retention: keep 2 full backups, expire dependent diffs with them
repo1-retention-full=2
repo1-retention-diff=6

# Storage efficiency
compress-type=zst
compress-level=6
repo1-block=y
repo1-bundle=y

# Parallelism and asynchronous WAL archiving
process-max=4
archive-async=y
spool-path=/var/spool/pgbackrest
start-fast=y
log-level-console=info
log-level-file=detail

[main]
pg1-path=/var/lib/postgresql/17/main
pg1-port=5432
pg1-user=postgres

A few of these earn their place. repo1-cipher-pass is the key to your entire backup set — store the config root:postgres, mode 0640, and keep the passphrase in your secrets manager, because a lost passphrase means unrecoverable backups. repo1-block=y turns on block incremental, so an incremental backup stores only the changed portions of large files rather than whole files. compress-type=zst is a meaningful upgrade on the gz default: better ratios at lower CPU cost. And archive-async=y decouples WAL archiving from the database’s foreground, which matters on a write-heavy cluster.

Wiring PostgreSQL to archive its WAL

Backups alone give you recovery points at backup granularity. PITR needs the write-ahead log streamed to the repository continuously, so PostgreSQL must call pgBackRest on every completed WAL segment. Set the following in postgresql.conf.

# postgresql.conf
wal_level = replica
archive_mode = on
archive_command = 'pgbackrest --stanza=main archive-push %p'
archive_timeout = 60

wal_level = replica is already the PostgreSQL 17 default, but state it explicitly. archive_mode = on requires a restart, not a reload. archive_timeout = 60 forces a segment switch at least once a minute, which bounds how much recent data you could lose if the primary dies before a segment fills — a low-traffic database would otherwise archive nothing for long stretches. Restart the cluster after these changes:

sudo systemctl restart postgresql@17-main

Create the stanza and take the first backup

The stanza is pgBackRest’s record of a cluster and its repository. Create it, then run check — it validates that WAL archiving and the repository are both working end to end. Never skip the check; it is the difference between believing you have backups and knowing it.

sudo -u postgres pgbackrest --stanza=main stanza-create
sudo -u postgres pgbackrest --stanza=main check

The three backup types map onto a sensible schedule. A full backup copies the entire cluster. A differential copies everything changed since the last full. An incremental copies everything changed since the last backup of any type. Run them from cron or systemd timers — for example a weekly full, a daily differential, and an hourly incremental:

# Weekly full (Sunday 01:00)
sudo -u postgres pgbackrest --stanza=main --type=full backup

# Daily differential (Mon-Sat 01:00)
sudo -u postgres pgbackrest --stanza=main --type=diff backup

# Hourly incremental
sudo -u postgres pgbackrest --stanza=main --type=incr backup

incr is the default type, so a bare backup is incremental; if no full exists yet, pgBackRest promotes it to a full automatically. Retention is handled for you: with repo1-retention-full=2, expiring an old full also expires the differentials and WAL that depended on it, so the repository stays bounded without a separate prune job. Confirm the set at any time with pgbackrest --stanza=main info.

Restoring to a point in time

This is the part everyone configures and nobody rehearses. Say a bad migration ran at 14:32 and you want the database as it stood at 14:30. Stop PostgreSQL, then restore with --type=time and a target timestamp. The --delta flag lets pgBackRest restore in place by comparing checksums and rewriting only what differs, rather than requiring an empty data directory.

sudo systemctl stop postgresql@17-main

sudo -u postgres pgbackrest --stanza=main \
  --type=time \
  --target="2026-10-25 14:30:00+00" \
  --target-action=promote \
  --delta restore

sudo systemctl start postgresql@17-main

pgBackRest selects the newest backup taken before the target, restores it, and writes the recovery settings that replay archived WAL forward to exactly 14:30 UTC. --target-action=promote tells PostgreSQL to stop recovery at the target and open the cluster for writes, rather than pausing. Give the timestamp an explicit timezone offset — +00 here — so there is no ambiguity about which 14:30 you meant. Watch the PostgreSQL log after start-up for the recovery stopping before commit and recovery has paused/archive recovery complete lines that confirm it landed on your target.

A backup you have not restored is a hypothesis

The setup above is straightforward. The discipline is not. Schedule a restore to a scratch host on a real cadence — monthly is a reasonable floor — restore to a known timestamp, and check that a row you know existed at that moment is present and one written afterwards is not. That test is the only thing that turns a repository full of encrypted objects into a recovery-time number you can put in front of a board, and recovery-time engineering is exactly the discipline that separates firms that survive an incident from those that discover their gaps during the incident itself. While you are hardening the host, get the boring fundamentals right too — memory and swap configuration included — because a restore that fails on an out-of-memory kill at 3am is no restore at all.

Configure pgBackRest once, and you have backups. Rehearse the restore, and you have a recovery strategy. Only the second one is worth anything the day you need it.

Free interactive tool

Website compliance checklist

What your site has to do, based on what it actually does

Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.

Can you trust the architecture you have?

Architecture diagrams rarely show the reality of how systems actually operate. An independent review establishes what is really there.