LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

pg_rewind

synchronize a PostgreSQL data directory with another copy

TLDR

Rewind target against a running source server
$ pg_rewind -D [target_dir] --source-server="host=[host] user=[rewind_user] dbname=[postgres]"
copy
Rewind against a locally shut-down source
$ pg_rewind -D [target_dir] --source-pgdata=[source_dir]
copy
Dry run (no changes written)
$ pg_rewind -n -D [target_dir] --source-server="[conninfo]"
copy
Rewind and write recovery configuration for standby mode
$ pg_rewind -R -D [target_dir] --source-server="[conninfo]"
copy
Show progress while copying data
$ pg_rewind -P -D [target_dir] --source-pgdata=[source_dir]
copy
Restore missing WAL files from the archive
$ pg_rewind -c -D [target_dir] --source-server="[conninfo]"
copy

SYNOPSIS

pg_rewind [option...] {-D | --target-pgdata} directory {--source-pgdata=directory | --source-server=connstr}

DESCRIPTION

pg_rewind resynchronizes a PostgreSQL data directory with another copy of the same cluster after their timelines have diverged. Typical use is to re-attach a former primary as a standby after a failover without taking a full base backup.It identifies the point where the timelines diverged, then copies from the source only the blocks that changed in the target after that point, along with all current configuration, WAL, and other required files. The source must have `walloghints` enabled or be initialized with data checksums, and `fullpagewrites` must be on.

PARAMETERS

-D, --target-pgdata dir

Target data directory to be modified. Must have been cleanly shut down.
--source-pgdata dir
File-system path of a cleanly shut-down source cluster.
--source-server connstr
libpq connection string for a running source server.
-n, --dry-run
Perform all work without modifying the target directory.
-N, --no-sync
Return without waiting for changes to be flushed to disk.
-P, --progress
Show progress while copying files.
-R, --write-recovery-conf
Create `standby.signal` and append connection settings to `postgresql.auto.conf`.
-c, --restore-target-wal
Use `restorecommand` to fetch WAL files missing from `pgwal`.
--config-file file
Main server configuration file for the target cluster.
--no-ensure-shutdown
Do not run single-user recovery; fail if the target is not cleanly shut down.
--sync-method method
Method used to flush changes: `fsync` (default) or `syncfs`.
--debug
Print verbose debugging output.
-V, --version
Print version information.
-?, --help
Show help.

CAVEATS

The target cluster must be cleanly shut down. The source must be on the same major version and share the same system identifier. Replication slots, statistics, and contents of `pgdynshmem` are not copied. Always take a backup of the target before running pgrewind on production data.

HISTORY

Introduced in PostgreSQL 9.5 (2016) by Heikki Linnakangas, and moved into the core distribution from a contrib module. In PostgreSQL 13 pg_rewind gained the ability to write recovery configuration (`-R`) and restore missing WAL from the archive (`-c`).

SEE ALSO

Copied to clipboard
Kai