├── 10 ├── docker-assets │ ├── usr │ │ └── local │ │ │ ├── bin │ │ │ ├── log_watch.sh │ │ │ ├── postgres.sh │ │ │ └── pg_backup.sh │ │ │ └── etc │ │ │ └── pg_backup.config │ └── etc │ │ └── supervisor │ │ └── supervisord.conf ├── hooks │ └── build └── Dockerfile ├── .travis.yml ├── docker-assets ├── usr │ └── local │ │ ├── bin │ │ ├── log_watch.sh │ │ ├── postgres.sh │ │ └── pg_backup.sh │ │ └── etc │ │ └── pg_backup.config └── etc │ └── supervisor │ └── supervisord.conf ├── 9.4 ├── docker-assets │ ├── usr │ │ └── local │ │ │ ├── bin │ │ │ ├── log_watch.sh │ │ │ ├── postgres.sh │ │ │ └── pg_backup.sh │ │ │ └── etc │ │ │ └── pg_backup.config │ └── etc │ │ └── supervisor │ │ └── supervisord.conf ├── hooks │ └── build └── Dockerfile ├── 9.5 ├── docker-assets │ ├── usr │ │ └── local │ │ │ ├── bin │ │ │ ├── log_watch.sh │ │ │ ├── postgres.sh │ │ │ └── pg_backup.sh │ │ │ └── etc │ │ │ └── pg_backup.config │ └── etc │ │ └── supervisor │ │ └── supervisord.conf ├── hooks │ └── build └── Dockerfile ├── 9.6 ├── docker-assets │ ├── usr │ │ └── local │ │ │ ├── bin │ │ │ ├── log_watch.sh │ │ │ ├── postgres.sh │ │ │ └── pg_backup.sh │ │ │ └── etc │ │ │ └── pg_backup.config │ └── etc │ │ └── supervisor │ │ └── supervisord.conf ├── hooks │ └── build └── Dockerfile ├── hooks └── build ├── LICENSE ├── Makefile ├── Dockerfile.template └── README.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: bash 2 | services: docker 3 | 4 | script: 5 | - bash -c "make all" 6 | - docker images 7 | -------------------------------------------------------------------------------- /docker-assets/usr/local/bin/log_watch.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | if [ ! -e /var/log/postgresql/pg_backup.log ]; then 3 | touch /var/log/postgresql/pg_backup.log 4 | chmod 666 /var/log/postgresql/pg_backup.log 5 | fi 6 | tail -f /var/log/postgresql/pg_backup.log 7 | -------------------------------------------------------------------------------- /10/docker-assets/usr/local/bin/log_watch.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | if [ ! -e /var/log/postgresql/pg_backup.log ]; then 3 | touch /var/log/postgresql/pg_backup.log 4 | chmod 666 /var/log/postgresql/pg_backup.log 5 | fi 6 | tail -f /var/log/postgresql/pg_backup.log 7 | -------------------------------------------------------------------------------- /9.4/docker-assets/usr/local/bin/log_watch.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | if [ ! -e /var/log/postgresql/pg_backup.log ]; then 3 | touch /var/log/postgresql/pg_backup.log 4 | chmod 666 /var/log/postgresql/pg_backup.log 5 | fi 6 | tail -f /var/log/postgresql/pg_backup.log 7 | -------------------------------------------------------------------------------- /9.5/docker-assets/usr/local/bin/log_watch.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | if [ ! -e /var/log/postgresql/pg_backup.log ]; then 3 | touch /var/log/postgresql/pg_backup.log 4 | chmod 666 /var/log/postgresql/pg_backup.log 5 | fi 6 | tail -f /var/log/postgresql/pg_backup.log 7 | -------------------------------------------------------------------------------- /9.6/docker-assets/usr/local/bin/log_watch.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | if [ ! -e /var/log/postgresql/pg_backup.log ]; then 3 | touch /var/log/postgresql/pg_backup.log 4 | chmod 666 /var/log/postgresql/pg_backup.log 5 | fi 6 | tail -f /var/log/postgresql/pg_backup.log 7 | -------------------------------------------------------------------------------- /10/hooks/build: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # $IMAGE_NAME var is injected into the build so the tag is correct. 4 | 5 | echo "Build hook running" 6 | docker build --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ 7 | --build-arg VCS_REF=`git rev-parse --short HEAD` \ 8 | -t $IMAGE_NAME . 9 | -------------------------------------------------------------------------------- /hooks/build: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # $IMAGE_NAME var is injected into the build so the tag is correct. 4 | 5 | echo "Build hook running" 6 | docker build --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ 7 | --build-arg VCS_REF=`git rev-parse --short HEAD` \ 8 | -t $IMAGE_NAME . 9 | -------------------------------------------------------------------------------- /9.4/hooks/build: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # $IMAGE_NAME var is injected into the build so the tag is correct. 4 | 5 | echo "Build hook running" 6 | docker build --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ 7 | --build-arg VCS_REF=`git rev-parse --short HEAD` \ 8 | -t $IMAGE_NAME . 9 | -------------------------------------------------------------------------------- /9.5/hooks/build: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # $IMAGE_NAME var is injected into the build so the tag is correct. 4 | 5 | echo "Build hook running" 6 | docker build --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ 7 | --build-arg VCS_REF=`git rev-parse --short HEAD` \ 8 | -t $IMAGE_NAME . 9 | -------------------------------------------------------------------------------- /9.6/hooks/build: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # $IMAGE_NAME var is injected into the build so the tag is correct. 4 | 5 | echo "Build hook running" 6 | docker build --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ 7 | --build-arg VCS_REF=`git rev-parse --short HEAD` \ 8 | -t $IMAGE_NAME . 9 | -------------------------------------------------------------------------------- /10/docker-assets/etc/supervisor/supervisord.conf: -------------------------------------------------------------------------------- 1 | [supervisord] 2 | nodaemon = true 3 | logfile = /var/log/postgresql/supervisord.log 4 | pidfile = /var/run/supervisor/supervisord.pid 5 | user = root 6 | 7 | [program:cron] 8 | command = cron -f -L 2 9 | autostart = true 10 | autorestart = true 11 | stdout_logfile = /dev/stdout 12 | stdout_logfile_maxbytes = 0 13 | 14 | [program:postgres] 15 | command = /usr/local/bin/postgres.sh 16 | autostart = true 17 | autorestart = true 18 | stdout_logfile = /dev/stdout 19 | stdout_logfile_maxbytes = 0 20 | user = postgres 21 | 22 | [program:pg_backup_log] 23 | command = /usr/local/bin/log_watch.sh 24 | autostart = true 25 | autorestart = true 26 | stdout_logfile = /dev/stdout 27 | stdout_logfile_maxbytes = 0 28 | -------------------------------------------------------------------------------- /9.4/docker-assets/etc/supervisor/supervisord.conf: -------------------------------------------------------------------------------- 1 | [supervisord] 2 | nodaemon = true 3 | logfile = /var/log/postgresql/supervisord.log 4 | pidfile = /var/run/supervisor/supervisord.pid 5 | user = root 6 | 7 | [program:cron] 8 | command = cron -f -L 2 9 | autostart = true 10 | autorestart = true 11 | stdout_logfile = /dev/stdout 12 | stdout_logfile_maxbytes = 0 13 | 14 | [program:postgres] 15 | command = /usr/local/bin/postgres.sh 16 | autostart = true 17 | autorestart = true 18 | stdout_logfile = /dev/stdout 19 | stdout_logfile_maxbytes = 0 20 | user = postgres 21 | 22 | [program:pg_backup_log] 23 | command = /usr/local/bin/log_watch.sh 24 | autostart = true 25 | autorestart = true 26 | stdout_logfile = /dev/stdout 27 | stdout_logfile_maxbytes = 0 28 | -------------------------------------------------------------------------------- /9.5/docker-assets/etc/supervisor/supervisord.conf: -------------------------------------------------------------------------------- 1 | [supervisord] 2 | nodaemon = true 3 | logfile = /var/log/postgresql/supervisord.log 4 | pidfile = /var/run/supervisor/supervisord.pid 5 | user = root 6 | 7 | [program:cron] 8 | command = cron -f -L 2 9 | autostart = true 10 | autorestart = true 11 | stdout_logfile = /dev/stdout 12 | stdout_logfile_maxbytes = 0 13 | 14 | [program:postgres] 15 | command = /usr/local/bin/postgres.sh 16 | autostart = true 17 | autorestart = true 18 | stdout_logfile = /dev/stdout 19 | stdout_logfile_maxbytes = 0 20 | user = postgres 21 | 22 | [program:pg_backup_log] 23 | command = /usr/local/bin/log_watch.sh 24 | autostart = true 25 | autorestart = true 26 | stdout_logfile = /dev/stdout 27 | stdout_logfile_maxbytes = 0 28 | -------------------------------------------------------------------------------- /9.6/docker-assets/etc/supervisor/supervisord.conf: -------------------------------------------------------------------------------- 1 | [supervisord] 2 | nodaemon = true 3 | logfile = /var/log/postgresql/supervisord.log 4 | pidfile = /var/run/supervisor/supervisord.pid 5 | user = root 6 | 7 | [program:cron] 8 | command = cron -f -L 2 9 | autostart = true 10 | autorestart = true 11 | stdout_logfile = /dev/stdout 12 | stdout_logfile_maxbytes = 0 13 | 14 | [program:postgres] 15 | command = /usr/local/bin/postgres.sh 16 | autostart = true 17 | autorestart = true 18 | stdout_logfile = /dev/stdout 19 | stdout_logfile_maxbytes = 0 20 | user = postgres 21 | 22 | [program:pg_backup_log] 23 | command = /usr/local/bin/log_watch.sh 24 | autostart = true 25 | autorestart = true 26 | stdout_logfile = /dev/stdout 27 | stdout_logfile_maxbytes = 0 28 | -------------------------------------------------------------------------------- /docker-assets/etc/supervisor/supervisord.conf: -------------------------------------------------------------------------------- 1 | [supervisord] 2 | nodaemon = true 3 | logfile = /var/log/postgresql/supervisord.log 4 | pidfile = /var/run/supervisor/supervisord.pid 5 | user = root 6 | 7 | [program:cron] 8 | command = cron -f -L 2 9 | autostart = true 10 | autorestart = true 11 | stdout_logfile = /dev/stdout 12 | stdout_logfile_maxbytes = 0 13 | 14 | [program:postgres] 15 | command = /usr/local/bin/postgres.sh 16 | autostart = true 17 | autorestart = true 18 | stdout_logfile = /dev/stdout 19 | stdout_logfile_maxbytes = 0 20 | user = postgres 21 | 22 | [program:pg_backup_log] 23 | command = /usr/local/bin/log_watch.sh 24 | autostart = true 25 | autorestart = true 26 | stdout_logfile = /dev/stdout 27 | stdout_logfile_maxbytes = 0 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 James Brink 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /docker-assets/usr/local/etc/pg_backup.config: -------------------------------------------------------------------------------- 1 | ############################## 2 | ## POSTGRESQL BACKUP CONFIG ## 3 | ############################## 4 | 5 | # Optional system user to run backups as. If the user the script is running as doesn't match this 6 | # the script terminates. Leave blank to skip check. 7 | BACKUP_USER=postgres 8 | 9 | # Optional hostname to adhere to pg_hba policies. Will default to "localhost" if none specified. 10 | HOSTNAME= 11 | 12 | # Optional username to connect to database as. Will default to "postgres" if none specified. 13 | USERNAME= 14 | 15 | # This dir will be created if it doesn't exist. This must be writable by the user the script is 16 | # running as. 17 | BACKUP_DIR=/var/backups/ 18 | 19 | # List of strings to match against in database name, separated by space or comma, for which we only 20 | # wish to keep a backup of the schema, not the data. Any database names which contain any of these 21 | # values will be considered candidates. (e.g. "system_log" will match "dev_system_log_2010-01") 22 | SCHEMA_ONLY_LIST="" 23 | 24 | # Will produce a custom-format backup if set to "yes" 25 | ENABLE_CUSTOM_BACKUPS=yes 26 | 27 | # Will produce a gzipped plain-format backup if set to "yes" 28 | ENABLE_PLAIN_BACKUPS=yes 29 | 30 | 31 | #### SETTINGS FOR ROTATED BACKUPS #### 32 | 33 | # Which day to take the weekly backup from (1-7 = Monday-Sunday) 34 | DAY_OF_WEEK_TO_KEEP=5 35 | 36 | # Number of days to keep daily backups 37 | DAYS_TO_KEEP=7 38 | 39 | # How many weeks to keep weekly backups 40 | WEEKS_TO_KEEP=5 41 | 42 | ###################################### 43 | -------------------------------------------------------------------------------- /10/docker-assets/usr/local/etc/pg_backup.config: -------------------------------------------------------------------------------- 1 | ############################## 2 | ## POSTGRESQL BACKUP CONFIG ## 3 | ############################## 4 | 5 | # Optional system user to run backups as. If the user the script is running as doesn't match this 6 | # the script terminates. Leave blank to skip check. 7 | BACKUP_USER=postgres 8 | 9 | # Optional hostname to adhere to pg_hba policies. Will default to "localhost" if none specified. 10 | HOSTNAME= 11 | 12 | # Optional username to connect to database as. Will default to "postgres" if none specified. 13 | USERNAME= 14 | 15 | # This dir will be created if it doesn't exist. This must be writable by the user the script is 16 | # running as. 17 | BACKUP_DIR=/var/backups/ 18 | 19 | # List of strings to match against in database name, separated by space or comma, for which we only 20 | # wish to keep a backup of the schema, not the data. Any database names which contain any of these 21 | # values will be considered candidates. (e.g. "system_log" will match "dev_system_log_2010-01") 22 | SCHEMA_ONLY_LIST="" 23 | 24 | # Will produce a custom-format backup if set to "yes" 25 | ENABLE_CUSTOM_BACKUPS=yes 26 | 27 | # Will produce a gzipped plain-format backup if set to "yes" 28 | ENABLE_PLAIN_BACKUPS=yes 29 | 30 | 31 | #### SETTINGS FOR ROTATED BACKUPS #### 32 | 33 | # Which day to take the weekly backup from (1-7 = Monday-Sunday) 34 | DAY_OF_WEEK_TO_KEEP=5 35 | 36 | # Number of days to keep daily backups 37 | DAYS_TO_KEEP=7 38 | 39 | # How many weeks to keep weekly backups 40 | WEEKS_TO_KEEP=5 41 | 42 | ###################################### 43 | -------------------------------------------------------------------------------- /9.4/docker-assets/usr/local/etc/pg_backup.config: -------------------------------------------------------------------------------- 1 | ############################## 2 | ## POSTGRESQL BACKUP CONFIG ## 3 | ############################## 4 | 5 | # Optional system user to run backups as. If the user the script is running as doesn't match this 6 | # the script terminates. Leave blank to skip check. 7 | BACKUP_USER=postgres 8 | 9 | # Optional hostname to adhere to pg_hba policies. Will default to "localhost" if none specified. 10 | HOSTNAME= 11 | 12 | # Optional username to connect to database as. Will default to "postgres" if none specified. 13 | USERNAME= 14 | 15 | # This dir will be created if it doesn't exist. This must be writable by the user the script is 16 | # running as. 17 | BACKUP_DIR=/var/backups/ 18 | 19 | # List of strings to match against in database name, separated by space or comma, for which we only 20 | # wish to keep a backup of the schema, not the data. Any database names which contain any of these 21 | # values will be considered candidates. (e.g. "system_log" will match "dev_system_log_2010-01") 22 | SCHEMA_ONLY_LIST="" 23 | 24 | # Will produce a custom-format backup if set to "yes" 25 | ENABLE_CUSTOM_BACKUPS=yes 26 | 27 | # Will produce a gzipped plain-format backup if set to "yes" 28 | ENABLE_PLAIN_BACKUPS=yes 29 | 30 | 31 | #### SETTINGS FOR ROTATED BACKUPS #### 32 | 33 | # Which day to take the weekly backup from (1-7 = Monday-Sunday) 34 | DAY_OF_WEEK_TO_KEEP=5 35 | 36 | # Number of days to keep daily backups 37 | DAYS_TO_KEEP=7 38 | 39 | # How many weeks to keep weekly backups 40 | WEEKS_TO_KEEP=5 41 | 42 | ###################################### 43 | -------------------------------------------------------------------------------- /9.5/docker-assets/usr/local/etc/pg_backup.config: -------------------------------------------------------------------------------- 1 | ############################## 2 | ## POSTGRESQL BACKUP CONFIG ## 3 | ############################## 4 | 5 | # Optional system user to run backups as. If the user the script is running as doesn't match this 6 | # the script terminates. Leave blank to skip check. 7 | BACKUP_USER=postgres 8 | 9 | # Optional hostname to adhere to pg_hba policies. Will default to "localhost" if none specified. 10 | HOSTNAME= 11 | 12 | # Optional username to connect to database as. Will default to "postgres" if none specified. 13 | USERNAME= 14 | 15 | # This dir will be created if it doesn't exist. This must be writable by the user the script is 16 | # running as. 17 | BACKUP_DIR=/var/backups/ 18 | 19 | # List of strings to match against in database name, separated by space or comma, for which we only 20 | # wish to keep a backup of the schema, not the data. Any database names which contain any of these 21 | # values will be considered candidates. (e.g. "system_log" will match "dev_system_log_2010-01") 22 | SCHEMA_ONLY_LIST="" 23 | 24 | # Will produce a custom-format backup if set to "yes" 25 | ENABLE_CUSTOM_BACKUPS=yes 26 | 27 | # Will produce a gzipped plain-format backup if set to "yes" 28 | ENABLE_PLAIN_BACKUPS=yes 29 | 30 | 31 | #### SETTINGS FOR ROTATED BACKUPS #### 32 | 33 | # Which day to take the weekly backup from (1-7 = Monday-Sunday) 34 | DAY_OF_WEEK_TO_KEEP=5 35 | 36 | # Number of days to keep daily backups 37 | DAYS_TO_KEEP=7 38 | 39 | # How many weeks to keep weekly backups 40 | WEEKS_TO_KEEP=5 41 | 42 | ###################################### 43 | -------------------------------------------------------------------------------- /9.6/docker-assets/usr/local/etc/pg_backup.config: -------------------------------------------------------------------------------- 1 | ############################## 2 | ## POSTGRESQL BACKUP CONFIG ## 3 | ############################## 4 | 5 | # Optional system user to run backups as. If the user the script is running as doesn't match this 6 | # the script terminates. Leave blank to skip check. 7 | BACKUP_USER=postgres 8 | 9 | # Optional hostname to adhere to pg_hba policies. Will default to "localhost" if none specified. 10 | HOSTNAME= 11 | 12 | # Optional username to connect to database as. Will default to "postgres" if none specified. 13 | USERNAME= 14 | 15 | # This dir will be created if it doesn't exist. This must be writable by the user the script is 16 | # running as. 17 | BACKUP_DIR=/var/backups/ 18 | 19 | # List of strings to match against in database name, separated by space or comma, for which we only 20 | # wish to keep a backup of the schema, not the data. Any database names which contain any of these 21 | # values will be considered candidates. (e.g. "system_log" will match "dev_system_log_2010-01") 22 | SCHEMA_ONLY_LIST="" 23 | 24 | # Will produce a custom-format backup if set to "yes" 25 | ENABLE_CUSTOM_BACKUPS=yes 26 | 27 | # Will produce a gzipped plain-format backup if set to "yes" 28 | ENABLE_PLAIN_BACKUPS=yes 29 | 30 | 31 | #### SETTINGS FOR ROTATED BACKUPS #### 32 | 33 | # Which day to take the weekly backup from (1-7 = Monday-Sunday) 34 | DAY_OF_WEEK_TO_KEEP=5 35 | 36 | # Number of days to keep daily backups 37 | DAYS_TO_KEEP=7 38 | 39 | # How many weeks to keep weekly backups 40 | WEEKS_TO_KEEP=5 41 | 42 | ###################################### 43 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | NAME=jamesbrink/postgres 3 | TEMPLATE=Dockerfile.template 4 | POSGTRES_SCRIPT=docker-assets/usr/local/bin/postgres.sh 5 | SED:=$(shell [[ `command -v gsed` ]] && echo gsed || echo sed) 6 | .PHONY: clean all 10 9.6 9.5 9.4 7 | .DEFAULT_GOAL := 10 8 | 9 | all: 10 9.6 9.5 9.4 10 | 11 | 10: 12 | rm -rf $(@) 13 | mkdir -p $(@) 14 | printf "`cat $(TEMPLATE)`" $(@) $(@) $(@) "2.4" > $(@)/Dockerfile 15 | cp -r docker-assets $(@) 16 | printf "`cat $(POSGTRES_SCRIPT)`" $(@) > $(@)/docker-assets/usr/local/bin/postgres.sh 17 | cp -rp hooks $(@) 18 | docker build -t $(NAME):$(@) --build-arg VCS_REF=`git rev-parse --short HEAD` \ 19 | --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` $(@) 20 | docker tag $(NAME):$(@) latest 21 | 22 | 9.6: 23 | rm -rf $(@) 24 | mkdir -p $(@) 25 | printf "`cat $(TEMPLATE)`" $(@) $(@) $(@) "2.3" > $(@)/Dockerfile 26 | cp -r docker-assets $(@) 27 | printf "`cat $(POSGTRES_SCRIPT)`" $(@) > $(@)/docker-assets/usr/local/bin/postgres.sh 28 | cp -rp hooks $(@) 29 | docker build -t $(NAME):$(@) --build-arg VCS_REF=`git rev-parse --short HEAD` \ 30 | --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` $(@) 31 | 32 | 9.5: 33 | rm -rf $(@) 34 | mkdir -p $(@) 35 | printf "`cat $(TEMPLATE)`" $(@) $(@) $(@) "2.2" > $(@)/Dockerfile 36 | cp -r docker-assets $(@) 37 | printf "`cat $(POSGTRES_SCRIPT)`" $(@) > $(@)/docker-assets/usr/local/bin/postgres.sh 38 | cp -rp hooks $(@) 39 | docker build -t $(NAME):$(@) --build-arg VCS_REF=`git rev-parse --short HEAD` \ 40 | --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` $(@) 41 | 42 | 9.4: 43 | rm -rf $(@) 44 | mkdir -p $(@) 45 | printf "`cat $(TEMPLATE)`" $(@) $(@) $(@) "2.1" > $(@)/Dockerfile 46 | cp -r docker-assets $(@) 47 | printf "`cat $(POSGTRES_SCRIPT)`" $(@) > $(@)/docker-assets/usr/local/bin/postgres.sh 48 | cp -rp hooks $(@) 49 | docker build -t $(NAME):$(@) --build-arg VCS_REF=`git rev-parse --short HEAD` \ 50 | --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` $(@) 51 | 52 | clean: 53 | rm -rf 10 9.4 9.5 9.6 54 | -------------------------------------------------------------------------------- /10/Dockerfile: -------------------------------------------------------------------------------- 1 | # PostgtreSQL Server 2 | # 3 | # VERSION 10 4 | 5 | FROM debian:jessie 6 | 7 | ARG VCS_REF 8 | ARG BUILD_DATE 9 | 10 | LABEL maintainer="James Brink, brink.james@gmail.com" \ 11 | decription="PostgreSQL Server" \ 12 | version="10" \ 13 | org.label-schema.name="postgres" \ 14 | org.label-schema.build-date=$BUILD_DATE \ 15 | org.label-schema.vcs-ref=$VCS_REF \ 16 | org.label-schema.vcs-url="https://github.com/jamesbrink/docker-postgres" \ 17 | org.label-schema.schema-version="1.0.0-rc1" 18 | 19 | ENV postgres_version=10 \ 20 | postgis_version=2.4 21 | 22 | # Install all needed packages, this includes adding packages from postgresql's 23 | # package database. Thisi repo provides postgis as well. 24 | RUN groupadd postgres \ 25 | && useradd -s /bin/bash -d /var/postgres -m -g postgres postgres \ 26 | && apt-get update \ 27 | && apt-get install -y \ 28 | wget \ 29 | inotify-tools \ 30 | supervisor \ 31 | && echo "deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main" > \ 32 | /etc/apt/sources.list.d/pgdg.list \ 33 | && wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \ 34 | && apt-get update \ 35 | && apt-get install -y \ 36 | postgresql-${postgres_version} \ 37 | postgresql-contrib-${postgres_version} \ 38 | postgresql-${postgres_version}-postgis-${postgis_version} \ 39 | postgresql-client-${postgres_version} \ 40 | && rm -rf /var/lib/apt/lists/* \ 41 | && mkdir -p /var/run/supervisor \ 42 | && chown -R postgres:postgres /var/run/supervisor \ 43 | && chown -R postgres:postgres /var/run/postgresql /var/backups /usr/local/etc \ 44 | && echo "listen_addresses='*'" >> /etc/postgresql/${postgres_version}/main/postgresql.conf \ 45 | && echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/${postgres_version}/main/pg_hba.conf \ 46 | && sed -i "s|data_directory =.*$|data_directory = '/var/lib/postgresql/data'|g" /etc/postgresql/${postgres_version}/main/postgresql.conf \ 47 | && rm -rf /var/lib/postgresql/* \ 48 | && touch /var/lib/postgresql/firstrun && chmod 666 /var/lib/postgresql/firstrun 49 | 50 | # Add our custom assets to the container. 51 | COPY docker-assets/ / 52 | 53 | # Set proper permissions on custom assets. 54 | RUN chown postgres:postgres /usr/local/bin/postgres.sh /usr/local/etc/pg_backup.config \ 55 | && chmod +x /usr/local/bin/postgres.sh \ 56 | && chmod +x /usr/local/bin/pg_backup.sh \ 57 | && chmod +x /usr/local/bin/log_watch.sh 58 | 59 | # Store postgres versions 60 | ENV POSTGRES_VERSION=$postgres_version \ 61 | POSTGIS_VERSION=$postgis_version \ 62 | # Locale setting 63 | LOCALE=en_US.UTF-8 \ 64 | # Initial default postgres settings 65 | USER=postgres \ 66 | PASSWORD=postgres \ 67 | DATABASE=postgres \ 68 | SCHEMA=public \ 69 | POSTGIS=false \ 70 | ENCODING=SQL_ASCII \ 71 | # Database backup settings 72 | BACKUP_ENABLED=false \ 73 | BACKUP_FREQUENCY=daily \ 74 | # TODO implement these 75 | BACKUP_RETENTION=7 \ 76 | BACKUP_EMAIL=postgres \ 77 | ENVIRONMENT=development 78 | 79 | VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql", "/var/backups"] 80 | 81 | EXPOSE 5432 82 | CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"] -------------------------------------------------------------------------------- /9.4/Dockerfile: -------------------------------------------------------------------------------- 1 | # PostgtreSQL Server 2 | # 3 | # VERSION 9.4 4 | 5 | FROM debian:jessie 6 | 7 | ARG VCS_REF 8 | ARG BUILD_DATE 9 | 10 | LABEL maintainer="James Brink, brink.james@gmail.com" \ 11 | decription="PostgreSQL Server" \ 12 | version="9.4" \ 13 | org.label-schema.name="postgres" \ 14 | org.label-schema.build-date=$BUILD_DATE \ 15 | org.label-schema.vcs-ref=$VCS_REF \ 16 | org.label-schema.vcs-url="https://github.com/jamesbrink/docker-postgres" \ 17 | org.label-schema.schema-version="1.0.0-rc1" 18 | 19 | ENV postgres_version=9.4 \ 20 | postgis_version=2.1 21 | 22 | # Install all needed packages, this includes adding packages from postgresql's 23 | # package database. Thisi repo provides postgis as well. 24 | RUN groupadd postgres \ 25 | && useradd -s /bin/bash -d /var/postgres -m -g postgres postgres \ 26 | && apt-get update \ 27 | && apt-get install -y \ 28 | wget \ 29 | inotify-tools \ 30 | supervisor \ 31 | && echo "deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main" > \ 32 | /etc/apt/sources.list.d/pgdg.list \ 33 | && wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \ 34 | && apt-get update \ 35 | && apt-get install -y \ 36 | postgresql-${postgres_version} \ 37 | postgresql-contrib-${postgres_version} \ 38 | postgresql-${postgres_version}-postgis-${postgis_version} \ 39 | postgresql-client-${postgres_version} \ 40 | && rm -rf /var/lib/apt/lists/* \ 41 | && mkdir -p /var/run/supervisor \ 42 | && chown -R postgres:postgres /var/run/supervisor \ 43 | && chown -R postgres:postgres /var/run/postgresql /var/backups /usr/local/etc \ 44 | && echo "listen_addresses='*'" >> /etc/postgresql/${postgres_version}/main/postgresql.conf \ 45 | && echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/${postgres_version}/main/pg_hba.conf \ 46 | && sed -i "s|data_directory =.*$|data_directory = '/var/lib/postgresql/data'|g" /etc/postgresql/${postgres_version}/main/postgresql.conf \ 47 | && rm -rf /var/lib/postgresql/* \ 48 | && touch /var/lib/postgresql/firstrun && chmod 666 /var/lib/postgresql/firstrun 49 | 50 | # Add our custom assets to the container. 51 | COPY docker-assets/ / 52 | 53 | # Set proper permissions on custom assets. 54 | RUN chown postgres:postgres /usr/local/bin/postgres.sh /usr/local/etc/pg_backup.config \ 55 | && chmod +x /usr/local/bin/postgres.sh \ 56 | && chmod +x /usr/local/bin/pg_backup.sh \ 57 | && chmod +x /usr/local/bin/log_watch.sh 58 | 59 | # Store postgres versions 60 | ENV POSTGRES_VERSION=$postgres_version \ 61 | POSTGIS_VERSION=$postgis_version \ 62 | # Locale setting 63 | LOCALE=en_US.UTF-8 \ 64 | # Initial default postgres settings 65 | USER=postgres \ 66 | PASSWORD=postgres \ 67 | DATABASE=postgres \ 68 | SCHEMA=public \ 69 | POSTGIS=false \ 70 | ENCODING=SQL_ASCII \ 71 | # Database backup settings 72 | BACKUP_ENABLED=false \ 73 | BACKUP_FREQUENCY=daily \ 74 | # TODO implement these 75 | BACKUP_RETENTION=7 \ 76 | BACKUP_EMAIL=postgres \ 77 | ENVIRONMENT=development 78 | 79 | VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql", "/var/backups"] 80 | 81 | EXPOSE 5432 82 | CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"] -------------------------------------------------------------------------------- /9.5/Dockerfile: -------------------------------------------------------------------------------- 1 | # PostgtreSQL Server 2 | # 3 | # VERSION 9.5 4 | 5 | FROM debian:jessie 6 | 7 | ARG VCS_REF 8 | ARG BUILD_DATE 9 | 10 | LABEL maintainer="James Brink, brink.james@gmail.com" \ 11 | decription="PostgreSQL Server" \ 12 | version="9.5" \ 13 | org.label-schema.name="postgres" \ 14 | org.label-schema.build-date=$BUILD_DATE \ 15 | org.label-schema.vcs-ref=$VCS_REF \ 16 | org.label-schema.vcs-url="https://github.com/jamesbrink/docker-postgres" \ 17 | org.label-schema.schema-version="1.0.0-rc1" 18 | 19 | ENV postgres_version=9.5 \ 20 | postgis_version=2.2 21 | 22 | # Install all needed packages, this includes adding packages from postgresql's 23 | # package database. Thisi repo provides postgis as well. 24 | RUN groupadd postgres \ 25 | && useradd -s /bin/bash -d /var/postgres -m -g postgres postgres \ 26 | && apt-get update \ 27 | && apt-get install -y \ 28 | wget \ 29 | inotify-tools \ 30 | supervisor \ 31 | && echo "deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main" > \ 32 | /etc/apt/sources.list.d/pgdg.list \ 33 | && wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \ 34 | && apt-get update \ 35 | && apt-get install -y \ 36 | postgresql-${postgres_version} \ 37 | postgresql-contrib-${postgres_version} \ 38 | postgresql-${postgres_version}-postgis-${postgis_version} \ 39 | postgresql-client-${postgres_version} \ 40 | && rm -rf /var/lib/apt/lists/* \ 41 | && mkdir -p /var/run/supervisor \ 42 | && chown -R postgres:postgres /var/run/supervisor \ 43 | && chown -R postgres:postgres /var/run/postgresql /var/backups /usr/local/etc \ 44 | && echo "listen_addresses='*'" >> /etc/postgresql/${postgres_version}/main/postgresql.conf \ 45 | && echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/${postgres_version}/main/pg_hba.conf \ 46 | && sed -i "s|data_directory =.*$|data_directory = '/var/lib/postgresql/data'|g" /etc/postgresql/${postgres_version}/main/postgresql.conf \ 47 | && rm -rf /var/lib/postgresql/* \ 48 | && touch /var/lib/postgresql/firstrun && chmod 666 /var/lib/postgresql/firstrun 49 | 50 | # Add our custom assets to the container. 51 | COPY docker-assets/ / 52 | 53 | # Set proper permissions on custom assets. 54 | RUN chown postgres:postgres /usr/local/bin/postgres.sh /usr/local/etc/pg_backup.config \ 55 | && chmod +x /usr/local/bin/postgres.sh \ 56 | && chmod +x /usr/local/bin/pg_backup.sh \ 57 | && chmod +x /usr/local/bin/log_watch.sh 58 | 59 | # Store postgres versions 60 | ENV POSTGRES_VERSION=$postgres_version \ 61 | POSTGIS_VERSION=$postgis_version \ 62 | # Locale setting 63 | LOCALE=en_US.UTF-8 \ 64 | # Initial default postgres settings 65 | USER=postgres \ 66 | PASSWORD=postgres \ 67 | DATABASE=postgres \ 68 | SCHEMA=public \ 69 | POSTGIS=false \ 70 | ENCODING=SQL_ASCII \ 71 | # Database backup settings 72 | BACKUP_ENABLED=false \ 73 | BACKUP_FREQUENCY=daily \ 74 | # TODO implement these 75 | BACKUP_RETENTION=7 \ 76 | BACKUP_EMAIL=postgres \ 77 | ENVIRONMENT=development 78 | 79 | VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql", "/var/backups"] 80 | 81 | EXPOSE 5432 82 | CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"] -------------------------------------------------------------------------------- /9.6/Dockerfile: -------------------------------------------------------------------------------- 1 | # PostgtreSQL Server 2 | # 3 | # VERSION 9.6 4 | 5 | FROM debian:jessie 6 | 7 | ARG VCS_REF 8 | ARG BUILD_DATE 9 | 10 | LABEL maintainer="James Brink, brink.james@gmail.com" \ 11 | decription="PostgreSQL Server" \ 12 | version="9.6" \ 13 | org.label-schema.name="postgres" \ 14 | org.label-schema.build-date=$BUILD_DATE \ 15 | org.label-schema.vcs-ref=$VCS_REF \ 16 | org.label-schema.vcs-url="https://github.com/jamesbrink/docker-postgres" \ 17 | org.label-schema.schema-version="1.0.0-rc1" 18 | 19 | ENV postgres_version=9.6 \ 20 | postgis_version=2.3 21 | 22 | # Install all needed packages, this includes adding packages from postgresql's 23 | # package database. Thisi repo provides postgis as well. 24 | RUN groupadd postgres \ 25 | && useradd -s /bin/bash -d /var/postgres -m -g postgres postgres \ 26 | && apt-get update \ 27 | && apt-get install -y \ 28 | wget \ 29 | inotify-tools \ 30 | supervisor \ 31 | && echo "deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main" > \ 32 | /etc/apt/sources.list.d/pgdg.list \ 33 | && wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \ 34 | && apt-get update \ 35 | && apt-get install -y \ 36 | postgresql-${postgres_version} \ 37 | postgresql-contrib-${postgres_version} \ 38 | postgresql-${postgres_version}-postgis-${postgis_version} \ 39 | postgresql-client-${postgres_version} \ 40 | && rm -rf /var/lib/apt/lists/* \ 41 | && mkdir -p /var/run/supervisor \ 42 | && chown -R postgres:postgres /var/run/supervisor \ 43 | && chown -R postgres:postgres /var/run/postgresql /var/backups /usr/local/etc \ 44 | && echo "listen_addresses='*'" >> /etc/postgresql/${postgres_version}/main/postgresql.conf \ 45 | && echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/${postgres_version}/main/pg_hba.conf \ 46 | && sed -i "s|data_directory =.*$|data_directory = '/var/lib/postgresql/data'|g" /etc/postgresql/${postgres_version}/main/postgresql.conf \ 47 | && rm -rf /var/lib/postgresql/* \ 48 | && touch /var/lib/postgresql/firstrun && chmod 666 /var/lib/postgresql/firstrun 49 | 50 | # Add our custom assets to the container. 51 | COPY docker-assets/ / 52 | 53 | # Set proper permissions on custom assets. 54 | RUN chown postgres:postgres /usr/local/bin/postgres.sh /usr/local/etc/pg_backup.config \ 55 | && chmod +x /usr/local/bin/postgres.sh \ 56 | && chmod +x /usr/local/bin/pg_backup.sh \ 57 | && chmod +x /usr/local/bin/log_watch.sh 58 | 59 | # Store postgres versions 60 | ENV POSTGRES_VERSION=$postgres_version \ 61 | POSTGIS_VERSION=$postgis_version \ 62 | # Locale setting 63 | LOCALE=en_US.UTF-8 \ 64 | # Initial default postgres settings 65 | USER=postgres \ 66 | PASSWORD=postgres \ 67 | DATABASE=postgres \ 68 | SCHEMA=public \ 69 | POSTGIS=false \ 70 | ENCODING=SQL_ASCII \ 71 | # Database backup settings 72 | BACKUP_ENABLED=false \ 73 | BACKUP_FREQUENCY=daily \ 74 | # TODO implement these 75 | BACKUP_RETENTION=7 \ 76 | BACKUP_EMAIL=postgres \ 77 | ENVIRONMENT=development 78 | 79 | VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql", "/var/backups"] 80 | 81 | EXPOSE 5432 82 | CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"] -------------------------------------------------------------------------------- /Dockerfile.template: -------------------------------------------------------------------------------- 1 | # PostgtreSQL Server 2 | # 3 | # VERSION %s 4 | 5 | FROM debian:jessie 6 | 7 | ARG VCS_REF 8 | ARG BUILD_DATE 9 | 10 | LABEL maintainer="James Brink, brink.james@gmail.com" \ 11 | decription="PostgreSQL Server" \ 12 | version="%s" \ 13 | org.label-schema.name="postgres" \ 14 | org.label-schema.build-date=$BUILD_DATE \ 15 | org.label-schema.vcs-ref=$VCS_REF \ 16 | org.label-schema.vcs-url="https://github.com/jamesbrink/docker-postgres" \ 17 | org.label-schema.schema-version="1.0.0-rc1" 18 | 19 | ENV postgres_version=%s \ 20 | postgis_version=%s 21 | 22 | # Install all needed packages, this includes adding packages from postgresql's 23 | # package database. Thisi repo provides postgis as well. 24 | RUN groupadd postgres \ 25 | && useradd -s /bin/bash -d /var/postgres -m -g postgres postgres \ 26 | && apt-get update \ 27 | && apt-get install -y \ 28 | wget \ 29 | inotify-tools \ 30 | supervisor \ 31 | && echo "deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main" > \ 32 | /etc/apt/sources.list.d/pgdg.list \ 33 | && wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \ 34 | && apt-get update \ 35 | && apt-get install -y \ 36 | postgresql-${postgres_version} \ 37 | postgresql-contrib-${postgres_version} \ 38 | postgresql-${postgres_version}-postgis-${postgis_version} \ 39 | postgresql-client-${postgres_version} \ 40 | && rm -rf /var/lib/apt/lists/* \ 41 | && mkdir -p /var/run/supervisor \ 42 | && chown -R postgres:postgres /var/run/supervisor \ 43 | && chown -R postgres:postgres /var/run/postgresql /var/backups /usr/local/etc \ 44 | && echo "listen_addresses='*'" >> /etc/postgresql/${postgres_version}/main/postgresql.conf \ 45 | && echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/${postgres_version}/main/pg_hba.conf \ 46 | && sed -i "s|data_directory =.*$|data_directory = '/var/lib/postgresql/data'|g" /etc/postgresql/${postgres_version}/main/postgresql.conf \ 47 | && rm -rf /var/lib/postgresql/* \ 48 | && touch /var/lib/postgresql/firstrun && chmod 666 /var/lib/postgresql/firstrun 49 | 50 | # Add our custom assets to the container. 51 | COPY docker-assets/ / 52 | 53 | # Set proper permissions on custom assets. 54 | RUN chown postgres:postgres /usr/local/bin/postgres.sh /usr/local/etc/pg_backup.config \ 55 | && chmod +x /usr/local/bin/postgres.sh \ 56 | && chmod +x /usr/local/bin/pg_backup.sh \ 57 | && chmod +x /usr/local/bin/log_watch.sh 58 | 59 | # Store postgres versions 60 | ENV POSTGRES_VERSION=$postgres_version \ 61 | POSTGIS_VERSION=$postgis_version \ 62 | # Locale setting 63 | LOCALE=en_US.UTF-8 \ 64 | # Initial default postgres settings 65 | USER=postgres \ 66 | PASSWORD=postgres \ 67 | DATABASE=postgres \ 68 | SCHEMA=public \ 69 | POSTGIS=false \ 70 | ENCODING=SQL_ASCII \ 71 | # Database backup settings 72 | BACKUP_ENABLED=false \ 73 | BACKUP_FREQUENCY=daily \ 74 | # TODO implement these 75 | BACKUP_RETENTION=7 \ 76 | BACKUP_EMAIL=postgres \ 77 | ENVIRONMENT=development 78 | 79 | VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql", "/var/backups"] 80 | 81 | EXPOSE 5432 82 | CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"] 83 | -------------------------------------------------------------------------------- /docker-assets/usr/local/bin/postgres.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # need to make the dir for /var/run/sql-version.tmp/ 3 | POSTGRES_VERSION=%s 4 | 5 | # Create postgres data directory and run initdb if needed 6 | # This is useful for docker volumes 7 | if [ ! -e /var/lib/postgresql/data ]; then 8 | echo "Creating data directory" 9 | mkdir -p /var/lib/postgresql/data 10 | touch /var/lib/postgresql/firstrun 11 | echo "Initializing database files" 12 | /usr/lib/postgresql/$POSTGRES_VERSION/bin/initdb -D /var/lib/postgresql/data/ 13 | fi 14 | 15 | # Create postgres backup directory if needed 16 | mkdir -p /var/backups 17 | 18 | create_user () { 19 | if [ -f /var/lib/postgresql/firstrun ]; then 20 | mkdir -p /var/run/postgresql/$POSTGRES_VERSION-main.pg_stat_tmp 21 | echo "Waiting for PostgreSQL to start" 22 | while [ ! -e /var/run/postgresql/$POSTGRES_VERSION-main.pid ]; do 23 | inotifywait -q -q -e create /var/run/postgresql/ 24 | done 25 | 26 | # We sleep here for 2 seconds to allow clean output, and speration from postgres startup messages 27 | sleep 2 28 | echo "Below are your configured options." 29 | echo -e "================\nUSER: $USER\nPASSWORD: $PASSWORD\nDATABASE: $DATABASE\nSCHEMA: $SCHEMA\nENCODING: $ENCODING\nPOSTGIS: $POSTGIS\n================" 30 | # Ensure template1 gets updated with proper encoding 31 | psql -c "UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';" 32 | psql -c "DROP DATABASE template1;" 33 | psql -c "CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = '$ENCODING';" 34 | psql -c "UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';" 35 | psql -d 'template1' -c "VACUUM FREEZE;" 36 | if [ "$USER" == "postgres" ]; then 37 | echo "ALTER USER :user WITH PASSWORD :'password' ;" | psql --set user=$USER --set password=$PASSWORD 38 | if [ "$DATABASE" != "postgres" ]; then 39 | createdb -E $ENCODING -T template0 $DATABASE 40 | fi 41 | else 42 | echo "CREATE USER :user WITH SUPERUSER PASSWORD :'password' ;" | psql --set user=$USER --set password=$PASSWORD && createdb -E $ENCODING -T template0 $DATABASE 43 | fi 44 | 45 | echo "CREATING SCHEMA $SCHEMA" 46 | echo "CREATE SCHEMA $SCHEMA;" | psql --set user=$USER --set password=$PASSWORD $DATABASE 47 | 48 | if echo $POSTGIS |grep -i -q true; then 49 | echo "CREATING EXTENSIONS" 50 | echo "CREATE EXTENSION postgis;CREATE EXTENSION postgis_topology;" | psql -d $DATABASE 51 | else 52 | echo "NOT CREATING EXTENSIONS" 53 | fi 54 | 55 | # Create .pgpass for use with backups 56 | echo "localhost:5432:*:$USER:$PASSWORD" > /var/lib/postgresql/.pgpass 57 | chmod 0600 /var/lib/postgresql/.pgpass 58 | 59 | # Update pg_backup with proper user 60 | sed -i "s/^USERNAME=.*$/USERNAME=$USER/" /usr/local/etc/pg_backup.config 61 | 62 | # Schedule backups 63 | if [ "${BACKUP_ENABLED,,}" == "true" ]; then 64 | # TODO rotate this log 65 | BACKUP_COMMAND="/usr/local/bin/pg_backup.sh -c /usr/local/etc/pg_backup.config >> /var/log/postgresql/pg_backup.log 2>&1" 66 | echo "Scheduling PostgreSQL Backups" 67 | 68 | case ${BACKUP_FREQUENCY,,} in 69 | hourly) 70 | echo "Scheduling Hourly Backups" 71 | echo -e "MAILTO=$BACKUP_EMAIL\n0 * * * * $BACKUP_COMMAND" | crontab 72 | echo -e "Resulting cron:\n`crontab -l`" 73 | ;; 74 | daily) 75 | echo "Scheduling Daily Backups" 76 | echo -e "MAILTO=$BACKUP_EMAIL\n0 0 * * * $BACKUP_COMMAND" | crontab 77 | echo -e "Resulting cron:\n`crontab -l`" 78 | ;; 79 | weekly) 80 | echo "Scheduling Weekly Backups" 81 | echo -e "MAILTO=$BACKUP_EMAIL\n0 0 * * 0 $BACKUP_COMMAND" | crontab 82 | echo -e "Resulting cron:\n`crontab -l`" 83 | ;; 84 | *) 85 | echo "$BACKUP_FREQUENCY is not valid for BACKUP_FREQUENCY, acceptable values are hourly, daily, or weekly" 86 | ;; 87 | esac 88 | 89 | fi 90 | 91 | rm /var/lib/postgresql/firstrun 92 | fi 93 | } 94 | create_user & 95 | exec /usr/lib/postgresql/$POSTGRES_VERSION/bin/postgres -D /var/lib/postgresql/data -c config_file=/etc/postgresql/$POSTGRES_VERSION/main/postgresql.conf 96 | -------------------------------------------------------------------------------- /10/docker-assets/usr/local/bin/postgres.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # need to make the dir for /var/run/sql-version.tmp/ 3 | POSTGRES_VERSION=10 4 | 5 | # Create postgres data directory and run initdb if needed 6 | # This is useful for docker volumes 7 | if [ ! -e /var/lib/postgresql/data ]; then 8 | echo "Creating data directory" 9 | mkdir -p /var/lib/postgresql/data 10 | touch /var/lib/postgresql/firstrun 11 | echo "Initializing database files" 12 | /usr/lib/postgresql/$POSTGRES_VERSION/bin/initdb -D /var/lib/postgresql/data/ 13 | fi 14 | 15 | # Create postgres backup directory if needed 16 | mkdir -p /var/backups 17 | 18 | create_user () { 19 | if [ -f /var/lib/postgresql/firstrun ]; then 20 | mkdir -p /var/run/postgresql/$POSTGRES_VERSION-main.pg_stat_tmp 21 | echo "Waiting for PostgreSQL to start" 22 | while [ ! -e /var/run/postgresql/$POSTGRES_VERSION-main.pid ]; do 23 | inotifywait -q -q -e create /var/run/postgresql/ 24 | done 25 | 26 | # We sleep here for 2 seconds to allow clean output, and speration from postgres startup messages 27 | sleep 2 28 | echo "Below are your configured options." 29 | echo -e "================ 30 | USER: $USER 31 | PASSWORD: $PASSWORD 32 | DATABASE: $DATABASE 33 | SCHEMA: $SCHEMA 34 | ENCODING: $ENCODING 35 | POSTGIS: $POSTGIS 36 | ================" 37 | # Ensure template1 gets updated with proper encoding 38 | psql -c "UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';" 39 | psql -c "DROP DATABASE template1;" 40 | psql -c "CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = '$ENCODING';" 41 | psql -c "UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';" 42 | psql -d 'template1' -c "VACUUM FREEZE;" 43 | if [ "$USER" == "postgres" ]; then 44 | echo "ALTER USER :user WITH PASSWORD :'password' ;" | psql --set user=$USER --set password=$PASSWORD 45 | if [ "$DATABASE" != "postgres" ]; then 46 | createdb -E $ENCODING -T template0 $DATABASE 47 | fi 48 | else 49 | echo "CREATE USER :user WITH SUPERUSER PASSWORD :'password' ;" | psql --set user=$USER --set password=$PASSWORD && createdb -E $ENCODING -T template0 $DATABASE 50 | fi 51 | 52 | echo "CREATING SCHEMA $SCHEMA" 53 | echo "CREATE SCHEMA $SCHEMA;" | psql --set user=$USER --set password=$PASSWORD $DATABASE 54 | 55 | if echo $POSTGIS |grep -i -q true; then 56 | echo "CREATING EXTENSIONS" 57 | echo "CREATE EXTENSION postgis;CREATE EXTENSION postgis_topology;" | psql -d $DATABASE 58 | else 59 | echo "NOT CREATING EXTENSIONS" 60 | fi 61 | 62 | # Create .pgpass for use with backups 63 | echo "localhost:5432:*:$USER:$PASSWORD" > /var/lib/postgresql/.pgpass 64 | chmod 0600 /var/lib/postgresql/.pgpass 65 | 66 | # Update pg_backup with proper user 67 | sed -i "s/^USERNAME=.*$/USERNAME=$USER/" /usr/local/etc/pg_backup.config 68 | 69 | # Schedule backups 70 | if [ "${BACKUP_ENABLED,,}" == "true" ]; then 71 | # TODO rotate this log 72 | BACKUP_COMMAND="/usr/local/bin/pg_backup.sh -c /usr/local/etc/pg_backup.config >> /var/log/postgresql/pg_backup.log 2>&1" 73 | echo "Scheduling PostgreSQL Backups" 74 | 75 | case ${BACKUP_FREQUENCY,,} in 76 | hourly) 77 | echo "Scheduling Hourly Backups" 78 | echo -e "MAILTO=$BACKUP_EMAIL 79 | 0 * * * * $BACKUP_COMMAND" | crontab 80 | echo -e "Resulting cron: 81 | `crontab -l`" 82 | ;; 83 | daily) 84 | echo "Scheduling Daily Backups" 85 | echo -e "MAILTO=$BACKUP_EMAIL 86 | 0 0 * * * $BACKUP_COMMAND" | crontab 87 | echo -e "Resulting cron: 88 | `crontab -l`" 89 | ;; 90 | weekly) 91 | echo "Scheduling Weekly Backups" 92 | echo -e "MAILTO=$BACKUP_EMAIL 93 | 0 0 * * 0 $BACKUP_COMMAND" | crontab 94 | echo -e "Resulting cron: 95 | `crontab -l`" 96 | ;; 97 | *) 98 | echo "$BACKUP_FREQUENCY is not valid for BACKUP_FREQUENCY, acceptable values are hourly, daily, or weekly" 99 | ;; 100 | esac 101 | 102 | fi 103 | 104 | rm /var/lib/postgresql/firstrun 105 | fi 106 | } 107 | create_user & 108 | exec /usr/lib/postgresql/$POSTGRES_VERSION/bin/postgres -D /var/lib/postgresql/data -c config_file=/etc/postgresql/$POSTGRES_VERSION/main/postgresql.conf -------------------------------------------------------------------------------- /9.4/docker-assets/usr/local/bin/postgres.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # need to make the dir for /var/run/sql-version.tmp/ 3 | POSTGRES_VERSION=9.4 4 | 5 | # Create postgres data directory and run initdb if needed 6 | # This is useful for docker volumes 7 | if [ ! -e /var/lib/postgresql/data ]; then 8 | echo "Creating data directory" 9 | mkdir -p /var/lib/postgresql/data 10 | touch /var/lib/postgresql/firstrun 11 | echo "Initializing database files" 12 | /usr/lib/postgresql/$POSTGRES_VERSION/bin/initdb -D /var/lib/postgresql/data/ 13 | fi 14 | 15 | # Create postgres backup directory if needed 16 | mkdir -p /var/backups 17 | 18 | create_user () { 19 | if [ -f /var/lib/postgresql/firstrun ]; then 20 | mkdir -p /var/run/postgresql/$POSTGRES_VERSION-main.pg_stat_tmp 21 | echo "Waiting for PostgreSQL to start" 22 | while [ ! -e /var/run/postgresql/$POSTGRES_VERSION-main.pid ]; do 23 | inotifywait -q -q -e create /var/run/postgresql/ 24 | done 25 | 26 | # We sleep here for 2 seconds to allow clean output, and speration from postgres startup messages 27 | sleep 2 28 | echo "Below are your configured options." 29 | echo -e "================ 30 | USER: $USER 31 | PASSWORD: $PASSWORD 32 | DATABASE: $DATABASE 33 | SCHEMA: $SCHEMA 34 | ENCODING: $ENCODING 35 | POSTGIS: $POSTGIS 36 | ================" 37 | # Ensure template1 gets updated with proper encoding 38 | psql -c "UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';" 39 | psql -c "DROP DATABASE template1;" 40 | psql -c "CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = '$ENCODING';" 41 | psql -c "UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';" 42 | psql -d 'template1' -c "VACUUM FREEZE;" 43 | if [ "$USER" == "postgres" ]; then 44 | echo "ALTER USER :user WITH PASSWORD :'password' ;" | psql --set user=$USER --set password=$PASSWORD 45 | if [ "$DATABASE" != "postgres" ]; then 46 | createdb -E $ENCODING -T template0 $DATABASE 47 | fi 48 | else 49 | echo "CREATE USER :user WITH SUPERUSER PASSWORD :'password' ;" | psql --set user=$USER --set password=$PASSWORD && createdb -E $ENCODING -T template0 $DATABASE 50 | fi 51 | 52 | echo "CREATING SCHEMA $SCHEMA" 53 | echo "CREATE SCHEMA $SCHEMA;" | psql --set user=$USER --set password=$PASSWORD $DATABASE 54 | 55 | if echo $POSTGIS |grep -i -q true; then 56 | echo "CREATING EXTENSIONS" 57 | echo "CREATE EXTENSION postgis;CREATE EXTENSION postgis_topology;" | psql -d $DATABASE 58 | else 59 | echo "NOT CREATING EXTENSIONS" 60 | fi 61 | 62 | # Create .pgpass for use with backups 63 | echo "localhost:5432:*:$USER:$PASSWORD" > /var/lib/postgresql/.pgpass 64 | chmod 0600 /var/lib/postgresql/.pgpass 65 | 66 | # Update pg_backup with proper user 67 | sed -i "s/^USERNAME=.*$/USERNAME=$USER/" /usr/local/etc/pg_backup.config 68 | 69 | # Schedule backups 70 | if [ "${BACKUP_ENABLED,,}" == "true" ]; then 71 | # TODO rotate this log 72 | BACKUP_COMMAND="/usr/local/bin/pg_backup.sh -c /usr/local/etc/pg_backup.config >> /var/log/postgresql/pg_backup.log 2>&1" 73 | echo "Scheduling PostgreSQL Backups" 74 | 75 | case ${BACKUP_FREQUENCY,,} in 76 | hourly) 77 | echo "Scheduling Hourly Backups" 78 | echo -e "MAILTO=$BACKUP_EMAIL 79 | 0 * * * * $BACKUP_COMMAND" | crontab 80 | echo -e "Resulting cron: 81 | `crontab -l`" 82 | ;; 83 | daily) 84 | echo "Scheduling Daily Backups" 85 | echo -e "MAILTO=$BACKUP_EMAIL 86 | 0 0 * * * $BACKUP_COMMAND" | crontab 87 | echo -e "Resulting cron: 88 | `crontab -l`" 89 | ;; 90 | weekly) 91 | echo "Scheduling Weekly Backups" 92 | echo -e "MAILTO=$BACKUP_EMAIL 93 | 0 0 * * 0 $BACKUP_COMMAND" | crontab 94 | echo -e "Resulting cron: 95 | `crontab -l`" 96 | ;; 97 | *) 98 | echo "$BACKUP_FREQUENCY is not valid for BACKUP_FREQUENCY, acceptable values are hourly, daily, or weekly" 99 | ;; 100 | esac 101 | 102 | fi 103 | 104 | rm /var/lib/postgresql/firstrun 105 | fi 106 | } 107 | create_user & 108 | exec /usr/lib/postgresql/$POSTGRES_VERSION/bin/postgres -D /var/lib/postgresql/data -c config_file=/etc/postgresql/$POSTGRES_VERSION/main/postgresql.conf -------------------------------------------------------------------------------- /9.5/docker-assets/usr/local/bin/postgres.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # need to make the dir for /var/run/sql-version.tmp/ 3 | POSTGRES_VERSION=9.5 4 | 5 | # Create postgres data directory and run initdb if needed 6 | # This is useful for docker volumes 7 | if [ ! -e /var/lib/postgresql/data ]; then 8 | echo "Creating data directory" 9 | mkdir -p /var/lib/postgresql/data 10 | touch /var/lib/postgresql/firstrun 11 | echo "Initializing database files" 12 | /usr/lib/postgresql/$POSTGRES_VERSION/bin/initdb -D /var/lib/postgresql/data/ 13 | fi 14 | 15 | # Create postgres backup directory if needed 16 | mkdir -p /var/backups 17 | 18 | create_user () { 19 | if [ -f /var/lib/postgresql/firstrun ]; then 20 | mkdir -p /var/run/postgresql/$POSTGRES_VERSION-main.pg_stat_tmp 21 | echo "Waiting for PostgreSQL to start" 22 | while [ ! -e /var/run/postgresql/$POSTGRES_VERSION-main.pid ]; do 23 | inotifywait -q -q -e create /var/run/postgresql/ 24 | done 25 | 26 | # We sleep here for 2 seconds to allow clean output, and speration from postgres startup messages 27 | sleep 2 28 | echo "Below are your configured options." 29 | echo -e "================ 30 | USER: $USER 31 | PASSWORD: $PASSWORD 32 | DATABASE: $DATABASE 33 | SCHEMA: $SCHEMA 34 | ENCODING: $ENCODING 35 | POSTGIS: $POSTGIS 36 | ================" 37 | # Ensure template1 gets updated with proper encoding 38 | psql -c "UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';" 39 | psql -c "DROP DATABASE template1;" 40 | psql -c "CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = '$ENCODING';" 41 | psql -c "UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';" 42 | psql -d 'template1' -c "VACUUM FREEZE;" 43 | if [ "$USER" == "postgres" ]; then 44 | echo "ALTER USER :user WITH PASSWORD :'password' ;" | psql --set user=$USER --set password=$PASSWORD 45 | if [ "$DATABASE" != "postgres" ]; then 46 | createdb -E $ENCODING -T template0 $DATABASE 47 | fi 48 | else 49 | echo "CREATE USER :user WITH SUPERUSER PASSWORD :'password' ;" | psql --set user=$USER --set password=$PASSWORD && createdb -E $ENCODING -T template0 $DATABASE 50 | fi 51 | 52 | echo "CREATING SCHEMA $SCHEMA" 53 | echo "CREATE SCHEMA $SCHEMA;" | psql --set user=$USER --set password=$PASSWORD $DATABASE 54 | 55 | if echo $POSTGIS |grep -i -q true; then 56 | echo "CREATING EXTENSIONS" 57 | echo "CREATE EXTENSION postgis;CREATE EXTENSION postgis_topology;" | psql -d $DATABASE 58 | else 59 | echo "NOT CREATING EXTENSIONS" 60 | fi 61 | 62 | # Create .pgpass for use with backups 63 | echo "localhost:5432:*:$USER:$PASSWORD" > /var/lib/postgresql/.pgpass 64 | chmod 0600 /var/lib/postgresql/.pgpass 65 | 66 | # Update pg_backup with proper user 67 | sed -i "s/^USERNAME=.*$/USERNAME=$USER/" /usr/local/etc/pg_backup.config 68 | 69 | # Schedule backups 70 | if [ "${BACKUP_ENABLED,,}" == "true" ]; then 71 | # TODO rotate this log 72 | BACKUP_COMMAND="/usr/local/bin/pg_backup.sh -c /usr/local/etc/pg_backup.config >> /var/log/postgresql/pg_backup.log 2>&1" 73 | echo "Scheduling PostgreSQL Backups" 74 | 75 | case ${BACKUP_FREQUENCY,,} in 76 | hourly) 77 | echo "Scheduling Hourly Backups" 78 | echo -e "MAILTO=$BACKUP_EMAIL 79 | 0 * * * * $BACKUP_COMMAND" | crontab 80 | echo -e "Resulting cron: 81 | `crontab -l`" 82 | ;; 83 | daily) 84 | echo "Scheduling Daily Backups" 85 | echo -e "MAILTO=$BACKUP_EMAIL 86 | 0 0 * * * $BACKUP_COMMAND" | crontab 87 | echo -e "Resulting cron: 88 | `crontab -l`" 89 | ;; 90 | weekly) 91 | echo "Scheduling Weekly Backups" 92 | echo -e "MAILTO=$BACKUP_EMAIL 93 | 0 0 * * 0 $BACKUP_COMMAND" | crontab 94 | echo -e "Resulting cron: 95 | `crontab -l`" 96 | ;; 97 | *) 98 | echo "$BACKUP_FREQUENCY is not valid for BACKUP_FREQUENCY, acceptable values are hourly, daily, or weekly" 99 | ;; 100 | esac 101 | 102 | fi 103 | 104 | rm /var/lib/postgresql/firstrun 105 | fi 106 | } 107 | create_user & 108 | exec /usr/lib/postgresql/$POSTGRES_VERSION/bin/postgres -D /var/lib/postgresql/data -c config_file=/etc/postgresql/$POSTGRES_VERSION/main/postgresql.conf -------------------------------------------------------------------------------- /9.6/docker-assets/usr/local/bin/postgres.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # need to make the dir for /var/run/sql-version.tmp/ 3 | POSTGRES_VERSION=9.6 4 | 5 | # Create postgres data directory and run initdb if needed 6 | # This is useful for docker volumes 7 | if [ ! -e /var/lib/postgresql/data ]; then 8 | echo "Creating data directory" 9 | mkdir -p /var/lib/postgresql/data 10 | touch /var/lib/postgresql/firstrun 11 | echo "Initializing database files" 12 | /usr/lib/postgresql/$POSTGRES_VERSION/bin/initdb -D /var/lib/postgresql/data/ 13 | fi 14 | 15 | # Create postgres backup directory if needed 16 | mkdir -p /var/backups 17 | 18 | create_user () { 19 | if [ -f /var/lib/postgresql/firstrun ]; then 20 | mkdir -p /var/run/postgresql/$POSTGRES_VERSION-main.pg_stat_tmp 21 | echo "Waiting for PostgreSQL to start" 22 | while [ ! -e /var/run/postgresql/$POSTGRES_VERSION-main.pid ]; do 23 | inotifywait -q -q -e create /var/run/postgresql/ 24 | done 25 | 26 | # We sleep here for 2 seconds to allow clean output, and speration from postgres startup messages 27 | sleep 2 28 | echo "Below are your configured options." 29 | echo -e "================ 30 | USER: $USER 31 | PASSWORD: $PASSWORD 32 | DATABASE: $DATABASE 33 | SCHEMA: $SCHEMA 34 | ENCODING: $ENCODING 35 | POSTGIS: $POSTGIS 36 | ================" 37 | # Ensure template1 gets updated with proper encoding 38 | psql -c "UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';" 39 | psql -c "DROP DATABASE template1;" 40 | psql -c "CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = '$ENCODING';" 41 | psql -c "UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';" 42 | psql -d 'template1' -c "VACUUM FREEZE;" 43 | if [ "$USER" == "postgres" ]; then 44 | echo "ALTER USER :user WITH PASSWORD :'password' ;" | psql --set user=$USER --set password=$PASSWORD 45 | if [ "$DATABASE" != "postgres" ]; then 46 | createdb -E $ENCODING -T template0 $DATABASE 47 | fi 48 | else 49 | echo "CREATE USER :user WITH SUPERUSER PASSWORD :'password' ;" | psql --set user=$USER --set password=$PASSWORD && createdb -E $ENCODING -T template0 $DATABASE 50 | fi 51 | 52 | echo "CREATING SCHEMA $SCHEMA" 53 | echo "CREATE SCHEMA $SCHEMA;" | psql --set user=$USER --set password=$PASSWORD $DATABASE 54 | 55 | if echo $POSTGIS |grep -i -q true; then 56 | echo "CREATING EXTENSIONS" 57 | echo "CREATE EXTENSION postgis;CREATE EXTENSION postgis_topology;" | psql -d $DATABASE 58 | else 59 | echo "NOT CREATING EXTENSIONS" 60 | fi 61 | 62 | # Create .pgpass for use with backups 63 | echo "localhost:5432:*:$USER:$PASSWORD" > /var/lib/postgresql/.pgpass 64 | chmod 0600 /var/lib/postgresql/.pgpass 65 | 66 | # Update pg_backup with proper user 67 | sed -i "s/^USERNAME=.*$/USERNAME=$USER/" /usr/local/etc/pg_backup.config 68 | 69 | # Schedule backups 70 | if [ "${BACKUP_ENABLED,,}" == "true" ]; then 71 | # TODO rotate this log 72 | BACKUP_COMMAND="/usr/local/bin/pg_backup.sh -c /usr/local/etc/pg_backup.config >> /var/log/postgresql/pg_backup.log 2>&1" 73 | echo "Scheduling PostgreSQL Backups" 74 | 75 | case ${BACKUP_FREQUENCY,,} in 76 | hourly) 77 | echo "Scheduling Hourly Backups" 78 | echo -e "MAILTO=$BACKUP_EMAIL 79 | 0 * * * * $BACKUP_COMMAND" | crontab 80 | echo -e "Resulting cron: 81 | `crontab -l`" 82 | ;; 83 | daily) 84 | echo "Scheduling Daily Backups" 85 | echo -e "MAILTO=$BACKUP_EMAIL 86 | 0 0 * * * $BACKUP_COMMAND" | crontab 87 | echo -e "Resulting cron: 88 | `crontab -l`" 89 | ;; 90 | weekly) 91 | echo "Scheduling Weekly Backups" 92 | echo -e "MAILTO=$BACKUP_EMAIL 93 | 0 0 * * 0 $BACKUP_COMMAND" | crontab 94 | echo -e "Resulting cron: 95 | `crontab -l`" 96 | ;; 97 | *) 98 | echo "$BACKUP_FREQUENCY is not valid for BACKUP_FREQUENCY, acceptable values are hourly, daily, or weekly" 99 | ;; 100 | esac 101 | 102 | fi 103 | 104 | rm /var/lib/postgresql/firstrun 105 | fi 106 | } 107 | create_user & 108 | exec /usr/lib/postgresql/$POSTGRES_VERSION/bin/postgres -D /var/lib/postgresql/data -c config_file=/etc/postgresql/$POSTGRES_VERSION/main/postgresql.conf -------------------------------------------------------------------------------- /docker-assets/usr/local/bin/pg_backup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Postgres backup script adapted from https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux 3 | 4 | ########################### 5 | ####### LOAD CONFIG ####### 6 | ########################### 7 | 8 | while [ $# -gt 0 ]; do 9 | case $1 in 10 | -c) 11 | if [ -r "$2" ]; then 12 | source "$2" 13 | shift 2 14 | else 15 | ${ECHO} "Unreadable config file \"$2\"" 1>&2 16 | exit 1 17 | fi 18 | ;; 19 | *) 20 | ${ECHO} "Unknown Option \"$1\"" 1>&2 21 | exit 2 22 | ;; 23 | esac 24 | done 25 | 26 | 27 | ########################### 28 | #### PRE-BACKUP CHECKS #### 29 | ########################### 30 | 31 | # Make sure we're running as the required backup user 32 | if [ "$BACKUP_USER" != "" -a "$(id -un)" != "$BACKUP_USER" ]; then 33 | echo "This script must be run as $BACKUP_USER. Exiting." 1>&2 34 | exit 1; 35 | fi; 36 | 37 | 38 | ########################### 39 | ### INITIALISE DEFAULTS ### 40 | ########################### 41 | 42 | if [ ! $HOSTNAME ]; then 43 | HOSTNAME="localhost" 44 | fi; 45 | 46 | if [ ! $USERNAME ]; then 47 | USERNAME="postgres" 48 | fi; 49 | 50 | ########################### 51 | #### START THE BACKUPS #### 52 | ########################### 53 | 54 | # Use IS8601 Date + Time 55 | FINAL_BACKUP_DIR=$BACKUP_DIR"`date +%Y-%m-%dT%H_%M_%SZ`/" 56 | 57 | echo "Making backup directory in $FINAL_BACKUP_DIR" 58 | 59 | if ! mkdir -p $FINAL_BACKUP_DIR; then 60 | echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2 61 | exit 1; 62 | fi; 63 | 64 | 65 | ########################### 66 | ### SCHEMA-ONLY BACKUPS ### 67 | ########################### 68 | 69 | for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ } 70 | do 71 | SCHEMA_ONLY_CLAUSE="$SCHEMA_ONLY_CLAUSE or datname ~ '$SCHEMA_ONLY_DB'" 72 | done 73 | 74 | SCHEMA_ONLY_QUERY="select datname from pg_database where false $SCHEMA_ONLY_CLAUSE order by datname;" 75 | 76 | echo -e "\n\nPerforming schema-only backups" 77 | echo -e "--------------------------------------------\n" 78 | 79 | SCHEMA_ONLY_DB_LIST=`psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$SCHEMA_ONLY_QUERY" postgres` 80 | 81 | echo -e "The following databases were matched for schema-only backup:\n${SCHEMA_ONLY_DB_LIST}\n" 82 | 83 | for DATABASE in $SCHEMA_ONLY_DB_LIST 84 | do 85 | echo "Schema-only backup of $DATABASE" 86 | 87 | if ! pg_dump -Fp -s -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress; then 88 | echo "[!!ERROR!!] Failed to backup database schema of $DATABASE" 1>&2 89 | else 90 | mv $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz 91 | fi 92 | done 93 | 94 | 95 | ########################### 96 | ###### FULL BACKUPS ####### 97 | ########################### 98 | 99 | for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ } 100 | do 101 | EXCLUDE_SCHEMA_ONLY_CLAUSE="$EXCLUDE_SCHEMA_ONLY_CLAUSE and datname !~ '$SCHEMA_ONLY_DB'" 102 | done 103 | 104 | FULL_BACKUP_QUERY="select datname from pg_database where not datistemplate and datallowconn $EXCLUDE_SCHEMA_ONLY_CLAUSE order by datname;" 105 | 106 | echo -e "\n\nPerforming full backups" 107 | echo -e "--------------------------------------------\n" 108 | 109 | for DATABASE in `psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$FULL_BACKUP_QUERY" postgres` 110 | do 111 | if [ $ENABLE_PLAIN_BACKUPS = "yes" ] 112 | then 113 | echo "Plain backup of $DATABASE" 114 | 115 | if ! pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then 116 | echo "[!!ERROR!!] Failed to produce plain backup database $DATABASE" 1>&2 117 | else 118 | mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz 119 | fi 120 | fi 121 | 122 | if [ $ENABLE_CUSTOM_BACKUPS = "yes" ] 123 | then 124 | echo "Custom backup of $DATABASE" 125 | 126 | if ! pg_dump -Fc -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" -f $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress; then 127 | echo "[!!ERROR!!] Failed to produce custom backup database $DATABASE" 1>&2 128 | else 129 | mv $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress $FINAL_BACKUP_DIR"$DATABASE".custom 130 | fi 131 | fi 132 | 133 | done 134 | 135 | echo -e "\nAll database backups complete!" 136 | -------------------------------------------------------------------------------- /10/docker-assets/usr/local/bin/pg_backup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Postgres backup script adapted from https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux 3 | 4 | ########################### 5 | ####### LOAD CONFIG ####### 6 | ########################### 7 | 8 | while [ $# -gt 0 ]; do 9 | case $1 in 10 | -c) 11 | if [ -r "$2" ]; then 12 | source "$2" 13 | shift 2 14 | else 15 | ${ECHO} "Unreadable config file \"$2\"" 1>&2 16 | exit 1 17 | fi 18 | ;; 19 | *) 20 | ${ECHO} "Unknown Option \"$1\"" 1>&2 21 | exit 2 22 | ;; 23 | esac 24 | done 25 | 26 | 27 | ########################### 28 | #### PRE-BACKUP CHECKS #### 29 | ########################### 30 | 31 | # Make sure we're running as the required backup user 32 | if [ "$BACKUP_USER" != "" -a "$(id -un)" != "$BACKUP_USER" ]; then 33 | echo "This script must be run as $BACKUP_USER. Exiting." 1>&2 34 | exit 1; 35 | fi; 36 | 37 | 38 | ########################### 39 | ### INITIALISE DEFAULTS ### 40 | ########################### 41 | 42 | if [ ! $HOSTNAME ]; then 43 | HOSTNAME="localhost" 44 | fi; 45 | 46 | if [ ! $USERNAME ]; then 47 | USERNAME="postgres" 48 | fi; 49 | 50 | ########################### 51 | #### START THE BACKUPS #### 52 | ########################### 53 | 54 | # Use IS8601 Date + Time 55 | FINAL_BACKUP_DIR=$BACKUP_DIR"`date +%Y-%m-%dT%H_%M_%SZ`/" 56 | 57 | echo "Making backup directory in $FINAL_BACKUP_DIR" 58 | 59 | if ! mkdir -p $FINAL_BACKUP_DIR; then 60 | echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2 61 | exit 1; 62 | fi; 63 | 64 | 65 | ########################### 66 | ### SCHEMA-ONLY BACKUPS ### 67 | ########################### 68 | 69 | for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ } 70 | do 71 | SCHEMA_ONLY_CLAUSE="$SCHEMA_ONLY_CLAUSE or datname ~ '$SCHEMA_ONLY_DB'" 72 | done 73 | 74 | SCHEMA_ONLY_QUERY="select datname from pg_database where false $SCHEMA_ONLY_CLAUSE order by datname;" 75 | 76 | echo -e "\n\nPerforming schema-only backups" 77 | echo -e "--------------------------------------------\n" 78 | 79 | SCHEMA_ONLY_DB_LIST=`psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$SCHEMA_ONLY_QUERY" postgres` 80 | 81 | echo -e "The following databases were matched for schema-only backup:\n${SCHEMA_ONLY_DB_LIST}\n" 82 | 83 | for DATABASE in $SCHEMA_ONLY_DB_LIST 84 | do 85 | echo "Schema-only backup of $DATABASE" 86 | 87 | if ! pg_dump -Fp -s -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress; then 88 | echo "[!!ERROR!!] Failed to backup database schema of $DATABASE" 1>&2 89 | else 90 | mv $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz 91 | fi 92 | done 93 | 94 | 95 | ########################### 96 | ###### FULL BACKUPS ####### 97 | ########################### 98 | 99 | for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ } 100 | do 101 | EXCLUDE_SCHEMA_ONLY_CLAUSE="$EXCLUDE_SCHEMA_ONLY_CLAUSE and datname !~ '$SCHEMA_ONLY_DB'" 102 | done 103 | 104 | FULL_BACKUP_QUERY="select datname from pg_database where not datistemplate and datallowconn $EXCLUDE_SCHEMA_ONLY_CLAUSE order by datname;" 105 | 106 | echo -e "\n\nPerforming full backups" 107 | echo -e "--------------------------------------------\n" 108 | 109 | for DATABASE in `psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$FULL_BACKUP_QUERY" postgres` 110 | do 111 | if [ $ENABLE_PLAIN_BACKUPS = "yes" ] 112 | then 113 | echo "Plain backup of $DATABASE" 114 | 115 | if ! pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then 116 | echo "[!!ERROR!!] Failed to produce plain backup database $DATABASE" 1>&2 117 | else 118 | mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz 119 | fi 120 | fi 121 | 122 | if [ $ENABLE_CUSTOM_BACKUPS = "yes" ] 123 | then 124 | echo "Custom backup of $DATABASE" 125 | 126 | if ! pg_dump -Fc -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" -f $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress; then 127 | echo "[!!ERROR!!] Failed to produce custom backup database $DATABASE" 1>&2 128 | else 129 | mv $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress $FINAL_BACKUP_DIR"$DATABASE".custom 130 | fi 131 | fi 132 | 133 | done 134 | 135 | echo -e "\nAll database backups complete!" 136 | -------------------------------------------------------------------------------- /9.4/docker-assets/usr/local/bin/pg_backup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Postgres backup script adapted from https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux 3 | 4 | ########################### 5 | ####### LOAD CONFIG ####### 6 | ########################### 7 | 8 | while [ $# -gt 0 ]; do 9 | case $1 in 10 | -c) 11 | if [ -r "$2" ]; then 12 | source "$2" 13 | shift 2 14 | else 15 | ${ECHO} "Unreadable config file \"$2\"" 1>&2 16 | exit 1 17 | fi 18 | ;; 19 | *) 20 | ${ECHO} "Unknown Option \"$1\"" 1>&2 21 | exit 2 22 | ;; 23 | esac 24 | done 25 | 26 | 27 | ########################### 28 | #### PRE-BACKUP CHECKS #### 29 | ########################### 30 | 31 | # Make sure we're running as the required backup user 32 | if [ "$BACKUP_USER" != "" -a "$(id -un)" != "$BACKUP_USER" ]; then 33 | echo "This script must be run as $BACKUP_USER. Exiting." 1>&2 34 | exit 1; 35 | fi; 36 | 37 | 38 | ########################### 39 | ### INITIALISE DEFAULTS ### 40 | ########################### 41 | 42 | if [ ! $HOSTNAME ]; then 43 | HOSTNAME="localhost" 44 | fi; 45 | 46 | if [ ! $USERNAME ]; then 47 | USERNAME="postgres" 48 | fi; 49 | 50 | ########################### 51 | #### START THE BACKUPS #### 52 | ########################### 53 | 54 | # Use IS8601 Date + Time 55 | FINAL_BACKUP_DIR=$BACKUP_DIR"`date +%Y-%m-%dT%H_%M_%SZ`/" 56 | 57 | echo "Making backup directory in $FINAL_BACKUP_DIR" 58 | 59 | if ! mkdir -p $FINAL_BACKUP_DIR; then 60 | echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2 61 | exit 1; 62 | fi; 63 | 64 | 65 | ########################### 66 | ### SCHEMA-ONLY BACKUPS ### 67 | ########################### 68 | 69 | for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ } 70 | do 71 | SCHEMA_ONLY_CLAUSE="$SCHEMA_ONLY_CLAUSE or datname ~ '$SCHEMA_ONLY_DB'" 72 | done 73 | 74 | SCHEMA_ONLY_QUERY="select datname from pg_database where false $SCHEMA_ONLY_CLAUSE order by datname;" 75 | 76 | echo -e "\n\nPerforming schema-only backups" 77 | echo -e "--------------------------------------------\n" 78 | 79 | SCHEMA_ONLY_DB_LIST=`psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$SCHEMA_ONLY_QUERY" postgres` 80 | 81 | echo -e "The following databases were matched for schema-only backup:\n${SCHEMA_ONLY_DB_LIST}\n" 82 | 83 | for DATABASE in $SCHEMA_ONLY_DB_LIST 84 | do 85 | echo "Schema-only backup of $DATABASE" 86 | 87 | if ! pg_dump -Fp -s -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress; then 88 | echo "[!!ERROR!!] Failed to backup database schema of $DATABASE" 1>&2 89 | else 90 | mv $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz 91 | fi 92 | done 93 | 94 | 95 | ########################### 96 | ###### FULL BACKUPS ####### 97 | ########################### 98 | 99 | for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ } 100 | do 101 | EXCLUDE_SCHEMA_ONLY_CLAUSE="$EXCLUDE_SCHEMA_ONLY_CLAUSE and datname !~ '$SCHEMA_ONLY_DB'" 102 | done 103 | 104 | FULL_BACKUP_QUERY="select datname from pg_database where not datistemplate and datallowconn $EXCLUDE_SCHEMA_ONLY_CLAUSE order by datname;" 105 | 106 | echo -e "\n\nPerforming full backups" 107 | echo -e "--------------------------------------------\n" 108 | 109 | for DATABASE in `psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$FULL_BACKUP_QUERY" postgres` 110 | do 111 | if [ $ENABLE_PLAIN_BACKUPS = "yes" ] 112 | then 113 | echo "Plain backup of $DATABASE" 114 | 115 | if ! pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then 116 | echo "[!!ERROR!!] Failed to produce plain backup database $DATABASE" 1>&2 117 | else 118 | mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz 119 | fi 120 | fi 121 | 122 | if [ $ENABLE_CUSTOM_BACKUPS = "yes" ] 123 | then 124 | echo "Custom backup of $DATABASE" 125 | 126 | if ! pg_dump -Fc -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" -f $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress; then 127 | echo "[!!ERROR!!] Failed to produce custom backup database $DATABASE" 1>&2 128 | else 129 | mv $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress $FINAL_BACKUP_DIR"$DATABASE".custom 130 | fi 131 | fi 132 | 133 | done 134 | 135 | echo -e "\nAll database backups complete!" 136 | -------------------------------------------------------------------------------- /9.5/docker-assets/usr/local/bin/pg_backup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Postgres backup script adapted from https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux 3 | 4 | ########################### 5 | ####### LOAD CONFIG ####### 6 | ########################### 7 | 8 | while [ $# -gt 0 ]; do 9 | case $1 in 10 | -c) 11 | if [ -r "$2" ]; then 12 | source "$2" 13 | shift 2 14 | else 15 | ${ECHO} "Unreadable config file \"$2\"" 1>&2 16 | exit 1 17 | fi 18 | ;; 19 | *) 20 | ${ECHO} "Unknown Option \"$1\"" 1>&2 21 | exit 2 22 | ;; 23 | esac 24 | done 25 | 26 | 27 | ########################### 28 | #### PRE-BACKUP CHECKS #### 29 | ########################### 30 | 31 | # Make sure we're running as the required backup user 32 | if [ "$BACKUP_USER" != "" -a "$(id -un)" != "$BACKUP_USER" ]; then 33 | echo "This script must be run as $BACKUP_USER. Exiting." 1>&2 34 | exit 1; 35 | fi; 36 | 37 | 38 | ########################### 39 | ### INITIALISE DEFAULTS ### 40 | ########################### 41 | 42 | if [ ! $HOSTNAME ]; then 43 | HOSTNAME="localhost" 44 | fi; 45 | 46 | if [ ! $USERNAME ]; then 47 | USERNAME="postgres" 48 | fi; 49 | 50 | ########################### 51 | #### START THE BACKUPS #### 52 | ########################### 53 | 54 | # Use IS8601 Date + Time 55 | FINAL_BACKUP_DIR=$BACKUP_DIR"`date +%Y-%m-%dT%H_%M_%SZ`/" 56 | 57 | echo "Making backup directory in $FINAL_BACKUP_DIR" 58 | 59 | if ! mkdir -p $FINAL_BACKUP_DIR; then 60 | echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2 61 | exit 1; 62 | fi; 63 | 64 | 65 | ########################### 66 | ### SCHEMA-ONLY BACKUPS ### 67 | ########################### 68 | 69 | for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ } 70 | do 71 | SCHEMA_ONLY_CLAUSE="$SCHEMA_ONLY_CLAUSE or datname ~ '$SCHEMA_ONLY_DB'" 72 | done 73 | 74 | SCHEMA_ONLY_QUERY="select datname from pg_database where false $SCHEMA_ONLY_CLAUSE order by datname;" 75 | 76 | echo -e "\n\nPerforming schema-only backups" 77 | echo -e "--------------------------------------------\n" 78 | 79 | SCHEMA_ONLY_DB_LIST=`psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$SCHEMA_ONLY_QUERY" postgres` 80 | 81 | echo -e "The following databases were matched for schema-only backup:\n${SCHEMA_ONLY_DB_LIST}\n" 82 | 83 | for DATABASE in $SCHEMA_ONLY_DB_LIST 84 | do 85 | echo "Schema-only backup of $DATABASE" 86 | 87 | if ! pg_dump -Fp -s -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress; then 88 | echo "[!!ERROR!!] Failed to backup database schema of $DATABASE" 1>&2 89 | else 90 | mv $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz 91 | fi 92 | done 93 | 94 | 95 | ########################### 96 | ###### FULL BACKUPS ####### 97 | ########################### 98 | 99 | for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ } 100 | do 101 | EXCLUDE_SCHEMA_ONLY_CLAUSE="$EXCLUDE_SCHEMA_ONLY_CLAUSE and datname !~ '$SCHEMA_ONLY_DB'" 102 | done 103 | 104 | FULL_BACKUP_QUERY="select datname from pg_database where not datistemplate and datallowconn $EXCLUDE_SCHEMA_ONLY_CLAUSE order by datname;" 105 | 106 | echo -e "\n\nPerforming full backups" 107 | echo -e "--------------------------------------------\n" 108 | 109 | for DATABASE in `psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$FULL_BACKUP_QUERY" postgres` 110 | do 111 | if [ $ENABLE_PLAIN_BACKUPS = "yes" ] 112 | then 113 | echo "Plain backup of $DATABASE" 114 | 115 | if ! pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then 116 | echo "[!!ERROR!!] Failed to produce plain backup database $DATABASE" 1>&2 117 | else 118 | mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz 119 | fi 120 | fi 121 | 122 | if [ $ENABLE_CUSTOM_BACKUPS = "yes" ] 123 | then 124 | echo "Custom backup of $DATABASE" 125 | 126 | if ! pg_dump -Fc -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" -f $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress; then 127 | echo "[!!ERROR!!] Failed to produce custom backup database $DATABASE" 1>&2 128 | else 129 | mv $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress $FINAL_BACKUP_DIR"$DATABASE".custom 130 | fi 131 | fi 132 | 133 | done 134 | 135 | echo -e "\nAll database backups complete!" 136 | -------------------------------------------------------------------------------- /9.6/docker-assets/usr/local/bin/pg_backup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Postgres backup script adapted from https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux 3 | 4 | ########################### 5 | ####### LOAD CONFIG ####### 6 | ########################### 7 | 8 | while [ $# -gt 0 ]; do 9 | case $1 in 10 | -c) 11 | if [ -r "$2" ]; then 12 | source "$2" 13 | shift 2 14 | else 15 | ${ECHO} "Unreadable config file \"$2\"" 1>&2 16 | exit 1 17 | fi 18 | ;; 19 | *) 20 | ${ECHO} "Unknown Option \"$1\"" 1>&2 21 | exit 2 22 | ;; 23 | esac 24 | done 25 | 26 | 27 | ########################### 28 | #### PRE-BACKUP CHECKS #### 29 | ########################### 30 | 31 | # Make sure we're running as the required backup user 32 | if [ "$BACKUP_USER" != "" -a "$(id -un)" != "$BACKUP_USER" ]; then 33 | echo "This script must be run as $BACKUP_USER. Exiting." 1>&2 34 | exit 1; 35 | fi; 36 | 37 | 38 | ########################### 39 | ### INITIALISE DEFAULTS ### 40 | ########################### 41 | 42 | if [ ! $HOSTNAME ]; then 43 | HOSTNAME="localhost" 44 | fi; 45 | 46 | if [ ! $USERNAME ]; then 47 | USERNAME="postgres" 48 | fi; 49 | 50 | ########################### 51 | #### START THE BACKUPS #### 52 | ########################### 53 | 54 | # Use IS8601 Date + Time 55 | FINAL_BACKUP_DIR=$BACKUP_DIR"`date +%Y-%m-%dT%H_%M_%SZ`/" 56 | 57 | echo "Making backup directory in $FINAL_BACKUP_DIR" 58 | 59 | if ! mkdir -p $FINAL_BACKUP_DIR; then 60 | echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2 61 | exit 1; 62 | fi; 63 | 64 | 65 | ########################### 66 | ### SCHEMA-ONLY BACKUPS ### 67 | ########################### 68 | 69 | for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ } 70 | do 71 | SCHEMA_ONLY_CLAUSE="$SCHEMA_ONLY_CLAUSE or datname ~ '$SCHEMA_ONLY_DB'" 72 | done 73 | 74 | SCHEMA_ONLY_QUERY="select datname from pg_database where false $SCHEMA_ONLY_CLAUSE order by datname;" 75 | 76 | echo -e "\n\nPerforming schema-only backups" 77 | echo -e "--------------------------------------------\n" 78 | 79 | SCHEMA_ONLY_DB_LIST=`psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$SCHEMA_ONLY_QUERY" postgres` 80 | 81 | echo -e "The following databases were matched for schema-only backup:\n${SCHEMA_ONLY_DB_LIST}\n" 82 | 83 | for DATABASE in $SCHEMA_ONLY_DB_LIST 84 | do 85 | echo "Schema-only backup of $DATABASE" 86 | 87 | if ! pg_dump -Fp -s -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress; then 88 | echo "[!!ERROR!!] Failed to backup database schema of $DATABASE" 1>&2 89 | else 90 | mv $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz 91 | fi 92 | done 93 | 94 | 95 | ########################### 96 | ###### FULL BACKUPS ####### 97 | ########################### 98 | 99 | for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ } 100 | do 101 | EXCLUDE_SCHEMA_ONLY_CLAUSE="$EXCLUDE_SCHEMA_ONLY_CLAUSE and datname !~ '$SCHEMA_ONLY_DB'" 102 | done 103 | 104 | FULL_BACKUP_QUERY="select datname from pg_database where not datistemplate and datallowconn $EXCLUDE_SCHEMA_ONLY_CLAUSE order by datname;" 105 | 106 | echo -e "\n\nPerforming full backups" 107 | echo -e "--------------------------------------------\n" 108 | 109 | for DATABASE in `psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$FULL_BACKUP_QUERY" postgres` 110 | do 111 | if [ $ENABLE_PLAIN_BACKUPS = "yes" ] 112 | then 113 | echo "Plain backup of $DATABASE" 114 | 115 | if ! pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then 116 | echo "[!!ERROR!!] Failed to produce plain backup database $DATABASE" 1>&2 117 | else 118 | mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz 119 | fi 120 | fi 121 | 122 | if [ $ENABLE_CUSTOM_BACKUPS = "yes" ] 123 | then 124 | echo "Custom backup of $DATABASE" 125 | 126 | if ! pg_dump -Fc -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" -f $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress; then 127 | echo "[!!ERROR!!] Failed to produce custom backup database $DATABASE" 1>&2 128 | else 129 | mv $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress $FINAL_BACKUP_DIR"$DATABASE".custom 130 | fi 131 | fi 132 | 133 | done 134 | 135 | echo -e "\nAll database backups complete!" 136 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker Container for PostgreSQL (latest version - 10.3) 2 | 3 | [![Build Status](https://travis-ci.org/jamesbrink/docker-postgres.svg?branch=master)](https://travis-ci.org/jamesbrink/docker-postgres) [![Docker Automated build](https://img.shields.io/docker/automated/jamesbrink/postgres.svg)](https://hub.docker.com/r/jamesbrink/postgres/) [![Docker Pulls](https://img.shields.io/docker/pulls/jamesbrink/postgres.svg)](https://hub.docker.com/r/jamesbrink/postgres/) [![Docker Stars](https://img.shields.io/docker/stars/jamesbrink/postgres.svg)](https://hub.docker.com/r/jamesbrink/postgres/) [![](https://images.microbadger.com/badges/image/jamesbrink/postgres.svg)](https://microbadger.com/images/jamesbrink/postgres "Get your own image badge on microbadger.com") [![](https://images.microbadger.com/badges/version/jamesbrink/postgres.svg)](https://microbadger.com/images/jamesbrink/postgres "Get your own version badge on microbadger.com") 4 | 5 | 6 | ## About 7 | 8 | This is a highly configurable container for [PostgreSQL 9.6](http://www.postgresql.org/). 9 | It allows for basic initial user/pass and schema configuration via ENV variables. 10 | 11 | 12 | ## Extensions 13 | 14 | This container is preloaded with the following extensions. 15 | 16 | * [PostgreSQL-Contrib](http://www.postgresql.org/docs/9.6/static/contrib.html) 17 | * [PostGIS](http://postgis.net/) 18 | 19 | 20 | 21 | ## Usage 22 | 23 | To run with default settings 24 | 25 | ``` 26 | docker run -P --name postgres jamesbrink/postgres 27 | ``` 28 | 29 | To run with customized settings 30 | 31 | ``` 32 | docker run -P --name postgres -e USER=foo -e PASSWORD=bar -e DATABASE=foo -e ENCODING=UTF8 jamesbrink/postgres 33 | ``` 34 | This will create a new container with the username and schema of `foo` encoded in UTF-8 and a password of `bar` 35 | 36 | To add PostGIS support to the database pass the environment variable POSTGIS=true. 37 | ``` 38 | docker run -P --name postgresql -e USER=foo -e PASSWORD=bar -e DATABASE=foo -e ENCODING=UTF8 -e POSTGIS=true jamesbrink/postgresql 39 | ``` 40 | 41 | Here is an example of the run. Take note of the user/pass and schema when you start the container as it will not be shown again. Of course you can change these settings and add additional users and schemas at any point. 42 | 43 | 44 | ```shell 45 | james@ubuntu:~$ docker run -P --name postgres jamesbrink/postgresql 46 | Waiting for PostgreSQL to start 47 | 2014-04-21 20:36:42 UTC LOG: database system was shut down at 2014-04-21 04:34:43 UTC 48 | 2014-04-21 20:36:42 UTC LOG: autovacuum launcher started 49 | 2014-04-21 20:36:42 UTC LOG: database system is ready to accept connections 50 | Below are your configured options. 51 | ================ 52 | USER: postgres 53 | PASSWORD: postgres 54 | DATABASE: public 55 | POSTGIS: false 56 | ================ 57 | ALTER ROLE 58 | ``` 59 | 60 | 61 | ## Container Linking 62 | 63 | Here are some examples of linking containers to postgresql 64 | 65 | First we create a container, here I am using a random password generated from openssl 66 | 67 | ```shell 68 | james@ubuntu:~$ docker run -P --name postgres -e PASSWORD=`openssl rand -hex 10` -e USER=james -e DATABASE=test jamesbrink/postgresql 69 | Waiting for PostgreSQL to start 70 | Below are your configured options. 71 | ================ 72 | USER: james 73 | PASSWORD: 5387fc737962925e2c70 74 | DATABASE: test 75 | POSTGIS: false 76 | ENCODING: SQL_ASCII 77 | ================ 78 | 2014-04-21 21:07:24 UTC LOG: database system was shut down at 2014-04-21 04:34:43 UTC 79 | 2014-04-21 21:07:24 UTC LOG: autovacuum launcher started 80 | 2014-04-21 21:07:24 UTC LOG: database system is ready to accept connections 81 | CREATE USER 82 | ``` 83 | 84 | With the postgres container up and running, lets create a new container and link it with an alias of `db`. 85 | 86 | ```shell 87 | james@ubuntu:~$ docker run -i -t --link postgres:db ubuntu /bin/bash 88 | ``` 89 | 90 | Now from inside the container ensure you have a postgresql client installed. 91 | 92 | ```shell 93 | root@47b16d7d1e13:/# apt-get install postgresql-client 94 | ``` 95 | 96 | You can now connect to the database in a variety of ways. lets first inspect the environment. The variables of interest here are all prefixed with `DB_` 97 | 98 | ```shell 99 | root@47b16d7d1e13:/# env 100 | HOSTNAME=47b16d7d1e13 101 | DB_NAME=/cocky_babbage/db 102 | TERM=xterm 103 | DB_PORT_5432_TCP_ADDR=172.17.0.2 104 | DB_ENV_DATABASE=test 105 | DB_PORT=tcp://172.17.0.2:5432 106 | DB_PORT_5432_TCP=tcp://172.17.0.2:5432 107 | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 108 | PWD=/ 109 | DB_ENV_PASSWORD=5387fc737962925e2c70 110 | DB_PORT_5432_TCP_PORT=5432 111 | SHLVL=1 112 | HOME=/ 113 | DB_ENV_USER=james 114 | DB_PORT_5432_TCP_PROTO=tcp 115 | _=/usr/bin/env 116 | ``` 117 | 118 | Connect manually. 119 | ```shell 120 | root@47b16d7d1e13:/# psql -h 172.17.0.2 -U james test 121 | Password for user james: 122 | psql (9.1.13, server 9.3.4) 123 | WARNING: psql version 9.1, server version 9.3. 124 | Some psql features might not work. 125 | SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256) 126 | Type "help" for help. 127 | test=# 128 | ``` 129 | 130 | 131 | Connect using ENV variables. 132 | 133 | ```shell 134 | root@47b16d7d1e13:/# PGPASSWORD=$DB_ENV_PASSWORD psql -h $DB_PORT_5432_TCP_ADDR -U $DB_ENV_USER $DB_ENV_DATABASE 135 | psql (9.1.13, server 9.3.4) 136 | WARNING: psql version 9.1, server version 9.3. 137 | Some psql features might not work. 138 | SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256) 139 | Type "help" for help. 140 | test=# 141 | ``` 142 | 143 | Create an application friendly URI. 144 | 145 | ```shell 146 | root@47b16d7d1e13:/# export DB_URI=postgres://$DB_ENV_USER:$DB_ENV_PASSWORD@$DB_PORT_5432_TCP_ADDR:$DB_PORT_5432_TCP_PORT/$DB_ENV_DATABASE 147 | root@47b16d7d1e13:/# echo $DB_URI 148 | postgres://james:5387fc737962925e2c70@172.17.0.2:5432/test 149 | ``` 150 | 151 | 152 | ## Data Volumes 153 | 154 | The following directories are setup as volumes and can be accessed from other containers. 155 | 156 | * /etc/postgresql 157 | * /var/lib/postgresql 158 | * /var/log/postgresql 159 | 160 | Example of connecting the volumes to a container. 161 | 162 | 163 | ```shell 164 | james@ubuntu:~$ docker run --volumes-from postgres -i -t ubuntu bash 165 | root@6c3e9e61530f:/# mount |grep postgresql 166 | /dev/disk/by-uuid/cb08824e-c579-4fbc-8fea-668fafa212cc on /etc/postgresql type ext4 (rw,relatime,errors=remount-ro,data=ordered) 167 | /dev/disk/by-uuid/cb08824e-c579-4fbc-8fea-668fafa212cc on /var/lib/postgresql type ext4 (rw,relatime,errors=remount-ro,data=ordered) 168 | /dev/disk/by-uuid/cb08824e-c579-4fbc-8fea-668fafa212cc on /var/log/postgresql type ext4 (rw,relatime,errors=remount-ro,data=ordered) 169 | ``` 170 | 171 | 172 | 173 | ## Environment Variables 174 | 175 | This is a list of the available environment variables which can be set at runtime using -e KEY=value. 176 | For example, to change the default password you can issue `docker run -P --name postgresql -e PASSWORD=mysecretpassword jamesbrink/postgresql` 177 | 178 | * `USER`: A superuser role. default: `postgres` 179 | * `PASSWORD`: The password for the user. default: `postgres` 180 | * `DATABASE`: Name of the database to create. default: `postgres` 181 | * `SCHEMA`: Name of the schema to create. default: `public` 182 | * `ENCODING`: Encoding of the schema we are about to create. default: `SQL_ASCII` 183 | * `LOCALE`: locale setting. default: `en_US.UTF-8` 184 | * `POSTGIS`: Enable PostGIS extensions on the schema. 185 | 186 | ## Backups 187 | 188 | Be sure to run regular backups of any production databases. This can be handled in many different ways and I will not go into details here about how you should handle your backups. For additional information on backing up databases refer to the [PostgreSQL 9.6 Documentation on Backups](http://www.postgresql.org/docs/9.6/static/backup.html) 189 | --------------------------------------------------------------------------------