PostgreSQL High Availability (HA) Configuration Guide 
This document outlines the procedure for configuring a High Availability (HA) environment for PostgreSQL. Implementing this setup enables automatic database failover and switchover, ensuring minimal downtime and improved service resilience.
Overview 
The PostgreSQL HA configuration requires a minimum of three nodes:
- Database Node 1 - Primary PostgreSQL instance
- Database Node 2 - Standby PostgreSQL instance
- Arbiter Node - Responsible for quorum management and split-brain detection in the event of a network partition
This topology provides fault tolerance and ensures continuous database availability.
Prerequisites 
All nodes must have Docker installed. Refer to the official Docker installation guide for your operating system: Docker Engine Installation Documentation
Configuration Parameters 
The following credentials and configuration values can be customized within the provided sample files:
| Default Value | Description | 
|---|---|
| password123 | Database password for connecting to apio_core | 
| adminpassword | Administrative password for PostgreSQL access | 
WARNING
Ensure that strong, unique passwords are used in production environments.
Deployment Considerations 
When deploying multiple Apio Core instances, each instance should be configured to connect to a distinct PostgreSQL node to optimize load distribution and improve fault tolerance.
Configuration of the First Database Node 
This section describes the procedure to configure the first PostgreSQL node (pg-0) within the High Availability cluster.
Host Configuration 
Add the IP addresses of the remaining cluster nodes to the /etc/hosts file to ensure proper hostname resolution:
echo "a.b.c.d pg-1" >> /etc/hosts
echo "a.b.c.d pg-2" >> /etc/hostsINFO
Replace a.b.c.d with the actual IP addresses of the second (pg-1) and third (pg-2) nodes.
Directory Permissions 
Ensure that the PostgreSQL data directory is writable by the container process:
chmod 777 /data/dbDocker Compose Setup 
Create a dedicated directory for the PostgreSQL Docker Compose configuration:
mkdir /opt/pg/
cd /opt/pg/
vi docker-compose.yamlInsert the following configuration into the docker-compose.yaml file:
services:
  pg-0:
    ports:
      - 5432:5432
    image: bitnamilegacy/postgresql-repmgr:17
    environment:
      POSTGRESQL_POSTGRES_PASSWORD: adminpassword
      POSTGRESQL_USERNAME: apio_core
      POSTGRESQL_PASSWORD: password123
      POSTGRESQL_DATABASE: apio_core
      REPMGR_PASSWORD: repmgrpassword
      REPMGR_PRIMARY_HOST: pg-0
      REPMGR_PARTNER_NODES: pg-0,pg-1,pg-2
      REPMGR_NODE_NAME: pg-0
      REPMGR_NODE_NETWORK_NAME: pg-0
    restart: unless-stopped
   volumes:
     - /data/db:/bitnami/postgresqlINFO
Update all passwords and network parameters according to your organizational security standards.
Service Deployment 
Once the configuration file has been created, deploy the services using Docker Compose:
docker compose up -dVerify that all containers are running successfully:
docker compose psConfiguration of the Second Database Node 
This section describes the procedure to configure the second PostgreSQL node (pg-1) within the High Availability cluster.
Host Configuration 
Add the IP addresses of the remaining cluster nodes to the /etc/hosts file to ensure proper hostname resolution:
echo "a.b.c.d pg-0" >> /etc/hosts
echo "a.b.c.d pg-2" >> /etc/hostsINFO
Replace a.b.c.d with the actual IP addresses of the first (pg-0) and third (pg-2) nodes.
Directory Permissions 
Ensure that the PostgreSQL data directory is writable by the container process:
chmod 777 /data/dbDocker Compose Setup 
Create a dedicated directory for the PostgreSQL Docker Compose configuration:
mkdir /opt/pg/
cd /opt/pg/
vi docker-compose.yamlInsert the following configuration into the docker-compose.yaml file:
services:
  pg-1:
    ports:
      - 5432:5432
    image: bitnamilegacy/postgresql-repmgr:17
    environment:
      POSTGRESQL_POSTGRES_PASSWORD: adminpassword
      POSTGRESQL_USERNAME: apio_core
      POSTGRESQL_PASSWORD: password123
      POSTGRESQL_DATABASE: customdatabase
      REPMGR_PASSWORD: repmgrpassword
      REPMGR_PRIMARY_HOST: pg-0
      REPMGR_PARTNER_NODES: pg-0,pg-1,pg-2
      REPMGR_NODE_NAME: pg-1
      REPMGR_NODE_NETWORK_NAME: pg-1
    restart: unless-stopped
    volumes:
      - /data/db:/bitnami/postgresqlINFO
Update all passwords and network parameters according to your organizational security standards.
Service Deployment 
Once the configuration file has been created, deploy the services using Docker Compose:
docker compose up -dVerify that all containers are running successfully:
docker compose psConfiguration of the Third Database Node (arbiter) 
This section describes the procedure to configure the Arbiter Node (pg-2) within the High Availability cluster.
Host Configuration 
Add the IP addresses of the remaining cluster nodes to the /etc/hosts file to ensure proper hostname resolution:
echo "a.b.c.d pg-0" >> /etc/hosts
echo "a.b.c.d pg-1" >> /etc/hostsINFO
Replace a.b.c.d with the actual IP addresses of the first (pg-0) and second (pg-1) nodes.
Directory Permissions 
Ensure that the PostgreSQL data directory is writable by the container process:
chmod 777 /data/dbDocker Compose Setup 
Create a dedicated directory for the PostgreSQL Docker Compose configuration:
mkdir /opt/pg/
cd /opt/pg/
vi docker-compose.yamlInsert the following configuration into the docker-compose.yaml file:
services:
  pg-2:
    ports:
      - 5432:5432
    image: bitnamilegacy/postgresql-repmgr:17
    environment:
      POSTGRESQL_POSTGRES_PASSWORD: adminpassword
      POSTGRESQL_USERNAME: apio_core
      POSTGRESQL_PASSWORD: password123
      POSTGRESQL_DATABASE: apio_core
      REPMGR_PASSWORD: repmgrpassword
      REPMGR_PRIMARY_HOST: pg-0
      REPMGR_PARTNER_NODES: pg-0,pg-1,pg-2
      REPMGR_NODE_NAME: pg-2
      REPMGR_NODE_NETWORK_NAME: pg-2
      REPMGR_NODE_TYPE: witness
    volumes:
      - /data/db:/bitnami/postgresql
    restart: unless-stoppedINFO
Update all passwords and network parameters according to your organizational security standards.
Service Deployment 
Once the configuration file has been created, deploy the services using Docker Compose:
docker compose up -dVerify that all containers are running successfully:
docker compose psSetup on APIO Core Servers 
Host Configuration 
Add the IP addresses of database nodes to the /etc/hosts file to ensure proper hostname resolution:
echo "a.b.c.d pg-0" >> /etc/hosts
echo "a.b.c.d pg-1" >> /etc/hostsDocker Compose Setup 
Append the following service configuration into the docker-compose.yaml file of Apio:
  db:
    image: bitnamilegacy/pgpool:4.6.3
    environment:
      PGPOOL_BACKEND_NODES: "0:pg-0:5432,1:pg-1:5432"
      PGPOOL_SR_CHECK_USER: apio_core
      PGPOOL_SR_CHECK_PASSWORD: password123
      PGPOOL_ENABLE_LDAP: no
      PGPOOL_POSTGRES_USERNAME: postgres
      PGPOOL_POSTGRES_PASSWORD: adminpassword
      PGPOOL_ADMIN_USERNAME: admin
      PGPOOL_ADMIN_PASSWORD: adminpassword
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "/opt/bitnami/scripts/pgpool/healthcheck.sh"]
      interval: 10s
      timeout: 5s
      retries: 5