Description
If we create a Dockerfile
FROM postgres:12.4-alpine
ADD /SQL/*.sql /docker-entrypoint-initdb.d/
and then use it in docker-compose-postgres-pgadmin4.yml
:
version: '3.5'
services:
postgres:
container_name: postgres
build: .
...
...
then it will automatically run those sql files for data setup.
What do you think?
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, run any executable *.sh scripts, and source any non-executable *.sh scripts found in that directory to do further initialization before starting the service.
Warning: 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. One common problem is that if one of your /docker-entrypoint-initdb.d scripts fails (which will cause the entrypoint script to exit) and your orchestrator restarts the container with the already initialized data directory, it will not continue on with your scripts.