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

3

8

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
Dygall answered 6/12, 2022 at 15:34 Comment(2)
You don't have a volume mapping for the database? – Monteith
Yes there is. I fixed the yaml file. – Dygall
M
16

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.

Monteith answered 6/12, 2022 at 20:26 Comment(0)
A
1

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 "$@"
Alverta answered 23/1 at 5:55 Comment(0)
T
0

It works like this for me:

version : "3"
services :
  postgres:
    image: "postgres:9.6"
    container_name: postgres
    volumes:
      - ./conf/postgres/:/docker-entrypoint-initdb.d/

folder ./conf/postgres

-- start.sql

CREATE USER myuser SUPERUSER;
ALTER USER myuser  WITH ENCRYPTED PASSWORD 'mypassword';

Termination answered 27/7 at 18:17 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.