From 280f2d8d33870b1dfdbda494b0b3a0e91262e2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wieche=C4=87?= Date: Fri, 18 Dec 2015 09:49:06 +0100 Subject: [PATCH 01/15] installing postgis packages --- Dockerfile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 \ From bbd12cf8fa9eb2f42d85d11685e1d194a368964d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wieche=C4=87?= Date: Fri, 18 Dec 2015 09:49:19 +0100 Subject: [PATCH 02/15] adding docs --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 4cf2e63..dbadd70 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,18 @@ docker run --name postgresql -itd \ sameersbn/postgresql:9.4-11 ``` +# 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 unaccent extension is disabled* ## Granting user access to a database From c017a10d8b0f185af8dc1518173bb23acc85a3b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wieche=C4=87?= Date: Fri, 18 Dec 2015 09:49:32 +0100 Subject: [PATCH 03/15] adding vars for postgis extension --- runtime/env-defaults | 4 ++++ 1 file changed, 4 insertions(+) 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} From 57abc71320ba20293c34c54700e96d6c382d33da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wieche=C4=87?= Date: Fri, 18 Dec 2015 10:25:13 +0100 Subject: [PATCH 04/15] adding routines to create PostGIS-enabled database normally, to enable PostGIS for a database (including template db), issuing CREATE EXTENSION IF NOT EXISTS postgis; should suffice. However, this is not working in single-user mode (see http://stackoverflow.com/q/28147177/1547895). Instead, we are running PostGIS's SQL scripts. --- runtime/functions | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) 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;" | \ From d7032183632d17bbaa2cc21e3bfffc948dc02a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wieche=C4=87?= Date: Fri, 18 Dec 2015 10:29:25 +0100 Subject: [PATCH 05/15] version bump --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index f36be50..fadb404 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -9.4-11 +9.4-11-postgis From b598e0a22326a28ee8de2769d2f814aa2bdfee1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wieche=C4=87?= Date: Fri, 18 Dec 2015 10:30:33 +0100 Subject: [PATCH 06/15] version bump --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index fadb404..fafb501 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -9.4-11-postgis +9.4-12-snapshot From 8c0a352a1050bda180946aec93511ec5489e7610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wieche=C4=87?= Date: Fri, 18 Dec 2015 10:42:10 +0100 Subject: [PATCH 07/15] updating TOC --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dbadd70..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,6 +181,7 @@ 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 @@ -192,8 +194,7 @@ docker run --name postgresql -itd \ --env 'DB_NAME=dbname' --env 'DB_POSTGIS=true' \ sameersbn/postgresql:9.4-11 ``` - -*By default the unaccent extension is disabled* +*By default the PostGIS extension is disabled* ## Granting user access to a database From 0c3d1f44512dea9fb4e4d6156a139802345e3141 Mon Sep 17 00:00:00 2001 From: Sameer Naik Date: Mon, 11 Jan 2016 12:08:01 +0530 Subject: [PATCH 08/15] format `Dockerfile` --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index be047f3..8ff384d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,12 +3,12 @@ MAINTAINER sameer@damagehead.com ENV PG_APP_HOME="/etc/docker-postgresql"\ PG_VERSION=9.4 \ + PG_POSTGIS_VERSION=2.1 \ PG_USER=postgres \ PG_HOME=/var/lib/postgresql \ PG_RUNDIR=/run/postgresql \ PG_LOGDIR=/var/log/postgresql \ - PG_CERTDIR=/etc/postgresql/certs \ - PG_POSTGIS_VERSION=2.1 + PG_CERTDIR=/etc/postgresql/certs ENV PG_BINDIR=/usr/lib/postgresql/${PG_VERSION}/bin \ PG_DATADIR=${PG_HOME}/${PG_VERSION}/main From f86c84bc54c1f791bd4c27e3810ed64545ffc748 Mon Sep 17 00:00:00 2001 From: Sameer Naik Date: Mon, 11 Jan 2016 12:08:23 +0530 Subject: [PATCH 09/15] readme: updated --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7ba1a73..d25145e 100644 --- a/README.md +++ b/README.md @@ -183,19 +183,23 @@ 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. +**Available in versions > `9.4-11`** + +PostGIS is a spatial database extender for PostgreSQL object-relational database. It adds support for geographic objects allowing location queries to be run in SQL. -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. +You can enable the PostGIS extension on database(s) by specifying `DB_POSTGIS=true`. For example, the following command enables the PostGIS 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 From 0c282408a59eefa685a8e0480b64a2b4de7d1fc0 Mon Sep 17 00:00:00 2001 From: Sameer Naik Date: Mon, 11 Jan 2016 12:08:48 +0530 Subject: [PATCH 10/15] docker-compose: add `DB_POSTGIS` env --- docker-compose.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose.yml b/docker-compose.yml index 715d000..533ef57 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,7 @@ PostgreSQL: - DB_TEMPLATE= - DB_UNACCENT= + - DB_POSTGIS= - REPLICATION_MODE= - REPLICATION_USER= From e294e79f5dce1e94d0fb6252695dce1173777f15 Mon Sep 17 00:00:00 2001 From: Sameer Naik Date: Mon, 11 Jan 2016 12:08:56 +0530 Subject: [PATCH 11/15] removed unused `DB_POSTGIS_HSTORE` and `DB_POSTGIS_TOPOLOGY` variables --- runtime/env-defaults | 3 --- 1 file changed, 3 deletions(-) diff --git a/runtime/env-defaults b/runtime/env-defaults index aa2af22..854e1c5 100644 --- a/runtime/env-defaults +++ b/runtime/env-defaults @@ -19,7 +19,4 @@ DB_PASS=${DB_PASS:-} DB_TEMPLATE=${DB_TEMPLATE:-template1} DB_UNACCENT=${DB_UNACCENT:-false} - DB_POSTGIS=${DB_POSTGIS:-false} -DB_POSTGIS_HSTORE=${DB_POSTGIS_HSTORE:-true} -DB_POSTGIS_TOPOLOGY=${DB_POSTGIS_TOPOLOGY:-true} From d7a7b67c1b2754ddf6feb091f7e035cf130f5425 Mon Sep 17 00:00:00 2001 From: Sameer Naik Date: Mon, 11 Jan 2016 12:10:28 +0530 Subject: [PATCH 12/15] updated Changelog --- Changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Changelog.md b/Changelog.md index 816c032..b7d98e4 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,6 +3,7 @@ **latest** - removed use of single-user mode - added `DB_TEMPLATE` variable to specify the database template +- added support for `PostGIS` extension **9.4-11** - added `PG_PASSWORD` variable to specify password for `postgres` user From d97c2c5c80c973afb86c0c8be998510a0d15ea59 Mon Sep 17 00:00:00 2001 From: Sameer Naik Date: Mon, 11 Jan 2016 12:26:22 +0530 Subject: [PATCH 13/15] display PostGIS extension loading message --- runtime/functions | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/functions b/runtime/functions index 695930c..8fa4f02 100755 --- a/runtime/functions +++ b/runtime/functions @@ -324,6 +324,7 @@ create_database() { fi if [[ ${DB_POSTGIS} == true ]]; then + echo "‣ Loading PostGIS extension..." psql -U ${PG_USER} -d ${database} -c "CREATE EXTENSION IF NOT EXISTS postgis;" >/dev/null 2>&1 psql -U ${PG_USER} -d ${database} -c "CREATE EXTENSION IF NOT EXISTS postgis_topology;" >/dev/null 2>&1 fi From c20600770896e489994d10aecd66e5b3b495c882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wieche=C4=87?= Date: Mon, 11 Jan 2016 10:09:50 +0100 Subject: [PATCH 14/15] fixing issue with PostGIS topology extension environment variable DB_POSTGIS_TOPOLOGY controls creation of 'postgis_topology' extension _only_ when user selects PostGIS extension. refs #44 --- README.md | 11 +++++++++++ runtime/env-defaults | 1 + runtime/functions | 5 ++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d25145e..2f3ee11 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,17 @@ docker run --name postgresql -itd \ sameersbn/postgresql:9.4-11 ``` +Additionally, creation of PostGIS topology extension can be enabled by specifying `DB_POSTGIS_TOPOLOGY=true`: + + +```bash +docker run --name postgresql -itd \ + --env 'DB_NAME=dbname' --env 'DB_POSTGIS=true' --env DB_POSTGIS_TOPOLOGY=true \ + sameersbn/postgresql:9.4-11 +``` + +Note that topology extension creation can be enabled only when PostGIS extension is enabled. + *By default the PostGIS extension is disabled* ## Granting user access to a database diff --git a/runtime/env-defaults b/runtime/env-defaults index 854e1c5..6b92ba6 100644 --- a/runtime/env-defaults +++ b/runtime/env-defaults @@ -20,3 +20,4 @@ DB_TEMPLATE=${DB_TEMPLATE:-template1} DB_UNACCENT=${DB_UNACCENT:-false} DB_POSTGIS=${DB_POSTGIS:-false} +DB_POSTGIS_TOPOLOGY=${DB_POSTGIS_TOPOLOGY:-false} diff --git a/runtime/functions b/runtime/functions index e5314b3..e5b368a 100755 --- a/runtime/functions +++ b/runtime/functions @@ -326,7 +326,10 @@ create_database() { if [[ ${DB_POSTGIS} == true ]]; then echo "‣ Loading PostGIS extension..." psql -U ${PG_USER} -d ${database} -c "CREATE EXTENSION IF NOT EXISTS postgis;" >/dev/null 2>&1 - psql -U ${PG_USER} -d ${database} -c "CREATE EXTENSION IF NOT EXISTS postgis_topology;" >/dev/null 2>&1 + if [[ ${DB_POSTGIS_TOPOLOGY} == true ]]; then + echo "‣ Loading PostGIS Topology extension..." + psql -U ${PG_USER} -d ${database} -c "CREATE EXTENSION IF NOT EXISTS postgis_topology;" >/dev/null 2>&1 + fi fi if [[ -n ${DB_USER} ]]; then From e2f791afa3af32a6a6a42698997684d47e3f5f5c Mon Sep 17 00:00:00 2001 From: John Kalantzis Date: Fri, 12 Feb 2016 14:59:03 +0000 Subject: [PATCH 15/15] Add support for hstore extension. --- docker-compose.yml | 1 + runtime/env-defaults | 1 + runtime/functions | 5 +++++ 3 files changed, 7 insertions(+) mode change 100644 => 100755 runtime/env-defaults diff --git a/docker-compose.yml b/docker-compose.yml index 96cd48d..07f2362 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,7 @@ PostgreSQL: - DB_TEMPLATE= - DB_UNACCENT= + - DB_HSTORE= - REPLICATION_MODE= - REPLICATION_USER= diff --git a/runtime/env-defaults b/runtime/env-defaults old mode 100644 new mode 100755 index 8636742..21e2184 --- a/runtime/env-defaults +++ b/runtime/env-defaults @@ -19,3 +19,4 @@ DB_PASS=${DB_PASS:-} DB_TEMPLATE=${DB_TEMPLATE:-template1} DB_UNACCENT=${DB_UNACCENT:-false} +DB_HSTORE=${DB_HSTORE:-false} diff --git a/runtime/functions b/runtime/functions index 93f2049..66b6efb 100755 --- a/runtime/functions +++ b/runtime/functions @@ -323,6 +323,11 @@ create_database() { psql -U ${PG_USER} -d ${database} -c "CREATE EXTENSION IF NOT EXISTS unaccent;" >/dev/null 2>&1 fi + if [[ ${DB_HSTORE} == true ]]; then + echo "‣ Loading hstore extension..." + psql -U ${PG_USER} -d ${database} -c "CREATE EXTENSION IF NOT EXISTS hstore;" >/dev/null 2>&1 + fi + if [[ -n ${DB_USER} ]]; then echo "‣ Granting access to ${DB_USER} user..." psql -U ${PG_USER} -c "GRANT ALL PRIVILEGES ON DATABASE \"${database}\" to \"${DB_USER}\";" >/dev/null