Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue/146 #147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docker-compose.replication.yml
Original file line number Diff line number Diff line change
@@ -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
38 changes: 7 additions & 31 deletions runtime/functions
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down