Skip to content

Commit 95707d7

Browse files
committed
Add support for locale and encoding, fix #406
Signed-off-by: Michael Scherer <[email protected]>
1 parent 50499ae commit 95707d7

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

src/Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ FROM {{ spec.s2i_base }}
99
# * $POSTGRESQL_DATABASE - Name of the database to create
1010
# * $POSTGRESQL_ADMIN_PASSWORD (Optional) - Password for the 'postgres'
1111
# PostgreSQL administrative account
12+
# * $POSTGRESQL_ENCODING - Database encoding. Default to UTF8
13+
# * $POSTGRESQL_LOCALE - Database locale. Default to en_US
1214

1315
ENV POSTGRESQL_VERSION={{ spec.version }} \
1416
{% if spec.prod != "rhel8" or spec.prod != "rhel9" or spec.version == "10" %}

src/Dockerfile.fedora

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ FROM quay.io/fedora/s2i-core:38
1313
# * $POSTGRESQL_DATABASE - Name of the database to create
1414
# * $POSTGRESQL_ADMIN_PASSWORD (Optional) - Password for the 'postgres'
1515
# PostgreSQL administrative account
16+
# * $POSTGRESQL_ENCODING - Database encoding. Default to UTF8
17+
# * $POSTGRESQL_LOCALE - Database locale. Default to en_US
1618

1719
ENV NAME=postgresql \
1820
VERSION=0 \

src/root/usr/share/container-scripts/postgresql/common.sh

+14-2
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ initdb_wrapper ()
210210
# Initialize the database cluster with utf8 support enabled by default.
211211
# This might affect performance, see:
212212
# http://www.postgresql.org/docs/{{ spec.version }}/static/locale.html
213-
LANG=${LANG:-en_US.utf8} "$@"
213+
ENCODING=${POSTGRESQL_ENCODING:-UTF8}
214+
LOCALE=${POSTGRESQL_LOCALE:-en_US}
215+
if [ ${LOCALE} == "C" ] ; then LANG=C; fi
216+
LANG=${LANG:-$LOCALE.$ENCODING} "$@" -E $ENCODING
214217
}
215218

216219
function initialize_database() {
@@ -243,7 +246,16 @@ EOF
243246
function create_users() {
244247
if [[ ",$postinitdb_actions," = *,simple_db,* ]]; then
245248
createuser "$POSTGRESQL_USER"
246-
createdb --owner="$POSTGRESQL_USER" "$POSTGRESQL_DATABASE"
249+
250+
EXTRA_ARGS=""
251+
if [ -v POSTGRESQL_ENCODING ]; then
252+
EXTRA_ARGS="$EXTRA_ARGS -E $POSTGRESQL_ENCODING"
253+
fi
254+
if [ -v POSTGRESQL_LOCALE ]; then
255+
EXTRA_ARGS="$EXTRA_ARGS -l $POSTGRESQL_LOCALE"
256+
fi
257+
258+
createdb $EXTRA_ARGS --owner="$POSTGRESQL_USER" "$POSTGRESQL_DATABASE"
247259
fi
248260

249261
if [ -v POSTGRESQL_MASTER_USER ]; then

test/run_test

+49
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ run_migration_test
3030
run_pgaudit_test
3131
run_new_pgaudit_test
3232
run_logging_test
33+
run_locales_test
3334
"
3435

3536
test $# -eq 1 -a "${1-}" == --list && echo "$TEST_LIST" && exit 0
@@ -1029,6 +1030,54 @@ run_new_pgaudit_test() {
10291030
run_pgaudit_test
10301031
}
10311032

1033+
run_locales_test() {
1034+
local data_dir config_dir name=pg-test-locales-1
1035+
# create a dir for data
1036+
create_volume_dir
1037+
data_dir="${volume_dir}"
1038+
1039+
DOCKER_ARGS="-e POSTGRESQL_ADMIN_PASSWORD=Mellon
1040+
-e POSTGRESQL_LOCALE=en_GB
1041+
-e POSTGRESQL_ENCODING=ISO885915
1042+
-v ${data_dir}:/var/lib/pgsql/data:Z" create_container $name
1043+
1044+
wait_ready "$name"
1045+
1046+
# LATIN9 is a alias for ISO885915
1047+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
1048+
SHOW SERVER_ENCODING;
1049+
EOSQL" | grep LATIN9
1050+
1051+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
1052+
SHOW LC_COLLATE;
1053+
EOSQL" | grep -vi LC_COLLA | grep en_GB
1054+
1055+
docker stop "$(get_cid "$name")"
1056+
1057+
name=pg-test-locales-2
1058+
# create a dir for data
1059+
create_volume_dir
1060+
data_dir="${volume_dir}"
1061+
1062+
DOCKER_ARGS="-e POSTGRESQL_ADMIN_PASSWORD=Natoar23ae
1063+
-e POSTGRESQL_LOCALE=C
1064+
-e POSTGRESQL_ENCODING=UTF8
1065+
-v ${data_dir}:/var/lib/pgsql/data:Z" create_container $name
1066+
1067+
wait_ready "$name"
1068+
1069+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
1070+
SHOW SERVER_ENCODING;
1071+
EOSQL" | grep UTF8
1072+
1073+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
1074+
SHOW LC_COLLATE;
1075+
EOSQL" | grep -vi LC_COLLA | grep C
1076+
1077+
docker stop "$(get_cid "$name")"
1078+
1079+
}
1080+
10321081
# configuration defaults
10331082
POSTGRESQL_MAX_CONNECTIONS=100
10341083
POSTGRESQL_MAX_PREPARED_TRANSACTIONS=0

0 commit comments

Comments
 (0)