mirror of
https://github.com/docker-library/postgres.git
synced 2025-04-19 11:42:18 +03:00
Add new "docker-ensure-initdb.sh" script
This mimics the behavior of `docker-entrypoint.sh` before it starts the PostgreSQL server. It has three main goals/uses: 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution (no-op if database is already initialized) 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use (error if database is already initialized)
This commit is contained in:
parent
d8c33605bb
commit
c86568af4a
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -1,3 +1,4 @@
|
||||
/*/**/Dockerfile linguist-generated
|
||||
/*/**/docker-ensure-initdb.sh linguist-generated
|
||||
/*/**/docker-entrypoint.sh linguist-generated
|
||||
/Dockerfile*.template linguist-language=Dockerfile
|
||||
|
3
12/alpine3.18/Dockerfile
generated
3
12/alpine3.18/Dockerfile
generated
@ -169,7 +169,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
12/alpine3.18/docker-ensure-initdb.sh
generated
Executable file
71
12/alpine3.18/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec su-exec postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
12/alpine3.18/docker-entrypoint.sh
generated
1
12/alpine3.18/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
12/alpine3.19/Dockerfile
generated
3
12/alpine3.19/Dockerfile
generated
@ -169,7 +169,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
12/alpine3.19/docker-ensure-initdb.sh
generated
Executable file
71
12/alpine3.19/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec su-exec postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
12/alpine3.19/docker-entrypoint.sh
generated
1
12/alpine3.19/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
12/bookworm/Dockerfile
generated
3
12/bookworm/Dockerfile
generated
@ -184,7 +184,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
12/bookworm/docker-ensure-initdb.sh
generated
Executable file
71
12/bookworm/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec gosu postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
12/bookworm/docker-entrypoint.sh
generated
1
12/bookworm/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
12/bullseye/Dockerfile
generated
3
12/bullseye/Dockerfile
generated
@ -184,7 +184,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
12/bullseye/docker-ensure-initdb.sh
generated
Executable file
71
12/bullseye/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec gosu postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
12/bullseye/docker-entrypoint.sh
generated
1
12/bullseye/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
13/alpine3.18/Dockerfile
generated
3
13/alpine3.18/Dockerfile
generated
@ -169,7 +169,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
13/alpine3.18/docker-ensure-initdb.sh
generated
Executable file
71
13/alpine3.18/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec su-exec postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
13/alpine3.18/docker-entrypoint.sh
generated
1
13/alpine3.18/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
13/alpine3.19/Dockerfile
generated
3
13/alpine3.19/Dockerfile
generated
@ -169,7 +169,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
13/alpine3.19/docker-ensure-initdb.sh
generated
Executable file
71
13/alpine3.19/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec su-exec postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
13/alpine3.19/docker-entrypoint.sh
generated
1
13/alpine3.19/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
13/bookworm/Dockerfile
generated
3
13/bookworm/Dockerfile
generated
@ -186,7 +186,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
13/bookworm/docker-ensure-initdb.sh
generated
Executable file
71
13/bookworm/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec gosu postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
13/bookworm/docker-entrypoint.sh
generated
1
13/bookworm/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
13/bullseye/Dockerfile
generated
3
13/bullseye/Dockerfile
generated
@ -186,7 +186,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
13/bullseye/docker-ensure-initdb.sh
generated
Executable file
71
13/bullseye/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec gosu postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
13/bullseye/docker-entrypoint.sh
generated
1
13/bullseye/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
14/alpine3.18/Dockerfile
generated
3
14/alpine3.18/Dockerfile
generated
@ -172,7 +172,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
14/alpine3.18/docker-ensure-initdb.sh
generated
Executable file
71
14/alpine3.18/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec su-exec postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
14/alpine3.18/docker-entrypoint.sh
generated
1
14/alpine3.18/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
14/alpine3.19/Dockerfile
generated
3
14/alpine3.19/Dockerfile
generated
@ -172,7 +172,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
14/alpine3.19/docker-ensure-initdb.sh
generated
Executable file
71
14/alpine3.19/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec su-exec postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
14/alpine3.19/docker-entrypoint.sh
generated
1
14/alpine3.19/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
14/bookworm/Dockerfile
generated
3
14/bookworm/Dockerfile
generated
@ -184,7 +184,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
14/bookworm/docker-ensure-initdb.sh
generated
Executable file
71
14/bookworm/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec gosu postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
14/bookworm/docker-entrypoint.sh
generated
1
14/bookworm/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
14/bullseye/Dockerfile
generated
3
14/bullseye/Dockerfile
generated
@ -184,7 +184,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
14/bullseye/docker-ensure-initdb.sh
generated
Executable file
71
14/bullseye/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec gosu postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
14/bullseye/docker-entrypoint.sh
generated
1
14/bullseye/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
15/alpine3.18/Dockerfile
generated
3
15/alpine3.18/Dockerfile
generated
@ -175,7 +175,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
15/alpine3.18/docker-ensure-initdb.sh
generated
Executable file
71
15/alpine3.18/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec su-exec postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
15/alpine3.18/docker-entrypoint.sh
generated
1
15/alpine3.18/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
15/alpine3.19/Dockerfile
generated
3
15/alpine3.19/Dockerfile
generated
@ -175,7 +175,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
15/alpine3.19/docker-ensure-initdb.sh
generated
Executable file
71
15/alpine3.19/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec su-exec postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
15/alpine3.19/docker-entrypoint.sh
generated
1
15/alpine3.19/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
15/bookworm/Dockerfile
generated
3
15/bookworm/Dockerfile
generated
@ -184,7 +184,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
15/bookworm/docker-ensure-initdb.sh
generated
Executable file
71
15/bookworm/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec gosu postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
15/bookworm/docker-entrypoint.sh
generated
1
15/bookworm/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
15/bullseye/Dockerfile
generated
3
15/bullseye/Dockerfile
generated
@ -184,7 +184,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
15/bullseye/docker-ensure-initdb.sh
generated
Executable file
71
15/bullseye/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec gosu postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
15/bullseye/docker-entrypoint.sh
generated
1
15/bullseye/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
16/alpine3.18/Dockerfile
generated
3
16/alpine3.18/Dockerfile
generated
@ -174,7 +174,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
16/alpine3.18/docker-ensure-initdb.sh
generated
Executable file
71
16/alpine3.18/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec su-exec postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
16/alpine3.18/docker-entrypoint.sh
generated
1
16/alpine3.18/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
16/alpine3.19/Dockerfile
generated
3
16/alpine3.19/Dockerfile
generated
@ -174,7 +174,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
16/alpine3.19/docker-ensure-initdb.sh
generated
Executable file
71
16/alpine3.19/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec su-exec postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
16/alpine3.19/docker-entrypoint.sh
generated
1
16/alpine3.19/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
16/bookworm/Dockerfile
generated
3
16/bookworm/Dockerfile
generated
@ -184,7 +184,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
16/bookworm/docker-ensure-initdb.sh
generated
Executable file
71
16/bookworm/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec gosu postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
16/bookworm/docker-entrypoint.sh
generated
1
16/bookworm/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
3
16/bullseye/Dockerfile
generated
3
16/bullseye/Dockerfile
generated
@ -184,7 +184,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
71
16/bullseye/docker-ensure-initdb.sh
generated
Executable file
71
16/bullseye/docker-ensure-initdb.sh
generated
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec gosu postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
1
16/bullseye/docker-entrypoint.sh
generated
1
16/bullseye/docker-entrypoint.sh
generated
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
@ -194,7 +194,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
@ -182,7 +182,8 @@ ENV PGDATA /var/lib/postgresql/data
|
||||
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
|
||||
VOLUME /var/lib/postgresql/data
|
||||
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
|
||||
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
|
||||
# We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
|
||||
|
@ -52,12 +52,12 @@ for version; do
|
||||
|
||||
echo "processing $dir ..."
|
||||
|
||||
cp -a docker-entrypoint.sh "$dir/"
|
||||
cp -a docker-entrypoint.sh docker-ensure-initdb.sh "$dir/"
|
||||
|
||||
case "$variant" in
|
||||
alpine*)
|
||||
template='Dockerfile-alpine.template'
|
||||
sed -i -e 's/gosu/su-exec/g' "$dir/docker-entrypoint.sh"
|
||||
sed -i -e 's/gosu/su-exec/g' "$dir/docker-entrypoint.sh" "$dir/docker-ensure-initdb.sh"
|
||||
;;
|
||||
*)
|
||||
template='Dockerfile-debian.template'
|
||||
|
71
docker-ensure-initdb.sh
Executable file
71
docker-ensure-initdb.sh
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
#
|
||||
# This script is intended for three main use cases:
|
||||
#
|
||||
# 1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior
|
||||
#
|
||||
# 2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
|
||||
# (no-op if database is already initialized)
|
||||
#
|
||||
# 3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
|
||||
# (error if database is already initialized)
|
||||
#
|
||||
|
||||
source /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# arguments to this script are assumed to be arguments to the "postgres" server (same as "docker-entrypoint.sh"), and most "docker-entrypoint.sh" functions assume "postgres" is the first argument (see "_main" over there)
|
||||
if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
|
||||
set -- postgres "$@"
|
||||
fi
|
||||
|
||||
# see also "_main" in "docker-entrypoint.sh"
|
||||
|
||||
docker_setup_env
|
||||
# setup data directories and permissions (when run as root)
|
||||
docker_create_db_directories
|
||||
if [ "$(id -u)" = '0' ]; then
|
||||
# then restart script as postgres user
|
||||
exec gosu postgres "$BASH_SOURCE" "$@"
|
||||
fi
|
||||
|
||||
# only run initialization on an empty data directory
|
||||
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
||||
docker_verify_minimum_env
|
||||
|
||||
# check dir permissions to reduce likelihood of half-initialized database
|
||||
ls /docker-entrypoint-initdb.d/ > /dev/null
|
||||
|
||||
docker_init_database_dir
|
||||
pg_setup_hba_conf "$@"
|
||||
|
||||
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
|
||||
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
|
||||
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
|
||||
docker_temp_server_start "$@"
|
||||
|
||||
docker_setup_db
|
||||
docker_process_init_files /docker-entrypoint-initdb.d/*
|
||||
|
||||
docker_temp_server_stop
|
||||
unset PGPASSWORD
|
||||
else
|
||||
self="$(basename "$0")"
|
||||
case "$self" in
|
||||
docker-ensure-initdb.sh)
|
||||
echo >&2 "$self: note: database already initialized in '$PGDATA'!"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
docker-enforce-initdb.sh)
|
||||
echo >&2 "$self: error: (unexpected) database found in '$PGDATA'!"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
echo >&2 "$self: error: unknown file name: $self"
|
||||
exit 99
|
||||
;;
|
||||
esac
|
||||
fi
|
@ -225,6 +225,7 @@ docker_setup_env() {
|
||||
: "${POSTGRES_HOST_AUTH_METHOD:=}"
|
||||
|
||||
declare -g DATABASE_ALREADY_EXISTS
|
||||
: "${DATABASE_ALREADY_EXISTS:=}"
|
||||
# look specifically for PG_VERSION, as it is expected in the DB dir
|
||||
if [ -s "$PGDATA/PG_VERSION" ]; then
|
||||
DATABASE_ALREADY_EXISTS='true'
|
||||
|
Loading…
x
Reference in New Issue
Block a user