PostgreSQL on Debian supports several replication technologies to achieve high availability, data redundancy, and workload distribution. Below are the primary methods, their configurations, and use cases:
Overview: A block-level, instance-wide replication method that copies WAL (Write-Ahead Logging) files from a primary (master) server to standby (slave) servers. The standby server replays WALs to stay synchronized with the primary. It is ideal for high availability and disaster recovery.
Configuration Steps:
On the Primary Server:
postgresql.conf
to enable replication:listen_addresses = '*' # Allow connections from all IPs
wal_level = replica # Enable WAL archiving for replication
max_wal_senders = 10 # Allow up to 10 replication clients
wal_keep_segments = 64 # Retain 64 WAL segments (adjust as needed)
hot_standby = on # Enable read-only queries on standby (optional)
pg_hba.conf
to allow standby connections:host replication replicator <standby_ip>/32 md5
sudo systemctl restart postgresql
.CREATE USER replicator WITH REPLICATION PASSWORD 'secure_password' LOGIN;
On the Standby Server:
pg_basebackup
to create a base backup from the primary:pg_basebackup -h <primary_ip> -D /var/lib/postgresql/<version>/main -U replicator -P -R
The -R
flag auto-generates a recovery.conf
file (PostgreSQL 12+ uses postgresql.auto.conf
).sudo systemctl start postgresql
.Verification:
Check replication status on the primary:
SELECT * FROM pg_stat_replication;
This shows connected standbys and their replication lag.
Overview: A table-level, selective replication method that decodes WAL logs into logical changes (e.g., INSERTs/UPDATEs/DELETEs) and applies them to subscribers. It supports cross-version sync, partial table replication, and is ideal for reporting databases or migrating data between versions.
Configuration Steps:
On the Publisher (Primary):
postgresql.conf
to enable logical replication:wal_level = logical # Required for logical decoding
max_replication_slots = 10 # Slots for subscribers (>= number of subscriptions)
max_wal_senders = 10 # Allow WAL senders for subscribers
CREATE PUBLICATION my_pub FOR TABLE my_table; -- Single table
-- OR for all tables:
CREATE PUBLICATION my_pub FOR ALL TABLES;
On the Subscriber (Standby):
postgresql.conf
allows replication:max_replication_slots = 10 # Match publisher’s max_replication_slots
max_logical_replication_workers = 10 # Workers for applying changes
CREATE SUBSCRIPTION my_sub
CONNECTION 'host=<primary_ip> dbname=my_db user=replicator password=secure_password'
PUBLICATION my_pub;
Key Notes:
FULL
for full-row replication).Overview: Tools that simplify replication management, add high availability, or support complex topologies. Common tools include:
Each method has trade-offs in complexity, performance, and flexibility. Select based on your workload (e.g., OLTP vs. OLAP), high availability requirements, and operational expertise.