Skip to content

WAL-G Continuous Backup and PITR for Self-Managed PostgreSQL

Ship PostgreSQL WAL and base backups to S3-compatible object storage with WAL-G, then restore a self-managed cluster to an exact point in time.

If you run PostgreSQL yourself and want backups that live in object storage instead of on a fragile local volume — plus the ability to restore to the second before a bad DELETE — WAL-G is the tool this guide walks through end to end. WAL-G streams base backups and closed write-ahead-log (WAL) segments straight to an S3-compatible bucket, and it fetches them back the same way. You wire it into PostgreSQL’s archive_command, take a base backup with wal-g backup-push, and when disaster strikes you lay the backup back down with wal-g backup-fetch, point restore_command at wal-g wal-fetch, set recovery_target_time, and let the server replay WAL forward to that exact moment. This works on self-managed PostgreSQL 15, 16, and 17. The configuration is short; the part that actually saves you is rehearsing the restore, so we do that here on a scratch data directory before you ever need it for real.

What you’ll accomplish

  • Install WAL-G and point it at an S3-compatible bucket with the right credentials and prefix.
  • Turn on continuous WAL archiving so every closed segment lands in object storage automatically.
  • Take a verified base backup and confirm it with wal-g backup-list.
  • Run a full point-in-time restore drill on a scratch data directory, stopping at a chosen recovery_target_time.
  • Confirm the restored cluster reached the target and left recovery cleanly.

Prerequisites

  • Self-managed PostgreSQL 15, 16, or 17. You need control of postgresql.conf and the ability to restart the server. This does not apply to managed Postgres — RDS, Cloud SQL, Azure Database, Supabase, and similar hide archive_command and the data directory, so WAL-G cannot hook in. Those platforms have their own PITR; use it.
  • Shell access as a superuser and as the postgres OS user. WAL-G runs as the same user that owns the data directory.
  • An S3-compatible bucket and credentials. Real AWS S3 works, and so does MinIO, Backblaze B2, Wasabi, Cloudflare R2, or any provider that speaks the S3 API. You need the bucket name, an access key and secret, the region, and — for anything other than AWS — the endpoint URL.
  • wal_level = replica (the default). WAL-G base backups use the same replication protocol as pg_basebackup, so replica is enough; you do not need logical.
  • Enough retention headroom in the bucket. WAL accumulates continuously between base backups. Budget storage and set a retention policy before you turn archiving on.

How it works

WAL-G plugs into two PostgreSQL mechanisms. First, archive_command: every time PostgreSQL finishes a 16 MB WAL segment, it calls your command, and WAL-G’s wal-push uploads that segment to the bucket. Second, the base backup: wal-g backup-push reads the whole data directory through the replication protocol, compresses and streams it to the same bucket, and records the WAL position where the backup began. Together they give you a full physical copy plus the continuous change stream needed to roll forward from it.

Diagram of WAL-G shipping base backups and WAL segments from a primary PostgreSQL server to S3-compatible object storage, then fetching them back to restore a recovered server to a recovery_target_time

Restore reverses the flow. wal-g backup-fetch pulls a chosen base backup (usually LATEST) back onto an empty data directory. You then set restore_command to wal-g wal-fetch, which downloads WAL segments on demand as the server asks for them, drop a recovery.signal file into the data directory to put PostgreSQL into recovery mode, and set recovery_target_time to the instant you want. On start, PostgreSQL replays WAL forward from the backup’s start position until it reaches the target, then pauses. On PostgreSQL 15 and later the old recovery.conf is gone — recovery settings go in postgresql.conf or postgresql.auto.conf, and the presence of recovery.signal is what triggers recovery. For the standalone PostgreSQL mechanics without WAL-G, see PITR with WAL archiving; for the base-backup fundamentals, see pg_basebackup physical backup.

Step-by-step

1. Install the WAL-G binary. Upstream publishes static binaries per PostgreSQL major version. Match the build to your server’s major version and drop it in the path.

curl -fsSL -o /tmp/wal-g.tar.gz \
  https://github.com/wal-g/wal-g/releases/download/v3.0.5/wal-g-pg-ubuntu-22.04-amd64.tar.gz
tar xf /tmp/wal-g.tar.gz -C /usr/local/bin/
mv /usr/local/bin/wal-g-pg-ubuntu-22.04-amd64 /usr/local/bin/wal-g
/usr/local/bin/wal-g --version

Success looks like a version string:

wal-g version v3.0.5	...	PostgreSQL

2. Configure WAL-G’s environment. WAL-G reads its settings from environment variables. Put them in a file the postgres user sources so every invocation — including the one PostgreSQL fires for archive_command — sees the same config. For AWS S3, WALG_S3_PREFIX plus the standard AWS_* keys is all you need. For any other S3-compatible provider, add AWS_ENDPOINT and force path-style addressing.

# /etc/wal-g.d/env  — readable only by the postgres user
export WALG_S3_PREFIX="s3://acme-pg-backups/prod"
export AWS_ACCESS_KEY_ID="AKIAEXAMPLE"
export AWS_SECRET_ACCESS_KEY="wJalrXUtnEXAMPLEKEY"
export AWS_REGION="us-east-1"
# For non-AWS S3-compatible storage (MinIO, R2, Wasabi, B2):
export AWS_ENDPOINT="https://s3.us-east-1.example.com"
export AWS_S3_FORCE_PATH_STYLE="true"
# Compression and how PostgreSQL reaches the running server:
export WALG_COMPRESSION_METHOD="brotli"
export PGHOST="/var/run/postgresql"

Lock the file down and confirm WAL-G can see the bucket:

chown postgres:postgres /etc/wal-g.d/env
chmod 600 /etc/wal-g.d/env
sudo -u postgres bash -c 'source /etc/wal-g.d/env && wal-g backup-list'

On an empty prefix this prints only a header (name modified wal_segment_backup_start) and no rows — that is the expected success signal that credentials and the endpoint resolve.

3. Turn on archiving and restart. Set archive_mode and an archive_command that sources the env and calls wal-push. The %p placeholder is the path to the segment PostgreSQL wants shipped.

# postgresql.conf
wal_level = replica
archive_mode = on
archive_command = 'bash -c "source /etc/wal-g.d/env && wal-g wal-push %p"'

archive_mode needs a full restart, not a reload:

sudo systemctl restart postgresql
sudo -u postgres psql -c "SHOW archive_command;"

Force a segment switch and confirm archiving succeeds:

sudo -u postgres psql -c "SELECT pg_switch_wal();"
sudo -u postgres psql -c "SELECT last_archived_wal, last_failed_wal FROM pg_stat_archiver;"

A populated last_archived_wal and a null last_failed_wal mean segments are reaching the bucket.

4. Take the first base backup. Run backup-push against the data directory. WAL-G streams it to the bucket and records the starting LSN.

sudo -u postgres bash -c 'source /etc/wal-g.d/env && wal-g backup-push /var/lib/postgresql/16/main'

Success ends with a log line naming the backup:

INFO: Finished writing part ...
INFO: Backup sentinel: {"BackupName":"base_000000010000000000000007",...}

5. Verify the backup is listed. Ask WAL-G what it stored.

sudo -u postgres bash -c 'source /etc/wal-g.d/env && wal-g backup-list --detail'

You should see one row with a base_... name, its start time, and the WAL segment it began at. That row is what backup-fetch LATEST will pull.

6. Restore drill: fetch the base backup to a scratch directory. Never rehearse on your live data directory. Create an empty one, fetch LATEST into it, and fix ownership.

sudo install -d -o postgres -g postgres -m 700 /var/lib/postgresql/16/restore_test
sudo -u postgres bash -c 'source /etc/wal-g.d/env && \
  wal-g backup-fetch /var/lib/postgresql/16/restore_test LATEST'

WAL-G reports the files it restored and exits 0. The directory now holds the base backup, but PostgreSQL has not yet replayed any WAL.

7. Set recovery targets and start the scratch cluster. Write the recovery settings, drop recovery.signal, and start a second server instance on a spare port. restore_command calls wal-fetch with %f (the segment name PostgreSQL wants) and %p (where to place it).

sudo -u postgres bash -c 'cat >> /var/lib/postgresql/16/restore_test/postgresql.conf <<EOF
port = 5433
restore_command = '"'"'bash -c "source /etc/wal-g.d/env && wal-g wal-fetch %f %p"'"'"'
recovery_target_time = '"'"'2026-07-26 14:30:00+00'"'"'
recovery_target_action = '"'"'promote'"'"'
EOF'
sudo -u postgres touch /var/lib/postgresql/16/restore_test/recovery.signal
sudo -u postgres /usr/lib/postgresql/16/bin/pg_ctl \
  -D /var/lib/postgresql/16/restore_test -l /tmp/restore_test.log start

8. Watch it reach the target. Tail the log until recovery announces it stopped at the target and promoted.

grep -E "recovery stopping|consistent recovery state|database system is ready" /tmp/restore_test.log

The success signals are recovery stopping before commit of transaction ... (or recovery stopping at ...) followed by database system is ready to accept connections. With recovery_target_action = promote, the server finishes recovery and comes up read-write.

Validate it works

Confirm the restored cluster is a live, promoted server and that it stopped where you asked:

sudo -u postgres psql -p 5433 -c "SELECT pg_is_in_recovery();"

f (false) means recovery finished and the cluster was promoted. Then check the data itself — query a row you know existed at 14:30 and one you know was written after, confirming the later write is absent:

sudo -u postgres psql -p 5433 -d appdb \
  -c "SELECT max(created_at) FROM orders;"

The newest created_at should sit at or before your recovery_target_time, never after. Finally, check the WAL archive itself for gaps across the timeline:

sudo -u postgres bash -c 'source /etc/wal-g.d/env && wal-g wal-verify integrity timeline'

An OK integrity status with no missing segments means the archive is continuous and future restores can roll forward without a hole. When the drill is done, stop and remove the scratch cluster so it does not keep archiving or hold a port.

Troubleshooting

  • archive_command failing and pg_wal filling up. If pg_stat_archiver shows a rising last_failed_wal, PostgreSQL keeps every unshipped segment on disk and the volume fills. Run the exact command by hand as postgres (source /etc/wal-g.d/env && wal-g wal-push /path/to/a/segment) and read the error. Most often it is a credential or endpoint problem, or the env file is not readable by postgres. Fix it, and PostgreSQL retries the backlog automatically.
  • Wrong WALG_S3_PREFIX or credentials. A prefix typo silently writes to — or reads from — the wrong path, so backup-list looks empty or a restore finds nothing. Confirm the prefix matches on both the archiving and the restoring host, and that both use the same AWS_ENDPOINT. For non-AWS providers, a missing AWS_S3_FORCE_PATH_STYLE=true is a common cause of bucket-not-found errors.
  • Missing WAL segment gap during restore. If recovery stops with requested WAL segment ... has already been removed or cannot find a segment, a gap exists between your base backup and your target time — usually because archiving failed silently for a stretch. wal-g wal-verify integrity timeline locates the gap. You can only recover to a point covered by an unbroken WAL chain from a base backup.
  • Restore never reaches the target. If recovery_target_time is earlier than the base backup’s own consistency point, PostgreSQL cannot go back that far and will error or stop at the earliest consistent point. Pick a target that is after the base backup you fetched. When in doubt, fetch an older backup whose start time precedes your target.
  • Storage permission errors. AccessDenied on push or fetch means the S3 key lacks s3:PutObject/s3:GetObject/s3:ListBucket on the prefix. Grant those on arn:aws:s3:::acme-pg-backups/prod/* (and ListBucket on the bucket) and retry.

When this is the wrong approach

  • Managed PostgreSQL. If you are on RDS, Cloud SQL, Azure Database, or Supabase, you cannot set archive_command or reach the data directory. Use the platform’s built-in PITR instead.
  • A tiny single database where pg_dump is enough. For one small database with a generous recovery-point objective, a nightly pg_dump is simpler to run and restore, and it survives major-version moves. WAL-G earns its complexity when the data is large, the RPO is tight, or the cluster runs on ephemeral storage.
  • When you cannot rehearse restores. A backup you have never restored is a hope, not a backup. If you have no environment to run the restore drill in, fix that first — the archive is only as good as your last successful restore test.

For the full WAL-G command set see the WAL-G documentation, and for the PostgreSQL mechanism underneath it see the continuous archiving documentation.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.