Scripts in the /docker-entrypoint-initdb.d folder are ignored

Scripts in the /docker-entrypoint-initdb.d folder are ignored

Problem Description:

I need to configure Postogres with some SQL commands, but everything I put in the /docker-entrypoint-initdb.d folder doesn’t get executed.
I’m using the postgres:9.6 image.

My Dockerfile is as follows:

FROM postgres:9.6

COPY init.sql /docker-entrypoint-initdb.d/
USER root
RUN chown postgres:postgres /docker-entrypoint-initdb.d/init.sql
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["postgres"]

I tried multiple commands in init.sql, for example:

CREATE DATABASE db_name;

Finally, this is the part of the yaml file that concerns the database.

db:
    image: postgres-am
    ports:
      - target: 5432
        published: 5432
        protocol: tcp
        mode: host

    environment:
      POSTGRES_PASSWORD: "postgres"
      PGDATA: "/var/lib/postgresql/data/pgdata"

    volumes:
      - db_data:/var/lib/postgresql/data

Solution – 1

Postgres only initializes the database if no database is found, when the container starts. Since you have a volume mapping on the database directory, chances are that a database already exists.

If you delete the db_data volume and start the container, postgres will see that there isn’t a database and then it’ll initialize one for you using the scripts in docker-entrypoint-initdb.d.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject