From 13bc883d64a7b18e41581a4dca14e0f7e4e31319 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sun, 25 Jan 2026 16:19:31 +0000 Subject: [PATCH] first commit --- replicator-side/docker-compose.yml | 19 +++++++++++++++ replicator-side/init-replica.sh | 27 +++++++++++++++++++++ replicator-side/pg_hba.conf | 4 ++++ replicator-side/postgresql.conf | 15 ++++++++++++ replicator-side/replica-entrypoint.sh | 34 +++++++++++++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 replicator-side/docker-compose.yml create mode 100755 replicator-side/init-replica.sh create mode 100644 replicator-side/pg_hba.conf create mode 100644 replicator-side/postgresql.conf create mode 100755 replicator-side/replica-entrypoint.sh diff --git a/replicator-side/docker-compose.yml b/replicator-side/docker-compose.yml new file mode 100644 index 0000000..d20deb7 --- /dev/null +++ b/replicator-side/docker-compose.yml @@ -0,0 +1,19 @@ +services: + postgres-replica: + image: postgres:17 + container_name: postgres-replica + restart: unless-stopped + ports: + - "7447:5432" + environment: + PRIMARY_HOST: 57.128.212.174 + PRIMARY_PORT: 7447 + REPLICATION_USER: replicator + REPLICATION_PASSWORD: replicator_password + volumes: + - ./data:/var/lib/postgresql/data + - ./postgresql.conf:/etc/postgresql/postgresql.conf:ro + - ./pg_hba.conf:/etc/postgresql/pg_hba.conf:ro + - ./replica-entrypoint.sh:/replica-entrypoint.sh:ro + command: ["/bin/bash", "/replica-entrypoint.sh"] + diff --git a/replicator-side/init-replica.sh b/replicator-side/init-replica.sh new file mode 100755 index 0000000..691ac1e --- /dev/null +++ b/replicator-side/init-replica.sh @@ -0,0 +1,27 @@ +#!/bin/bash +set -e + +DATA_DIR="/var/lib/postgresql/data" + +if [ -f "$DATA_DIR/PG_VERSION" ]; then + echo "Data directory already initialized, skipping base backup" + exit 0 +fi + +echo "Initializing replica from primary..." + +rm -rf "$DATA_DIR"/* +export PGPASSWORD="$REPLICATION_PASSWORD" + +pg_basebackup \ + -h "$PRIMARY_HOST" \ + -p "$PRIMARY_PORT" \ + -U "$REPLICATION_USER" \ + -D "$DATA_DIR" \ + -Fp \ + -Xs \ + -R \ + -P + +echo "Replica base backup completed." + diff --git a/replicator-side/pg_hba.conf b/replicator-side/pg_hba.conf new file mode 100644 index 0000000..868237b --- /dev/null +++ b/replicator-side/pg_hba.conf @@ -0,0 +1,4 @@ +local all all trust +host all all 127.0.0.1/32 trust +host all all ::1/128 trust + diff --git a/replicator-side/postgresql.conf b/replicator-side/postgresql.conf new file mode 100644 index 0000000..12e8cb6 --- /dev/null +++ b/replicator-side/postgresql.conf @@ -0,0 +1,15 @@ +listen_addresses = '*' +port = 5432 + +wal_level = replica +hot_standby = on +max_connections = 100 + +hot_standby_feedback = on +wal_keep_size = 256MB + +logging_collector = on +log_destination = 'stderr' + +hba_file = '/etc/postgresql/pg_hba.conf' + diff --git a/replicator-side/replica-entrypoint.sh b/replicator-side/replica-entrypoint.sh new file mode 100755 index 0000000..8de301f --- /dev/null +++ b/replicator-side/replica-entrypoint.sh @@ -0,0 +1,34 @@ +#!/bin/bash +set -e + +DATA_DIR="/var/lib/postgresql/data" + +if [ ! -f "$DATA_DIR/PG_VERSION" ]; then + echo "Running pg_basebackup from primary..." + + rm -rf "$DATA_DIR"/* + export PGPASSWORD="$REPLICATION_PASSWORD" + + pg_basebackup \ + -h "$PRIMARY_HOST" \ + -p "$PRIMARY_PORT" \ + -U "$REPLICATION_USER" \ + -D "$DATA_DIR" \ + -Fp \ + -Xs \ + -R \ + -P + + echo "Fixing ownership and permissions..." + chown -R postgres:postgres "$DATA_DIR" + chmod 0700 "$DATA_DIR" +else + echo "Replica already initialized" +fi + +echo "Starting postgres as postgres user..." + +exec gosu postgres postgres \ + -c config_file=/etc/postgresql/postgresql.conf \ + -c hba_file=/etc/postgresql/pg_hba.conf +