92

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?

1
  • 2
    Scale up or scale out. Another option is to have multiple postgres containers. Fits my one db per app architecture and perhaps less your 2 apps over 8 dbs. But for others, it is worth considering. Then you don't have to think about any janky workarounds, and have more independant dbs that you can bring up and down easier. Though it is much more resource demanding.
    – flurdy
    Commented Apr 10, 2020 at 18:53

4 Answers 4

64

Usually when I need more than one database in a docker project it's a test database. I find it easier to simply spin up a second docker container, without worrying about scripts or volume separation. The main trick is to not conflict the default ports (e.g. 5432 for postgres) and you're good to go. Then docker-compose can be something as simple as this:

version: '3.0'

services: 

  db:
    image: postgres
    environment: 
      - POSTGRES_DB
      - POSTGRES_USER
      - POSTGRES_PASSWORD
    ports:
      - ${POSTGRES_DEV_PORT}:5432
    volumes:
      - app-volume:/var/lib/postgresql/data

  db-test:
    image: postgres
    environment: 
      - POSTGRES_DB
      - POSTGRES_USER
      - POSTGRES_PASSWORD
    ports:
      - ${POSTGRES_TEST_PORT}:5432
    # Notice I don't even use a volume here since I don't care to persist test data between runs

volumes:
  app-volume: #

Caveat: Obviously, more containers will typically imply in a higher memory footprint

7
  • 2
    I actually use this when running my CI pipline in jenkins...I just change the port for the postgres container....and off it goes... Commented Jul 18, 2020 at 16:30
  • 1
    Thanks for this. I had spent hours trying to bend the creation of a new database and running the migrations into one container. Commented Feb 9, 2021 at 10:41
  • 3
    You could as well just go with a different network instead of different ports - depending on your use case.
    – kaiser
    Commented Jun 2, 2021 at 9:10
  • 1
    Can the volumes point to the same location? Commented Apr 11, 2022 at 17:27
  • 1
    ..and each additional container will eat additional memory =( Commented Jun 4, 2022 at 21:47
52

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

3
  • 1
    This does not address the last question: how to selectively fil the created DBs?
    – achimh
    Commented May 28, 2019 at 14:08
  • 2
    The question of selective fill is still not answered. From what I saw in the documentation, you could maybe add a \c <DB> to each sql file you load to select the DB. Feels hacky still. BTW: from the two repos you mentioned one is a fork of the other and the other is already mentioned in the other answer.
    – achimh
    Commented Sep 15, 2019 at 8:54
  • Incomplete, ensure the script has the correct permissions: chmod +x ./<path-to-sh-file>/the-multiple-database-file.sh
    – Lunatic
    Commented Aug 6 at 9:03
12

Well - take a look at this Github project: https://github.com/mrts/docker-postgresql-multiple-databases

According to official postgres docker image documentation:

If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files and source any *.sh scripts found in that directory to do further initialization before starting the service.

You will find the prepared script on that repo which you could use.

3
  • 1
    This is actually saying the same as the accepted answer - I don't understand the down votes Commented May 4, 2018 at 14:19
  • 4
    @JavierArias: I think that is because it forces users to read same info twice and spent additional time for nothing Commented Jun 4, 2022 at 21:51
  • Incomplete, ensure the script has the correct permissions: chmod +x ./<path-to-sh-file>/the-multiple-database-file.sh
    – Lunatic
    Commented Aug 6 at 9:03
1

Inspired by the accepted answer, but not satisfied with the fact that the db user is hardcoded, I've created a fully functional solution (docker-compose.yml file with related bash script) to solve that issue.

Same mechanism is used -> add the database name values to the POSTGRES_MULTIPLE_DATABASES variable and build or mount, but this time the related user will be responsible for these databases.

You can use the docker-compose file, replace the placeholders with the current values and everything is runnable. Everything is explained in the README file.

Here is the github repository.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.