Skip to content

Commit

Permalink
Random optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
NyakudyaA committed Mar 1, 2021
1 parent e7e1d99 commit f3264d4
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 41 deletions.
7 changes: 1 addition & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@ 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



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.

Expand Down
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<PGSCHEMA>`
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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
March 2021
7 changes: 0 additions & 7 deletions base_build/gdal_install.sh

This file was deleted.

2 changes: 1 addition & 1 deletion docker-compose.build.yml
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions scripts/env-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 17 additions & 18 deletions scripts/setup-conf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF
cat > ${ROOT_CONF}/postgis.conf <<EOF
data_directory = '${DATADIR}'
port = 5432
superuser_reserved_connections= 10
Expand All @@ -42,10 +39,8 @@ echo "include '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 <<EOF

cat > ${ROOT_CONF}/logical_replication.conf <<EOF
wal_level = ${WAL_LEVEL}
max_wal_senders = ${PG_MAX_WAL_SENDERS}
wal_keep_size = ${PG_WAL_KEEP_SIZE}
Expand All @@ -59,10 +54,8 @@ fi

# Create a config for streaming replication
if [[ "${REPLICATION}" =~ [Tt][Rr][Uu][Ee] && "$WAL_LEVEL" == 'replica' ]]; then
if [[ -f ${ROOT_CONF}/streaming_replication.conf ]];then
rm $CONF/streaming_replication.conf
fi
cat >> ${ROOT_CONF}/streaming_replication.conf <<EOF

cat > ${ROOT_CONF}/streaming_replication.conf <<EOF
wal_level = ${WAL_LEVEL}
archive_mode = ${ARCHIVE_MODE}
archive_command = '${ARCHIVE_COMMAND}'
Expand All @@ -82,13 +75,19 @@ EOF
echo "include '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
Expand Down
17 changes: 17 additions & 0 deletions scripts/setup-database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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}"
Expand Down
5 changes: 5 additions & 0 deletions scripts/setup-pg_hba.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]]
Expand Down
6 changes: 0 additions & 6 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f3264d4

Please sign in to comment.