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

  1. 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."

  2. 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.

  1. After creating the EC2 instances, follow these steps to log in to both the primary and standby servers using the provided PEM key.

  1. 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:

  1. Edit the postgresql.conf in the primary server

  1. Create a replication user to enable replication in the standby server

  1. Edit the pg_hba.conf file in the primary server

  1. Restart the PostgreSQL service

Standby Server Configuration:

  1. Edit the postgresql.conf file in the standby server.

  1. Edit the pg_hba.conf on the standby server

  1. Now, we are setting up the replication and taking a pg_basebackup of the master server on the slave server.

  1. 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:

  1. Stop the postgresql-14 and postgresql-15 servers

  1. Run the pg_upgrade command

  1. Edit the postgresql.conf file

  1. Edit the pg_hba.conf file

  1. Start the primary server

Standby Server:

  1. Stop the postgresql-14 and postgresql-15 servers

  1. Now run the rsync command on the master server.

  1. Place a standby.signal file in the slave’s data directory to indicate that this is a standby server.

  1. Edit the postgresql.conf file

  1. Start the standby server

Verify Replication

Last updated

Was this helpful?