Skip to content

Adding postgis #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ 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

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 \
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9.4-11
9.4-12-snapshot
4 changes: 4 additions & 0 deletions runtime/env-defaults
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot see where DB_POSTGIS_HSTORE and DB_POSTGIS_TOPOLOGY variables are used? Did you intend to commit these variables?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Sameer,

sorry for sloppiness on my part. I don't know how to fix that in this pull request, so I have created another one.

I decided to drop the hstore extension, as I it looks like it's not a PostGIS specific extension.

27 changes: 25 additions & 2 deletions runtime/functions
Original file line number Diff line number Diff line change
Expand Up @@ -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;" | \
Expand Down