pg_rewind needs credentials and a recovery plan
pg_rewind is a recovery tool for a PostgreSQL cluster whose timeline has diverged from a new source. It can make an old primary usable as a standby, but it does not make the old data harmless. The command compares timeline histories, finds the divergence point, and changes the target to match the source while preserving the records needed for recovery.
The PostgreSQL documentation requires the target server to be shut down cleanly before the command runs. It also requires WAL in the target's pg_wal directory reaching back to the divergence point. If the needed history has been recycled, the command cannot reconstruct it. That is why a backup and a tested WAL-retention policy matter before any failover drill.
The source connection is a normal database connection, not a replication connection. It needs a role with enough permission to execute the functions used by pg_rewind, or a superuser role. That creates a credential path to a production server. Do not copy a superuser password into an operator shell for convenience. Create a dedicated role, restrict it to the required functions, require TLS with a verified server identity, and keep the credential in a controlled secret store.
The recovery decision has data consequences. Files that diverged are copied from the source or removed from the target. Non-WAL-logged changes may not be handled the way an operator expects. A target with configuration files or local artifacts that were edited during the incident may need those changes reviewed separately after the rewind. Capture the old postgresql.conf, postgresql.auto.conf, role changes, and extension state before the command.
Run the operation from an isolated administration host. Restrict the source database port to that host and the database cluster. Record the source target, timeline IDs, start and end time, operator, and command version. Test the result by starting the target as a standby, checking recovery progress, and verifying that it can read from the source. Do not promote it until replication and application checks pass.
A failover exercise should include a case where the command cannot run because the required WAL is gone. The team should know whether to restore a base backup, rebuild from a snapshot, or continue with another recovery method. That decision must be made before an outage, not after the old primary has been modified.
Finally, rotate the credential after the recovery window and review the source role. A temporary role that remains active after the drill is an unmanaged database account. Remove it, revoke its certificate or password, and confirm the old target cannot use it.
pg_rewind is useful because it understands PostgreSQL timelines. Operators still have to understand authority, retention, and the difference between a recovered replica and a trusted database.
Top comments (0)