Sub topic 3
Preparing the Primary for PostgreSQL Streaming Replication
Prerequisites and the primary's role
This chapter assumes you can edit postgresql.conf and pg_hba.conf, restart or reload PostgreSQL, use psql, and manage basic network access. You should also understand that a transaction becomes durable when PostgreSQL records its commit in the write-ahead log (WAL).
In physical streaming replication, the primary sends WAL records to a standby. The primary needs three things:
- WAL records detailed enough for physical replication.
- One or more sender processes for standby connections.
- An authenticated connection rule for a replication user.
The standby does not connect to a normal application database. It connects using the special database name replication.
Configure WAL and sender processes
Add settings like these to postgresql.conf on the primary:
listen_addresses = '*'
wal_level = replica
max_wal_senders = 10
wal_keep_size = '1GB'wal_level = replica tells PostgreSQL to generate the WAL information required for physical replication and WAL archiving. It must be set before the server starts. Changing it requires a restart.
max_wal_senders sets the maximum number of processes that can send WAL to standbys, backup tools, or other clients. Set it higher than the number of standbys you expect to connect at the same time. Leave room for tools such as pg_basebackup.
wal_keep_size requests that PostgreSQL retain at least this much recent WAL for streaming standbys. It is a safety margin, not a complete replacement for a replication slot or WAL archive. A standby that falls too far behind may still need archived WAL.
listen_addresses = '*' allows connections on all available network interfaces. A narrower address, such as the primary's private IP address, is safer when it meets your topology.
A configuration reload applies many settings, but wal_level requires a restart. Check the server's reported values rather than assuming that the file was accepted:
SHOW wal_level;
SHOW max_wal_senders;
SHOW listen_addresses;Create and authorize the replication role
Create a dedicated login role. The REPLICATION attribute allows the role to start a replication connection and use tools such as pg_basebackup.
CREATE ROLE replicator
WITH REPLICATION LOGIN
PASSWORD 'change-me';Use a unique, managed password in a real deployment. Do not place a production password in a shared script or commit it to source control.
Add a narrowly scoped rule to pg_hba.conf on the primary:
# Replication connections from the standby subnet
host replication replicator 10.0.0.0/24 scram-sha-256The fields mean:
| Field | Meaning |
|---|---|
| `host` | A TCP connection |
| `replication` | The special replication connection target |
| `replicator` | The permitted PostgreSQL role |
| `10.0.0.0/24` | The allowed client network |
| `scram-sha-256` | The password authentication method |
Place specific rules before broader rules because PostgreSQL uses the first matching rule. Reload pg_hba.conf after editing it. Confirm that the operating-system firewall also permits the standby's address to reach the PostgreSQL port.
Verify the connection path
The configuration path is:
Test the role and network path from the standby host with psql or pg_basebackup. A failed connection can result from the firewall, listen_addresses, the pg_hba.conf address range, the password, or a server that has not been restarted after changing wal_level.
Exercise
Why should maxwalsenders be larger than the number of standbys?
Show answer
A sender process is needed for each active streaming connection, and extra capacity may be needed for pg_basebackup or another replication client.