For those who seek help in different areas of software and hardware platform.

How To Create a Highly Available PostgreSQL Cluster on CentOS/RHEL 7

This step by step guide will show you how to set up a highly available PostgreSQL cluster using Patroni and HAProxy on CentOS/RHEL 7. These steps can also be applied (slight changes may be required) if you are running an earlier release of CentOS or RHEL 6.


To follow this tutorial, you will need 4 (physical or virtual) machines with CentOS Linux release 7.7.1908 server with minimal installed, having sudo non-root user privileges. We have prepared the following 4 machines for this guide. However, if you wish you can add up more machines in your cluster environment.

node1192.168.10.1Postgresql, Patroni
node2192.168.10.2Postgresql, Patroni


When you are ready with the above prerequisites, you can move to the below steps.


Installing PostgreSQL

In this step, we will install postgresql on two of the nodes (node1 and node2) one by one using the below command:
yum -y install epel-release
yum -y install
We are installing postgresql version 12. If you wish you can install any other postgresql version of your choice:
yum -y install postgresql12-server postgresql12 postgresql12-devel

Installing Patroni

Patroni is an open-source python package that manages Postgres configuration. It can be configured to handle tasks like replication, backups, and restorations.

We will install patroni on (node1, node2) with below command:
yum -y install
Repeat these steps on remaining nodes (node2 in our case) as well. When you are finished with the above on each node (designated for postgresql and patroni), you can move to the next step.

Configuring Patroni

Patroni can be configured using a YAML file which is by default located under /opt/app/patroni/etc/.

You need to copy /opt/app/patroni/etc/postgresql.yml.sample file on (node1) with below command:
cp -p /opt/app/patroni/etc/postgresql.yml.sample /opt/app/patroni/etc/postgresql.yml
Now edit /opt/app/patroni/etc/postgresql.yml file with your preferred text editor i.e. vi, nano, etc
vi /opt/app/patroni/etc/postgresql.yml
Remove everything from this file and add below configuration parameters. Change name to something unique, and change listen and connect_address (under postgresql and restapi) to the appropriate values on each node.
scope: postgres
namespace: /pg_cluster/
name: node1



    ttl: 30
    loop_wait: 10
    retry_timeout: 10
    maximum_lag_on_failover: 1048576
      use_pg_rewind: true
      use_slots: true

  - encoding: UTF8
  - data-checksums

  - host replication replicator md5
  - host replication replicator md5
  - host replication replicator md5
  - host all all md5

      password: admin
        - createrole
        - createdb

  data_dir: /var/lib/pgsql/12/data
  bin_dir: /usr/pgsql-12/bin
  pgpass: /tmp/pgpass
      username: replicator
      password: reppassword
      username: postgres
      password: postgrespassword

    nofailover: false
    noloadbalance: false
    clonefrom: false
    nosync: false
Save and close the file when you are finished.

You can see the below screenshot of /opt/app/patroni/etc/postgresql.yml from node1 in our setup:

and below screenshot of /opt/app/patroni/etc/postgresql.yml from node2 in our setup:

Installing etcd

Etcd is a fault-tolerant, distributed key-value store that is used to store the state of the Postgres cluster. Using Patroni, all of the Postgres nodes make use of etcd to keep the Postgres cluster up and running. 

For the sake of this guide, we will use a single-server etcd cluster. However, in production, it may be best to use a larger etcd cluster so that one etcd node fails, it doesn’t affect your Postgres servers. 

Type below command to install etcd on a node that is designated for (etcd in our case):
yum -y install etcd

Configuring etcd

At this point, you need to edit the /etc/etcd/etcd.conf file like below:
vi /etc/etcd/etcd.conf
update, replace the following highlighted parameters to reflect yours:
Save and close the file when you are finished.

At this point, start the etcd service with below command:
systemctl enable etcd
systemctl start etcd
Verify the etcd status with below command:
systemctl status etcd

If etcd service failed to start, reboot your machine.

Next, start patroni service on your first node (node1 in our case) with below command:
systemctl enable patroni
systemctl start patroni
Verify the Patroni status with below command:
systemctl status patroni
If everything is set up correctly, the output from the node1 (master) will look similar to like below:

When starting patroni on subsequent nodes, (node2 in our case) the output will look similar to like below:

Make sure you have performed these steps on each of the nodes with Postgres and Patroni installed (node1, node2 in our case).

Installing HAProxy

When developing an application that uses a database, it can be cumbersome to keep track of the database endpoints if they keep changing. Using HAProxy simplifies this by giving a single endpoint to which you can connect the application.

HAProxy forwards the connection to whichever node is currently the master. It does this using a REST endpoint that Patroni provides. Patroni ensures that, at any given time, only the master Postgres node will appear as online, forcing HAProxy to connect to the correct node.

Type the below command to install HAProxy on (node4 in our case):
yum -y install haproxy

Configuring HAProxy

With the Postgres cluster set up, you need a method to connect to the master regardless of which of the servers in the cluster is the master. This is where HAProxy steps in. All Postgres clients (your applications, psql, etc.) will connect to HAProxy which will make sure you connect to the master in the cluster.

You need to edit /etc/haproxy/haproxy.cfg like below:
vi /etc/haproxy/haproxy.cfg
Remove everything from this file, add and update following configuration parameters to reflect yours:
        maxconn 100
        log local2

        log global
        mode tcp
        retries 2
        timeout client 30m
        timeout connect 4s
        timeout server 30m
        timeout check 5s

listen stats
    mode http
    bind *:7000
    stats enable
    stats uri /

listen postgres
    bind *:5000
    option httpchk
    http-check expect status 200
    default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
    server node1 maxconn 100 check port 8008
    server node2 maxconn 100 check port 8008
Save and close the file when you are finished.

If HAProxy service is already running, you can simply restart with systemctl restart haproxy command otherwise start HAProxy service with below command:
systemctl start haproxy
systemctl enable haproxy
systemctl status haproxy
If everything goes well, you will see the output similar to like below:

If HAProxy fails to start, check for configuration file syntax errors with below command:
haproxy -c -V -f /etc/haproxy/haproxy.cfg

Testing Postgres HA Cluster Setup

Connect Postgres clients to the HAProxy IP address of the node on which you installed HAProxy (in this guide, on port 5000.

You can also access HAProxy node on port 7000 using any of your preferred web browsers to see the HAProxy dashboard like below: 

As you can see, the postgresql_192.168.10.1_5432 row is highlighted in green. This indicates that is currently acting as the master.

If you kill the primary node (using sudo systemctl stop patroni or by completely shutting down the server), the dashboard will look similar to like below: 

In the Postgres section, the postgresql_192.168.10.1_5432 row is now red and the postgresql_192.168.10.2_5432 row is highlighted in green. This indicates that is currently acting as the master. 

Note: In this case, it just so happens that the second Postgres server is promoted to master. This might not always be the case if you have more than two nodes in the cluster. It is equally likely that the third, fourth or fifth server may be promoted to master. 

When you bring up the first server, it will rejoin the cluster as a slave and will sync up with the master.

Wrapping up 

You now have a robust, highly available Postgres cluster ready for use. While the setup in this tutorial should go far in making your Postgres deployment highly available, here are a few more steps you can take to improve it further: 

  • Use a larger etcd cluster to improve availability.
  • Use PgBouncer to pool connections. 
  • Add another HAProxy server and configure IP failover to create a highly available HAProxy cluster.


Post a Comment

Comments with links will not be published.

Video Tutorials