Streaming replication is a common PostgreSQL high-availability setup, consisting of a primary server (read-write) and one or more standby replicas (read-only). However, a key limitation of streaming replication is that it doesn’t automatically failover to a replica if the primary server goes down, requiring manual intervention from a DBA to promote the replica to read-write mode. This also means the setup needs constant monitoring. This is where repmgr comes into play, addressing this critical issue by automating failover to a replica in the event of a primary server failure, eliminating the need for DBA intervention
Installing repmgr and PostgreSQL
The setup below will be performed on two VMs running Ubuntu 22, with PostgreSQL 16 being used for the configuration.
install repmgr on both primary and secondary server
sudo apt install repmgr
configuring primary server
On the primary server, some parameters need to be adjusted in the postgresql.conf file, which can be found in /etc/postgresql/16/main. Below are the parameters that need to be edited
sudo nano /etc/postgresql/16/main/postgresql.conf
listen_addresses = ‘*’ max_wal_senders = 10 max_replication_slots = 10 wal_level = ‘hot_standby’ or ‘replica’ or ‘logical’ hot_standby = on archive_mode = on archive_command = ‘/bin/true’ shared_preload_libraries = ‘repmgr’
On the primary server, create another superuser called repmgr and create a database assigned to the repmgr user. This database will be used by repmgr for storing statistics.
create user repmgr; ALTER USER repmgr WITH SUPERUSER; create database repmgr with owner repmgr;
next update the pg_hba.conf file with connection allowed in both ipv4 and replication section as follow