PostgreSQL High Availability in 2026: Patroni, pgBouncer, and Replication Strategies
A practical guide to PostgreSQL high availability in 2026 — streaming replication, automated failover with Patroni, connection pooling with pgBouncer, and avoiding split-brain in production.
On this page
The first time a primary Postgres database went down on my watch, I had a runbook, a pager alert, and eleven minutes of downtime while I manually promoted a replica and rewired connection strings by hand. That was fine for a side project. It is not fine for anything that makes money while it’s asleep. The gap between “we have a replica” and “we have high availability” is the entire subject of this post — and it’s a bigger gap than most teams realize until the primary actually falls over at 3 AM.
High availability in Postgres isn’t one feature you turn on. It’s three separate problems stacked on top of each other: getting data copied to a standby in real time, deciding automatically and correctly when to promote that standby, and making sure application traffic finds the new primary without anyone touching a config file. In 2026, the default answer to all three is streaming replication, Patroni, and pgBouncer — and this post walks through why, and how they fit together.
Why PostgreSQL High Availability Is Harder Than It Looks
Postgres doesn’t ship a built-in HA controller. Unlike some databases with a cluster manager baked into the core, vanilla Postgres gives you the primitives — streaming replication, WAL shipping, synchronous commit — and leaves failover orchestration entirely up to you. That’s a deliberate design choice: failover is a distributed systems problem, and getting it wrong is worse than not having it at all.
Here’s the failure mode that catches people: a naive HA setup where two nodes each think they should be primary. Both accept writes. Both diverge. By the time someone notices, you have two conflicting histories of your data and no clean way to merge them. This is split-brain, and it’s the reason “just add a replica and promote it when the primary dies” is not a high availability strategy — it’s a data corruption strategy with extra steps.
A real PostgreSQL HA architecture in 2026 has to answer three questions correctly, every time, even under network partitions:
- Is the primary actually down, or can I just not reach it? (the hardest question — see split-brain below)
- Which replica is most caught up, and safe to promote?
- How does the application find the new primary within seconds, not minutes?
Streaming replication answers the “getting data there” part. Patroni answers questions 1 and 2. pgBouncer, combined with Patroni’s routing, answers question 3.

PostgreSQL Streaming Replication Setup: The Foundation
Everything in a PostgreSQL HA cluster sits on top of streaming replication, so it’s worth being precise about what it actually does. A replica connects to the primary’s WAL (write-ahead log) stream and applies each record as it arrives, keeping a near-real-time copy of the database. The two modes you’ll choose between:
Asynchronous replication (the default) — the primary commits a transaction and returns success to the client without waiting for any replica to confirm receipt. Fast, but a crash right after commit can lose the last few transactions that hadn’t shipped yet.
-- On the primary, postgresql.conf
wal_level = replica
max_wal_senders = 10
max_replication_slots = 10
Synchronous replication — the primary waits for at least one replica to confirm the WAL record is durably received before returning success to the client. Zero data loss on failover, at the cost of added commit latency.
-- On the primary
synchronous_standby_names = 'ANY 1 (replica1, replica2)'
synchronous_commit = on
ANY 1 (...) (Postgres’s quorum-based synchronous replication) means any one of the named standbys acknowledging is enough — so a single slow replica doesn’t stall every write, but you still get a durability guarantee. This is the setting most production HA clusters land on: full synchronous-to-all is too slow, pure async is too risky for financial or transactional data.
Replication slots are the other piece worth understanding. A slot tells the primary “don’t recycle WAL segments this replica still needs,” which prevents a lagging or briefly disconnected replica from becoming permanently unrecoverable. Without slots, a replica that falls behind during a network blip might find the WAL it needs has already been deleted, forcing a full resync from scratch — exactly the kind of surprise you don’t want during an incident.
SELECT * FROM pg_create_physical_replication_slot('replica1_slot');
The trade-off: slots also mean a replica that’s permanently gone (not just lagging) will cause WAL to pile up on the primary indefinitely, eating disk. Monitoring slot lag is non-negotiable once you’re running slots in production.
Patroni: Automated Failover Without the 3 AM Manual Promotion
Streaming replication gets data to your standbys. It does nothing about deciding when to promote one, or making sure two nodes don’t both decide they’re primary at once. That’s Patroni’s job.
Patroni is a Python-based cluster manager that wraps each Postgres node, continuously checks its health, and coordinates leader election through a separate consensus store — typically etcd, though Consul and ZooKeeper are also supported. The consensus store is the actual source of truth for “who is primary,” and this is the detail that makes Patroni safe where naive scripts aren’t: Postgres nodes don’t get to unilaterally decide they’re the leader. They have to win a lease from etcd first.
# patroni.yml (abbreviated)
scope: postgres-cluster
name: node1
etcd3:
hosts: 10.0.1.10:2379,10.0.1.11:2379,10.0.1.12:2379
postgresql:
listen: 10.0.1.10:5432
data_dir: /var/lib/postgresql/16/main
parameters:
synchronous_commit: "on"
synchronous_standby_names: "ANY 1 (*)"
bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
The mechanics: each Postgres node runs a Patroni agent that holds a leader lease in etcd with a short TTL (30 seconds in the example above). The current primary renews that lease continuously. If the primary crashes, hangs, or gets network-partitioned away from etcd, the lease expires, and the remaining Patroni agents run a leader election among the replicas that are eligible — meaning within maximum_lag_on_failover bytes of the old primary’s WAL position.
This is how Patroni avoids split-brain: because leadership is decided by quorum consensus in etcd, not by each node’s local view of the world, a partitioned old primary can’t keep believing it’s still in charge. If it can’t reach a majority of the etcd cluster, Patroni demotes it automatically — it fences itself rather than risk conflicting writes. This is the same core idea as Raft or Paxos leader election, just applied specifically to Postgres failover.
A few operational details that matter in practice:
- Run etcd with an odd number of nodes (3 or 5), never 2 — even-numbered clusters can’t break ties during a partition, which defeats the whole point.
maximum_lag_on_failoverstops Patroni from promoting a replica that’s dangerously far behind — better a slightly longer outage than promoting a replica missing thousands of transactions.- Patroni also exposes a REST API (
GET /master,GET /replica) that health checks and load balancers can poll directly, which matters for the pgBouncer piece next.
Patroni vs repmgr, the other common option: repmgr is simpler and doesn’t require a separate consensus store, but that simplicity is exactly the problem — without an external quorum mechanism, repmgr is more exposed to split-brain during network partitions unless you layer in fencing yourself. Most teams building new HA clusters in 2026 reach for Patroni specifically because etcd-backed consensus is a solved, well-tested problem, and repmgr’s operator has to solve more of that themselves.
pgBouncer: Connection Pooling That Also Solves Failover Routing
Here’s the part people underestimate: even a flawless Patroni failover is useless if your application is still holding connections open to the now-demoted old primary, or if every one of your app servers needs its own logic for “who’s the primary right now.”
pgBouncer solves two problems at once here.
Problem one: Postgres connections are expensive. Each Postgres connection is a full OS process with its own memory overhead — commonly cited around 5-10MB per idle connection, and that adds up fast with modern app frameworks that open dozens of connections per instance. pgBouncer sits in front of Postgres and multiplexes many client connections onto a much smaller pool of actual database connections.
# pgbouncer.ini
[databases]
mydb = host=127.0.0.1 port=5432 dbname=mydb
[pgbouncer]
listen_port = 6432
pool_mode = transaction
max_client_conn = 1000
default_pool_size = 25
pool_mode = transaction is the setting that matters most for real workloads — a backend connection is only assigned to a client for the duration of a single transaction, then returned to the pool. This gets you far higher effective concurrency than session mode, with the caveat that session-level features (prepared statements across transactions, SET outside a transaction, advisory locks held across transactions) don’t work the same way and need auditing before you switch.
Problem two: routing writes to whoever is currently primary. In an HA cluster, “primary” is a moving target. The common pattern is to run pgBouncer (or a lightweight layer like HAProxy in front of it) configured against Patroni’s REST health-check endpoints, so traffic automatically follows the elected leader:
# HAProxy health check against Patroni's REST API
backend postgres_primary
option httpchk GET /master
server node1 10.0.1.10:5432 check port 8008
server node2 10.0.1.11:5432 check port 8008
server node3 10.0.1.12:5432 check port 8008
Patroni’s /master endpoint returns 200 only from the current leader and a non-200 from replicas, so the load balancer naturally drains traffic away from a demoted node within a couple of health-check intervals — no application code changes, no manual DNS updates, no connection string edits at 3 AM.

Putting It Together: A Real PostgreSQL HA Architecture in 2026
A typical production setup looks like this:
- 3 Postgres nodes (1 primary + 2 replicas), synchronous replication with
ANY 1quorum - 3-node etcd cluster for Patroni’s consensus store (can colocate with the Postgres nodes or run separately)
- Patroni on each Postgres node, managing local Postgres and reporting health to etcd
- HAProxy or pgBouncer in front, routing writes to the current leader via Patroni’s REST API and (optionally) load-balancing reads across replicas
- Replication slots on the primary, monitored for lag so a stuck replica doesn’t silently pile up WAL
This gives you: automatic failover in the time it takes a lease to expire plus an election (typically 10-30 seconds with the config above), zero-data-loss on failover if you’re running synchronous commit, and application servers that never need to know or care which node is currently primary.
Common Gotchas in Production PostgreSQL HA
Fencing the old primary. Even with etcd-backed leader election, a “zombie” old primary that comes back online after a partition heals needs to be forced to rejoin as a replica, not attempt to serve traffic. Patroni handles this automatically by checking the DCS on recovery, but it’s worth testing explicitly — pull the network cable on your primary in staging and watch what happens when it reconnects.
Synchronous replication and availability trade-offs. synchronous_standby_names = 'ANY 1 (*)' means writes block if no replica is reachable — which trades a small availability hit for a strong durability guarantee. Decide deliberately whether your workload wants “never lose a committed write” or “never block writes,” because you cannot fully have both.
pgBouncer and long-lived session state. If your application relies on session-level Postgres features, transaction pooling mode will break in subtle, hard-to-reproduce ways. Test this explicitly before flipping the switch in production, not after.
Monitoring replication lag, not just replica existence. A replica that’s technically “up” but seconds or minutes behind is a landmine for maximum_lag_on_failover — and worse, if promoted anyway, means real data loss. Alert on lag in bytes and in time, not just on the replica process being alive.
FAQ: PostgreSQL High Availability
Do I need Patroni if I already have replicas? Replicas alone give you a manual disaster recovery option, not high availability. Without an automated, consensus-based failover mechanism, promoting a replica is a manual, error-prone, and slow process — exactly what HA is meant to eliminate.
Is pgBouncer required, or just recommended? It’s not strictly required — you can point applications directly at Postgres — but without it you lose connection pooling (a real cost at scale) and you need some other mechanism for routing traffic to the current primary after failover.
Can I run Patroni without etcd? Patroni supports etcd, Consul, ZooKeeper, and even a Kubernetes-native DCS. You need some external consensus store; Patroni doesn’t do leader election on its own without one.
What’s a realistic failover time? With the TTL and loop settings shown above, expect 10-30 seconds from primary failure to a new leader being elected and serving traffic. Tuning ttl and loop_wait lower speeds this up but increases the risk of false-positive failovers during transient network hiccups — there’s a real trade-off here, not a free lunch.
High availability in Postgres has quietly become a solved problem — not because Postgres grew a built-in cluster manager, but because Patroni, etcd, and pgBouncer compose cleanly into something that behaves like one. The pieces are boring, well-understood, and battle-tested, which is exactly what you want from anything holding your database up while you’re asleep.