PostgreSQL High Availability with Patroni, etcd & HAProxy
Build a 3-node PostgreSQL cluster with automatic failover using Patroni, etcd and HAProxy.
Overview & Architecture
PostgreSQL is famously reliable, but out of the box it has no built-in automatic failover. If the...
Environment & Prerequisites
Servers You need four Linux servers (Ubuntu 24.04 LTS or 26.04 — this guide uses the PGDG reposit...
etcd — the Distributed Configuration Store
etcd is the distributed configuration store (DCS) that Patroni uses to elect a leader and hold cl...
PostgreSQL 18 & Patroni — Install & Configure
Run everything on this page on the three database nodes — pg-sv01, pg-sv02, pg-sv03. Install Post...
Bootstrapping the Cluster
With Patroni configured on all three nodes, we bring the cluster up. The order matters: the first...
HAProxy — Connection Routing
Clients shouldn't connect to a specific database node — that node might be a replica today and go...
Failover & Recovery Testing
Now the payoff: we kill the primary and watch the cluster heal itself with no human intervention....
Production Hardening & Troubleshooting
The cluster we built is complete and self-healing, but a few choices were made to keep the lab to...