Search code examples
postgresqldockerdocker-compose

Multiple databases in docker and docker-compose


I have a project consisting of two main Java apps that use eight Postgres databases, so is there a way in docker-compose to build eight different databases so that each one has a different owner and password? Can I even do that in docker-compose?

Example:

services:
    postgresql:
        build: db/.
        ports:
            - "5432:5432"
        environment:
          - POSTGRES_DB=database1
          - POSTGRES_USER=database1
          - POSTGRES_PASSWORD=database1

I know I can put all the .sql files in the docker-entrypoint-initdb.d and Postgres will make them automatically, but how do I declare what .sql file goes in what database?


Solution

  • According to this Github issue might be possible to achieve multiple databases by using bash scripts which you will have to pass in your Dockerfile

    EDIT:

    To create multiple Databases you could use the following script:

    https://github.com/mrts/docker-postgresql-multiple-databases

    or

    https://github.com/MartinKaburu/docker-postgresql-multiple-databases

    Which suggest that you have to clone one of the above git repos and mount it as a volume to: /docker-entrypoint-initdb.d then you would be able to pass multiple database names by using: POSTGRES_MULTIPLE_DATABASES variable