Open Source

Patroni PostgreSQL HA

An open-source project that builds a highly available PostgreSQL cluster on Patroni, etcd, PgBouncer, and HAProxy with a single Ansible playbook run.

Apache License 2.0 · Open SourceGitHub Repository

Key Strengths

01

Automatic failover

When the leader node fails, a standby is promoted automatically within seconds, minimizing service disruption.

02

Split-brain prevention

Leaders are elected through etcd quorum (majority) consensus, eliminating the possibility of two leaders running at once.

03

Fixed virtual IP (VIP)

Applications connect through a VIP managed by Keepalived, so no configuration change is needed even after a failover.

04

Zero-downtime configuration

PostgreSQL settings are applied at runtime through Patroni and rolled out consistently across the cluster.

Components

Proven, standard open-source building blocks with clearly separated responsibilities at each layer.

PostgreSQL

The database itself. Leader and standbys are built on streaming replication (supports 14–17, default 16).

Patroni

The cluster conductor. Automates leader election, replication setup, and failover.

etcd

The cluster state store. Keeps leader information safe through quorum consensus.

PgBouncer

A lightweight connection pooler that brokers high connection volumes and reduces database load.

HAProxy

The traffic guide. Detects the current leader and routes read/write traffic to the right node.

Keepalived

The VIP manager. Maintains a single fixed entry point even during failures.

Architecture

The connection path flows from applications through the VIP, HAProxy, and PgBouncer to PostgreSQL, backed by etcd consensus.

Application
Always connects to the same VIP, even after failover
VIP:5000 · write
VIP:5001 · read
Fixed VIP
Keepalived assigns it to a live HAProxy node
HAProxy
Routes to the current leader via health checks (/primary · /replica)
/primary
/replica
Leader node
Read-write
PgBouncer · connection pooling
PostgreSQL leader
Replica node
Read-only
PgBouncer · connection pooling
PostgreSQL replica
Streaming replication
Patroni
Runs on every DB node — leader election and promote/demote control
Record · renew · watch the leader key
etcd cluster
Shared ledger · quorum of 3 — records and watches the leader key
etcd-1
etcd-2
etcd-3
Access layer
Keepalived · HAProxy
Fixed VIP endpoint
Leader/replica routing
Health-check switchover
Read load balancing
Pooling layer
PgBouncer
Connection pooling
Brokers high connection volume
Transaction pooling mode
Reduced database load
Data layer
PostgreSQL · Patroni
Streaming replication
Automatic leader election
Automatic failover
Runtime configuration
Consensus layer
etcd cluster
Cluster state storage
Quorum (majority) consensus
Split-brain prevention
3-node setup recommended
Recommended stack
PostgreSQL 14–17Patroni 3.xetcd 3.5.xPgBouncer 1.21+HAProxy 2.x+Ansible 2.15+

Deployment

The entire process is automated with Ansible playbooks, with two topologies to match your infrastructure.

Separated topology

Three dedicated etcd nodes plus two or more database nodes — suited to large, mission-critical environments.

Converged topology

All components co-located on three servers, delivering HA with minimal resources.

Ansible automation

Idempotent playbooks for Debian/Ubuntu and RHEL/Rocky that are safe to run repeatedly.

Get started on GitHub

The full playbooks and installation guide are open source and available in the repository.