The official recommendation for creating multiple databases is as follows:
If you would like to do additional initialization in an image derived from
this one, add one or more `.sql,.sql.gz, or.shscripts under/docker-entrypoint-initdb.d(creating the directory if necessary). After the entrypoint callsinitdbto create the defaultpostgresuser and database, it will run any.sqlfiles and source any.sh` scripts found in that
directory to do further initialization before starting the service.*
This directory contains a script to create multiple databases using that mechanism.
Open docker-compose.yml file and edit the POSTGRES_MULTIPLE_DATABASES environment variable with a list of db names you'd like to be created in the postgres container. The db names should be comma(,) separated.
Save the file and simpy run:
docker-compose up
to get the container up and running. Your databases would automatically be created and will be exposed by default on port 5432.
Thanks to @mrts for the inspiration and original script.