Hero Image

PostgreSQL Streaming Replication on Gentoo Linux

PostgreSQL Streaming Replication on Gentoo Linux

This guide sets up a primary/standby streaming replication pair.

Prerequisites

Two servers running PostgreSQL on Gentoo Linux:

  • Primary: 192.168.1.10
  • Standby: 192.168.1.11

Step 1 – Configure the Primary

Edit /var/lib/postgres/data/postgresql.conf on the primary:

wal_level = replica
max_wal_senders = 5
wal_keep_size = 512MB
listen_addresses = '*'

Edit /var/lib/postgres/data/pg_hba.conf on the primary:

host replication replicator 192.168.1.11/32 scram-sha-256

Create the replication user:

CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'ReplPass123!';

Reload/restart the primary:

systemctl restart postgresql

Step 2 – Configure the Standby

Stop PostgreSQL on the standby and wipe the data directory:

systemctl stop postgresql
rm -rf /var/lib/pgsql/data/*   # adjust path for Gentoo Linux

Use pg_basebackup to clone the primary:

sudo -u postgres pg_basebackup -h 192.168.1.10 -U replicator \
    -D /var/lib/pgsql/data -P -R --wal-method=stream

The -R flag creates standby.signal and populates postgresql.auto.conf.

Step 3 – Start the Standby

systemctl start postgresql

Step 4 – Monitor replication

On the primary:

SELECT client_addr, state, sent_lsn, write_lsn, flush_lsn, replay_lsn
FROM pg_stat_replication;

On the standby:

SELECT now() - pg_last_xact_replay_timestamp() AS replication_lag;