Skip to content
Latchkey

pg_basebackup: Usage, Options & Common CI Errors

pg_basebackup copies an entire running PostgreSQL cluster at the file level.

pg_basebackup creates the physical backups used to seed replicas or restore an entire cluster. Unlike pg_dump it copies the whole data directory and needs replication privileges.

What it does

pg_basebackup connects to a running server over the replication protocol and produces a binary copy of the entire database cluster - all databases at once - suitable for point-in-time recovery or standby setup. It is physical, not logical like pg_dump.

Common usage

Terminal
pg_basebackup -h primary -U repl -D ./backup -Fp -Xs -P
pg_basebackup -h primary -U repl -D ./backup.tar -Ft -z   # tar + gzip
pg_basebackup -h db -U repl -D ./standby -R   # write standby config
PGPASSWORD=secret pg_basebackup -h primary -U repl -D ./b -c fast

Options

FlagWhat it does
-D <dir>Destination directory
-F p|tOutput format: plain / tar
-X s|fInclude WAL via stream / fetch
-z / -Zgzip-compress tar output
-RWrite standby.signal + connection config
-PShow progress

Common errors in CI

FATAL: no pg_hba.conf entry for replication connection from host ... - the server is not configured to allow replication from the runner; add a replication entry and a role with the REPLICATION attribute. "FATAL: number of requested standby connections exceeds max_wal_senders" means max_wal_senders is too low. The destination directory must be empty; otherwise pg_basebackup errors that it is not empty.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →