From 868c3fa108f5cae4f88d7c646ef0ce71ab8de47b Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Thu, 5 Oct 2023 20:46:12 +0200 Subject: [PATCH] Add support for locale and encoding, fix #406 --- src/Dockerfile | 2 + src/Dockerfile.fedora | 2 + .../container-scripts/postgresql/common.sh | 16 +++++- test/run_test | 49 +++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/Dockerfile b/src/Dockerfile index a63d00ea..728a9860 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -9,6 +9,8 @@ FROM {{ spec.s2i_base }} # * $POSTGRESQL_DATABASE - Name of the database to create # * $POSTGRESQL_ADMIN_PASSWORD (Optional) - Password for the 'postgres' # PostgreSQL administrative account +# * $POSTGRESQL_ENCODING - Database encoding. Default to UTF8 +# * $POSTGRESQL_LOCALE - Database locale. Default to en_US ENV POSTGRESQL_VERSION={{ spec.version }} \ {% if spec.prod != "rhel8" or spec.prod != "rhel9" or spec.version == "10" %} diff --git a/src/Dockerfile.fedora b/src/Dockerfile.fedora index 932ede86..7366f338 100644 --- a/src/Dockerfile.fedora +++ b/src/Dockerfile.fedora @@ -9,6 +9,8 @@ FROM quay.io/fedora/s2i-core:38 # * $POSTGRESQL_DATABASE - Name of the database to create # * $POSTGRESQL_ADMIN_PASSWORD (Optional) - Password for the 'postgres' # PostgreSQL administrative account +# * $POSTGRESQL_ENCODING - Database encoding. Default to UTF8 +# * $POSTGRESQL_LOCALE - Database locale. Default to en_US ENV NAME=postgresql \ VERSION=0 \ diff --git a/src/root/usr/share/container-scripts/postgresql/common.sh b/src/root/usr/share/container-scripts/postgresql/common.sh index f725ba21..2de9f0f4 100644 --- a/src/root/usr/share/container-scripts/postgresql/common.sh +++ b/src/root/usr/share/container-scripts/postgresql/common.sh @@ -202,7 +202,10 @@ initdb_wrapper () # Initialize the database cluster with utf8 support enabled by default. # This might affect performance, see: # http://www.postgresql.org/docs/{{ spec.version }}/static/locale.html - LANG=${LANG:-en_US.utf8} "$@" + ENCODING=${POSTGRESQL_ENCODING:-UTF8} + LOCALE=${POSTGRESQL_LOCALE:-en_US} + if [ ${LOCALE} == "C" ] ; then LANG=C; fi + LANG=${LANG:-$LOCALE.$ENCODING} "$@" -E $ENCODING } function initialize_database() { @@ -235,7 +238,16 @@ EOF function create_users() { if [[ ",$postinitdb_actions," = *,simple_db,* ]]; then createuser "$POSTGRESQL_USER" - createdb --owner="$POSTGRESQL_USER" "$POSTGRESQL_DATABASE" + + EXTRA_ARGS="" + if [ -v POSTGRESQL_ENCODING ]; then + EXTRA_ARGS="$EXTRA_ARGS -E $POSTGRESQL_ENCODING" + fi + if [ -v POSTGRESQL_LOCALE ]; then + EXTRA_ARGS="$EXTRA_ARGS -l $POSTGRESQL_LOCALE" + fi + + createdb $EXTRA_ARGS --owner="$POSTGRESQL_USER" "$POSTGRESQL_DATABASE" fi if [ -v POSTGRESQL_MASTER_USER ]; then diff --git a/test/run_test b/test/run_test index 9dc6929b..5b893a5e 100755 --- a/test/run_test +++ b/test/run_test @@ -29,6 +29,7 @@ run_upgrade_test run_migration_test run_pgaudit_test run_logging_test +run_locales_test " test $# -eq 1 -a "${1-}" == --list && echo "$TEST_LIST" && exit 0 @@ -983,6 +984,54 @@ run_logging_test() echo " Success!" } +run_locales_test() { + local data_dir config_dir name=pg-test-locales-1 + # create a dir for data + create_volume_dir + data_dir="${volume_dir}" + + DOCKER_ARGS="-e POSTGRESQL_ADMIN_PASSWORD=Mellon + -e POSTGRESQL_LOCALE=en_GB + -e POSTGRESQL_ENCODING=ISO885915 + -v ${data_dir}:/var/lib/pgsql/data:Z" create_container $name + + wait_ready "$name" + + # LATIN9 is a alias for ISO885915 + docker exec -i $(get_cid "$name") bash -c "psql <