The Principal Dev – Masterclass for Tech Leads

The Principal Dev – Masterclass for Tech Leads28-29 May

Join

Tests
Status Coverage
Status

image

Patroni: A Template for PostgreSQL HA with ZooKeeper, etcd or Consul

You can find a searchable version of this documentation at patroni.readthedocs.io.

Patroni is a Python template for building PostgreSQL high availability (HA) clusters. It supports several distributed configuration stores, including ZooKeeper, etcd, Consul, and Kubernetes.

Supported PostgreSQL versions: 9.3 to 18.

Note to Citus users: Since version 3.0, Patroni integrates with the Citus extension. See the Citus support page for details.

Note to Kubernetes users: Patroni runs natively on Kubernetes. See the Kubernetes guide for more information.

::: {.contents local="" depth="2" backlinks="none"} :::

How Patroni Works

Patroni (formerly known as Zalando's Patroni) started as a fork of Governor and adds support for modern HA patterns.

For additional background info, see:

Development Status

Patroni is actively developed and welcomes contributions.

Community

Connect with the Patroni community on GitHub or Slack:

Requirements and Installation

Pre-requirements for macOS

Install the requirements on macOS with Homebrew:

brew install postgresql etcd haproxy libyaml python

Psycopg choices

Patroni requires a PostgreSQL Python driver. Recent versions of psycopg2 no longer install a binary package by default, which means building from source may require a C compiler and development libraries.

Options:

  1. Install using the package manager from your Linux distribution:
sudo apt-get install python3-psycopg2
sudo yum install python3-psycopg2
  1. Install one of the supported Python packages with pip:

Installing with pip ^^^^^^^^^^^^^^^^^^

Install Patroni with optional dependency groups:

pip install patroni[dependencies]

Available dependency extras:

For example:

pip install patroni[psycopg3,etcd3,aws]

Note: external tools used by bootstrap or replica creation scripts (for example WAL-G) must be installed separately.

Running and Configuring

A minimal cluster can be started from different terminals:

> etcd --data-dir=data/etcd --enable-v2=true
> ./patroni.py postgres0.yml
> ./patroni.py postgres1.yml

Then verify cluster behavior and experiment with the YAML configuration files.

Add more postgres*.yml files to scale the cluster.

Memory issue on Python 3.11+

If you run Patroni on a system with strict memory limits, for example with vm.overcommit_memory=2 (recommended for PostgreSQL), and use Python 3.11 or newer, you may observe unexpected behavior:

This is caused by a Python 3.11+ issue. Under strict memory conditions, starting a new thread may hang indefinitely when there is not enough free memory.

Recent Patroni releases (4.1.1+, 4.0.8+) reduce the impact of this issue by starting all required threads early in startup before memory pressure builds.

Additional recommendations (Linux, glibc)

When running with vm.overcommit_memory=2 (recommended for PostgreSQL), we also recommend starting Patroni with the following environment variables configured:

In addition, you may tune the following Patroni configuration parameters:

HAProxy support

Patroni includes an HAProxy configuration for a single application endpoint. Start it with:

> haproxy -f haproxy.cfg

Then connect with:

> psql --host 127.0.0.1 --port 5000 postgres

Configuration References

YAML configuration

For complete YAML options, see docs/dynamic_configuration.rst and the example file postgres0.yml.

Environment configuration

For environment variable configuration, see docs/ENVIRONMENT.rst.

Replication choices

Patroni uses PostgreSQL streaming replication. It supports:

See the replication modes documentation for details.

Application connections

Applications should connect with a non-superuser. Using a superuser can consume reserved connections for Patroni and cause undesirable behavior if the leader becomes unavailable.

Join libs.tech

...and unlock some superpowers

GitHub

We won't share your data with anyone else.