Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

How to Set Up a PostgreSQL Replication Architecture Database in a Docker-Compose Environment

1 min read

A replication architecture database is a valuable approach for separating read and write responsibilities into different database servers. This allows for efficient scaling of the database based on the type of operations or transactions that occur. In this guide, we will set up a replication architecture database in a Docker environment using Docker Compose, specifically for our development environment.

Technologies Used

  • PostgreSQL
  • Docker Compose

Getting Started

  • Install Docker Desktop

Docker Compose Setup

Begin by creating a docker-compose.yaml file to define the services, configurations, and dependencies for the PostgreSQL replication setup.

version: "3"
services:
  postgresql-master:
    image: bitnami/postgresql
    restart: always
    ports:
      - '5432:5432'
    volumes:
      - postgresql_master_data:/bitnami/postgresql
      - ./db.sql:/docker-entrypoint-initdb.d/db.sql
    environment:
      - POSTGRESQL_PGAUDIT_LOG=READ,WRITE
      - POSTGRESQL_LOG_HOSTNAME=true
      - POSTGRESQL_REPLICATION_MODE=master
      - POSTGRESQL_REPLICATION_USER=repl_user
      - POSTGRESQL_REPLICATION_PASSWORD=repl_user
      - POSTGRESQL_USERNAME=postgres
      - POSTGRESQL_PASSWORD=postgres
      - POSTGRESQL_DATABASE=development_database
      - ALLOW_EMPTY_PASSWORD=yes

  postgresql-slave:
    image: bitnami/postgresql
    restart: always
    ports:
      - '5432'
    depends_on:
      - postgresql-master
    environment:
      - POSTGRESQL_PASSWORD=postgres
      - POSTGRESQL_MASTER_HOST=postgresql-master
      - POSTGRESQL_PGAUDIT_LOG=READ
      - POSTGRESQL_LOG_HOSTNAME=true
      - POSTGRESQL_REPLICATION_MODE=slave
      - POSTGRESQL_REPLICATION_USER=repl_user
      - POSTGRESQL_REPLICATION_PASSWORD=repl_user
      - POSTGRESQL_MASTER_PORT_NUMBER=5432
      - ALLOW_EMPTY_PASSWORD=yes

volumes:
  postgresql_master_data:
    driver: local

This docker-compose.yaml file defines two services: postgresql-master and postgresql-slave. The postgresql-master service acts as the master node, while the postgresql-slave service acts as the slave node.

Explanation of Key Configurations

  • PostgreSQL Master Service:
    • POSTGRESQL_REPLICATION_MODE=master: Specifies that this PostgreSQL instance is the master.
    • POSTGRESQL_REPLICATION_USER and POSTGRESQL_REPLICATION_PASSWORD: Credentials for replication.
    • POSTGRESQL_DATABASE: Name of the development database.
    • POSTGRESQL_PGAUDIT_LOG=READ,WRITE: Enables audit logging for read and write operations.
  • PostgreSQL Slave Service:
    • POSTGRESQL_REPLICATION_MODE=slave: Specifies that this PostgreSQL instance is the slave.
    • POSTGRESQL_MASTER_HOST: Points to the host of the master node.
    • POSTGRESQL_MASTER_PORT_NUMBER: Port number on which the master node is running.
    • POSTGRESQL_PGAUDIT_LOG=READ: Enables audit logging for read operations.

Running the Setup

To start the PostgreSQL replication setup, navigate to the directory containing the docker-compose.yaml file and run:

docker-compose up -d

This will launch the services in detached mode. You can then access the master and slave databases separately, and any changes made in the master will be replicated to the slave.

This Docker-Compose setup provides a convenient way to implement PostgreSQL database replication for your development environment. Adjust the configurations based on your specific requirements and scaling needs.

Keep Reading

  • Testcontainers and Playwright

    Testcontainers and Playwright

    Discover how Testcontainers-Playwright simplifies browser automation and testing without local Playwright installations. Learn about its features, limitations, compatibility, and usage with code examples.

    Read More

  • Getting Started with the Low-Cost RPLIDAR Using NVIDIA Jetson Nano

    Getting Started with the Low-Cost RPLIDAR Using NVIDIA Jetson Nano

    Conclusion Getting started with low-code RPlidar with Jetson Nano is an exciting journey that can open up a wide range of possibilities for building robotics projects. In this blog post, we covered the basic steps to get started with low-code RPlidar with Jetson Nano, including setting up ROS, installing the RPlidar driver and viewing RPlidar…

    Read More

  • Docker and Wasm Containers – Better Together

    Docker and Wasm Containers – Better Together

    Learn how Docker Desktop and CLI both manages Linux containers and Wasm containers side by side.

    Read More

  • Does Kubernetes Have a Future? A Technical Deep Dive and Analysis

    Does Kubernetes Have a Future? A Technical Deep Dive and Analysis

    Since its inception in 2014, Kubernetes has revolutionized the way we think about deploying and managing containerized applications. As the orchestrator of choice for many organizations, Kubernetes has become synonymous with cloud-native infrastructure. However, with the rapid evolution of technology and new paradigms emerging in the tech landscape, it’s fair to ask: does Kubernetes have…

    Read More

  • Web Development with WebAssembly: A New Era of Performance and Portability

    Web Development with WebAssembly: A New Era of Performance and Portability

    Web development has come a long way, but the journey is far from over. We’ve seen the rise of dynamic websites, responsive designs, and JavaScript-heavy applications that can do everything from playing video games to managing business data. But there’s always been one lingering challenge: performance. As web applications become more complex, achieving optimal performance…

    Read More

  • How Cloud-Native Technologies Enhance Connectivity for Travelers in Indonesia

    How Cloud-Native Technologies Enhance Connectivity for Travelers in Indonesia

    With its breathtaking landscapes and rich cultural heritage, Indonesia is a magnet for travelers. In a world where staying connected is as important as exploring new destinations, advancements in cloud-native technologies have transformed how tourists experience seamless connectivity. Solutions like the Indonesia eSIM offer a modern, hassle-free way to stay online, allowing travelers to share…

    Read More

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server
Index