Skip to content

Commit c247663

Browse files
committed
fix(build): Add docker-entrypoint.sh
1 parent 0470fed commit c247663

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

docker-entrypoint.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/bin/bash
2+
set -e
3+
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+
9+
if [ "$1" = 'postgres' ]; then
10+
mkdir -p "$PGDATA"
11+
chmod 700 "$PGDATA"
12+
chown -R postgres "$PGDATA"
13+
14+
chmod g+s /run/postgresql
15+
chown -R postgres /run/postgresql
16+
17+
# look specifically for PG_VERSION, as it is expected in the DB dir
18+
if [ ! -s "$PGDATA/PG_VERSION" ]; then
19+
gosu postgres initdb
20+
21+
# check password first so we can output the warning before postgres
22+
# messes it up
23+
if [ "$POSTGRES_PASSWORD" ]; then
24+
pass="PASSWORD '$POSTGRES_PASSWORD'"
25+
authMethod=md5
26+
else
27+
# The - option suppresses leading tabs but *not* spaces. :)
28+
cat >&2 <<-'EOWARN'
29+
****************************************************
30+
WARNING: No password has been set for the database.
31+
This will allow anyone with access to the
32+
Postgres port to access your database. In
33+
Docker's default configuration, this is
34+
effectively any other container on the same
35+
system.
36+
Use "-e POSTGRES_PASSWORD=password" to set
37+
it in "docker run".
38+
****************************************************
39+
EOWARN
40+
41+
pass=
42+
authMethod=trust
43+
fi
44+
45+
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA/pg_hba.conf"
46+
47+
# internal start of server in order to allow set-up using psql-client
48+
# does not listen on TCP/IP and waits until start finishes
49+
gosu postgres pg_ctl -D "$PGDATA" \
50+
-o "-c listen_addresses=''" \
51+
-w start
52+
53+
: ${POSTGRES_USER:=postgres}
54+
: ${POSTGRES_DB:=$POSTGRES_USER}
55+
export POSTGRES_USER POSTGRES_DB
56+
57+
if [ "$POSTGRES_DB" != 'postgres' ]; then
58+
psql --username postgres <<-EOSQL
59+
CREATE DATABASE "$POSTGRES_DB" ;
60+
EOSQL
61+
echo
62+
fi
63+
64+
if [ "$POSTGRES_USER" = 'postgres' ]; then
65+
op='ALTER'
66+
else
67+
op='CREATE'
68+
fi
69+
70+
psql --username postgres <<-EOSQL
71+
$op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
72+
EOSQL
73+
echo
74+
75+
echo
76+
for f in /docker-entrypoint-initdb.d/*; do
77+
case "$f" in
78+
*.sh) echo "$0: running $f"; . "$f" ;;
79+
*.sql) echo "$0: running $f"; psql --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" < "$f" && echo ;;
80+
*) echo "$0: ignoring $f" ;;
81+
esac
82+
echo
83+
done
84+
85+
gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop
86+
set_listen_addresses '*'
87+
88+
echo
89+
echo 'PostgreSQL init process complete; ready for start up.'
90+
echo
91+
fi
92+
93+
exec gosu postgres "$@"
94+
fi
95+
96+
exec "$@"

0 commit comments

Comments
 (0)