pg_basebackup(1)
Take a base backup of a PostgreSQL cluster without connecting to the database server.
Synopsis
pg_basebackup [OPTION]... [DIRECTORY]Description
pg_basebackup is used to take base backups of a running PostgreSQL database cluster. The backup is performed without blocking other clients from accessing the database, and automatically incorporates all changes to the database cluster made during the backup.
The backup must be taken from a superuser or a role with the appropriate replication privileges. A connection to the server in recovery mode is not supported.
The resulting backup can be used to set up a new standby server or restore a database cluster to a previous state.
Common options
| Flag | What it does |
|---|---|
-D, --pgdata=DIRECTORY | Write the output to the specified directory; required unless -F tar is used |
-F, --format=plain|tar | Output format: plain (default, directory structure) or tar (tar archives) |
-h, --host=HOSTNAME | Server host or socket directory; defaults to local socket |
-U, --username=USERNAME | PostgreSQL user for connection; defaults to current OS user |
-P, --progress | Display progress information during backup |
-v, --verbose | Enable verbose output |
-R, --write-recovery-conf | Write recovery.conf file for use as a standby server |
-X, --wal-method=fetch|stream | Include WAL: fetch (copy after backup) or stream (stream during backup) |
-z, --gzip | Compress tar output with gzip; implies -F tar |
-Z, --compress=0-9 | Compress tar output with specified level (0-9); implies -F tar |
-c, --checkpoint=fast|spread | Set checkpoint mode: fast (immediate) or spread (default, over time) |
--max-rate=RATE | Limit transfer rate to RATE KB/s |
Examples
Take a plain backup to /var/lib/postgresql/backup using the replication user
pg_basebackup -D /var/lib/postgresql/backup -U replication -h localhostCreate a tar backup and pipe through gzip to compress, connecting to remote host
pg_basebackup -D /backup -F tar -h db.example.com -U postgres | gzip > backup.tar.gzTake backup with recovery.conf for standby setup and stream WAL during backup
pg_basebackup -D /var/lib/postgresql/backup -U replication -R -X streamBackup with progress display, verbose output, and rate-limited to 10 MB/s
pg_basebackup -D /backup -U replication -P -v --max-rate=10000Create a compressed tar archive backup from a remote PostgreSQL server
pg_basebackup -F tar -z -D /backup -U replication -h 192.168.1.50Take backup with fast checkpoint and fetch WAL segments after backup completes
pg_basebackup -D /backup -U replication -c fast -X fetch