├── docker-compose.yml ├── init └── create-multiple-postgresql-databases.sh └── README.md /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | networks: 3 | oprativ-network: 4 | driver: bridge 5 | services: 6 | {{ SERVICE_NAME }}-db: 7 | image: postgres:latest 8 | container_name: {{ CONTAINER_NAME }} 9 | ports: 10 | - 5432:5432 11 | volumes: 12 | - {{ VOLUME_NAME }}-data:/var/lib/postgresql/data 13 | - ./init/create-multiple-postgresql-databases.sh:/docker-entrypoint-initdb.d/create-multiple-postgresql-databases.sh 14 | environment: 15 | POSTGRES_MULTIPLE_DATABASES: "{{ DATABASE_1 }}, {{ DATABASE_2 }}, {{ DATABASE_3 }}" 16 | POSTGRES_PASSWORD: {{ PASSWORD }} 17 | POSTGRES_USER: "{{ DB_USER }}" 18 | networks: 19 | - {{ NETWORK_NAME }} 20 | volumes: 21 | {{ VOLUME_NAME }}-data: 22 | 23 | -------------------------------------------------------------------------------- /init/create-multiple-postgresql-databases.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | set -u 5 | 6 | function create_database_grant_privilege() { 7 | local database=$1 8 | echo " Creating database '$database' and granting privileges to '$POSTGRES_USER'" 9 | psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL 10 | CREATE DATABASE $database; 11 | GRANT ALL PRIVILEGES ON DATABASE $database TO $POSTGRES_USER; 12 | EOSQL 13 | } 14 | 15 | if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then 16 | echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" 17 | for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do 18 | create_database_grant_privilege $db 19 | done 20 | echo "Multiple databases created" 21 | fi 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Using multiple databases with the official PostgreSQL Docker image 2 | 3 | This is the implementation of the [official recommendation](https://hub.docker.com/_/postgres/) for creating multiple databases. 4 | 5 | This repository contains a docker compose file with placeholders in mustache implementation with bash script to create multiple databases with minor improvement for granting privileges to related database user using that mechanism. 6 | 7 | ## Usage 8 | 9 | ### By mounting a volume 10 | 11 | Clone the repository, replace the placeholder values in the `docker-compose.yml` file. 12 | 13 | ### By building a custom image 14 | 15 | Clone the repository, replace the placeholder values with current, build and push the image to your Docker repository, 16 | for example for Google Private Repository do the following: 17 | 18 | docker build --tag=eu.gcr.io// . 19 | gcloud docker -- push eu.gcr.io// 20 | 21 | ### Non-standard database names 22 | 23 | If you need to use non-standard database names (hyphens, uppercase letters etc), quote them on this way in docker compose file: 24 | `- POSTGRES_MULTIPLE_DATABASES: "non-standard-db-name-1","non-standard-db-name-2"` 25 | --------------------------------------------------------------------------------