Patroni: Automatic Postgres Failover Without a Cloud Provider
Template-driven high availability for Postgres, built on the same consensus idea as etcd

Contents
Managed Postgres on the big cloud platforms sells one feature above everything else: when the primary database dies, something else takes over automatically, usually inside a minute, without a human being paged first. Running Postgres yourself has traditionally meant giving that up — a primary fails, and someone has to notice, promote a replica by hand, and repoint every application’s connection string before anything writes again. Patroni closes most of that gap for self-managed Postgres, and it does it with the same consensus trick etcd uses to decide who’s in charge of a cluster: a distributed lock that only one node can hold at a time. Before reaching for a cluster at all, it is worth checking the arithmetic in Your Startup Does Not Have a Big Data Problem, which shows how much a single Postgres server handles.
What Patroni actually is
Patroni is not a replacement for Postgres replication — it still relies on native Postgres streaming replication to keep replicas current. What Patroni adds is the decision-making layer on top: a small Python daemon that runs alongside each Postgres instance, continuously checks the instance’s health, and coordinates with its peers through a distributed configuration store (etcd, Consul, ZooKeeper, or plain Kubernetes API objects all work) to agree on which node currently holds the “leader lock.” Whichever Patroni instance holds that lock runs its local Postgres as the read-write primary; every other instance Patroni manages is kept as a streaming replica, and Patroni actively prevents them from being promoted by hand or by accident.
The honest way to describe this is: Patroni is a leader-election wrapper around Postgres. That framing matters when debugging — most Patroni problems trace back to either the distributed store it depends on (an unreachable etcd cluster means Patroni can’t tell who should be primary) or to Postgres’s own replication state (a replica too far behind to safely promote), not to anything Patroni’s own code is doing wrong.

How failover actually happens
The Patroni instance that holds the leader lock does so via a lease with a TTL (30 seconds by default) and has to keep renewing it. If the primary’s Patroni process can’t renew the lease (because the node is down, the network is partitioned, or Postgres itself has wedged), the lease expires and the other nodes notice within that TTL window. At that point the healthiest remaining replica — the one with the most caught-up replication position — acquires the lock and promotes itself to primary.
“Healthiest” is decided by comparing each replica’s last-received WAL position, not by a fixed priority order, which matters in the common case where a secondary replica is slightly behind due to network variance rather than a configured preference. Patroni exposes this through its REST API, which is worth knowing exists independently of the failover logic, because it’s also the interface most load balancers use to route traffic correctly:
| |
That’s the piece that actually solves the “repoint every application” problem: rather than application config pointing at a specific hostname, it points at a load balancer (HAProxy is the usual pairing) configured to health-check the /primary and /replica endpoints and route write traffic only to whichever node currently answers 200 on /primary. When failover happens, the load balancer notices within a couple of health-check intervals and redirects writes to the new primary without any application restart.
A minimal three-node configuration, one YAML file per node with the node’s own identity substituted in:
| |
maximum_lag_on_failover is the setting most worth understanding before a real incident: it caps how far behind (in bytes of WAL) a replica is allowed to be and still qualify for promotion. Set it too high and a badly-lagging replica can be promoted, meaning recently committed transactions are lost. Set it too low on a cluster with naturally bursty write load and replicas, which are briefly behind for entirely normal reasons, get excluded from an election they should be eligible for.
The split-brain problem, and why the consensus store matters
The scenario Patroni is explicitly designed around is the one that makes naive failover scripts dangerous: a network partition that isolates the primary from its peers while the primary itself keeps running and keeps accepting writes. Without a consensus mechanism, the isolated-but-alive primary and a newly-promoted replica on the other side of the partition would both believe they’re the one true primary, and both would accept writes — a split brain, and one of those write streams is going to be silently discarded during the recovery.
Patroni avoids this because the leader lock lives in the external consensus store, not on the Postgres nodes themselves. A primary that’s partitioned away from etcd can’t renew its lease, so it loses leadership from the store’s point of view even though it’s still technically running — and Patroni’s own daemon on that node, once it notices the lease is gone, demotes its local Postgres and refuses to serve writes, specifically to avoid exactly this scenario. The correctness of the whole setup depends on the consensus store itself tolerating the same partition, which is why Patroni’s etcd (or Consul) cluster needs to be spread across the same failure domains as the Postgres nodes, not bolted onto a single box that becomes a new single point of failure.
Running it, and the parts that bite
patronictl is the day-to-day operational tool — patronictl list shows cluster state (who’s leader, who’s a replica, replication lag per node) in one table, and patronictl switchover performs a controlled, zero-surprise handover for planned maintenance rather than waiting for an actual failure to test the mechanism.
Configuration drift is a more common real-world failure mode than dramatic node death: Patroni manages postgresql.conf settings that affect replication (wal_level, max_wal_senders, hot_standby) through its own templating, and manually editing postgresql.conf directly on a managed node gets silently overwritten on the next Patroni-driven restart. Changes belong in Patroni’s own postgresql.parameters section of its config, applied via patronictl edit-config, not in the Postgres config file by hand.
The second common trap is maximum_lag_on_failover combined with synchronous replication. Patroni supports synchronous replication, where the primary waits for acknowledgement from at least one replica before confirming a commit — which guarantees zero data loss on failover but means a slow or disconnected replica can stall every write on the primary. That trade-off should be a deliberate choice rather than a default left in place by accident; most homelab and small-production setups are better served by asynchronous replication with a tight maximum_lag_on_failover, accepting the small risk of losing the last few transactions in an actual failure in exchange for writes never stalling because one replica is having a bad day.
Patroni versus the alternatives
Postgres has several other high-availability tools, and the differences matter when picking one. repmgr manages replication topology and provides manual and semi-automatic failover, but it doesn’t use an external consensus store the way Patroni does, which means its split-brain protection is weaker in a genuine network partition — it’s a reasonable choice for a cluster where a human is expected to confirm every failover, less so for one that needs to heal itself at 3am. pg_auto_failover, built by the Citus team, takes a similar consensus-backed approach to Patroni but bundles its own lightweight monitor process instead of depending on etcd or Consul, trading the operational overhead of a separate consensus cluster for a tighter, Postgres-specific tool with a smaller surface area. Kubernetes-native operators such as CloudNativePG or the Zalando Postgres Operator handle failover with Patroni-like logic (the Zalando operator runs Patroni itself, while CloudNativePG uses its own instance manager) inside Kubernetes, which is the right call if Postgres is already going to live inside a cluster that has its own consensus store and scheduler to lean on.
The honest comparison point is: Patroni is the right pick when Postgres needs HA but isn’t already living inside Kubernetes, and when there’s appetite for running (or reusing) a small etcd or Consul cluster alongside it. It’s the most battle-tested of the non-Kubernetes options — it originated at Zalando and has seen years of production use across a wide range of deployment sizes — which counts for a lot in a tool whose entire job is to behave correctly during the rare, high-stakes moment when something has already gone wrong.
Troubleshooting a cluster that won’t behave
The failure that causes the most confusion is a cluster stuck with no leader at all — patronictl list shows every node as a replica, none as leader. This almost always traces back to the consensus store, not Postgres: if a majority of the etcd (or Consul) members backing Patroni are unreachable, no node can safely acquire the leader lock, by design, because Patroni can’t be sure a leader doesn’t already exist on the other side of a partition. Checking the consensus store’s own health — etcdctl endpoint health — before looking anywhere in Postgres saves time here.
A second common symptom is a replica that refuses to rejoin after being disconnected for a while, logging about diverging timelines. This happens when a replica was briefly promoted (during a flapping network, for instance) and then the original primary came back — the two now have incompatible WAL histories. Patroni’s pg_rewind integration handles the common case automatically by rewinding the diverged node back to a point where it can resume streaming from the new primary, but it depends on wal_log_hints or checksums being enabled on the cluster from the start; retrofitting that onto a running cluster needs a full re-initialisation of the data directory, which is worth doing at setup time rather than discovering the hard way during an actual incident.
Finally, watch loop_wait and ttl together rather than either in isolation. loop_wait controls how often each Patroni instance checks in with the consensus store; ttl controls how long a lease is valid before it expires. Setting loop_wait too close to ttl leaves almost no margin for a single slow check-in to cause an unwanted failover under otherwise normal conditions — a brief GC pause or disk hiccup on the primary is enough to trigger a promotion that didn’t need to happen. The Patroni defaults (loop_wait: 10, ttl: 30) build in a reasonable margin; shrinking them to get faster failover detection trades that margin away.
Who this is actually for
A single Postgres instance with good backups is the right answer for a huge share of self-hosted workloads, and Patroni is meaningful overhead — three database nodes plus a three-node etcd cluster plus a load balancer — that only pays for itself when the cost of a few minutes of write downtime during a manual failover is actually higher than the cost of running and understanding that much infrastructure. Where it earns its complexity is anywhere a Postgres outage has a real consequence attached — a service other people depend on, a billing system, anything where “someone will notice and fix it eventually” isn’t an acceptable answer. For a personal project backed by Supabase’s self-hosted stack or a single-box Postgres doing full-text search duty, the honest answer is usually that good backups and a documented manual promotion procedure cover the actual risk better than a three-node cluster that itself needs monitoring, patching, and understanding of its own failure modes.




