2014-05-04 19:14:46 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2014-08-05 08:45:28 +00:00
|
|
|
# This script will run as the postgres user due to the Dockerfile USER directive
|
|
|
|
|
2014-05-04 19:14:46 +00:00
|
|
|
DATADIR="/var/lib/postgresql/9.3/main"
|
|
|
|
CONF="/etc/postgresql/9.3/main/postgresql.conf"
|
|
|
|
POSTGRES="/usr/lib/postgresql/9.3/bin/postgres"
|
|
|
|
INITDB="/usr/lib/postgresql/9.3/bin/initdb"
|
|
|
|
SQLDIR="/usr/share/postgresql/9.3/contrib/postgis-2.1/"
|
|
|
|
|
|
|
|
# test if DATADIR is existent
|
|
|
|
if [ ! -d $DATADIR ]; then
|
|
|
|
echo "Creating Postgres data at $DATADIR"
|
|
|
|
mkdir -p $DATADIR
|
|
|
|
fi
|
|
|
|
|
2014-05-08 23:25:52 +00:00
|
|
|
# Note that $USERNAME and $PASS below are optional paramters that can be passed
|
|
|
|
# via docker run e.g.
|
|
|
|
#docker run --name="postgis" -e USERNAME=qgis -e 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.
|
2014-05-04 19:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
# test if DATADIR has content
|
|
|
|
if [ ! "$(ls -A $DATADIR)" ]; then
|
2014-08-04 15:10:07 +00:00
|
|
|
|
|
|
|
# No content yet - first time pg is being run!
|
|
|
|
# Initialise db
|
2014-05-04 19:14:46 +00:00
|
|
|
echo "Initializing Postgres Database at $DATADIR"
|
2014-08-05 08:45:28 +00:00
|
|
|
#chown -R postgres $DATADIR
|
|
|
|
$INITDB $DATADIR
|
2014-05-04 19:14:46 +00:00
|
|
|
fi
|
|
|
|
|
2014-05-08 23:25:52 +00:00
|
|
|
# Make sure we have a user set up
|
|
|
|
if [ -z "$USERNAME" ]; then
|
|
|
|
USERNAME=postgis
|
|
|
|
fi
|
|
|
|
if [ -z "$PASS" ]; then
|
|
|
|
PASS=postgis
|
|
|
|
#PASS=`pwgen -c -n -1 12`
|
|
|
|
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
|
2014-08-05 08:45:28 +00:00
|
|
|
echo "postgresql user: $USERNAME" > /tmp/PGPASSWORD.txt
|
|
|
|
echo "postgresql password: $PASS" >> /tmp/PGPASSWORD.txt
|
|
|
|
$POSTGRES --single -D $DATADIR -c config_file=$CONF" <<< "CREATE USER $USERNAME WITH SUPERUSER ENCRYPTED PASSWORD '$PASS';
|
2014-05-08 23:25:52 +00:00
|
|
|
|
2014-05-04 19:14:46 +00:00
|
|
|
trap "echo \"Sending SIGTERM to postgres\"; killall -s SIGTERM postgres" SIGTERM
|
|
|
|
|
2014-08-05 08:45:28 +00:00
|
|
|
$POSTGRES -D $DATADIR -c config_file=$CONF &
|
2014-05-04 19:14:46 +00:00
|
|
|
|
|
|
|
# Wait for the db to start up before trying to use it....
|
|
|
|
|
|
|
|
sleep 10
|
|
|
|
|
2014-08-05 08:45:28 +00:00
|
|
|
RESULT=`psql -l | grep postgis | wc -l`
|
2014-05-04 19:14:46 +00:00
|
|
|
if [[ $RESULT == '1' ]]
|
|
|
|
then
|
|
|
|
echo 'Postgis Already There'
|
|
|
|
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
|
2014-08-04 15:10:07 +00:00
|
|
|
echo "Creating template postgis"
|
2014-08-05 08:45:28 +00:00
|
|
|
createdb template_postgis -E UTF8 -T template0
|
2014-08-04 15:10:07 +00:00
|
|
|
echo "Enabling template_postgis as a template"
|
2014-08-05 08:45:28 +00:00
|
|
|
psql template1 -c "UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_postgis';"
|
2014-08-04 15:10:07 +00:00
|
|
|
echo "Loading postgis.sql"
|
2014-08-05 08:45:28 +00:00
|
|
|
psql template_postgis -f $SQLDIR/postgis.sql
|
2014-08-04 15:10:07 +00:00
|
|
|
echo "Loading spatial_ref_sys.sql"
|
2014-08-05 08:45:28 +00:00
|
|
|
psql template_postgis -f $SQLDIR/spatial_ref_sys.sql
|
2014-08-04 15:10:07 +00:00
|
|
|
|
2014-05-04 19:14:46 +00:00
|
|
|
# Needed when importing old dumps using e.g ndims for constraints
|
2014-08-04 15:10:07 +00:00
|
|
|
echo "Loading legacy sql"
|
2014-08-05 08:45:28 +00:00
|
|
|
psql template_postgis -f $SQLDIR/legacy_minimal.sql
|
2014-08-21 08:43:55 +00:00
|
|
|
psql template_postgis -f $SQLDIR/legacy_gist.sql
|
2014-08-04 15:10:07 +00:00
|
|
|
echo "Granting on geometry columns"
|
2014-08-05 08:45:28 +00:00
|
|
|
psql template_postgis -c 'GRANT ALL ON geometry_columns TO PUBLIC;'
|
2014-08-04 15:10:07 +00:00
|
|
|
echo "Granting on geography columns"
|
2014-08-05 08:45:28 +00:00
|
|
|
psql template_postgis -c 'GRANT ALL ON geography_columns TO PUBLIC;'
|
2014-08-04 15:10:07 +00:00
|
|
|
echo "Granting on spatial ref sys"
|
2014-08-05 08:45:28 +00:00
|
|
|
psql template_postgis -c 'GRANT ALL ON spatial_ref_sys TO PUBLIC;'
|
2014-08-05 08:56:00 +00:00
|
|
|
# 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
|
|
|
|
createdb -O docker -T template_postgis gis
|
2014-05-04 19:14:46 +00:00
|
|
|
fi
|
2014-08-05 08:56:00 +00:00
|
|
|
# This should show up in docker logs afterwards
|
2014-08-05 08:45:28 +00:00
|
|
|
psql -l
|
2014-05-04 19:14:46 +00:00
|
|
|
|
|
|
|
wait $!
|