diff --git a/docker-compose.replication.yml b/docker-compose.replication.yml new file mode 100644 index 0000000..3cff15e --- /dev/null +++ b/docker-compose.replication.yml @@ -0,0 +1,57 @@ +version: "3.7" + +services: + + master-db: + restart: always + container_name: master-db + build: . + ports: + - 5432:5432 + environment: + - DEBUG=true + + - DB_USER=dbuser + - DB_PASS=dbuserpass + - DB_NAME=dbname + - DB_TEMPLATE= + + - DB_EXTENSION= + + - REPLICATION_MODE= + - REPLICATION_USER=repluser + - REPLICATION_PASS=repluserpass + - REPLICATION_SSLMODE= + # command: "--wal_keep_segments=32 --logging_collector=off" + volumes: + - ./postgresql-master:/var/lib/postgresql + + slave-db: + restart: always + container_name: slave-db + build: . + ports: + - 9432:5432 + depends_on: + - master-db + links: + - master-db + environment: + - DEBUG=true + + - DB_USER= + - DB_PASS= + - DB_NAME= + - DB_TEMPLATE= + + - DB_EXTENSION= + + - REPLICATION_MODE=slave + - REPLICATION_HOST=master-db + - REPLICATION_PORT=5432 + - REPLICATION_USER=repluser + - REPLICATION_PASS=repluserpass + - REPLICATION_SSLMODE=prefer + # command: "--wal_keep_segments=32 --logging_collector=off" + volumes: + - ./postgresql-slave:/var/lib/postgresql \ No newline at end of file diff --git a/runtime/functions b/runtime/functions index 65069e3..89a65a2 100755 --- a/runtime/functions +++ b/runtime/functions @@ -6,7 +6,7 @@ source ${PG_APP_HOME}/env-defaults PG_CONF=${PG_DATADIR}/postgresql.conf PG_HBA_CONF=${PG_DATADIR}/pg_hba.conf PG_IDENT_CONF=${PG_DATADIR}/pg_ident.conf -PG_RECOVERY_CONF=${PG_DATADIR}/recovery.conf +PG_STANDBY_SIGNAL=${PG_DATADIR}/standby.signal ## Execute command as PG_USER exec_as_postgres() { @@ -76,22 +76,6 @@ set_postgresql_param() { fi } -set_recovery_param() { - local key=${1} - local value=${2} - local hide=${3} - if [[ -n ${value} ]]; then - local current=$(exec_as_postgres sed -n -e "s/^\(.*\)\(${key}=\)\([^ ']*\)\(.*\)$/\3/p" ${PG_RECOVERY_CONF}) - if [[ "${current}" != "${value}" ]]; then - case ${hide} in - true) echo "‣ Setting primary_conninfo parameter: ${key}" ;; - *) echo "‣ Setting primary_conninfo parameter: ${key} = '${value}'" ;; - esac - exec_as_postgres sed -i "s|${key}=[^ ']*|${key}=${value}|" ${PG_RECOVERY_CONF} - fi - fi -} - set_hba_param() { local value=${1} if ! grep -q "$(sed "s| | \\\+|g" <<< ${value})" ${PG_HBA_CONF}; then @@ -266,22 +250,14 @@ set_resolvconf_perms() { configure_recovery() { if [[ ${REPLICATION_MODE} == slave ]]; then echo "Configuring recovery..." - if [[ ! -f ${PG_RECOVERY_CONF} ]]; then - # initialize recovery.conf on the firstrun (slave only) - exec_as_postgres touch ${PG_RECOVERY_CONF} - ( echo "standby_mode = 'on'"; - echo "primary_conninfo = 'host=${REPLICATION_HOST} port=${REPLICATION_PORT} user=${REPLICATION_USER} password=${REPLICATION_PASS} sslmode=${REPLICATION_SSLMODE}'"; - ) > ${PG_RECOVERY_CONF} - else - set_recovery_param "host" "${REPLICATION_HOST}" - set_recovery_param "port" "${REPLICATION_PORT}" - set_recovery_param "user" "${REPLICATION_USER}" - set_recovery_param "password" "${REPLICATION_PASS}" "true" - set_recovery_param "sslmode" "${REPLICATION_SSLMODE}" + if [[ ! -f ${PG_STANDBY_SIGNAL} ]]; then + # initialize standby.signal on the firstrun (slave only) + exec_as_postgres touch ${PG_STANDBY_SIGNAL} fi + set_postgresql_param "primary_conninfo" "host=${REPLICATION_HOST} port=${REPLICATION_PORT} user=${REPLICATION_USER} password=${REPLICATION_PASS} sslmode=${REPLICATION_SSLMODE}" "false" else - # recovery.conf can only exist on a slave node, its existence otherwise causes problems - rm -rf ${PG_RECOVERY_CONF} + # standby.signal can only exist on a slave node, its existence otherwise causes problems + rm -rf ${PG_STANDBY_SIGNAL} fi }