Skip to content

Commit 868c3fa

Browse files
committed
Add support for locale and encoding, fix #406
1 parent 62a947e commit 868c3fa

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
@@ -9,6 +9,8 @@ FROM quay.io/fedora/s2i-core:38
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 NAME=postgresql \
1416
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
@@ -983,6 +984,54 @@ run_logging_test()
983984
echo " Success!"
984985
}
985986

987+
run_locales_test() {
988+
local data_dir config_dir name=pg-test-locales-1
989+
# create a dir for data
990+
create_volume_dir
991+
data_dir="${volume_dir}"
992+
993+
DOCKER_ARGS="-e POSTGRESQL_ADMIN_PASSWORD=Mellon
994+
-e POSTGRESQL_LOCALE=en_GB
995+
-e POSTGRESQL_ENCODING=ISO885915
996+
-v ${data_dir}:/var/lib/pgsql/data:Z" create_container $name
997+
998+
wait_ready "$name"
999+
1000+
# LATIN9 is a alias for ISO885915
1001+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
1002+
SHOW SERVER_ENCODING;
1003+
EOSQL" | grep LATIN9
1004+
1005+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
1006+
SHOW LC_COLLATE;
1007+
EOSQL" | grep -vi LC_COLLA | grep en_GB
1008+
1009+
docker stop "$(get_cid "$name")"
1010+
1011+
name=pg-test-locales-2
1012+
# create a dir for data
1013+
create_volume_dir
1014+
data_dir="${volume_dir}"
1015+
1016+
DOCKER_ARGS="-e POSTGRESQL_ADMIN_PASSWORD=Natoar23ae
1017+
-e POSTGRESQL_LOCALE=C
1018+
-e POSTGRESQL_ENCODING=UTF8
1019+
-v ${data_dir}:/var/lib/pgsql/data:Z" create_container $name
1020+
1021+
wait_ready "$name"
1022+
1023+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
1024+
SHOW SERVER_ENCODING;
1025+
EOSQL" | grep UTF8
1026+
1027+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
1028+
SHOW LC_COLLATE;
1029+
EOSQL" | grep -vi LC_COLLA | grep C
1030+
1031+
docker stop "$(get_cid "$name")"
1032+
1033+
}
1034+
9861035
# configuration defaults
9871036
POSTGRESQL_MAX_CONNECTIONS=100
9881037
POSTGRESQL_MAX_PREPARED_TRANSACTIONS=0

0 commit comments

Comments
 (0)