PostgreSQL Replication: Setting Up Warm Standby with WAL Shipping

PostgreSQL Replication: Setting Up Warm Standby with WAL Shipping

PostgreSQL's Write-Ahead Logging (WAL) mechanism provides a reliable foundation for replication by continuously shipping transaction logs from a primary server to one or more standby servers. Setting up warm standby replication ensures you have a ready-to-activate copy of your database for disaster recovery.

Configuring WAL-Based Replication

On the primary server, enable WAL archiving by setting archive_mode to on and configuring archive_command to copy WAL segments to a shared location accessible by the standby. Adjust wal_level to archive (or hot_standby in PostgreSQL 9.0+) and increase wal_keep_segments to retain enough WAL files during network interruptions.

On the standby server, create a recovery.conf file that specifies the restore_command for retrieving archived WAL segments. The standby continuously replays these segments, staying as close to the primary as the archiving interval allows. For tighter replication lag, combine WAL archiving with streaming replication introduced in PostgreSQL 9.0.

Test your failover procedure regularly by promoting the standby with pg_ctl promote or the trigger file mechanism. Document the exact steps for DNS changes, application reconfiguration, and client reconnection. A well-rehearsed failover procedure can mean the difference between minutes and hours of downtime during an actual disaster.

Back to Blog