Skip to content

Commit f54d715

Browse files
committed
Add support for locale and encoding, fix sclorg#406
1 parent 64e2765 commit f54d715

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
@@ -202,7 +202,10 @@ initdb_wrapper ()
202202
# Initialize the database cluster with utf8 support enabled by default.
203203
# This might affect performance, see:
204204
# http://www.postgresql.org/docs/{{ spec.version }}/static/locale.html
205-
LANG=${LANG:-en_US.utf8} "$@"
205+
ENCODING=${POSTGRESQL_ENCODING:-UTF8}
206+
LOCALE=${POSTGRESQL_LOCALE:-en_US}
207+
if [ ${LOCALE} == "C" ] ; then LANG=C; fi
208+
LANG=${LANG:-$LOCALE.$ENCODING} "$@" -E $ENCODING
206209
}
207210

208211
function initialize_database() {
@@ -235,7 +238,16 @@ EOF
235238
function create_users() {
236239
if [[ ",$postinitdb_actions," = *,simple_db,* ]]; then
237240
createuser "$POSTGRESQL_USER"
238-
createdb --owner="$POSTGRESQL_USER" "$POSTGRESQL_DATABASE"
241+
242+
EXTRA_ARGS=""
243+
if [ -v POSTGRESQL_ENCODING ]; then
244+
EXTRA_ARGS="$EXTRA_ARGS -E $POSTGRESQL_ENCODING"
245+
fi
246+
if [ -v POSTGRESQL_LOCALE ]; then
247+
EXTRA_ARGS="$EXTRA_ARGS -l $POSTGRESQL_LOCALE"
248+
fi
249+
250+
createdb $EXTRA_ARGS --owner="$POSTGRESQL_USER" "$POSTGRESQL_DATABASE"
239251
fi
240252

241253
if [ -v POSTGRESQL_MASTER_USER ]; then

test/run_test

+49
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ run_upgrade_test
2929
run_migration_test
3030
run_pgaudit_test
3131
run_logging_test
32+
run_locales_test
3233
"
3334

3435
test $# -eq 1 -a "${1-}" == --list && echo "$TEST_LIST" && exit 0
@@ -996,6 +997,54 @@ run_logging_test()
996997
echo " Success!"
997998
}
998999

1000+
run_locales_test() {
1001+
local data_dir config_dir name=pg-test-locales-1
1002+
# create a dir for data
1003+
create_volume_dir
1004+
data_dir="${volume_dir}"
1005+
1006+
DOCKER_ARGS="-e POSTGRESQL_ADMIN_PASSWORD=Mellon
1007+
-e POSTGRESQL_LOCALE=en_GB
1008+
-e POSTGRESQL_ENCODING=ISO885915
1009+
-v ${data_dir}:/var/lib/pgsql/data:Z" create_container $name
1010+
1011+
wait_ready "$name"
1012+
1013+
# LATIN9 is a alias for ISO885915
1014+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
1015+
SHOW SERVER_ENCODING;
1016+
EOSQL" | grep LATIN9
1017+
1018+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
1019+
SHOW LC_COLLATE;
1020+
EOSQL" | grep -vi LC_COLLA | grep en_GB
1021+
1022+
docker stop "$(get_cid "$name")"
1023+
1024+
name=pg-test-locales-2
1025+
# create a dir for data
1026+
create_volume_dir
1027+
data_dir="${volume_dir}"
1028+
1029+
DOCKER_ARGS="-e POSTGRESQL_ADMIN_PASSWORD=Natoar23ae
1030+
-e POSTGRESQL_LOCALE=C
1031+
-e POSTGRESQL_ENCODING=UTF8
1032+
-v ${data_dir}:/var/lib/pgsql/data:Z" create_container $name
1033+
1034+
wait_ready "$name"
1035+
1036+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
1037+
SHOW SERVER_ENCODING;
1038+
EOSQL" | grep UTF8
1039+
1040+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
1041+
SHOW LC_COLLATE;
1042+
EOSQL" | grep -vi LC_COLLA | grep C
1043+
1044+
docker stop "$(get_cid "$name")"
1045+
1046+
}
1047+
9991048
# configuration defaults
10001049
POSTGRESQL_MAX_CONNECTIONS=100
10011050
POSTGRESQL_MAX_PREPARED_TRANSACTIONS=0

0 commit comments

Comments
 (0)