Skip to content

Commit 32727b4

Browse files
committed
Be a drop-in replacement for upstream
- Updates environment variables to have the same names - Based on current docker-entrypoint.sh - Fixes #2
2 parents d8ccc1c + d308f7e commit 32727b4

File tree

3 files changed

+55
-44
lines changed

3 files changed

+55
-44
lines changed

docker-compose.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ services:
77
image: 'danieldent/postgres-replication'
88
restart: 'always'
99
environment:
10-
PGUSER: 'postgres'
11-
PGPASSWORD: 'postgres'
10+
POSTGRES_USER: 'postgres'
11+
POSTGRES_PASSWORD: 'postgres'
1212
PGDATA: '/var/lib/postgresql/data/pgdata'
1313
volumes:
1414
- '/var/lib/postgresql/data'
@@ -20,8 +20,8 @@ services:
2020
image: 'danieldent/postgres-replication'
2121
restart: 'always'
2222
environment:
23-
PGUSER: 'postgres'
24-
PGPASSWORD: 'postgres'
23+
POSTGRES_USER: 'postgres'
24+
POSTGRES_PASSWORD: 'postgres'
2525
PGDATA: '/var/lib/postgresql/data/pgdata'
2626
REPLICATE_FROM: 'pg-master'
2727
volumes:

docker-entrypoint.sh

+50-39
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
#!/bin/bash
2-
set -e
2+
3+
# Backwards compatibility for old variable names (deprecated)
4+
if [ "x$PGUSER" != "x" ]; then
5+
POSTGRES_USER=$PGUSER
6+
fi
7+
if [ "x$PGPASSWORD" != "x" ]; then
8+
POSTGRES_PASSWORD=$PGPASSWORD
9+
fi
310

411
# Based on official postgres package's entrypoint script (https://hub.docker.com/_/postgres/)
512
# Modified to be able to set up a slave. The docker-entrypoint-initdb.d hook provided is inadequate.
613

7-
set_listen_addresses() {
8-
sedEscapedValue="$(echo "$1" | sed 's/[\/&]/\\&/g')"
9-
sed -ri "s/^#?(listen_addresses\s*=\s*)\S+/\1'$sedEscapedValue'/" "$PGDATA/postgresql.conf"
10-
}
14+
set -e
15+
16+
if [ "${1:0:1}" = '-' ]; then
17+
set -- postgres "$@"
18+
fi
1119

1220
if [ "$1" = 'postgres' ]; then
1321
mkdir -p "$PGDATA"
22+
chmod 700 "$PGDATA"
1423
chown -R postgres "$PGDATA"
1524

1625
chmod g+s /run/postgresql
@@ -19,25 +28,24 @@ if [ "$1" = 'postgres' ]; then
1928
# look specifically for PG_VERSION, as it is expected in the DB dir
2029
if [ ! -s "$PGDATA/PG_VERSION" ]; then
2130
if [ "x$REPLICATE_FROM" == "x" ]; then
22-
gosu postgres initdb
23-
else
24-
until ping -c 1 -W 1 ${REPLICATE_FROM}
25-
do
26-
echo "Waiting for master to ping..."
27-
sleep 1s
28-
done
29-
until gosu postgres pg_basebackup -h ${REPLICATE_FROM} -D ${PGDATA} -U ${PGUSER} -vP
30-
do
31-
echo "Waiting for master to connect..."
32-
sleep 1s
33-
done
34-
chmod 700 ${PGDATA}
35-
fi
31+
eval "gosu postgres initdb $POSTGRES_INITDB_ARGS"
32+
else
33+
until ping -c 1 -W 1 ${REPLICATE_FROM}
34+
do
35+
echo "Waiting for master to ping..."
36+
sleep 1s
37+
done
38+
until gosu postgres pg_basebackup -h ${REPLICATE_FROM} -D ${PGDATA} -U ${POSTGRES_USER} -vP
39+
do
40+
echo "Waiting for master to connect..."
41+
sleep 1s
42+
done
43+
fi
3644

3745
# check password first so we can output the warning before postgres
3846
# messes it up
39-
if [ "$PGPASSWORD" ]; then
40-
pass="PASSWORD '$PGPASSWORD'"
47+
if [ "$POSTGRES_PASSWORD" ]; then
48+
pass="PASSWORD '$POSTGRES_PASSWORD'"
4149
authMethod=md5
4250
else
4351
# The - option suppresses leading tabs but *not* spaces. :)
@@ -50,7 +58,7 @@ if [ "$1" = 'postgres' ]; then
5058
effectively any other container on the same
5159
system.
5260
53-
Use "-e PGPASSWORD=password" to set
61+
Use "-e POSTGRES_PASSWORD=password" to set
5462
it in "docker run".
5563
****************************************************
5664
EOWARN
@@ -65,49 +73,52 @@ if [ "$1" = 'postgres' ]; then
6573
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA/pg_hba.conf"
6674

6775
# internal start of server in order to allow set-up using psql-client
68-
# does not listen on TCP/IP and waits until start finishes
76+
# does not listen on external TCP/IP and waits until start finishes
6977
gosu postgres pg_ctl -D "$PGDATA" \
70-
-o "-c listen_addresses=''" \
78+
-o "-c listen_addresses='localhost'" \
7179
-w start
7280

73-
: ${PGUSER:=postgres}
74-
: ${POSTGRES_DB:=$PGUSER}
75-
export PGUSER POSTGRES_DB
81+
: ${POSTGRES_USER:=postgres}
82+
: ${POSTGRES_DB:=$POSTGRES_USER}
83+
export POSTGRES_USER POSTGRES_DB
84+
85+
psql=( psql -v ON_ERROR_STOP=1 )
7686

7787
if [ "$POSTGRES_DB" != 'postgres' ]; then
78-
psql --username postgres <<-EOSQL
88+
"${psql[@]}" --username postgres <<-EOSQL
7989
CREATE DATABASE "$POSTGRES_DB" ;
8090
EOSQL
8191
echo
8292
fi
8393

84-
if [ "$PGUSER" = 'postgres' ]; then
94+
if [ "$POSTGRES_USER" = 'postgres' ]; then
8595
op='ALTER'
8696
else
8797
op='CREATE'
8898
fi
89-
90-
psql --username postgres <<-EOSQL
91-
$op USER "$PGUSER" WITH SUPERUSER $pass ;
99+
"${psql[@]}" --username postgres <<-EOSQL
100+
$op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
92101
EOSQL
93102
echo
103+
104+
fi
94105

95-
fi
106+
psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )
96107

97108
echo
98109
for f in /docker-entrypoint-initdb.d/*; do
99110
case "$f" in
100-
*.sh) echo "$0: running $f"; . "$f" ;;
101-
*.sql) echo "$0: running $f"; psql --username "$PGUSER" --dbname "$POSTGRES_DB" < "$f" && echo ;;
102-
*) echo "$0: ignoring $f" ;;
111+
*.sh) echo "$0: running $f"; . "$f" ;;
112+
*.sql) echo "$0: running $f"; "${psql[@]}" < "$f"; echo ;;
113+
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
114+
*) echo "$0: ignoring $f" ;;
103115
esac
104116
echo
105117
done
106118

107-
if [ "x$REPLICATE_FROM" == "x" ]; then
119+
if [ "x$REPLICATE_FROM" == "x" ]; then
108120
gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop
109-
fi
110-
set_listen_addresses '*'
121+
fi
111122

112123
echo
113124
echo 'PostgreSQL init process complete; ready for start up.'

setup-replication.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ else
1313

1414
cat > ${PGDATA}/recovery.conf <<EOF
1515
standby_mode = on
16-
primary_conninfo = 'host=${REPLICATE_FROM} port=5432 user=${PGUSER} password=${PGPASSWORD}'
16+
primary_conninfo = 'host=${REPLICATE_FROM} port=5432 user=${POSTGRES_USER} password=${POSTGRES_PASSWORD}'
1717
trigger_file = '/tmp/touch_me_to_promote_to_me_master'
1818
EOF
1919
chown postgres ${PGDATA}/recovery.conf

0 commit comments

Comments
 (0)