Patroni for PostgreSQL

Written by Ashnik Team

| Feb 18, 2025

2 min read

Using Patroni for PostgreSQL High Availability

Imagine this: your PostgreSQL database suddenly goes down, and your application grinds to a halt. Transactions are lost, users are frustrated, and your team scrambles for a fix. High availability (HA) isn’t a luxury—it’s a necessity.

PostgreSQL has built-in replication features, but managing failovers manually is complex and error-prone. This is where Patroni for PostgreSQL High Availability comes in—a battle-tested solution designed to automate and simplify high availability in PostgreSQL clusters.

This guide explores how Patroni for PostgreSQL High Availability ensures HA, how to set it up, and key best practices for optimal performance.

What is Patroni for PostgreSQL High Availability?

Patroni is an open-source cluster management tool for PostgreSQL that automates failover, leader election, and replication. It leverages etcd, Consul, or ZooKeeper as a distributed configuration store to ensure consistent failover management.

Why Use Patroni for PostgreSQL High Availability?

  • Automated Failover: Detects primary node failure and promotes a replica instantly.
  • Distributed Consensus: Uses etcd, Consul, or ZooKeeper to manage cluster state.
  • Easy Configuration: Simplifies replication setup with minimal manual intervention.
  • Flexible and Extensible: Works well with tools like HAProxy and PgBouncer.

If your application requires 99.99% uptime, Patroni for PostgreSQL High Availability is a must-have.

Patroni High Availability Architecture

Below is a simple architectural diagram showing how Patroni for PostgreSQL High Availability manages leader election, failover handling, and replication:

dsaimage

Diagram Explanation:

  • Primary Node (Leader): Handles all write operations.
  • Replica Nodes (Followers): Replicate data from the leader and take over in case of failure.
  • Distributed Configuration Store (etcd/Consul/ZooKeeper): Maintains cluster state and coordinates leader election.
  • HAProxy (Optional): Routes traffic dynamically to the active leader.

Setting Up Patroni for PostgreSQL High Availability

Install Dependencies

Before setting up Patroni for PostgreSQL High Availability, ensure you have PostgreSQL installed along with the necessary dependencies:

sudo apt update && sudo apt install -y postgresql python3-pip etcd
pip3 install patroni[etcd]

For CentOS/RHEL:

sudo yum install -y postgresql python3-pip etcd
pip3 install patroni[etcd]

Configure etcd for Cluster Coordination

Patroni needs a distributed store for leader election. Here’s how to configure etcd:

etcd --name my-etcd \
--initial-advertise-peer-urls http://127.0.0.1:2380 \
--listen-peer-urls http://127.0.0.1:2380 \
--listen-client-urls http://127.0.0.1:2379 \
--advertise-client-urls http://127.0.0.1:2379

Confirm etcd is running:

etcdctl member list

Create a patroni.yml configuration file for each PostgreSQL instance:

scope: my_cluster
namespace: /db/
name: node1
etcd:
host: 127.0.0.1:2379
restapi:
listen: 0.0.0.0:8008
connect_address: 127.0.0.1:8008
postgresql:
listen: 0.0.0.0:5432
connect_address: 127.0.0.1:5432
data_dir: /var/lib/postgresql/12/main
replication:
username: replicator
password: secret
superuser:
username: postgres
password: secret

Start Patroni

Run Patroni on each node:

patroni /etc/patroni.yml

This initializes the cluster, setting up replication and leader election automatically.

Verify High Availability Setup

To check the cluster status:

curl http://127.0.0.1:8008

Or use Patroni’s built-in command:

patronictl -c /etc/patroni.yml list

You should see one node as Leader and others as Replicas.

Common Issues and Troubleshooting

What happens if a node falsely detects failure?

False failures can cause unnecessary failovers. To minimize this, fine-tune Patroni for PostgreSQL High Availability failover sensitivity:

timeouts:
postgresql: 30
ttl: 20
retry_timeout: 10

Additionally, ensure network stability to prevent false-positive failure detection.

How do I manually promote a replica in an emergency?

If the leader is down and automatic failover is not working, manually promote a replica:

patronictl switchover --force --leader --candidate

This forces the designated replica to become the new primary.

Conclusion

Patroni for PostgreSQL High Availability makes database management efficient, automated, and reliable. Instead of manually handling failovers, let Patroni do the heavy lifting—ensuring seamless operations and minimal downtime.

At Ashnik, we specialize in open-source database solutions, including Patroni for PostgreSQL High Availability setups. If you’re looking to optimize your database architecture, subscribe to The Ashnik Times for more expert insights!

Additionally, we have now included an internal link to our PostgreSQL HA Guide here and added an alt text to the architecture diagram. Let us know if you need further refinements!


Go to Top