From 274eab725f9d68532ebdb21ec9a970df76b7ac09 Mon Sep 17 00:00:00 2001 From: toast-ts <96593068+toast-ts@users.noreply.github.com> Date: Sat, 27 Jan 2024 19:21:22 +1100 Subject: [PATCH] Setup a master/slave replication in Postgres --- docker-compose.yml | 4 +++- .../configs/postgres-hba.conf | 11 +++++++++ postgres-replica-stuff/configs/postgres.conf | 23 +++++++++++++++++++ postgres-replica-stuff/docker-compose.yml | 15 ++++++++++++ .../postgres-setup-replica.sql | 8 +++++++ tsconfig.json | 8 +++---- 6 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 postgres-replica-stuff/configs/postgres-hba.conf create mode 100644 postgres-replica-stuff/configs/postgres.conf create mode 100644 postgres-replica-stuff/docker-compose.yml create mode 100644 postgres-replica-stuff/postgres-setup-replica.sql diff --git a/docker-compose.yml b/docker-compose.yml index 662da97..7a61ddc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,13 +6,15 @@ services: ports: - 6379:6379/tcp db: - container_name: postgres-db + container_name: postgres-master image: postgres:16.1-alpine3.19 restart: unless-stopped ports: - 5432:5432/tcp volumes: - /var/lib/docker/volumes/daggerbot-db:/var/lib/postgresql/data:rw + - ./postgres-replica-stuff/configs/postgres-hba.conf:/var/lib/postgresql/data/pg_hba.conf:rw + - ./postgres-replica-stuff/configs/postgres.conf:/var/lib/postgresql/data/postgresql.conf:rw environment: POSTGRES_USER: ${POSTGRES_USER} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} diff --git a/postgres-replica-stuff/configs/postgres-hba.conf b/postgres-replica-stuff/configs/postgres-hba.conf new file mode 100644 index 0000000..82089e7 --- /dev/null +++ b/postgres-replica-stuff/configs/postgres-hba.conf @@ -0,0 +1,11 @@ +# TYPE DATABASE USER ADDRESS METHOD +# "local" is for Unix domain socket connections only +local all all trust +# IPv4 local connections: +host all all 127.0.0.1/32 trust +# Allow replication connections from localhost, by a user with the +# replication privilege. +local replication all trust +host replication replicator 0.0.0.0/0 trust + +host all all all scram-sha-256 diff --git a/postgres-replica-stuff/configs/postgres.conf b/postgres-replica-stuff/configs/postgres.conf new file mode 100644 index 0000000..192ca67 --- /dev/null +++ b/postgres-replica-stuff/configs/postgres.conf @@ -0,0 +1,23 @@ +listen_addresses = '*' + +max_connections = 128 +shared_buffers = 256MB +effective_cache_size = 4GB +maintenance_work_mem = 128MB +work_mem = 8MB +checkpoint_completion_target = 0.9 +wal_buffers = 16MB +default_statistics_target = 100 +random_page_cost = 2.5 +effective_io_concurrency = 1 +min_wal_size = 64MB +max_wal_size = 2GB +max_worker_processes = 6 +max_parallel_workers_per_gather = 4 + +# Standby replication setup +wal_level = replica +max_wal_senders = 12 +max_replication_slots = 12 +hot_standby = on +hot_standby_feedback = on diff --git a/postgres-replica-stuff/docker-compose.yml b/postgres-replica-stuff/docker-compose.yml new file mode 100644 index 0000000..54f593f --- /dev/null +++ b/postgres-replica-stuff/docker-compose.yml @@ -0,0 +1,15 @@ +# This is for the Slave node, Master node is in the root directory of the same file. +services: + db-slave: + container_name: postgres-slave + image: postgres:16.1-alpine3.19 + restart: unless-stopped + ports: + - 5433:5432/tcp + volumes: + - /var/lib/docker/volumes/daggerbot-db-slave:/var/lib/postgresql/data:rw + - ./configs/postgres-hba.conf:/var/lib/postgresql/data/pg_hba.conf:rw + - ./configs/postgres.conf:/var/lib/postgresql/data/postgresql.conf:rw + environment: + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB} diff --git a/postgres-replica-stuff/postgres-setup-replica.sql b/postgres-replica-stuff/postgres-setup-replica.sql new file mode 100644 index 0000000..6521c02 --- /dev/null +++ b/postgres-replica-stuff/postgres-setup-replica.sql @@ -0,0 +1,8 @@ +-- This is for the slave. +CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD ''; + +-- This is for the master. +SELECT * FROM pg_create_physical_replication_slot('replication_slot_slave'); +SELECT * FROM pg_replication_slots; + +-- https://medium.com/swlh/postgresql-replication-with-docker-c6a904becf77 diff --git a/tsconfig.json b/tsconfig.json index 9db2728..bdd2702 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,11 +12,11 @@ "target": "ES2022", "module": "ESNext", "baseUrl": "./", - "rootDir": "src/", - "outDir": "dist/", + "rootDir": "src", + "outDir": "dist", "moduleResolution": "bundler", "typeRoots": [ "./src/interfaces.d.ts" ] }, - "include": [ "src/" ], - "exclude": [ ".yarn/cache", ".yarn/unplugged", ".git", "src/errorBlocklist.json", "src/config.json", "src/DB-Beta.config.json" ] + "include": [ "src" ], + "exclude": [ ".yarn/cache", ".yarn/unplugged", ".git", "src/*.json", "postgres-replica-stuff" ] }