forked from kartoza/docker-postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-postgis.sh
executable file
·141 lines (119 loc) · 5.13 KB
/
start-postgis.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
# This script will run as the postgres user due to the Dockerfile USER directive
DATADIR="/var/lib/postgresql/9.5/main"
CONF="/etc/postgresql/9.5/main/postgresql.conf"
POSTGRES="/usr/lib/postgresql/9.5/bin/postgres"
INITDB="/usr/lib/postgresql/9.5/bin/initdb"
SQLDIR="/usr/share/postgresql/9.5/contrib/postgis-2.2/"
LOCALONLY="-c listen_addresses='127.0.0.1, ::1'"
# /etc/ssl/private can't be accessed from within container for some reason
# (@andrewgodwin says it's something AUFS related) - taken from https://github.com/orchardup/docker-postgresql/blob/master/Dockerfile
cp -r /etc/ssl /tmp/ssl-copy/
chmod -R 0700 /etc/ssl
chown -R postgres /tmp/ssl-copy
rm -r /etc/ssl
mv /tmp/ssl-copy /etc/ssl
# Needed under debian, wasnt needed under ubuntu
mkdir /var/run/postgresql/9.5-main.pg_stat_tmp
chmod 0777 /var/run/postgresql/9.5-main.pg_stat_tmp
# test if DATADIR is existent
if [ ! -d $DATADIR ]; then
echo "Creating Postgres data at $DATADIR"
mkdir -p $DATADIR
fi
# needs to be done as root:
chown -R postgres:postgres $DATADIR
# Note that $POSTGRES_USER and $POSTGRES_PASS below are optional paramters that can be passed
# via docker run e.g.
#docker run --name="postgis" -e POSTGRES_USER=qgis -e POSTGRES_PASS=qgis -d -v
#/var/docker-data/postgres-dat:/var/lib/postgresql -t qgis/postgis:6
# If you dont specify a user/password in docker run, we will generate one
# here and create a user called 'docker' to go with it.
# test if DATADIR has content
if [ ! "$(ls -A $DATADIR)" ]; then
# No content yet - first time pg is being run!
# Initialise db
echo "Initializing Postgres Database at $DATADIR"
#chown -R postgres $DATADIR
su - postgres -c "$INITDB $DATADIR"
fi
# Make sure we have a user set up
if [ -z "$POSTGRES_USER" ]; then
POSTGRES_USER=docker
fi
if [ -z "$POSTGRES_PASS" ]; then
POSTGRES_PASS=docker
fi
# Enable hstore and topology by default
if [ -z "$HSTORE" ]; then
HSTORE=true
fi
if [ -z "$TOPOLOGY" ]; then
TOPOLOGY=true
fi
# Custom IP range via docker run -e (https://docs.docker.com/engine/reference/run/#env-environment-variables)
# Usage is: docker run [...] -e ALLOW_IP_RANGE='192.168.0.0/16'
if [ "$ALLOW_IP_RANGE" ]
then
echo "host all all $ALLOW_IP_RANGE md5" >> /etc/postgresql/9.5/main/pg_hba.conf
fi
# redirect user/pass into a file so we can echo it into
# docker logs when container starts
# so that we can tell user their password
echo "postgresql user: $POSTGRES_USER" > /tmp/PGPASSWORD.txt
echo "postgresql password: $POSTGRES_PASS" >> /tmp/PGPASSWORD.txt
su - postgres -c "$POSTGRES --single -D $DATADIR -c config_file=$CONF <<< \"CREATE USER $POSTGRES_USER WITH SUPERUSER ENCRYPTED PASSWORD '$POSTGRES_PASS';\""
trap "echo \"Sending SIGTERM to postgres\"; killall -s SIGTERM postgres" SIGTERM
su - postgres -c "$POSTGRES -D $DATADIR -c config_file=$CONF $LOCALONLY &"
# wait for postgres to come up
until `nc -z 127.0.0.1 5432`; do
echo "$(date) - waiting for postgres (localhost-only)..."
sleep 1
done
echo "postgres ready"
RESULT=`su - postgres -c "psql -l | grep postgis | wc -l"`
if [[ ${RESULT} == '1' ]]
then
echo 'Postgis Already There'
if [[ ${HSTORE} == "true" ]]; then
echo 'HSTORE is only useful when you create the postgis database.'
fi
if [[ ${TOPOLOGY} == "true" ]]; then
echo 'TOPOLOGY is only useful when you create the postgis database.'
fi
else
echo "Postgis is missing, installing now"
# Note the dockerfile must have put the postgis.sql and spatialrefsys.sql scripts into /root/
# We use template0 since we want t different encoding to template1
echo "Creating template postgis"
su - postgres -c "createdb template_postgis -E UTF8 -T template0"
echo "Enabling template_postgis as a template"
CMD="UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_postgis';"
su - postgres -c "psql -c \"$CMD\""
echo "Loading postgis extension"
su - postgres -c "psql template_postgis -c 'CREATE EXTENSION postgis;'"
if [[ ${HSTORE} == "true" ]]
then
echo "Enabling hstore in the template"
su - postgres -c "psql template_postgis -c 'CREATE EXTENSION hstore;'"
fi
if [[ ${TOPOLOGY} == "true" ]]
then
echo "Enabling topology in the template"
su - postgres -c "psql template_postgis -c 'CREATE EXTENSION postgis_topology;'"
fi
# Needed when importing old dumps using e.g ndims for constraints
echo "Loading legacy sql"
su - postgres -c "psql template_postgis -f $SQLDIR/legacy_minimal.sql"
su - postgres -c "psql template_postgis -f $SQLDIR/legacy_gist.sql"
# Create a default db called 'gis' that you can use to get up and running quickly
# It will be owned by the docker db user
su - postgres -c "createdb -O $POSTGRES_USER -T template_postgis gis"
fi
# This should show up in docker logs afterwards
su - postgres -c "psql -l"
PID=`cat /var/run/postgresql/9.5-main.pid`
kill -9 ${PID}
echo "Postgres initialisation process completed .... restarting in foreground"
SETVARS="POSTGIS_ENABLE_OUTDB_RASTERS=1 POSTGIS_GDAL_ENABLED_DRIVERS=ENABLE_ALL"
su - postgres -c "$SETVARS $POSTGRES -D $DATADIR -c config_file=$CONF"