PostgreSQL HA with Patroni and etcd

February 13, 2026 | PostgreSQL Patroni HA

Automated failover with 3-node Patroni+etcd.

Automated PostgreSQL HA with Patroni

Patroni is an open-source tool that manages PostgreSQL high availability with automatic failover. Combined with etcd for distributed consensus, it provides a production-ready HA cluster that promotes a standby to primary within seconds when the primary fails.

Architecture: 3-Node Patroni + etcd

  • Node 1: PostgreSQL primary + Patroni agent + etcd member
  • Node 2: PostgreSQL standby + Patroni agent + etcd member
  • Node 3: PostgreSQL standby + Patroni agent + etcd member
  • HAProxy: Routes traffic to the current leader automatically

Patroni Configuration

# patroni.yml
scope: postgres-cluster
name: node1

etcd3:
  hosts: 10.0.1.10:2379,10.0.1.11:2379,10.0.1.12:2379

bootstrap:
  dcs:
    ttl: 30
    loop_wait: 10
    retry_timeout: 10
    maximum_lag_on_failover: 1048576
    postgresql:
      use_pg_rewind: true
      parameters:
        max_connections: 200
        shared_buffers: 4GB
        wal_level: replica

  initdb:
    - encoding: UTF8
    - data-checksums

postgresql:
  listen: 0.0.0.0:5432
  connect_address: 10.0.1.10:5432
  data_dir: /var/lib/postgresql/16/main
  authentication:
    replication:
      username: replicator
      password: secure_password
    superuser:
      username: postgres
      password: secure_password

Automatic Failover Process

  1. Patroni detects the primary is unresponsive (via etcd leader key expiry)
  2. Remaining nodes hold an election based on replication lag and priority
  3. The winning standby is promoted to primary
  4. Other standbys are reconfigured to follow the new primary
  5. HAProxy health checks detect the new primary and route traffic

Testing Failover

# Trigger a manual switchover
patronictl -c /etc/patroni/patroni.yml switchover --master node1 --candidate node2

# Check cluster status
patronictl -c /etc/patroni/patroni.yml list

Eazy SaaS Tip: Always test failover in a staging environment before production. We recommend monthly failover drills to ensure your HA setup works correctly and your team knows the runbook.