Skip to content

Commit c1cb835

Browse files
author
Matthew Fisher
committed
ref(rootfs): copy over docker-entrypoint.sh
1 parent bf182fb commit c1cb835

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

rootfs/docker-entrypoint.sh

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

0 commit comments

Comments
 (0)