#!/bin/bash if [[ -z "${MASTER_ADDRESS}" ]]; then echo "Cannot startup, need MASTER_ADDRESS" else MASTER_ADDRESS="${MASTER_ADDRESS}" echo "copy from ${MASTER_ADDRESS}" fi echo "init default scripts" export POSTGRES_PASSWORD="replica" docker-entrypoint.sh postgres -v echo "add user" #add replication user cat <> /var/lib/postgresql/data/pg_hba.conf host replication postgres $MASTER_ADDRESS/24 trust host all all all md5 EOT echo "add replica rules" #add replica rules cat <> /var/lib/postgresql/data/postgresql.conf listen_addresses = '*' wal_level = hot_standby archive_mode = on archive_command = 'cd .' max_wal_senders = 8 hot_standby = on max_connections = 1000 EOT #create copy mkdir /tmp/pg_copy cp /var/lib/postgresql/data/*.conf /tmp/pg_copy echo "copy from master" rm -rf /var/lib/postgresql/data mkdir /var/lib/postgresql/data pg_basebackup -v -P -R -X stream -c fast -h ${MASTER_ADDRESS} -U ${MASTER_USER} -D /var/lib/postgresql/data rm /var/lib/postgresql/data/*.conf cp /tmp/pg_copy/*.conf /var/lib/postgresql/data/ chown postgres:postgres -R /var/lib/postgresql/data echo "run postgres" #run postgres docker-entrypoint.sh postgres