Manage failover with repmgr
Manage failover with repmgr — PostgreSQL Tutorial. This lesson gives you a practical mental model for repmgr failover, a step-by-step walkthrough, and troubleshooting tips. Next: what's after failover management?
Focus: manage failover with repmgr
Your PostgreSQL primary just crashed at 2 AM. The application is down, the on-call phone is ringing, and you're scrambling to remember which standby server to promote. This is the exact pain that repmgr solves: it automates and manages failover, turning a chaotic emergency into a controlled, predictable process. In this lesson, you'll learn how to manage failover with repmgr — from the core concepts to a hands-on walkthrough that will save you when things go wrong.
The problem this lesson solves
Managing failover manually is error-prone and slow. You have a primary server and one or more standby servers, each streaming WAL data to stay in sync. When the primary dies, you need to:
- Detect the failure.
- Pick the most up-to-date standby.
- Promote it to become the new primary.
- Reconfigure all the other standbys to follow the new primary.
Doing this by hand involves multiple SSH sessions, careful pg_ctl promote commands, and a lot of finger-crossing. A single mistake—like promoting a stale standby—can cause data loss or split-brain scenarios where two servers think they're the primary. repmgr automates this entire workflow, providing monitoring, failover detection, and orchestration so you can recover in seconds, not minutes.
Core concept / mental model
Think of repmgr as the air traffic controller for your PostgreSQL cluster. It maintains a catalog of all servers in the replication group, knows their roles (primary or standby), and tracks their health and WAL position. When a failure happens, repmgr coordinates the transition, just as a controller redirects landing aircraft when a runway becomes unavailable.
The key components you'll work with:
- repmgr – the main command-line tool for cluster management.
- repmgrd – a daemon that runs on each node, watching for failures and executing failover automatically.
- repmgr.conf – the configuration file that defines each node's connection parameters and failover options.
- repmgr metadata – a set of tables in a database (usually
repmgr) that store node information, including roles and last known WAL location.
Important terms to know:
- Failover – the process of promoting a standby to primary after the original primary fails.
- Promotion – making a standby writable and assigning it the primary role.
- Witness server – a lightweight node that helps the cluster determine quorum and prevent split-brain.
- Rejoin – reattaching a former primary or standby to the cluster after recovery.
How it works step by step
Setting up repmgr involves adopting a cluster, configuring each node, and then enabling the failover daemon. Here's the logical sequence:
- Install repmgr on all nodes (primary, standbys, and optional witness).
- Configure repmgr.conf on each node with the correct connection strings and cluster name.
- Initialize the metadata on the primary with
repmgr -f /etc/repmgr.conf primary register. - Clone the standby from the primary using
repmgr standby cloneand then register it. - Start repmgrd on each node to enable automatic failover detection.
- Test failover manually with
repmgr standby promote(or automatically when repmgrd detects a primary outage).
Each step builds on the previous, so a solid foundation in streaming replication is essential before you begin.
Hands-on walkthrough
Let's walk through a complete scenario where you have a primary called node1 and a standby node2. We'll see how to register the cluster, perform a manual promotion, and then rejoin the old primary.
Step 1: Configure repmgr.conf on both nodes
# /etc/repmgr.conf on node1 (primary)
node_id=1
node_name='node1'
conninfo='host=node1 dbname=repmgr user=repmgr password=secret'
log_file='/var/log/repmgr/repmgr.log'
data_directory='/var/lib/postgresql/15/main'
# /etc/repmgr.conf on node2 (standby)
node_id=2
node_name='node2'
conninfo='host=node2 dbname=repmgr user=repmgr password=secret'
log_file='/var/log/repmgr/repmgr.log'
data_directory='/var/lib/postgresql/15/main'
Step 2: Register the primary and clone the standby
On the primary (node1), run:
repmgr -f /etc/repmgr.conf primary register
Then on the standby (node2), clone and register:
repmgr -f /etc/repmgr.conf standby clone node1
repmgr -f /etc/repmgr.conf standby register
Step 3: Promote the standby manually
When the primary fails, promote node2:
repmgr -f /etc/repmgr.conf standby promote
Expected output (truncated):
NOTICE: promoting standby node2
DETAIL: promoting standby "node2" using pg_promote()
NOTICE: waiting for promotion to complete
NOTICE: STANDBY PROMOTE successful
DETAIL: node "node2" was successfully promoted to primary
Step 4: Rejoin the old primary
Once the old primary (node1) is repaired, bring it back as a standby under the new primary:
repmgr -f /etc/repmgr.conf node rejoin --force-rewind
This uses the pg_rewind mechanism to sync the old primary with the new primary and re-registers it as a standby.
Compare options / when to choose what
| Method | Automation level | Pros | Cons |
|---|---|---|---|
Manual failover with pg_ctl promote |
Low | Full control, no extra tools | Slow, error-prone, requires manual reconfiguration of all nodes |
| repmgr with manual promotion | Medium | Faster than pure manual, good for controlled maintenance | Still requires human to trigger promotion |
| repmgr with repmgrd automatic failover | High | Automatic detection and promotion, minimal downtime | Requires extra daemon and careful configuration to avoid split-brain |
| Patroni | High | Uses consensus (etcd/consul), handles more complex topologies | Extra infrastructure dependency, steeper learning curve |
When to choose what:
- Manual promotion – when you need to do a planned switchover for maintenance, or when you want to verify every step.
- repmgr with repmgrd – when you want reliable automatic failover without adding a distributed consensus layer. Ideal for a simple primary/standby setup.
- Patroni – when you have multiple standby nodes and need advanced features like synchronous replication and automated leader election. Overkill for a simple two-node cluster.
For most teams starting out, repmgr with repmgrd is the sweet spot—it's lightweight, well-documented, and integrates seamlessly with streaming replication.
Troubleshooting & edge cases
"repmgr: ERROR: node 1 not in cluster"
This usually means the primary hasn't been registered, or the metadata database is not accessible. Check that the repmgr database exists and that the connection string in repmgr.conf points to the correct database.
"repmgr: ERROR: standby not registered"
If the standby was cloned but not registered, repmgr can't track it. Run repmgr -f /etc/repmgr.conf standby register on the standby after cloning.
Split-brain after network partition
If the network between the primary and standby fails, repmgrd may promote the standby while the primary is still alive, causing both to accept writes. To mitigate:
- Use a witness server to establish quorum; with 2 nodes plus witness, a majority is required to call a failover.
- Set failover=automatic only on the standby, and ensure the witness is reachable before promoting.
# In repmgr.conf on the witness
node_id=3
node_name='witness'
conninfo='host=witness dbname=repmgr user=repmgr password=secret'
data_directory='/var/lib/postgresql/15/main'
"pg_rewind failed" during rejoin
The old primary may have diverged too much from the new primary. This happens if the old primary had transactions that weren't replicated. Check the PostgreSQL logs and ensure wal_log_hints is enabled on both nodes to allow pg_rewind to work.
# postgresql.conf on all nodes
wal_log_hints = on
repmgrd not starting
Check that the repmgr database user has the correct permissions, and that the log file path is writable by the postgres user. Also verify that the repmgr.conf file is readable by the daemon user.
What you learned & what's next
You now understand how to manage failover with repmgr—from registering nodes and promoting a standby to rejoining a repaired primary. You've seen how repmgr automates the detection and promotion process, and you know how to avoid common pitfalls like split-brain and stale standby promotion. These skills will let you build a resilient PostgreSQL cluster with recovery times measured in seconds, not hours.
With that foundation, you're ready to move to the next lesson in the track: PostgreSQL high-availability patterns—where you'll explore strategies like synchronous replication, load balancing, and multi-data-center setups that build on the failover concepts you've just mastered.
Practice recap
Now, set up a two-node repmgr cluster in a test environment (Docker or VirtualBox). Register the primary, clone a standby, then simulate a failure by stopping the primary and promoting the standby manually. Finally, rejoin the old primary using repmgr node rejoin --force-rewind. Observe the log files at each step to internalize the failover flow.
Common mistakes
- Forgetting to enable
wal_log_hintson all nodes —pg_rewindfails when rejoining a former primary. - Not configuring a witness server in a two-node cluster, which can lead to split-brain during network partitions.
- Using
repmgr standby registeron the primary instead of the standby — metadata gets corrupted. - Running
repmgr node rejoinwithout stopping and cleaning the old primary's data directory first. - Ignoring the repmgr log file when troubleshooting — it contains the exact error message for every failed operation.
Variations
- Use
repmgr standby switchoverfor planned demotion of the primary without downtime — perfect for maintenance windows. - Adopt Patroni with etcd or Consul for automatic failover in complex clusters with multiple standbys, though it adds external dependencies.
- Combine repmgr with a load balancer like HAProxy to redirect traffic automatically after failover, reducing manual intervention.
Real-world use cases
- A fintech startup uses repmgr to automatically promote a standby within seconds when their primary database server fails during peak trading hours.
- An e-commerce platform runs a two-node PostgreSQL cluster with repmgr and a witness to survive split-brain scenarios, ensuring no data loss during regional outages.
- A DevOps team performing regular maintenance uses repmgr's switchover feature to rotate the primary without any application downtime during schema migrations.
Key takeaways
- repmgr automates failover by tracking node roles and WAL positions in a metadata database, reducing recovery time from minutes to seconds.
- Proper setup requires registering the primary, cloning and registering standbys, and enabling repmgrd for automatic detection.
- Always test manual promotion before relying on automatic failover, so you understand each step and can debug issues.
- A witness server is critical in a two-node cluster to prevent split-brain and ensure a quorum for promotion decisions.
- Rejoining a failed primary uses
pg_rewind, which requireswal_log_hints=onto work reliably. - Compare repmgr with Patroni based on your cluster size and need for external consensus; simple clusters often don't require the extra complexity.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.