What pg_auto_failover does and when a team needs it
A team running one primary PostgreSQL server needs an answer to a simple question: what happens when that server stops responding at 2 a.m.? PostgreSQL can replicate changes to a standby and can promote that standby by hand, but it does not detect the primary failure or tell the standby to take over. That job belongs to an external tool, as the PostgreSQL failover documentation makes clear. pg_auto_failover is such a tool. It is an open-source PostgreSQL extension that watches the health of each node and promotes a standby when the primary is lost. Its design centers on a separate monitor node that holds the cluster's state, plus a small keeper agent beside each PostgreSQL server that reports to the monitor and carries out the transitions it orders.
pg_auto_failover fits a team that wants a dedicated failover tool with a small set of moving parts and a clearly defined state machine. The usual layout is three nodes: the monitor, a primary and a standby. The standby is synchronous by default, so a committed transaction is written to two servers before the application sees success. That trades some write latency for less data at risk during a failover. Teams that already run a distributed configuration store for other purposes may find Patroni a better fit, since its leader lock lives there. Teams that prefer repmgr's model, with its own daemon and an optional witness server, may choose that instead. The deciding factor is usually which components your operators already know how to run, monitor and repair at night.
Before adopting it, confirm three things against the project documentation and your own environment. First, which PostgreSQL major versions the release you plan to deploy supports, since the tool publishes its own compatibility list alongside PostgreSQL's support window. Second, how your network behaves when the monitor cannot reach a database node, because the monitor is the component that decides which server is primary, and losing contact with it may stall transitions. Third, whether you need read replicas across sites or delayed standbys, which may require separate planning and could push the design toward other tools. Answering these questions early shapes the node count, the network paths and the runbooks your team will write.
How the monitor, primary and standby fit together
The typical layout starts with the client application, which connects through a routing layer to whichever node is currently the primary. The monitor is a separate role, not a data node: it receives health reports from the primary and standbys and decides state transitions, so it can promote a standby when the primary fails. The synchronous standby holds the replicated WAL that makes a promotion safe, and an additional standby adds read capacity or a further failover candidate. When you review this layout, check that the monitor is isolated from the data nodes and that its own backups and availability are planned.
Typical pg_auto_failover layout with a separate monitor node and two standbys.
How the monitor decides when to fail over
pg_auto_failover places a monitor node in charge of every role change. The monitor tracks each node's goal state, checks health from its own network position, and sequences promotion so only one primary accepts writes at a time. These five parts determine how quickly a failover happens and whether it is safe.
Goal states drive every transition
The monitor assigns each node a goal state and nodes report their current state back. When the two differ, the monitor issues the next instruction and the node moves toward it. The state names and the order of steps belong to the release you deploy, so read the state documentation for that version instead of relying on memory. Check that every node reconnects to the monitor after a restart, because a node that cannot report stays out of the sequence.
Health checks from the monitor
The monitor decides whether the primary is healthy using its own network position. A partition between the monitor and the primary can look like a primary failure even while application clients still reach the primary. PostgreSQL does not detect primary failure itself, so this check is central to the design. Test it directly by cutting the monitor's path to the primary while clients stay connected, then confirm what the monitor does. Ask how long it waits before acting and where that setting lives.
PostgreSQL failover docsPromotion candidate selection
After the monitor judges the primary unhealthy, it chooses a standby to promote from the nodes it considers eligible. Eligibility depends on replication state and on the synchronous standby setting, so a lagging standby may be excluded from promotion. Confirm which standbys count as candidates in your layout and what the monitor does when none qualifies. Those answers determine whether a failover trades a short write outage for possible data loss, so record them in the runbook.
Old primary demotion before promotion
The former primary must stop accepting writes before the new primary begins to serve them. The monitor sequences this order, but the step depends on the old primary being reachable to receive the instruction. If the old primary is isolated, it may keep accepting writes from clients that still have a route to it. Test this case on purpose and confirm that fencing outside the monitor, such as network rules or host power control, closes the gap.
Monitor availability and protection
In the three-node layout the monitor is a single decision point. Its loss blocks new transitions even though the existing primary and standby keep replicating. Confirm how the running cluster behaves while the monitor is down and what recovery looks like. Place the monitor on its own host or failure domain, back up its metadata database, and monitor its service and disk. Include a monitor restore in your failover tests so the procedure is proven before an incident.
Which layout and standby settings fit each team
This table compares the layouts and standby settings pg_auto_failover offers, and what each costs in write latency, data loss exposure and day-to-day work. Read each row against your recovery point target, your write latency budget and the number of people who will run the cluster. The main takeaway: synchronous standbys protect committed data at the cost of commit latency, and the monitor must be protected whatever layout you choose.
| Layout or setting | Write latency and data loss exposure | Operational load | Choose when |
|---|---|---|---|
| Three-node minimal layout (monitor, primary, one standby) | A standby in the synchronous quorum makes each commit wait for it | Monitor plus two data nodes to patch, back up and watch | Small teams that need automatic failover and can accept the standby's write wait |
| Synchronous standby (default) | Commits wait for the standby, so acknowledged writes exist on two nodes | Low node count, but confirm how the monitor behaves when the standby is unavailable | Workloads where losing committed transactions is unacceptable |
| Asynchronous standby | Commits do not wait for the standby, so latency is lower but recent commits can be lost on failover | Same node count; the team must monitor lag and document the lost-write window | Latency-sensitive workloads that have a stated and accepted data loss tolerance |
| Additional standbys | More standbys can raise how many nodes must confirm a commit, which adds latency | Each extra node adds patching, backup and monitoring work | Teams that need a second failover target or read capacity; confirm the synchronous count setting in the docs |
| Monitor node placement | The monitor is not in the write path, but its loss blocks automatic failover | A separate node to protect, back up and keep available | Any team running automatic failover; place it in a separate failure domain from the data nodes |
| Cross-site standby | Network delay between sites adds to every synchronous commit | Network tests and failover drills across the link before go-live | Teams placing a standby in another site; measure round-trip time before choosing synchronous mode |
How to set up the three-node cluster
This sequence builds the monitor, the primary and the secondary in the order pg_auto_failover expects, so each node registers with the monitor before the next one joins. The result is a three-node cluster in which the monitor tracks node state and coordinates failover between the two data nodes.
Prepare the monitor host and network
The infrastructure or platform team provisions a dedicated host for the monitor, separate from both data nodes, and installs the same PostgreSQL and pg_auto_failover packages on all three machines. Confirm that each host resolves the others by the names you will register, that the monitor port and PostgreSQL ports are open between the hosts, and that
pg_autoctl versionreturns the same build on every host. Keep the monitor's data directory on storage you can back up, because the monitor holds the cluster's state. Done means all three hosts reach each other on the required ports and report matching software.Create the monitor node
On the monitor host, run
pg_autoctl create monitorwith a data directory, a hostname that the data nodes can reach and an authentication setting that matches your access policy. Start the monitor as a service withpg_autoctl runor the service unit your packaging provides. Runpg_autoctl show urito capture the monitor connection string that the data nodes will use. Done means the monitor service is running and the URI resolves from both data node hosts. Confirm the authentication method with your security team before you proceed, because the data nodes will connect to the monitor with it.Initialize the primary data node
On the first data node, run
pg_autoctl create postgreswith the monitor URI from the previous step, plus a node name, hostname and data directory. The monitor registers the node and assigns it the primary role once the node comes online. Runpg_autoctl runto start the keeper service, then checkpg_autoctl show statefrom any host. Done means the state output lists this node as the primary and reports it as healthy. Confirm that the PostgreSQL port answers for client connections, because the other nodes will not be able to join until the primary is stable.Add the secondary data node
On the second data node, run
pg_autoctl create postgreswith the same monitor URI and its own node name and hostname. The monitor assigns the secondary role and the node builds its copy of the data from the primary, so allow the initial copy to finish before you judge the result. Start the keeper service withpg_autoctl runand watchpg_autoctl show stateuntil the secondary reports a healthy state. Done means both data nodes appear in the state output with one primary and one secondary, and neither shows an error state.Verify replication and synchronous commit
On the primary, query the
pg_stat_replicationview to confirm that the secondary is connected and streaming. Check that thesync_statecolumn for the secondary shows a synchronous state, and that thesynchronous_standby_namessetting on the primary matches what pg_auto_failover manages for this cluster. Runpg_autoctl show stateagain to confirm both nodes report healthy and that the monitor sees no pending action. Done means replication is streaming, the secondary is counted as synchronous, and the monitor reports a stable cluster. Ask your team to confirm whether the synchronous default fits your write latency and data loss requirements before go-live.Run a controlled switchover before go-live
Run
pg_autoctl perform switchoverfrom a host that can reach the monitor, so the monitor promotes the secondary and demotes the primary in a planned way. Watchpg_autoctl show stateuntil the roles have swapped and both nodes report healthy again. Confirm that a test client connected through your application path reconnects without manual changes to its configuration, and record how long reconnection took in your own environment. Done means the switchover completes, both nodes return to a healthy state and the test client resumes work. Keep the result with your runbook, because the failure tests in the next steps depend on this baseline.
Running maintenance and switchovers without outages
Planned work on a pg_auto_failover cluster should go through the monitor. The pg_autoctl perform switchover command asks the monitor to promote a healthy standby and move the primary role in an orderly sequence, so the old primary is demoted before the new one accepts writes. If you stop PostgreSQL on the primary by hand, the monitor sees a failure and may start an automatic failover you did not plan for. For a node that needs patching or a reboot, pg_autoctl enable maintenance tells the monitor to stop managing that node's state, and pg_autoctl disable maintenance returns it to normal management. After the work, the node rejoins as a standby, and the monitor expects it to catch up before it counts as healthy again. Confirm the exact subcommands and state transitions against the pg_auto_failover version you run, because the monitor's behavior is what your change plan depends on.
Rehearse each operation in a non-production copy of the cluster before the change window. Record the output of pg_autoctl show state and the synchronous standby status before you start, then check both after each step so you can see whether the monitor's view matches actual replication. Taking a standby out of service removes a copy that protects committed writes, so decide in advance whether the change can proceed while only one healthy standby remains, and what you will do if that standby becomes unhealthy during the work. Write the expected state after each step into the runbook, so an operator under pressure can tell a normal transition from a fault.
Clients do not follow a switchover on their own. pg_auto_failover moves the primary role, but application connections still have to find the current primary. Use a libpq connection string that lists every node and sets target_session_attrs=read-write, or place a proxy in front of the cluster. Expect sessions to drop at the moment of promotion, and build retry logic that reopens connections and safely repeats in-flight work. Measure how long reconnection takes during rehearsal, because that figure is the planned interruption your change window must cover. The application team should own that test, since a switchover that succeeds at the database layer can still stall a service whose connection pool holds stale sessions. The PostgreSQL high availability documentation covers the broader design choices behind these routing options.
Protecting the monitor and the replication path
In a pg_auto_failover design, the monitor decides which node is primary, so it needs the same protection as the data nodes and a few extra safeguards of its own. The cards below cover the monitor's recovery path, its access, the replication credentials, network scope, encryption between nodes and an audit trail of state changes.
Back up the monitor database
The monitor stores the formation and node state that pg_auto_failover uses to choose failovers. Losing it does not lose application data, but it does remove the coordinator that decides promotions, so the monitor needs its own backup schedule and a tested restore. Confirm that the monitor's backup is stored off the monitor host and that someone has restored it into a scratch environment at least once.
Continuous archiving docsRestrict monitor access
Anyone who can write to the monitor database can change node state and potentially influence failover decisions. Limit login to the pg_auto_failover processes and a small operator group, and keep the monitor's superuser credentials in a secrets store rather than in shell history or configuration files. Review who holds those credentials as part of onboarding and offboarding.
Scope the replication role
The replication role that standbys use should have only the replication privilege, not superuser rights, and its password or certificate should be unique to this cluster. Match the pg_hba.conf entries for replication to the exact standby addresses or subnet, rather than allowing replication from any host. Verify each entry with a standby that should connect and a host that should be refused.
Limit network reach
The monitor, primary and standbys need to reach each other on PostgreSQL ports, and the monitor must be reachable by every data node. Restrict those ports with host firewall rules or security groups to the cluster's addresses only. Document which nodes must reach which ports, since a blocked path between a standby and the monitor can stop failover from completing.
Encrypt traffic between nodes
Node-to-monitor and primary-to-standby traffic carries credentials and replicated data, so enable TLS on each connection and verify certificates rather than accepting any server. Plan certificate issuance, renewal and rotation before go-live, because an expired certificate can block a standby from rejoining after a failover. Confirm the exact sslmode each node uses in its connection string.
Audit state changes
Record every promotion, role change and node registration with a timestamp and the node involved, and keep those records in a store the database operators cannot edit. Forward PostgreSQL server logs from the monitor and data nodes to central logging so that a failover can be reconstructed during an incident review. Check during testing that a deliberately triggered failover produces a complete trail.
Questions teams ask before running pg_auto_failover
What drives the cost of running pg_auto_failover?
The cost depends on how many servers the design needs, how many standbys you want, how much of the surrounding environment must be built or changed, and how much testing you require before go-live. The main components are the monitor node, the primary and standby servers, the network and storage those servers sit on, client connection routing, backup and monitoring integration, and the failover tests that prove the design works. Recurring costs include patching, monitoring, on-call time and periodic failover drills. Ask any partner for a quote that separates one-time build work from recurring operations, and that names each server, each integration and each test. DeployOpen does not publish prices; a quote follows from the written scope.
How long does a pg_auto_failover implementation take?
Duration depends on the number of nodes, the state of your infrastructure, how quickly access and change approvals arrive, and how many failure scenarios must be tested before production. Your team should ask for a timeline in the written scope with milestones for environment preparation, cluster build, failover testing and handover. No duration is given here because the research supplied for this page does not include one. Ask the partner which dependencies on your side could extend the schedule, such as network changes, certificate issuance or change windows.
What skills does our own team need to run pg_auto_failover?
Your team needs working knowledge of PostgreSQL replication, including streaming replication, synchronous commit and how a standby is promoted. They also need Linux administration, systemd service management, TLS and network configuration, and enough monitoring experience to notice a stuck or unhealthy node. Someone must be able to read the monitor's state output and perform maintenance operations without causing an outage. Plan for at least two people who can operate the cluster, so that knowledge does not sit with one engineer. Confirm the exact supported versions and commands against the pg_auto_failover documentation for the release you deploy.
What does the work require from our own team?
Your team provides the things only your organization controls. These include server or virtual machine allocation in the networks you choose, DNS names, firewall rules between the monitor, primary and standbys, service accounts, certificates and key material, access to the application connection path, and a named owner for approvals. Your team also decides the recovery objectives, the synchronous commit policy and which applications must reconnect automatically after a failover. Expect to take part in review sessions and in the failover tests, since those results must be understood by the people who will run the cluster afterwards.
Can our team implement pg_auto_failover without a partner?
Yes, a capable PostgreSQL team can deploy pg_auto_failover on its own, provided it has the skills described above and enough time to build and test the design. The risk lies in the details that are easy to skip: protecting the monitor, choosing the synchronous standby behavior, planning maintenance, and running failure tests before production. A partner is most useful when you need an independent design review, a documented build, or evidence from failover testing for an assessment. Your team can also do part of the work and buy the testing or review from a partner.
What does a partner's scope for this work usually cover?
A partner's written scope should list the topology, the number of monitor and data nodes, the PostgreSQL versions, the operating systems, the network and storage assumptions, the client connection approach, the backup and monitoring integration, the maintenance runbooks and the failover tests to be run. DeployOpen plans, implements, hardens, documents and supports self-hosted open-source platforms in your own infrastructure, and its engagements are fixed-scope with scope, deliverables and timelines agreed in writing before work starts. DeployOpen does not issue audit reports or certifications; if you need compliance evidence, the independent auditor you choose makes that judgment.
What risks should we check before choosing this design?
The main risks are a monitor that becomes a single point of failure, a standby that is not in sync when the primary fails, and a cluster that has never been tested under failure. Ask how the monitor itself is protected, how backups of the monitor and data nodes are taken and restored, and what happens during a network partition. Confirm the synchronous standby behavior with the pg_auto_failover documentation, because it determines how writes behave when a standby is unavailable. Also ask who approves maintenance and how upgrades will be sequenced, since PostgreSQL major upgrades need their own plan.
When to choose pg_auto_failover over Patroni or repmgr
Choose pg_auto_failover when you want a dedicated monitor service to make failover decisions and your team is comfortable running one more small, separate role alongside the PostgreSQL nodes. The monitor is a PostgreSQL-backed service that tracks the state of each node and coordinates promotion, so the design does not require an etcd, Consul or ZooKeeper cluster as Patroni does. The trade-off is that the monitor becomes a component you must place, back up and protect, which the monitor section of this page covers. If your organization already runs a distributed key-value store for other services, the case for Patroni gets stronger, and if it does not, the case for avoiding one is the main reason to consider pg_auto_failover.
Compare the operational footprint in concrete terms. A typical pg_auto_failover layout uses a monitor node plus a primary and at least one standby, so the three-node count is a starting point, not a fixed quorum you must size as etcd does. Patroni's leader election depends on the distributed configuration store staying healthy, and repmgr relies on a witness server and fencing scripts that you write and test yourself. Ask each candidate tool's maintainers, and your own platform team, which of these components already have an owner, monitoring and upgrade path. Count the extra roles, not only the servers, because a monitor that nobody watches is a single point of failure you added on purpose.
Let existing tooling and skills settle close calls. If your operators already run etcd for Patroni or have a documented repmgr runbook, switching tools has a learning and migration cost that rarely pays off during an outage. Confirm which PostgreSQL versions each tool supports for your target release before you commit, since support windows differ from the PostgreSQL versioning policy dates. For the detailed setup of the alternatives, read the sibling topics on PostgreSQL high availability with Patroni, etcd and HAProxy and PostgreSQL failover with repmgr. Use the comparison to shortlist one design, then run the failure tests for that design before go-live.
How DeployOpen scopes and delivers this failover design
DeployOpen plans and implements pg_auto_failover inside infrastructure you control, starting from your recovery targets and the server layout you already run. Each step, from the three-node layout and monitor to standby additions and maintenance runbooks, is agreed in writing before the build starts.
How we scope
- Confirm the three-node layout, monitor placement and network paths between nodes, the monitor and the standbys.
- Agree which PostgreSQL major version runs, and record its support end date from the versioning policy.
- Write the scope covering the synchronous standby decision, extra standbys, maintenance procedures and monitor protection.
How we build
- Install the monitor and formation, then register the primary and standbys and confirm each reports its expected state.
- Configure synchronous standby behavior and test that writes wait for the standby before a commit returns.
- Run failover, switchover and monitor-loss tests in a staging copy of your environment, and record the observed results.
How we hand over
- Deliver configuration, the formation layout, and a runbook for planned switchovers, node replacement and monitor recovery.
- Document the failure tests performed, their results, and the steps to repeat them after changes.
- Walk your operators through the runbooks and confirm they can perform each step without DeployOpen present.
How we support
- Offer ongoing support, upgrades and continuous testing as a retainer after handover, scoped in writing.
- Plan minor PostgreSQL and pg_auto_failover upgrades as scheduled work with a rollback path.
- Retest failover after changes to network, storage, PostgreSQL version or node count, and report the findings.

