Home/Engineer/Databases/PostgreSQL Streaming Replica…
DatabasesAdvancedlinuxpostgresqlreplicationha

PostgreSQL Streaming Replication — Primary/Replica High Availability Setup

Configure Primary–Replica high availability with PostgreSQL Streaming Replication, and learn WAL-based real-time synchronization and failover principles through hands-on practice.

Contents
  1. What Is PostgreSQL Streaming Replication?
  2. Environment
  3. Primary Server Configuration
  4. Replica Server Configuration
  5. Verify Replication Status
  6. Synchronous Replication (Zero Data Loss)
  7. Manual Failover (Promote Replica to Primary)
  8. Replication Slots — Prevent WAL Loss
  9. Monitoring Queries
  10. Summary

What Is PostgreSQL Streaming Replication?

PostgreSQL Streaming Replication is a built-in high-availability feature that keeps data in sync by streaming WAL (Write-Ahead Log) records from the Primary to Replica (Standby) servers in real time.

  • Hot Standby: Allows read-only queries on the Replica
  • Synchronous (sync) / asynchronous (async) replication modes
  • WAL-based: Transaction-level replication with minimal data loss

Environment

RoleIPOS
Primary192.168.1.10Ubuntu 22.04
Replica192.168.1.11Ubuntu 22.04

Both servers must run the same PostgreSQL version (17 in this guide).

Bash
#
sudo apt update && sudo apt install -y postgresql-17

Primary Server Configuration

1. Create a replication user

SQL
-- psql after
CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'StrongPass!23';

2. Edit postgresql.conf

Bash
sudo nano /etc/postgresql/17/main/postgresql.conf
INI
listen_addresses = '*'
wal_level = replica # WAL
max_wal_senders = 5 # when Replica ( settings)
wal_keep_size = 512 # WAL (MB)
synchronous_commit = on # when 'remote_apply'

3. pg_hba.conf — Allow Replica connections

Bash
sudo nano /etc/postgresql/17/main/pg_hba.conf
CODE
# Replica IP
host  replication  replicator  192.168.1.11/32  md5
Bash
sudo systemctl restart postgresql

Replica Server Configuration

1. Wipe the existing data directory and take a base backup

Bash
sudo systemctl stop postgresql
sudo rm -rf /var/lib/postgresql/17/main/*

# Primary backup
sudo -u postgres pg_basebackup   -h 192.168.1.10   -U replicator   -D /var/lib/postgresql/17/main   -Fp -Xs -P -R
# -R: standby.signal + recovery settings

2. postgresql.conf (Replica-specific options)

INI
hot_standby = on #
primary_conninfo = 'host=192.168.1.10 port=5432 user=replicator password=StrongPass!23'

When you use pg_basebackup -R, these settings are written automatically to postgresql.auto.conf.

3. Start the Replica

Bash
sudo systemctl start postgresql

Verify Replication Status

On the Primary

SQL
SELECT client_addr, state, sent_lsn, write_lsn, flush_lsn, replay_lsn, sync_state
FROM pg_stat_replication;
CODE
client_addr   | state     | sync_state
--------------+-----------+-----------
192.168.1.11  | streaming | async

On the Replica

SQL
SELECT * FROM pg_stat_wal_receiver;
-- status: streaming

Synchronous Replication (Zero Data Loss)

INI
# Primary postgresql.conf
synchronous_standby_names = 'replica1'
synchronous_commit = remote_apply
INI
# Replica postgresql.conf
application_name = replica1

In synchronous mode, Primary commits wait until the Replica acknowledges — this has a performance cost.


Manual Failover (Promote Replica to Primary)

If the Primary fails, promote the Replica to become the new Primary.

Bash
# Replica
sudo -u postgres pg_ctl promote -D /var/lib/postgresql/17/main
# or
sudo -u postgres psql -c "SELECT pg_promote();"

After promotion, point the application's database connection to the Replica IP.


Replication Slots — Prevent WAL Loss

Create a slot so WAL is not removed while the Replica is offline.

SQL
-- Primary
SELECT pg_create_physical_replication_slot('replica1_slot');
INI
# Replica postgresql.auto.conf
primary_slot_name = 'replica1_slot'

Caution: If the Replica stays offline for a long time, the Primary disk can fill up — monitoring is required.


Monitoring Queries

SQL
-- (lag) check
SELECT
  client_addr,
  pg_wal_lsn_diff(sent_lsn, replay_lsn) AS replay_lag_bytes,
  write_lag, flush_lag, replay_lag
FROM pg_stat_replication;

Summary

StepLocationKey setting
Enable WALPrimarywal_level = replica
Replication userPrimaryREPLICATION privilege
Base backupReplicapg_basebackup -R
Allow readsReplicahot_standby = on
FailoverReplicapg_promote()
#postgresql#replication#ha#streaming#failover
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Topic-related official docs (auto-linked · not per-claim sources)PostgreSQL documentation ↗

Questions & answers

Ask a question about this guide. We'll review and reply.