├── Dockerfile ├── LICENSE ├── README.md └── create-multiple-postgresql-databases.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM library/postgres:9.6-alpine 2 | 3 | COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/ 4 | 5 | RUN chmod +x /docker-entrypoint-initdb.d/create-multiple-postgresql-databases.sh 6 | 7 | EXPOSE 5432 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Mart Sõmermaa 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Using multiple databases with the official PostgreSQL Docker image 2 | 3 | The [official recommendation](https://hub.docker.com/_/postgres/) for creating 4 | multiple databases is as follows: 5 | 6 | *If you would like to do additional initialization in an image derived from 7 | this one, add one or more `*.sql`, `*.sql.gz`, or `*.sh` scripts under 8 | `/docker-entrypoint-initdb.d` (creating the directory if necessary). After the 9 | entrypoint calls `initdb` to create the default `postgres` user and database, 10 | it will run any `*.sql` files and source any `*.sh` scripts found in that 11 | directory to do further initialization before starting the service.* 12 | 13 | This directory contains a script to create multiple databases using that 14 | mechanism. 15 | 16 | ## Usage 17 | 18 | ### By mounting a volume 19 | 20 | Clone the repository, mount its directory as a volume into 21 | `/docker-entrypoint-initdb.d` and declare database names separated by commas in 22 | `POSTGRES_MULTIPLE_DATABASES` environment variable as follows 23 | (`docker-compose` syntax): 24 | 25 | myapp-postgresql: 26 | image: postgres:9.6.2 27 | volumes: 28 | - ../docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d 29 | environment: 30 | - POSTGRES_MULTIPLE_DATABASES="DB1,ownerOfDB1: DB2,ownerOfDB2: ...DB(n), ownerOfDB(n)" 31 | - POSTGRES_PASSWORD= 32 | 33 | ### By building a custom image 34 | 35 | Clone the repository, build and push the image to your Docker repository, 36 | for example for Google Private Repository do the following: 37 | 38 | docker build --tag=eu.gcr.io/your-project/postgres-multi-db . 39 | gcloud docker -- push eu.gcr.io/your-project/postgres-multi-db 40 | 41 | You still need to pass the `POSTGRES_MULTIPLE_DATABASES` environment variable 42 | to the container: 43 | 44 | myapp-postgresql: 45 | image: eu.gcr.io/your-project/postgres-multi-db 46 | environment: 47 | - POSTGRES_MULTIPLE_DATABASES="DB1,ownerOfDB1: DB2,ownerOfDB2: ...DB(n), ownerOfDB(n)" 48 | - POSTGRES_PASSWORD= 49 | 50 | ### Non-standard database names 51 | 52 | If you need to use non-standard database names (hyphens, uppercase letters etc), quote them in `POSTGRES_MULTIPLE_DATABASES`: 53 | 54 | environment: 55 | - POSTGRES_MULTIPLE_DATABASES="DB1","ownerOfDB1": "DB2","ownerOfDB2": ..."DB(n)", "ownerOfDB(n)" 56 | -------------------------------------------------------------------------------- /create-multiple-postgresql-databases.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | set -u 5 | 6 | function create_user_and_database() { 7 | local database=$(echo $1 | tr ',' ' ' | awk '{print $1}') 8 | local owner=$(echo $1 | tr ',' ' ' | awk '{print $2}') 9 | echo " Creating user and database '$database'" 10 | psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL 11 | CREATE USER $owner IF NOT EXISTS; 12 | CREATE DATABASE $database; 13 | GRANT ALL PRIVILEGES ON DATABASE $database TO $owner; 14 | EOSQL 15 | } 16 | 17 | if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then 18 | echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" 19 | for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ':' ' '); do 20 | create_user_and_database $db 21 | done 22 | echo "Multiple databases created" 23 | fi 24 | --------------------------------------------------------------------------------