How to install the repmgr tool for easy management of Replication in PostgreSQL

In a previous article we saw how to set up PostgreSQL's Native Streaming Replication. This time we will see how to install and configure it. repmgr (Replication Manager), to automate cluster management and make easy switchovers. In this article we will see a step-by-step guide with the entire process for PostgreSQL 14 on Oracle Linux.
Installing repmgr
First we install the package repmgr at both nodes (dpgrpostgres1 and dpgrpostgres2):
curl https://dl.enterprisedb.com/default/release/get/14/rpm | sudo bash sudo yum install -y repmgr14
Alternatively, if the repository doesn't work, we can build it ourselves from github:
cd ~ rm -rf repmgr git clone https://github.com/2ndQuadrant/repmgr.git cd ~/repmgr ./configure make sudo make install
After the installation is complete, we check it by looking at the version:
repmgr --version
Again we make sure that the owner is the user postgres who is not a super user:
sudo mkdir -p /etc/repmgr/14 sudo chown -R postgres:postgres /etc/repmgr
Then we add the repmgr binaries to the bash profile path so we can call it more easily:
cd ~ echo 'export PATH=$PATH:/usr/pgsql-14/bin' >> ~/.bash_profile source ~/.bash_profile
The postgresql.conf and pg_hba.conf settings
In the Primary we open it postgresql.conf and set the following settings:
sudo -u postgres vi $PGDATA/postgresql.conf
listen_addresses = '*' wal_level = replica wal_log_hints = on max_wal_senders = 10 wal_keep_size = 1024 archive_mode = on #hot_standby = on #uncomment for read only replica.
We add login permission for the replication user to Primary in the pg_hba.conf:
sudo -u postgres vi $PGDATA/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD host repmgr repmgr 10.0.0.206/32 md5 host repmgr repmgr 10.0.0.9/32 md5 host replication repmgr 10.0.0.206/32 md5 host replication repmgr 10.0.0.9/32 md5
After making all these changes to Primary, we restart it:
sudo systemctl restart postgresql-14
Creating the repmgr user
To Primary node, we create the user and database for repmgr:
sudo -u postgres psql
CREATE ROLE repmgr WITH SUPERUSER LOGIN PASSWORD 'password'; CREATE DATABASE repmgr OWNER repmgr;
Then we need to create a file pgpass in the profile of each node so that we don't have to enter a password every time we run a command:
su postgres cd ~ vi .pgpass
10.0.0.9:5432:*:repmgr:password
10.0.0.206:5432:*:repmgr:password
localhost:5432:*:repmgr:password
chmod 600 .pgpass chown postgres:postgres .pgpass
The repmgr.conf configuration
We create the configuration file /etc/repmgr/14/repmgr.conf at each of the two nodes.
In Primary (dpgrpostgres1):
vi /etc/repmgr/14/repmgr.conf
node_id=1
node_name='dpgrpostgres1'
conninfo='host= 10.0.0.9 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/var/lib/pgsql/14/data'
pg_bindir='/usr/pgsql-14/bin'
ssh_options='-p 22 -o StrictHostKeyChecking=no'
On Standby (dpgrpostgres2):
vi /etc/repmgr/14/repmgr.conf
node_id=2
node_name='dpgrpostgres2'
conninfo='host=10.0.0.206 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/var/lib/pgsql/14/data'
pg_bindir='/usr/pgsql-14/bin'
ssh_options='-p 22 -o StrictHostKeyChecking=no'
Setting up two-way communication with SSH
On the Primary node, we create the key and display it so we can manually put it on Standby:
sudo -u postgres ssh-keygen -t rsa -N "" -f /var/lib/pgsql/.ssh/id_rsa sudo -u postgres cat /var/lib/pgsql/.ssh/id_rsa.pub
In Standby we run the following by putting the corresponding ssh-rsa key from the previous step:
sudo -u postgres mkdir -p /var/lib/pgsql/.ssh sudo -u postgres sh -c 'echo " ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA/EedjVl+61wgNfzYwrf3rvmEAOYp50/Q1P3R884R4DNWbdZwv7bn1wLiUmV/iD1SUIXiupKK4Y9nxCOXoIuieaS4ejT4weMlU1S1N25KYbBsZ3eH+LtHIxyeMTU478tCalguvEm5ldLIJdY/H4MNo2TNT3e/uBXN5szagp52kowjN5qukYxuzRPoYannz/wGwx/GO5pcMos0iZFpTsicVHW0nCXvr7PH+H/Lpt+D3OgCMBih1Y1vO2feRC0wHLD6MjVH0JR8razNgiYGbNLlXEANe5ksEgnashsHh0DETfi/SnogbAnSTiNs4bxGWKNAinpLBIRmRR69H/wpT/z3 postgres@dpgrpostgres2 " >> /var/lib/pgsql/.ssh/authorized_keys' sudo -u postgres chmod 700 /var/lib/pgsql/.ssh sudo -u postgres chmod 600 /var/lib/pgsql/.ssh/authorized_keys
We test from the Primary node that we are connecting to the Standby:
sudo su - postgres ssh postgres@10.0.0.206
If we see that it is playing correctly, we can also check the standby log (sudo tail -f /var/log/secure) we do the reverse process for the ssh connection from Standby to Primary.
Registering Nodes in the Cluster
First we register the Primary node:
su postgres repmgr -f /etc/repmgr/14/repmgr.conf primary register
Then, in the Standby node, stop the database if it is running, clean the data folder and use repmgr to clone directly from the Primary:
sudo systemctl stop postgresql-14 su postgres rm -rf $PGDATA/* repmgr -h 10.0.0.9 -U repmgr -d repmgr -f /etc/repmgr/14/repmgr.conf standby clone exit sudo systemctl start postgresql-14
Finally, we register the standby:
su postgres repmgr -f /etc/repmgr/14/repmgr.conf standby register
To check the status of the cluster at any time:
sudo -u postgres /usr/pgsql-14/bin/repmgr -f /etc/repmgr/14/repmgr.conf cluster show

How to do Manual Switchover with repmgr
With repmgr, role switching is done automatically without having to manually delete folders and recreate them. pg_basebackup.
If we want the current Standby (dpgrpostgres2) to become the new Primary, we execute by Standby the following command first as a test dry run:
su postgres repmgr -f /etc/repmgr/14/repmgr.conf standby switchover --dry-run
And if it doesn't show any problems, we run it normally:
repmgr -f /etc/repmgr/14/repmgr.conf standby switchover


