Skip to content

Commit a82c28e

Browse files
committed
Merge pull request #75 from infosiftr/better-initdb
Improve initdb logic to properly source *.sql files as well, without the use of --single
2 parents 574a975 + 66c7b2d commit a82c28e

7 files changed

+308
-70
lines changed

9.0/docker-entrypoint.sh

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
#!/bin/bash
22
set -e
33

4+
set_listen_addresses() {
5+
sedEscapedValue="$(echo "$1" | sed 's/[\/&]/\\&/g')"
6+
sed -ri "s/^#?(listen_addresses\s*=\s*)\S+/\1'$sedEscapedValue'/" "$PGDATA/postgresql.conf"
7+
}
8+
49
if [ "$1" = 'postgres' ]; then
510
mkdir -p "$PGDATA"
611
chown -R postgres "$PGDATA"
712

813
chmod g+s /run/postgresql
9-
chown -R postgres:postgres /run/postgresql
14+
chown -R postgres /run/postgresql
1015

1116
# look specifically for PG_VERSION, as it is expected in the DB dir
1217
if [ ! -s "$PGDATA/PG_VERSION" ]; then
1318
gosu postgres initdb
1419

15-
sed -ri "s/^#(listen_addresses\s*=\s*)\S+/\1'*'/" "$PGDATA"/postgresql.conf
16-
1720
# check password first so we can output the warning before postgres
1821
# messes it up
1922
if [ "$POSTGRES_PASSWORD" ]; then
@@ -39,11 +42,29 @@ if [ "$1" = 'postgres' ]; then
3942
authMethod=trust
4043
fi
4144

45+
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA/pg_hba.conf"
46+
47+
set_listen_addresses '' # we're going to start up postgres, but it's not ready for use yet (this is initialization), so don't listen to the outside world yet
48+
49+
gosu postgres "$@" &
50+
pid="$!"
51+
for i in {30..0}; do
52+
if echo 'SELECT 1' | psql --username postgres &> /dev/null; then
53+
break
54+
fi
55+
echo 'PostgreSQL init process in progress...'
56+
sleep 1
57+
done
58+
if [ "$i" = 0 ]; then
59+
echo >&2 'PostgreSQL init process failed'
60+
exit 1
61+
fi
62+
4263
: ${POSTGRES_USER:=postgres}
4364
: ${POSTGRES_DB:=$POSTGRES_USER}
4465

4566
if [ "$POSTGRES_DB" != 'postgres' ]; then
46-
gosu postgres postgres --single -jE <<-EOSQL
67+
psql --username postgres <<-EOSQL
4768
CREATE DATABASE "$POSTGRES_DB" ;
4869
EOSQL
4970
echo
@@ -55,18 +76,31 @@ if [ "$1" = 'postgres' ]; then
5576
op='CREATE'
5677
fi
5778

58-
gosu postgres postgres --single -jE <<-EOSQL
79+
psql --username postgres <<-EOSQL
5980
$op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
6081
EOSQL
6182
echo
6283

63-
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA"/pg_hba.conf
84+
echo
85+
for f in /docker-entrypoint-initdb.d/*; do
86+
case "$f" in
87+
*.sh) echo "$0: running $f"; . "$f" ;;
88+
*.sql) echo "$0: running $f"; psql --username postgres --dbname "$POSTGRES_DB" < "$f" && echo ;;
89+
*) echo "$0: ignoring $f" ;;
90+
esac
91+
echo
92+
done
6493

65-
if [ -d /docker-entrypoint-initdb.d ]; then
66-
for f in /docker-entrypoint-initdb.d/*.sh; do
67-
[ -f "$f" ] && . "$f"
68-
done
94+
if ! kill -s TERM "$pid" || ! wait "$pid"; then
95+
echo >&2 'PostgreSQL init process failed'
96+
exit 1
6997
fi
98+
99+
set_listen_addresses '*'
100+
101+
echo
102+
echo 'PostgreSQL init process complete; ready for start up.'
103+
echo
70104
fi
71105

72106
exec gosu postgres "$@"

9.1/docker-entrypoint.sh

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
#!/bin/bash
22
set -e
33

4+
set_listen_addresses() {
5+
sedEscapedValue="$(echo "$1" | sed 's/[\/&]/\\&/g')"
6+
sed -ri "s/^#?(listen_addresses\s*=\s*)\S+/\1'$sedEscapedValue'/" "$PGDATA/postgresql.conf"
7+
}
8+
49
if [ "$1" = 'postgres' ]; then
510
mkdir -p "$PGDATA"
611
chown -R postgres "$PGDATA"
712

813
chmod g+s /run/postgresql
9-
chown -R postgres:postgres /run/postgresql
14+
chown -R postgres /run/postgresql
1015

1116
# look specifically for PG_VERSION, as it is expected in the DB dir
1217
if [ ! -s "$PGDATA/PG_VERSION" ]; then
1318
gosu postgres initdb
1419

15-
sed -ri "s/^#(listen_addresses\s*=\s*)\S+/\1'*'/" "$PGDATA"/postgresql.conf
16-
1720
# check password first so we can output the warning before postgres
1821
# messes it up
1922
if [ "$POSTGRES_PASSWORD" ]; then
@@ -39,11 +42,29 @@ if [ "$1" = 'postgres' ]; then
3942
authMethod=trust
4043
fi
4144

45+
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA/pg_hba.conf"
46+
47+
set_listen_addresses '' # we're going to start up postgres, but it's not ready for use yet (this is initialization), so don't listen to the outside world yet
48+
49+
gosu postgres "$@" &
50+
pid="$!"
51+
for i in {30..0}; do
52+
if echo 'SELECT 1' | psql --username postgres &> /dev/null; then
53+
break
54+
fi
55+
echo 'PostgreSQL init process in progress...'
56+
sleep 1
57+
done
58+
if [ "$i" = 0 ]; then
59+
echo >&2 'PostgreSQL init process failed'
60+
exit 1
61+
fi
62+
4263
: ${POSTGRES_USER:=postgres}
4364
: ${POSTGRES_DB:=$POSTGRES_USER}
4465

4566
if [ "$POSTGRES_DB" != 'postgres' ]; then
46-
gosu postgres postgres --single -jE <<-EOSQL
67+
psql --username postgres <<-EOSQL
4768
CREATE DATABASE "$POSTGRES_DB" ;
4869
EOSQL
4970
echo
@@ -55,18 +76,31 @@ if [ "$1" = 'postgres' ]; then
5576
op='CREATE'
5677
fi
5778

58-
gosu postgres postgres --single -jE <<-EOSQL
79+
psql --username postgres <<-EOSQL
5980
$op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
6081
EOSQL
6182
echo
6283

63-
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA"/pg_hba.conf
84+
echo
85+
for f in /docker-entrypoint-initdb.d/*; do
86+
case "$f" in
87+
*.sh) echo "$0: running $f"; . "$f" ;;
88+
*.sql) echo "$0: running $f"; psql --username postgres --dbname "$POSTGRES_DB" < "$f" && echo ;;
89+
*) echo "$0: ignoring $f" ;;
90+
esac
91+
echo
92+
done
6493

65-
if [ -d /docker-entrypoint-initdb.d ]; then
66-
for f in /docker-entrypoint-initdb.d/*.sh; do
67-
[ -f "$f" ] && . "$f"
68-
done
94+
if ! kill -s TERM "$pid" || ! wait "$pid"; then
95+
echo >&2 'PostgreSQL init process failed'
96+
exit 1
6997
fi
98+
99+
set_listen_addresses '*'
100+
101+
echo
102+
echo 'PostgreSQL init process complete; ready for start up.'
103+
echo
70104
fi
71105

72106
exec gosu postgres "$@"

9.2/docker-entrypoint.sh

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
#!/bin/bash
22
set -e
33

4+
set_listen_addresses() {
5+
sedEscapedValue="$(echo "$1" | sed 's/[\/&]/\\&/g')"
6+
sed -ri "s/^#?(listen_addresses\s*=\s*)\S+/\1'$sedEscapedValue'/" "$PGDATA/postgresql.conf"
7+
}
8+
49
if [ "$1" = 'postgres' ]; then
510
mkdir -p "$PGDATA"
611
chown -R postgres "$PGDATA"
712

813
chmod g+s /run/postgresql
9-
chown -R postgres:postgres /run/postgresql
14+
chown -R postgres /run/postgresql
1015

1116
# look specifically for PG_VERSION, as it is expected in the DB dir
1217
if [ ! -s "$PGDATA/PG_VERSION" ]; then
1318
gosu postgres initdb
1419

15-
sed -ri "s/^#(listen_addresses\s*=\s*)\S+/\1'*'/" "$PGDATA"/postgresql.conf
16-
1720
# check password first so we can output the warning before postgres
1821
# messes it up
1922
if [ "$POSTGRES_PASSWORD" ]; then
@@ -39,11 +42,29 @@ if [ "$1" = 'postgres' ]; then
3942
authMethod=trust
4043
fi
4144

45+
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA/pg_hba.conf"
46+
47+
set_listen_addresses '' # we're going to start up postgres, but it's not ready for use yet (this is initialization), so don't listen to the outside world yet
48+
49+
gosu postgres "$@" &
50+
pid="$!"
51+
for i in {30..0}; do
52+
if echo 'SELECT 1' | psql --username postgres &> /dev/null; then
53+
break
54+
fi
55+
echo 'PostgreSQL init process in progress...'
56+
sleep 1
57+
done
58+
if [ "$i" = 0 ]; then
59+
echo >&2 'PostgreSQL init process failed'
60+
exit 1
61+
fi
62+
4263
: ${POSTGRES_USER:=postgres}
4364
: ${POSTGRES_DB:=$POSTGRES_USER}
4465

4566
if [ "$POSTGRES_DB" != 'postgres' ]; then
46-
gosu postgres postgres --single -jE <<-EOSQL
67+
psql --username postgres <<-EOSQL
4768
CREATE DATABASE "$POSTGRES_DB" ;
4869
EOSQL
4970
echo
@@ -55,18 +76,31 @@ if [ "$1" = 'postgres' ]; then
5576
op='CREATE'
5677
fi
5778

58-
gosu postgres postgres --single -jE <<-EOSQL
79+
psql --username postgres <<-EOSQL
5980
$op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
6081
EOSQL
6182
echo
6283

63-
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA"/pg_hba.conf
84+
echo
85+
for f in /docker-entrypoint-initdb.d/*; do
86+
case "$f" in
87+
*.sh) echo "$0: running $f"; . "$f" ;;
88+
*.sql) echo "$0: running $f"; psql --username postgres --dbname "$POSTGRES_DB" < "$f" && echo ;;
89+
*) echo "$0: ignoring $f" ;;
90+
esac
91+
echo
92+
done
6493

65-
if [ -d /docker-entrypoint-initdb.d ]; then
66-
for f in /docker-entrypoint-initdb.d/*.sh; do
67-
[ -f "$f" ] && . "$f"
68-
done
94+
if ! kill -s TERM "$pid" || ! wait "$pid"; then
95+
echo >&2 'PostgreSQL init process failed'
96+
exit 1
6997
fi
98+
99+
set_listen_addresses '*'
100+
101+
echo
102+
echo 'PostgreSQL init process complete; ready for start up.'
103+
echo
70104
fi
71105

72106
exec gosu postgres "$@"

9.3/docker-entrypoint.sh

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
#!/bin/bash
22
set -e
33

4+
set_listen_addresses() {
5+
sedEscapedValue="$(echo "$1" | sed 's/[\/&]/\\&/g')"
6+
sed -ri "s/^#?(listen_addresses\s*=\s*)\S+/\1'$sedEscapedValue'/" "$PGDATA/postgresql.conf"
7+
}
8+
49
if [ "$1" = 'postgres' ]; then
510
mkdir -p "$PGDATA"
611
chown -R postgres "$PGDATA"
712

813
chmod g+s /run/postgresql
9-
chown -R postgres:postgres /run/postgresql
14+
chown -R postgres /run/postgresql
1015

1116
# look specifically for PG_VERSION, as it is expected in the DB dir
1217
if [ ! -s "$PGDATA/PG_VERSION" ]; then
1318
gosu postgres initdb
1419

15-
sed -ri "s/^#(listen_addresses\s*=\s*)\S+/\1'*'/" "$PGDATA"/postgresql.conf
16-
1720
# check password first so we can output the warning before postgres
1821
# messes it up
1922
if [ "$POSTGRES_PASSWORD" ]; then
@@ -39,11 +42,29 @@ if [ "$1" = 'postgres' ]; then
3942
authMethod=trust
4043
fi
4144

45+
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA/pg_hba.conf"
46+
47+
set_listen_addresses '' # we're going to start up postgres, but it's not ready for use yet (this is initialization), so don't listen to the outside world yet
48+
49+
gosu postgres "$@" &
50+
pid="$!"
51+
for i in {30..0}; do
52+
if echo 'SELECT 1' | psql --username postgres &> /dev/null; then
53+
break
54+
fi
55+
echo 'PostgreSQL init process in progress...'
56+
sleep 1
57+
done
58+
if [ "$i" = 0 ]; then
59+
echo >&2 'PostgreSQL init process failed'
60+
exit 1
61+
fi
62+
4263
: ${POSTGRES_USER:=postgres}
4364
: ${POSTGRES_DB:=$POSTGRES_USER}
4465

4566
if [ "$POSTGRES_DB" != 'postgres' ]; then
46-
gosu postgres postgres --single -jE <<-EOSQL
67+
psql --username postgres <<-EOSQL
4768
CREATE DATABASE "$POSTGRES_DB" ;
4869
EOSQL
4970
echo
@@ -55,18 +76,31 @@ if [ "$1" = 'postgres' ]; then
5576
op='CREATE'
5677
fi
5778

58-
gosu postgres postgres --single -jE <<-EOSQL
79+
psql --username postgres <<-EOSQL
5980
$op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
6081
EOSQL
6182
echo
6283

63-
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA"/pg_hba.conf
84+
echo
85+
for f in /docker-entrypoint-initdb.d/*; do
86+
case "$f" in
87+
*.sh) echo "$0: running $f"; . "$f" ;;
88+
*.sql) echo "$0: running $f"; psql --username postgres --dbname "$POSTGRES_DB" < "$f" && echo ;;
89+
*) echo "$0: ignoring $f" ;;
90+
esac
91+
echo
92+
done
6493

65-
if [ -d /docker-entrypoint-initdb.d ]; then
66-
for f in /docker-entrypoint-initdb.d/*.sh; do
67-
[ -f "$f" ] && . "$f"
68-
done
94+
if ! kill -s TERM "$pid" || ! wait "$pid"; then
95+
echo >&2 'PostgreSQL init process failed'
96+
exit 1
6997
fi
98+
99+
set_listen_addresses '*'
100+
101+
echo
102+
echo 'PostgreSQL init process complete; ready for start up.'
103+
echo
70104
fi
71105

72106
exec gosu postgres "$@"

0 commit comments

Comments
 (0)