
Achieve Automatic Failover in PostgreSQL with pg_auto_failover
PostgreSQL is a powerful, open-source relational database with a strong reputation for reliability, robustness, and performance. It supports replication to achieve high availability for a very long. But it lacks an important feature on its own, that is the concept of automated failover. Of course, there are a lot of open-source tools available to do it, but it won’t come under the core PostgreSQL packages. Additional knowledge and maintenance are required to handle it.
In this blog post, we will look at the tool pg_auto_failover which is very simple to use, easy for configuration, and maintenance.
pg_auto_failover is an open-source tool(basically an extension) that can be used to perform automated failover in the PostgreSQL production environments. You can find its source code here.
Simple Architecture:
The architecture with pg_auto_failover requires N+1 nodes where N is the number of PostgreSQL servers and the additional one node is required, it is a monitor node that acts both as a witness and an orchestrator.
A simple architecture with 2 PostgreSQL servers and pg_auto_failover.

Here I am using 3 VMs to show the demonstration, the details are as follows
Installation of pg_auto_failover(On all the nodes):
Pg_auto_failover is available in the package managers for Linux distributions. The following steps for installation can be used in Fedora, CentOS, or Red Hat distributions. For other distributions, please refer here.
Perform the following steps in the monitor and the DB servers
- Adding the required packages to the system
- Install pg_auto_failover
- Confirm the installation
pg_autoctl will be installed along with pg_auto_failover extension and other PostgreSQL client /server/ library packages. pg_autoctl is a binary with subcommands to initialize and manage the PostgreSQL service.
Note: If you are using PostgreSQL 14 or others, your binaries will be placed in the respective directories. (/usr/pgsql-XX/bin/pg_autoctl )
Configuration of pg_auto_failover:
Once we are done with installation on all the DB and the monitor servers, we are ready to start the configuration.
Configure Monitor First(Only on monitor):
We need to first configure the monitor as it will periodically monitor the DB nodes and watches their health. For complete configuration, we will be using pg_autoctl commands along with the subcommands for respective operations.
- Create your data and tmp directories(if not exist) and change the ownership to postgres user.
Note: make sure your system has user postgres. If not, create it before performing the above steps.
- Execute the below command to configure the monitor
Where
pgdata —> Absolute path of the data directory
pgctl —> Absolute path to the pg_ctl binary
The above successful execution of the command will print the following message
It also installs the pgautofailover extension and grants access to the new user autoctl_node in the local PostgreSQL database.
Note: The monitor server also runs the PostgreSQL service to store the state changes in the database servers(Primary or Secondary), only for its own purpose.
- Execute the following command to start the monitor server by postgres user.
- Check by login the PostgreSQL service (By postgres user)
Where
-d —> pg_auto_failover is the database created by the monitor setup
5. Run the pg_autoctl service as a monitor(Monitor server) under postgres user
It starts to listen on port 5432 in the monitor server for further configuration and monitoring
It is perfect now.
After post successful configuration of the monitor server, it is time to move to set up the DB server.
Configuring DB Servers(Only on DB servers):
- The following command will be executed on the DB1, will configure the primary database.
Where
pgdata —> Absolute path of the data directory
username —> User to be created in the database
dbname —> Database to be created in the database
hostname —> Hostname of the primary database (Current Server)
pg_ctl —> Absolute path of the pg_ctl binary
monitor:
autoctl_node —> Default user to connect to the monitor server. It will be created in the monitor server at the time of configuration as mentioned earlier.
192.168.33.22:5432 —> Hostname or IP of the monitor server along with the port.
pg_auto_failover —> Database created in the monitor server at the time of configuration
- Run the pg_autoctl service as keeper(DB server) under postgres user
Checking the current status
It is showing the status as single as currently, only one server is available.
Let’s do the same on DB2 as well
Only changed the value for option hostname here. Used the IP of the server DB2.
Then, run the following
Now, check the current status. You could see the following
While configuring the DB2, the pg_auto_failover performs the standby setup as well by performing pg_basebackup for the initial sync.
You can use the status command on any nodes in the architecture.
That’s all about the configuration. Try to do some testing with various scenarios
- Shutting down the PostgreSQL service
- Shutdown the VM
This will do the failover on its own.
In the next blog, we will look at the more advanced features available and also with the multiple standby architecture which is more opt for the production systems.