diff --git a/Dockerfile b/Dockerfile index f0870e8..e7e702c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,8 @@ ENV PG_APP_HOME="/etc/docker-postgresql"\ PG_HOME=/var/lib/postgresql \ PG_RUNDIR=/run/postgresql \ PG_LOGDIR=/var/log/postgresql \ - PG_CERTDIR=/etc/postgresql/certs + PG_CERTDIR=/etc/postgresql/certs \ + PG_POSTGIS_VERSION=2.1 ENV PG_BINDIR=/usr/lib/postgresql/${PG_VERSION}/bin \ PG_DATADIR=${PG_HOME}/${PG_VERSION}/main @@ -15,7 +16,7 @@ ENV PG_BINDIR=/usr/lib/postgresql/${PG_VERSION}/bin \ RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \ && echo 'deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main' > /etc/apt/sources.list.d/pgdg.list \ && apt-get update \ - && DEBIAN_FRONTEND=noninteractive apt-get install -y postgresql-${PG_VERSION} postgresql-client-${PG_VERSION} postgresql-contrib-${PG_VERSION} \ + && DEBIAN_FRONTEND=noninteractive apt-get install -y postgresql-${PG_VERSION} postgresql-client-${PG_VERSION} postgresql-contrib-${PG_VERSION} postgresql-${PG_VERSION}-postgis-${PG_POSTGIS_VERSION} postgresql-${PG_VERSION}-postgis-${PG_POSTGIS_VERSION}-scripts \ && ln -sf ${PG_DATADIR}/postgresql.conf /etc/postgresql/${PG_VERSION}/main/postgresql.conf \ && ln -sf ${PG_DATADIR}/pg_hba.conf /etc/postgresql/${PG_VERSION}/main/pg_hba.conf \ && ln -sf ${PG_DATADIR}/pg_ident.conf /etc/postgresql/${PG_VERSION}/main/pg_ident.conf \ diff --git a/README.md b/README.md index 4cf2e63..41e8e2b 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ - [Creating database user](#creating-database-user) - [Creating databases](#creating-databases) - [Enabling unaccent extension](#enabling-unaccent-extension) + - [Enabling PostGIS extension](#enabling-postgis-extension) - [Granting user access to a database](#granting-user-access-to-a-database) - [Creating replication user](#creating-replication-user) - [Setting up a replication cluster](#setting-up-a-replication-cluster) @@ -180,9 +181,21 @@ docker run --name postgresql -itd \ --env 'DB_NAME=dbname' --env 'DB_UNACCENT=true' \ sameersbn/postgresql:9.4-11 ``` - *By default the unaccent extension is disabled* +# Enabling PostGIS extension + +PostGIS is spatial extension to PostgreSQL. + +You can enable the PostGIS extension on database(s) by specifying `DB_POSTGIS=true`. For example, the following command enables the unaccent extension for the `dbname` database. + +```bash +docker run --name postgresql -itd \ + --env 'DB_NAME=dbname' --env 'DB_POSTGIS=true' \ + sameersbn/postgresql:9.4-11 +``` +*By default the PostGIS extension is disabled* + ## Granting user access to a database If the `DB_USER` and `DB_PASS` variables are specified along with the `DB_NAME` variable, then the user specified in `DB_USER` will be granted access to all the databases listed in `DB_NAME`. Note that if the user and/or databases do not exist, they will be created. diff --git a/VERSION b/VERSION index f36be50..fafb501 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -9.4-11 +9.4-12-snapshot diff --git a/runtime/env-defaults b/runtime/env-defaults index 17e374b..84f5efe 100644 --- a/runtime/env-defaults +++ b/runtime/env-defaults @@ -18,3 +18,7 @@ DB_USER=${DB_USER:-} DB_PASS=${DB_PASS:-} DB_UNACCENT=${DB_UNACCENT:-false} + +DB_POSTGIS=${DB_POSTGIS:-false} +DB_POSTGIS_HSTORE=${DB_POSTGIS_HSTORE:-true} +DB_POSTGIS_TOPOLOGY=${DB_POSTGIS_TOPOLOGY:-true} diff --git a/runtime/functions b/runtime/functions index 128b535..250a7ff 100755 --- a/runtime/functions +++ b/runtime/functions @@ -308,8 +308,31 @@ create_database() { echo -n "Creating database(s): " for database in $(awk -F',' '{for (i = 1 ; i <= NF ; i++) print $i}' <<< "${DB_NAME}"); do echo -n "${database} " - echo "CREATE DATABASE \"${database}\";" | \ - exec_as_postgres ${PG_BINDIR}/postgres --single -D ${PG_DATADIR} >/dev/null 2>&1 + + if [[ ${DB_POSTGIS} == true ]]; then + + echo "CREATE DATABASE template_postgis WITH ENCODING = 'UTF8' TEMPLATE = template0;" | \ + exec_as_postgres ${PG_BINDIR}/postgres --single -D ${PG_DATADIR} >/dev/null 2>&1 + + echo "UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_postgis';" | \ + exec_as_postgres ${PG_BINDIR}/postgres --single -D ${PG_DATADIR} >/dev/null 2>&1 + + # because we can't create extensions in single-user mode, we will just read the SQL code for PostGIS + PG_POSTGIS_HOME=/usr/share/postgresql/${PG_VERSION}/contrib/postgis-${PG_POSTGIS_VERSION} + exec_as_postgres ${PG_BINDIR}/postgres --single template_postgis -D ${PG_DATADIR} -j \ + < ${PG_POSTGIS_HOME}/postgis.sql >/dev/null 2>&1 + exec_as_postgres ${PG_BINDIR}/postgres --single template_postgis -D ${PG_DATADIR} -j \ + < ${PG_POSTGIS_HOME}/topology.sql >/dev/null 2>&1 + exec_as_postgres ${PG_BINDIR}/postgres --single template_postgis -D ${PG_DATADIR} -j \ + < ${PG_POSTGIS_HOME}/spatial_ref_sys.sql >/dev/null 2>&1 + + # create PostGIS database + echo "CREATE DATABASE \"${database}\" WITH TEMPLATE = template_postgis;" | \ + exec_as_postgres ${PG_BINDIR}/postgres --single -D ${PG_DATADIR} >/dev/null 2>&1 + else + echo "CREATE DATABASE \"${database}\";" | \ + exec_as_postgres ${PG_BINDIR}/postgres --single -D ${PG_DATADIR} >/dev/null 2>&1 + fi if [[ ${DB_UNACCENT} == true ]]; then echo "CREATE EXTENSION IF NOT EXISTS unaccent;" | \