Files
deploy/cluster/postgres/standby-entrypoint.sh
T
2026-06-25 09:41:16 +08:00

38 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
set -e
# PostgreSQL standby bootstrap entrypoint
# If data dir is empty, pull base backup from primary, then hand off to postgres entrypoint
PRIMARY_HOST="${PG_PRIMARY_HOST:-host1}"
PRIMARY_PORT="${PG_PRIMARY_PORT:-5432}"
REPL_PASSWORD="${REPL_PASSWORD:-Bjang09@686-Repl}"
PGDATA="/var/lib/postgresql/data"
if [ ! -s "$PGDATA/PG_VERSION" ]; then
echo "[standby] Data directory empty, bootstrapping from primary $PRIMARY_HOST:$PRIMARY_PORT"
# Wait for primary to accept connections
for i in $(seq 1 30); do
if PGPASSWORD="$REPL_PASSWORD" pg_isready -h "$PRIMARY_HOST" -p "$PRIMARY_PORT" -U replicator >/dev/null 2>&1; then
break
fi
echo "[standby] Waiting for primary to be ready (attempt $i/30)..."
sleep 2
done
# Pull base backup from primary
echo "[standby] Running pg_basebackup..."
PGPASSWORD="$REPL_PASSWORD" pg_basebackup -h "$PRIMARY_HOST" -p "$PRIMARY_PORT" \
-U replicator -D "$PGDATA" -P -R \
-S replication_slot_slave --checkpoint=fast --wal-method=stream
# Ensure standby signal file exists
touch "$PGDATA/standby.signal"
echo "[standby] Bootstrap complete, starting standby..."
fi
# Hand off to the standard postgres entrypoint
exec /usr/local/bin/docker-entrypoint.sh "$@"