Postgresql HA
Overview
This document provides a comprehensive guide on how to install PostgreSQL on an EC2 instance, set up replication for high availability, and perform version upgrades.
Pre-requisites
AWS Account
Two Ubuntu EC2 Instances
Install PostgreSQL
Set Up EC2 Instances
a. Log in to the AWS Management Console: - Open the AWS Management Console and search for "EC2."
b. Create the Primary Server: - Navigate to "Instances" and click on "Launch Instance." - Configure the instance with the desired specifications and choose an appropriate name, such as "Primary Server."
c. Create the Standby Server: - Repeat the process to launch another instance. - Name this instance "Standby Server."
Configure Security Groups
a. Edit Security Group for Primary Server: - Go to the EC2 dashboard, select the Primary Server instance, and click on the "Security" tab. - Edit the inbound and outbound rules of the security group associated with the Primary Server to allow traffic from the Standby Server. This includes: - Allowing PostgreSQL traffic (default port 5432). - Adding custom TCP rules to allow connections from the IP address of the Standby Server.

b. Edit Security Group for Standby Server: - Similarly, go to the EC2 dashboard, select the Standby Server instance, and click on the "Security" tab. - Edit the security group to allow the necessary traffic from the Primary Server. - Ensure the Standby Server can receive data from the Primary Server by setting up appropriate inbound and outbound rules.

After creating the EC2 instances, follow these steps to log in to both the primary and standby servers using the provided PEM key.
After logging into both the primary and standby EC2 instances, follow these commands to install PostgreSQL
Enabling Replication
To set up replication between the primary and standby PostgreSQL servers, follow these steps:
Primary Server Configuration:
Edit the postgresql.conf in the primary server
Create a replication user to enable replication in the standby server
Edit the pg_hba.conf file in the primary server
Restart the PostgreSQL service
Standby Server Configuration:
Edit the postgresql.conf file in the standby server.
Edit the pg_hba.conf on the standby server
Now, we are setting up the replication and taking a pg_basebackup of the master server on the slave server.
Check the replication using the commands below:
Upgrade PostgreSQL
Below are the steps to upgrade PostgreSQL on the primary and standby servers using pg_upgrade and rsync from version 14 to version 15.
Install postgresql-15 in both primary and standby servers
On Primary Server:
Stop the postgresql-14 and postgresql-15 servers
Run the pg_upgrade command
Edit the postgresql.conf file
Edit the pg_hba.conf file
Start the primary server
Standby Server:
Stop the postgresql-14 and postgresql-15 servers
Now run the rsync command on the master server.
Place a
standby.signalfile in the slave’s data directory to indicate that this is a standby server.
Edit the postgresql.conf file
Start the standby server
Verify Replication
Last updated
Was this helpful?