Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 943
Luigi Avatar asked Mar 06 '26 10:03

Luigi


2 Answers

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.

like image 188
Hans Kilian Avatar answered Mar 09 '26 06:03

Hans Kilian


The accepted answer was correct (when it was written)

There was a subsequent discussion in github with the maintainer of the postgres docker image about supporting a mechanism similar to the mysql /always-init.d/ etc.

The link to that discussion: https://github.com/docker-library/postgres/pull/496

The solution with docker is to provide a custom entry-point, there is a bit of a bug on version shared in issue 496 so I'm posting a more updated version here in hopes others will find it useful:

#!/usr/bin/env bash
## copied from: https://github.com/docker-library/postgres/pull/496#issue-358838955
set -Eeo pipefail

echo "🐘 custom-entry-point"
# Example using the functions of the postgres entrypoint to customize startup to always run files in /always-initdb.d/

source "$(which docker-entrypoint.sh)"

docker_setup_env
docker_create_db_directories
# assumption: we are already running as the owner of PGDATA

# This is needed if the container is started as `root`
#if [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then
if [ "$(id -u)" = '0' ]; then
  exec gosu postgres "$BASH_SOURCE" "$@"
fi


if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
  echo "🐘 db is missing"
  docker_verify_minimum_env
  docker_init_database_dir
  pg_setup_hba_conf

  # only required for '--auth[-local]=md5' on POSTGRES_INITDB_ARGS
  export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"

  docker_temp_server_start "$@" -c max_locks_per_transaction=256
  docker_setup_db
  docker_process_init_files /docker-entrypoint-initdb.d/*
  docker_temp_server_stop
else
  echo "🐘 db already exists"
  docker_temp_server_start "$@"
  docker_process_init_files /always-initdb.d/*
  docker_temp_server_stop
fi

echo "🐘 .. starting!"
exec postgres "$@"
like image 40
Brian Horakh Avatar answered Mar 09 '26 06:03

Brian Horakh