Run additional script when Postgres container starts

Today, I've just explored a feature which allows you to set up initial data like CREATE ROLE or GRANT <PRIVILEGE> when you start a postgres instance on Docker for the first time.

You may learn more details here

TL;DR:

We create *.sql or *.sh script to run SQL statement or bash script to prepare initial data. Postgres container will run any *.sql and *.sh found in the directory (which containing docker-compose.yml) to do further initialization before starting the service.

NOTE: scripts in /docker-entrypoint-initdb.d are only run if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup.

And visit my example code at my gist