├── Dockerfile ├── LICENSE ├── README.md └── create-multiple-postgresql-databases.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM postgres:9.6 2 | COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/ 3 | -------------------------------------------------------------------------------- /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,db2 31 | - POSTGRES_USER: myapp 32 | - POSTGRES_PASSWORD: 33 | 34 | ### By building a custom image 35 | 36 | Clone the repository, build and push the image to your Docker repository, 37 | for example for Google Private Repository do the following: 38 | 39 | docker build --tag=eu.gcr.io/your-project/postgres-multi-db . 40 | gcloud docker -- push eu.gcr.io/your-project/postgres-multi-db 41 | 42 | You still need to pass the `POSTGRES_MULTIPLE_DATABASES` environment variable 43 | to the container: 44 | 45 | myapp-postgresql: 46 | image: eu.gcr.io/your-project/postgres-multi-db 47 | environment: 48 | - POSTGRES_MULTIPLE_DATABASES: db1,db2 49 | - POSTGRES_USER: myapp 50 | - POSTGRES_PASSWORD: 51 | 52 | ### Non-standard database names 53 | 54 | If you need to use non-standard database names (hyphens, uppercase letters etc), quote them in `POSTGRES_MULTIPLE_DATABASES`: 55 | 56 | environment: 57 | - POSTGRES_MULTIPLE_DATABASES: "test-db-1","test-db-2" 58 | -------------------------------------------------------------------------------- /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=$1 8 | echo " Creating user and database '$database'" 9 | psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL 10 | CREATE USER $database; 11 | CREATE DATABASE $database; 12 | GRANT ALL PRIVILEGES ON DATABASE $database TO $database; 13 | EOSQL 14 | } 15 | 16 | if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then 17 | echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" 18 | for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do 19 | create_user_and_database $db 20 | done 21 | echo "Multiple databases created" 22 | fi 23 | --------------------------------------------------------------------------------