|
1 | 1 | # Using multiple databases with the official PostgreSQL Docker image |
2 | 2 |
|
3 | | -The [official recommendation](https://hub.docker.com/_/postgres/) for creating |
4 | | -multiple databases is as follows: |
| 3 | +A PostgreSQL docker build that allows the creation of multiple databases in one container |
5 | 4 |
|
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. |
| 5 | +This build uses a script to create additional databases and grants all privileges to the databases to the admin user. |
15 | 6 |
|
16 | 7 | ## Usage |
17 | 8 |
|
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 |
| 9 | + postgres: |
| 10 | + restart: always |
| 11 | + image: irobbierobinson/multi-db-postgres:latest |
27 | 12 | 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 |
| 13 | + - ./pg-db-data:/var/lib/postgresql/data |
47 | 14 | environment: |
48 | | - - POSTGRES_MULTIPLE_DATABASES=db1,db2 |
49 | | - - POSTGRES_USER=myapp |
50 | | - - POSTGRES_PASSWORD= |
| 15 | + - POSTGRES_PASSWORD=postgres |
| 16 | + - POSTGRES_USER=postgres |
| 17 | + - POSTGRES_DB=postgres |
| 18 | + - POSTGRES_ADDITIONAL_DATABASES=db,keycloak |
| 19 | + ports: |
| 20 | + - 5432:5432 |
| 21 | + networks: |
| 22 | + - backend |
| 23 | + |
| 24 | +## Environment Variables |
| 25 | + |
| 26 | +`POSTGRES_PASSWORD` - Admin Password (default: postgres) |
| 27 | +`POSTGRES_USER` - Admin Username (default: postgres) |
| 28 | +`POSTGRES_DB` - Default Postgresql DB Name (default: postgres) |
| 29 | +`POSTGRES_ADDITIONAL_DATABASES` - Additional databases that will be created alongside the default (separated by commas). |
51 | 30 |
|
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" |
0 commit comments