diff --git a/Dockerfile b/Dockerfile index e58b6da..a9daaa6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,7 +17,7 @@ RUN set -eux \ && apt-get update \ && apt-get -y --no-install-recommends install \ locales gnupg2 wget ca-certificates rpl pwgen software-properties-common iputils-ping \ - apt-transport-https curl \ + apt-transport-https curl gettex \ && dpkg-divert --local --rename --add /sbin/initctl @@ -25,11 +25,6 @@ RUN set -eux \ RUN apt-get -y update; apt-get -y install build-essential autoconf libxml2-dev zlib1g-dev netcat gdal-bin - -# Temporary - PostgreSQL requires this which is not available in bullseye -ADD base_build/gdal_install.sh /gdal_install.sh -RUN chmod 0755 /gdal_install.sh;/bin/bash /gdal_install.sh - # Generating locales takes a long time. Utilize caching by runnig it by itself # early in the build process. diff --git a/README.md b/README.md index 9e58c04..56adcbb 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,14 @@ the extension is installed with the image. ` Specifies whether extensions will also be installed in template1 database.` +### Schema Initialisation +* `-e SCHEMA_NAME=` +You can pass a comma separated value of schema names which will be created when the + database initialises. The default behaviour is to create the schema in the first + database specified in the environment variable `POSTGRES_DBNAME`. If you need to + create matching schemas in all the databases that will be created you use + the environment variable `ALL_DATABASES=TRUE` + #### Configures archive mode This image uses the initial PostgreSQL values which disables the archiving option by default. @@ -247,10 +255,16 @@ all connections. #### Additional configuration -You can also define any other configuration to add to `postgres.conf`, separated by '\n' e.g.: +You can also define any other configuration to add to `extra.conf`, separated by '\n' e.g.: * `-e EXTRA_CONF="log_destination = 'stderr'\nlogging_collector = on"` +You can alternatively mount an extra config file into the setting's folder i.e + +``` +docker run --name "postgis" -v /data/extra.conf:/settings/extra.conf -p 25432:5432 -d -t kartoza/postgis +``` + If you want to reinitialize the data directory from scratch, you need to do: 1. Do backup, move data, etc. Any preparations before deleting your data directory. @@ -557,4 +571,4 @@ Tim Sutton (tim@kartoza.com) Gavin Fleming (gavin@kartoza.com) Rizky Maulana (rizky@kartoza.com) Admire Nyakudya (admire@kartoza.com) -October 2020 \ No newline at end of file +March 2021 \ No newline at end of file diff --git a/base_build/gdal_install.sh b/base_build/gdal_install.sh deleted file mode 100644 index 50f9590..0000000 --- a/base_build/gdal_install.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash - -if [[ ${IMAGE_VERSION} =~ [Bb][Uu][Ll][Ll][Ss][Ee][Yy][Ee] ]]; then - wget --progress=bar:force:noscroll -c --no-check-certificate http://ftp.br.debian.org/debian/pool/main/g/gdal/libgdal27_3.1.4+dfsg-1+b1_amd64.deb - dpkg -i libgdal27_3.1.4+dfsg-1+b1_amd64.deb -fi - diff --git a/docker-compose.build.yml b/docker-compose.build.yml index 5bbea86..26d3ea9 100644 --- a/docker-compose.build.yml +++ b/docker-compose.build.yml @@ -1,5 +1,5 @@ # Used solely for docker-compose build -version: '3' +version: '3.9' services: postgis-base: image: kartoza/postgis:base-${DISTRO}-${IMAGE_VERSION}-${IMAGE_VARIANT} diff --git a/docker-compose.yml b/docker-compose.yml index b2d2546..37fd44f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,7 @@ volumes: services: db: - image: kartoza/postgis:13.1 + image: kartoza/postgis:13-3.1 volumes: - postgis-data:/var/lib/postgresql - dbbackups:/backups diff --git a/scripts/env-data.sh b/scripts/env-data.sh index c308ed8..0863902 100644 --- a/scripts/env-data.sh +++ b/scripts/env-data.sh @@ -258,6 +258,10 @@ if [ -z "$PASSWORD_AUTHENTICATION" ]; then PASSWORD_AUTHENTICATION="scram-sha-256" fi +if [ -z "${ALL_DATABASES}" ]; then + ALL_DATABASES=FALSE +fi + # Compatibility with official postgres variable # Official postgres variable gets priority if [ -n "${POSTGRES_PASSWORD}" ]; then diff --git a/scripts/setup-conf.sh b/scripts/setup-conf.sh index d8e709a..d27e437 100644 --- a/scripts/setup-conf.sh +++ b/scripts/setup-conf.sh @@ -15,10 +15,7 @@ cat $CONF.template > $CONF sed -i '/data_directory/d' $CONF # Create a config to optimise postgis -if [[ -f ${ROOT_CONF}/postgis.conf ]];then - rm $CONF/postgis.conf -fi -cat >> ${ROOT_CONF}/postgis.conf < ${ROOT_CONF}/postgis.conf <> $CONF # Create a config for logical replication if [[ "${REPLICATION}" =~ [Tt][Rr][Uu][Ee] && "$WAL_LEVEL" == 'logical' ]]; then - if [[ -f ${ROOT_CONF}/logical_replication.conf ]];then - rm $CONF/logical_replication.conf - fi -cat >> ${ROOT_CONF}/logical_replication.conf < ${ROOT_CONF}/logical_replication.conf <> ${ROOT_CONF}/streaming_replication.conf < ${ROOT_CONF}/streaming_replication.conf <> $CONF fi -if [[ -f ${ROOT_CONF}/extra.conf ]];then - rm $CONF/extra.conf -fi +if [[ ! -f ${ROOT_CONF}/extra.conf ]]; then + # If it doesn't exists, copy from /settings directory if exists + if [[ -f /settings/extra.conf ]]; then + cp -f /settings/extra.conf ${ROOT_CONF}/extra.conf + echo "include 'extra.conf'" >> $CONF + else + # default value + if [[ ! -z $EXTRA_CONF ]]; then + echo -e $EXTRA_CONF >> ${ROOT_CONF}/extra.conf + echo "include 'extra.conf'" >> $CONF + fi + fi -if [[ ! -z $EXTRA_CONF ]]; then - echo -e $EXTRA_CONF >> ${ROOT_CONF}/extra.conf - echo "include 'extra.conf'" >> $CONF fi # Optimise PostgreSQL shared memory for PostGIS diff --git a/scripts/setup-database.sh b/scripts/setup-database.sh index c255f6b..0cc8624 100644 --- a/scripts/setup-database.sh +++ b/scripts/setup-database.sh @@ -53,8 +53,10 @@ fi # It will be owned by the docker db user # Since we now pass a comma separated list in database creation we need to search for all databases as a test + for db in $(echo ${POSTGRES_DBNAME} | tr ',' ' '); do RESULT=`su - postgres -c "psql -t -c \"SELECT count(1) from pg_database where datname='${db}';\""` + if [[ ${RESULT} -eq 0 ]]; then echo "Create db ${db}" su - postgres -c "createdb -O ${POSTGRES_USER} ${db}" @@ -79,6 +81,21 @@ for db in $(echo ${POSTGRES_DBNAME} | tr ',' ' '); do fi done +# Create schemas in the DB +for db in $(echo ${POSTGRES_DBNAME} | tr ',' ' '); do + for schemas in $(echo ${SCHEMA_NAME} | tr ',' ' '); do + SCHEMA_RESULT=`PGPASSWORD=${POSTGRES_PASS} psql -t ${db} -U ${POSTGRES_USER} -p 5432 -h localhost -c "select count(1) from information_schema.schemata where schema_name = '${schemas}' and catalog_name = '${db}';"` + if [[ ${SCHEMA_RESULT} -eq 0 ]] && [[ "${ALL_DATABASES}" =~ [Ff][Aa][Ll][Ss][Ee] ]]; then + echo "Creating schema ${schemas} in database ${SINGLE_DB}" + PGPASSWORD=${POSTGRES_PASS} psql ${SINGLE_DB} -U ${POSTGRES_USER} -p 5432 -h localhost -c " CREATE SCHEMA IF NOT EXISTS ${schemas};" + elif [[ ${SCHEMA_RESULT} -eq 0 ]] && [[ "${ALL_DATABASES}" =~ [Tt][Rr][Uu][Ee] ]]; then + echo "Creating schema ${schemas} in database ${db}" + PGPASSWORD=${POSTGRES_PASS} psql ${db} -U ${POSTGRES_USER} -p 5432 -h localhost -c " CREATE SCHEMA IF NOT EXISTS ${schemas};" + fi + done +done + + CRON_LOCKFILE="${ROOT_CONF}/.cron_ext.lock" if [ ! -f "${CRON_LOCKFILE}" ]; then su - postgres -c "psql -c 'CREATE EXTENSION IF NOT EXISTS pg_cron cascade;' ${SINGLE_DB}" diff --git a/scripts/setup-pg_hba.sh b/scripts/setup-pg_hba.sh index fd2f0b5..912083d 100644 --- a/scripts/setup-pg_hba.sh +++ b/scripts/setup-pg_hba.sh @@ -12,6 +12,11 @@ fi # Reconfigure pg_hba if environment settings changed cat ${ROOT_CONF}/pg_hba.conf.template > ${ROOT_CONF}/pg_hba.conf +# Restrict subnet to docker private network +echo "host all all 172.0.0.0/8 ${PASSWORD_AUTHENTICATION}" >> $ROOT_CONF/pg_hba.conf +# And allow access from DockerToolbox / Boot to docker on OSX +echo "host all all 192.168.0.0/16 ${PASSWORD_AUTHENTICATION}" >> $ROOT_CONF/pg_hba.conf + # 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" ]] diff --git a/scripts/setup.sh b/scripts/setup.sh index f2f78c5..3ec65b0 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -5,12 +5,6 @@ chmod 600 /etc/ssl/private/ssl-cert-snakeoil.key # These tasks are run as root source /scripts/env-data.sh - -# Restrict subnet to docker private network -echo "host all all 172.0.0.0/8 ${PASSWORD_AUTHENTICATION}" >> $ROOT_CONF/pg_hba.conf -# And allow access from DockerToolbox / Boot to docker on OSX -echo "host all all 192.168.0.0/16 ${PASSWORD_AUTHENTICATION}" >> $ROOT_CONF/pg_hba.conf - # Create backup template for conf cat $CONF > $CONF.template cat $ROOT_CONF/pg_hba.conf > $ROOT_CONF/pg_hba.conf.template