Docker images for postgres extended with management bash scripts.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: first commit

Marcos Gabarda 89ffea73

+382
+17
.editorconfig
··· 1 + # editorconfig.org 2 + root = true 3 + 4 + [*] 5 + indent_style = space 6 + indent_size = 2 7 + end_of_line = lf 8 + charset = utf-8 9 + trim_trailing_whitespace = true 10 + insert_final_newline = true 11 + 12 + [*.md] 13 + trim_trailing_whitespace = false 14 + 15 + [Makefile] 16 + indent_style = tab 17 + indent_size = 4
+11
.gitignore
··· 1 + .DS_Store 2 + 3 + # Editor directories and files 4 + .idea 5 + .vscode 6 + *.suo 7 + *.ntvs* 8 + *.njsproj 9 + *.sln 10 + *.sw* 11 +
+36
Makefile
··· 1 + # Variables 2 + # ------------------------------------------------------------------------------ 3 + POSTGRES_IMAGE_ID=marcosgabarda/postgres 4 + POSTGIS_IMAGE_ID=marcosgabarda/postgis 5 + 6 + .PHONY: build_postgres push_postgres build_postgis push_postgis 7 + 8 + build_postgres: 9 + docker buildx build . -f ./postgres/10/Dockerfile --tag ${POSTGRES_IMAGE_ID}:10 --platform linux/amd64,linux/arm64 10 + docker buildx build . -f ./postgres/11/Dockerfile --tag ${POSTGRES_IMAGE_ID}:11 --platform linux/amd64,linux/arm64 11 + docker buildx build . -f ./postgres/12/Dockerfile --tag ${POSTGRES_IMAGE_ID}:12 --platform linux/amd64,linux/arm64 12 + docker buildx build . -f ./postgres/13/Dockerfile --tag ${POSTGRES_IMAGE_ID}:13 --platform linux/amd64,linux/arm64 13 + docker buildx build . -f ./postgres/14/Dockerfile --tag ${POSTGRES_IMAGE_ID}:14 --tag ${POSTGRES_IMAGE_ID}:latest --platform linux/amd64,linux/arm64 14 + 15 + push_postgres: 16 + docker buildx build . -f ./postgres/10/Dockerfile --tag ${POSTGRES_IMAGE_ID}:10 --platform linux/amd64,linux/arm64 --push 17 + docker buildx build . -f ./postgres/11/Dockerfile --tag ${POSTGRES_IMAGE_ID}:11 --platform linux/amd64,linux/arm64 --push 18 + docker buildx build . -f ./postgres/12/Dockerfile --tag ${POSTGRES_IMAGE_ID}:12 --platform linux/amd64,linux/arm64 --push 19 + docker buildx build . -f ./postgres/13/Dockerfile --tag ${POSTGRES_IMAGE_ID}:13 --platform linux/amd64,linux/arm64 --push 20 + docker buildx build . -f ./postgres/14/Dockerfile --tag ${POSTGRES_IMAGE_ID}:14 --tag ${POSTGRES_IMAGE_ID}:latest --platform linux/amd64,linux/arm64 --push 21 + 22 + build_postgis: 23 + docker buildx build . -f ./postgis/11/Dockerfile --tag ${POSTGIS_IMAGE_ID}:11-2.5 --tag ${POSTGIS_IMAGE_ID}:11 --platform linux/amd64,linux/arm64 24 + docker buildx build . -f ./postgis/12/Dockerfile --tag ${POSTGIS_IMAGE_ID}:12-3.0 --tag ${POSTGIS_IMAGE_ID}:12 --platform linux/amd64,linux/arm64 25 + docker buildx build . -f ./postgis/13/Dockerfile --tag ${POSTGIS_IMAGE_ID}:13-3.1 --tag ${POSTGIS_IMAGE_ID}:13 --platform linux/amd64,linux/arm64 26 + docker buildx build . -f ./postgis/14/Dockerfile --tag ${POSTGIS_IMAGE_ID}:14-3.4 --tag ${POSTGIS_IMAGE_ID}:13 --tag ${POSTGIS_IMAGE_ID}:latest --platform linux/amd64,linux/arm64 27 + 28 + push_postgis: 29 + docker buildx build . -f ./postgis/11/Dockerfile --tag ${POSTGIS_IMAGE_ID}:11-2.5 --tag ${POSTGIS_IMAGE_ID}:11 --platform linux/amd64,linux/arm64 --push 30 + docker buildx build . -f ./postgis/12/Dockerfile --tag ${POSTGIS_IMAGE_ID}:12-3.0 --tag ${POSTGIS_IMAGE_ID}:12 --platform linux/amd64,linux/arm64 --push 31 + docker buildx build . -f ./postgis/13/Dockerfile --tag ${POSTGIS_IMAGE_ID}:13-3.1 --tag ${POSTGIS_IMAGE_ID}:13 --platform linux/amd64,linux/arm64 --push 32 + docker buildx build . -f ./postgis/14/Dockerfile --tag ${POSTGIS_IMAGE_ID}:14-3.4 --tag ${POSTGIS_IMAGE_ID}:14 --tag ${POSTGIS_IMAGE_ID}:latest --platform linux/amd64,linux/arm64 --push 33 + 34 + build: build_postgres build_postgis 35 + 36 + push: push_postgres push_postgis
+34
README.md
··· 1 + # Extended PostgreSQL Docker Image 2 + 3 + Docker image for `postgres` extended with management bash scripts. 4 + 5 + ## Build images 6 + 7 + To build the images and push them to the registry: 8 + 9 + make build 10 + make push 11 + 12 + ## Docker-compose example 13 + 14 + Example of a `docker-compose.yml` file that uses this image: 15 + 16 + version: '3' 17 + 18 + volumes: 19 + postgres_data: {} 20 + postgres_backups: {} 21 + 22 + services: 23 + 24 + service: 25 + image: service:latest 26 + command: ./start 27 + 28 + postgres: 29 + image: marcosgabarda/postgres:latest 30 + volumes: 31 + - postgres_data:/var/lib/postgresql/data 32 + - postgres_backups:/backups 33 + env_file: 34 + - ./.env
+5
maintenance/_sourced/constants.sh
··· 1 + #!/usr/bin/env bash 2 + 3 + 4 + BACKUP_DIR_PATH='/backups' 5 + BACKUP_FILE_PREFIX='backup'
+12
maintenance/_sourced/countdown.sh
··· 1 + #!/usr/bin/env bash 2 + 3 + 4 + countdown() { 5 + declare desc="A simple countdown. Source: https://superuser.com/a/611582" 6 + local seconds="${1}" 7 + local d=$(($(date +%s) + "${seconds}")) 8 + while [ "$d" -ge `date +%s` ]; do 9 + echo -ne "$(date -u --date @$(($d - `date +%s`)) +%H:%M:%S)\r"; 10 + sleep 0.1 11 + done 12 + }
+41
maintenance/_sourced/messages.sh
··· 1 + #!/usr/bin/env bash 2 + 3 + 4 + message_newline() { 5 + echo 6 + } 7 + 8 + message_debug() 9 + { 10 + echo -e "DEBUG: ${@}" 11 + } 12 + 13 + message_welcome() 14 + { 15 + echo -e "\e[1m${@}\e[0m" 16 + } 17 + 18 + message_warning() 19 + { 20 + echo -e "\e[33mWARNING\e[0m: ${@}" 21 + } 22 + 23 + message_error() 24 + { 25 + echo -e "\e[31mERROR\e[0m: ${@}" 26 + } 27 + 28 + message_info() 29 + { 30 + echo -e "\e[37mINFO\e[0m: ${@}" 31 + } 32 + 33 + message_suggestion() 34 + { 35 + echo -e "\e[33mSUGGESTION\e[0m: ${@}" 36 + } 37 + 38 + message_success() 39 + { 40 + echo -e "\e[32mSUCCESS\e[0m: ${@}" 41 + }
+16
maintenance/_sourced/yes_no.sh
··· 1 + #!/usr/bin/env bash 2 + 3 + 4 + yes_no() { 5 + declare desc="Prompt for confirmation. \$\"\{1\}\": confirmation message." 6 + local arg1="${1}" 7 + 8 + local response= 9 + read -r -p "${arg1} (y/[n])? " response 10 + if [[ "${response}" =~ ^[Yy]$ ]] 11 + then 12 + exit 0 13 + else 14 + exit 1 15 + fi 16 + }
+38
maintenance/backup
··· 1 + #!/usr/bin/env bash 2 + 3 + 4 + ### Create a database backup. 5 + ### 6 + ### Usage: 7 + ### $ docker-compose -f <environment>.yml (exec |run --rm) postgres backup 8 + 9 + 10 + set -o errexit 11 + set -o pipefail 12 + set -o nounset 13 + 14 + 15 + working_dir="$(dirname ${0})" 16 + source "${working_dir}/_sourced/constants.sh" 17 + source "${working_dir}/_sourced/messages.sh" 18 + 19 + 20 + message_welcome "Backing up the '${POSTGRES_DB}' database..." 21 + 22 + 23 + if [[ "${POSTGRES_USER}" == "postgres" ]]; then 24 + message_error "Backing up as 'postgres' user is not supported. Assign 'POSTGRES_USER' env with another one and try again." 25 + exit 1 26 + fi 27 + 28 + export PGHOST="${POSTGRES_HOST}" 29 + export PGPORT="${POSTGRES_PORT}" 30 + export PGUSER="${POSTGRES_USER}" 31 + export PGPASSWORD="${POSTGRES_PASSWORD}" 32 + export PGDATABASE="${POSTGRES_DB}" 33 + 34 + backup_filename="${BACKUP_FILE_PREFIX}_$(date +'%Y_%m_%dT%H_%M_%S').sql.gz" 35 + pg_dump | gzip > "${BACKUP_DIR_PATH}/${backup_filename}" 36 + 37 + 38 + message_success "'${POSTGRES_DB}' database backup '${backup_filename}' has been created and placed in '${BACKUP_DIR_PATH}'."
+22
maintenance/backups
··· 1 + #!/usr/bin/env bash 2 + 3 + 4 + ### View backups. 5 + ### 6 + ### Usage: 7 + ### $ docker-compose -f <environment>.yml (exec |run --rm) postgres backups 8 + 9 + 10 + set -o errexit 11 + set -o pipefail 12 + set -o nounset 13 + 14 + 15 + working_dir="$(dirname ${0})" 16 + source "${working_dir}/_sourced/constants.sh" 17 + source "${working_dir}/_sourced/messages.sh" 18 + 19 + 20 + message_welcome "These are the backups you have got:" 21 + 22 + ls -lht "${BACKUP_DIR_PATH}"
+41
maintenance/createreaduser
··· 1 + #!/usr/bin/env bash 2 + 3 + 4 + ### Creates a read only user. 5 + ### 6 + ### Usage: 7 + ### $ docker-compose -f <environment>.yml (exec |run --rm) postgres createreaduser 8 + 9 + 10 + set -o errexit 11 + set -o nounset 12 + 13 + working_dir="$(dirname ${0})" 14 + source "${working_dir}/_sourced/constants.sh" 15 + source "${working_dir}/_sourced/messages.sh" 16 + 17 + if [ -z "$POSTGRES_READ_ONLY_USER" ] || [ -z "$POSTGRES_READ_ONLY_PASSWORD" ] ; then 18 + message_error "The environment variables POSTGRES_READ_ONLY_USER and POSTGRES_READ_ONLY_PASSWORD should be configured." 19 + exit 1 20 + fi 21 + 22 + export PGHOST="${POSTGRES_HOST}" 23 + export PGPORT="${POSTGRES_PORT}" 24 + export PGUSER="${POSTGRES_USER}" 25 + export PGPASSWORD="${POSTGRES_PASSWORD}" 26 + export PGDATABASE="${POSTGRES_DB}" 27 + 28 + message_info "Creating read only user for '${POSTGRES_DB}' database..." 29 + 30 + psql -tc "SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = '${POSTGRES_READ_ONLY_USER}'" | grep -q 1 || psql -c "CREATE ROLE \"${POSTGRES_READ_ONLY_USER}\" WITH LOGIN PASSWORD '${POSTGRES_READ_ONLY_PASSWORD}' NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION VALID UNTIL 'infinity';" 31 + 32 + message_info "Grant permission to '${POSTGRES_READ_ONLY_USER}' user..." 33 + 34 + psql -v ON_ERROR_STOP=1 <<-EOSQL 35 + GRANT CONNECT ON DATABASE ${POSTGRES_DB} TO "${POSTGRES_READ_ONLY_USER}"; 36 + GRANT USAGE ON SCHEMA public TO "${POSTGRES_READ_ONLY_USER}"; 37 + GRANT SELECT ON ALL TABLES IN SCHEMA public TO "${POSTGRES_READ_ONLY_USER}"; 38 + GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO "${POSTGRES_READ_ONLY_USER}"; 39 + EOSQL 40 + 41 + message_success "The user '${POSTGRES_READ_ONLY_USER}' has been created with read-only permissions."
+55
maintenance/restore
··· 1 + #!/usr/bin/env bash 2 + 3 + 4 + ### Restore database from a backup. 5 + ### 6 + ### Parameters: 7 + ### <1> filename of an existing backup. 8 + ### 9 + ### Usage: 10 + ### $ docker-compose -f <environment>.yml (exec |run --rm) postgres restore <1> 11 + 12 + 13 + set -o errexit 14 + set -o pipefail 15 + set -o nounset 16 + 17 + 18 + working_dir="$(dirname ${0})" 19 + source "${working_dir}/_sourced/constants.sh" 20 + source "${working_dir}/_sourced/messages.sh" 21 + 22 + 23 + if [[ -z ${1+x} ]]; then 24 + message_error "Backup filename is not specified yet it is a required parameter. Make sure you provide one and try again." 25 + exit 1 26 + fi 27 + backup_filename="${BACKUP_DIR_PATH}/${1}" 28 + if [[ ! -f "${backup_filename}" ]]; then 29 + message_error "No backup with the specified filename found. Check out the 'backups' maintenance script output to see if there is one and try again." 30 + exit 1 31 + fi 32 + 33 + message_welcome "Restoring the '${POSTGRES_DB}' database from the '${backup_filename}' backup..." 34 + 35 + if [[ "${POSTGRES_USER}" == "postgres" ]]; then 36 + message_error "Restoring as 'postgres' user is not supported. Assign 'POSTGRES_USER' env with another one and try again." 37 + exit 1 38 + fi 39 + 40 + export PGHOST="${POSTGRES_HOST}" 41 + export PGPORT="${POSTGRES_PORT}" 42 + export PGUSER="${POSTGRES_USER}" 43 + export PGPASSWORD="${POSTGRES_PASSWORD}" 44 + export PGDATABASE="${POSTGRES_DB}" 45 + 46 + message_info "Dropping the database..." 47 + dropdb "${PGDATABASE}" 48 + 49 + message_info "Creating a new database..." 50 + createdb --owner="${POSTGRES_USER}" 51 + 52 + message_info "Applying the backup to the new database..." 53 + gunzip -c "${backup_filename}" | psql "${POSTGRES_DB}" 54 + 55 + message_success "The '${POSTGRES_DB}' database has been restored from the '${backup_filename}' backup."
+6
postgis/11/Dockerfile
··· 1 + FROM postgis/postgis:11-2.5-alpine 2 + 3 + COPY ./maintenance /usr/local/bin/maintenance 4 + RUN chmod +x /usr/local/bin/maintenance/* 5 + RUN mv /usr/local/bin/maintenance/* /usr/local/bin \ 6 + && rmdir /usr/local/bin/maintenance
+6
postgis/12/Dockerfile
··· 1 + FROM postgis/postgis:12-3.0-alpine 2 + 3 + COPY ./maintenance /usr/local/bin/maintenance 4 + RUN chmod +x /usr/local/bin/maintenance/* 5 + RUN mv /usr/local/bin/maintenance/* /usr/local/bin \ 6 + && rmdir /usr/local/bin/maintenance
+6
postgis/13/Dockerfile
··· 1 + FROM postgis/postgis:13-3.1-alpine 2 + 3 + COPY ./maintenance /usr/local/bin/maintenance 4 + RUN chmod +x /usr/local/bin/maintenance/* 5 + RUN mv /usr/local/bin/maintenance/* /usr/local/bin \ 6 + && rmdir /usr/local/bin/maintenance
+6
postgis/14/Dockerfile
··· 1 + FROM postgis/postgis:14-3.4-alpine 2 + 3 + COPY ./maintenance /usr/local/bin/maintenance 4 + RUN chmod +x /usr/local/bin/maintenance/* 5 + RUN mv /usr/local/bin/maintenance/* /usr/local/bin \ 6 + && rmdir /usr/local/bin/maintenance
+6
postgres/10/Dockerfile
··· 1 + FROM postgres:10 2 + 3 + COPY ./maintenance /usr/local/bin/maintenance 4 + RUN chmod +x /usr/local/bin/maintenance/* 5 + RUN mv /usr/local/bin/maintenance/* /usr/local/bin \ 6 + && rmdir /usr/local/bin/maintenance
+6
postgres/11/Dockerfile
··· 1 + FROM postgres:11 2 + 3 + COPY ./maintenance /usr/local/bin/maintenance 4 + RUN chmod +x /usr/local/bin/maintenance/* 5 + RUN mv /usr/local/bin/maintenance/* /usr/local/bin \ 6 + && rmdir /usr/local/bin/maintenance
+6
postgres/12/Dockerfile
··· 1 + FROM postgres:12 2 + 3 + COPY ./maintenance /usr/local/bin/maintenance 4 + RUN chmod +x /usr/local/bin/maintenance/* 5 + RUN mv /usr/local/bin/maintenance/* /usr/local/bin \ 6 + && rmdir /usr/local/bin/maintenance
+6
postgres/13/Dockerfile
··· 1 + FROM postgres:13 2 + 3 + COPY ./maintenance /usr/local/bin/maintenance 4 + RUN chmod +x /usr/local/bin/maintenance/* 5 + RUN mv /usr/local/bin/maintenance/* /usr/local/bin \ 6 + && rmdir /usr/local/bin/maintenance
+6
postgres/14/Dockerfile
··· 1 + FROM postgres:14 2 + 3 + COPY ./maintenance /usr/local/bin/maintenance 4 + RUN chmod +x /usr/local/bin/maintenance/* 5 + RUN mv /usr/local/bin/maintenance/* /usr/local/bin \ 6 + && rmdir /usr/local/bin/maintenance