PostgreSQL High Availability with built-in Raft consensus and load balancing.
pg-ha is a Rust-based PostgreSQL HA solution aimed at the same job as Patroni: automatic failover and synchronous replication, without etcd, ZooKeeper, Consul, or HAProxy. It does not implement every Patroni switch. Open gaps are listed in the TODO.
- No etcd/ZooKeeper/Consul — embedded Raft consensus via openraft
- No HAProxy — built-in TCP proxy with active health checking
- Single binary deployment — one process per node, zero external services required
- TLS encrypted — Raft RPC (2380) is always mutual TLS:
raft.tls(cert+key+ca_cert) is required, and only certificates whose SAN matches a cluster member host are accepted (plaintext needs an explicitallow_insecure_raft: true, development only). The REST API is optional: setrestapi.tlscert+keyfor HTTPS and addca_cert+require_client_cert: truefor mutual TLS; omitting it leaves REST as plaintext (default). Basic Auth credentials on the REST API are only protected once REST TLS is enabled. - Graceful shutdown — drain (renewing the leader lock), CHECKPOINT + stop bounded by the leader lease, lock release only after PostgreSQL stopped
| Document | 中文 | English |
|---|---|---|
| Architecture | 架构 | Architecture |
| Deployment | 部署指南 | Deployment Guide |
| Operations | 运维指南 | Operations Guide |
| Invariants | 不变量 | Invariants |
| TODO | 待办 | TODO |
Each node runs a single pg-ha agent process containing:
- HA Engine — periodic decision loop managing failover and promotion
- Raft DCS — embedded consensus cluster for leader election and state storage
- REST API — health checks (compatible with HAProxy) and management endpoints
- TCP Proxy — read/write splitting across PostgreSQL backends
# Clone and start a 3-node dev cluster (needs Docker; builds the Linux binaries first, PostgreSQL 18 by default)
git clone https://github.com/seraphico/pg-ha.git && cd pg-ha
make up
# Verify cluster
curl -s http://localhost:8008/cluster | jq .
# Connect via proxy (RW port); the dev password is `secret`
psql -h localhost -p 6432 -U postgres
# Stop cluster
make downWhat it does on a real 3-node dev cluster (recorded output, PostgreSQL 18):
| Crate | Purpose |
|---|---|
pg-ha |
Main binary (composes all subsystems) |
pg-ha-core |
HA logic, PG lifecycle, cluster types, configuration |
pg-ha-dcs |
Raft state machine, network transport, DCS trait impl |
pg-ha-proxy |
TCP proxy for PostgreSQL connections with health checking |
pg-ha-api |
axum REST API for health checks and management |
pg-ha-ctl |
CLI tool for cluster administration |
Apache-2.0