Skip to content

Commit 38aebc6

Browse files
committed
Add support for locale and encoding, fix #406
1 parent 8aafd89 commit 38aebc6

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/root/usr/share/container-scripts/postgresql/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ initialization by passing `-e VAR=VALUE` to the Docker run command.
7474
**`POSTGRESQL_ADMIN_PASSWORD`**
7575
Password for the `postgres` admin account (optional)
7676

77+
The following environment variables are optional, and only used when the database is initialzed
78+
79+
**`POSTGRESQL_ENCODING`**
80+
Database encoding. Default to UTF8
81+
82+
**`POSTGRESQL_LOCALE`**
83+
Database locale. Default to en_US
7784

7885
Alternatively, the following options are related to migration scenario:
7986

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ initdb_wrapper ()
190190
# Initialize the database cluster with utf8 support enabled by default.
191191
# This might affect performance, see:
192192
# http://www.postgresql.org/docs/{{ spec.version }}/static/locale.html
193-
LANG=${LANG:-en_US.utf8} "$@"
193+
ENCODING=${POSTGRESQL_ENCODING:-UTF8}
194+
LOCALE=${POSTGRESQL_LOCALE:-en_US}
195+
if [ ${LOCALE} == "C" ] ; then LANG=C; fi
196+
LANG=${LANG:-$LOCALE.$ENCODING} "$@" -E $ENCODING
194197
}
195198

196199
function initialize_database() {
@@ -223,7 +226,16 @@ EOF
223226
function create_users() {
224227
if [[ ",$postinitdb_actions," = *,simple_db,* ]]; then
225228
createuser "$POSTGRESQL_USER"
226-
createdb --owner="$POSTGRESQL_USER" "$POSTGRESQL_DATABASE"
229+
230+
EXTRA_ARGS=""
231+
if [ -v POSTGRESQL_ENCODING ]; then
232+
EXTRA_ARGS="$EXTRA_ARGS -E $POSTGRESQL_ENCODING"
233+
fi
234+
if [ -v POSTGRESQL_LOCALE ]; then
235+
EXTRA_ARGS="$EXTRA_ARGS -l $POSTGRESQL_LOCALE"
236+
fi
237+
238+
createdb $EXTRA_ARGS --owner="$POSTGRESQL_USER" "$POSTGRESQL_DATABASE"
227239
fi
228240

229241
if [ -v POSTGRESQL_MASTER_USER ]; then

test/run_test

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

3435
test $# -eq 1 -a "${1-}" == --list && echo "$TEST_LIST" && exit 0
@@ -863,6 +864,54 @@ EOSQL"
863864
grep -E 'AUDIT: SESSION,.*,.*,READ,SELECT,,,SELECT' "${data_dir}"/userdata/log/postgresql-*.log
864865
}
865866

867+
function run_locales_test() {
868+
local data_dir config_dir name=pg-test-locales-1
869+
# create a dir for data
870+
create_volume_dir
871+
data_dir="${volume_dir}"
872+
873+
DOCKER_ARGS="-e POSTGRESQL_ADMIN_PASSWORD=Mellon
874+
-e POSTGRESQL_LOCALE=en_GB
875+
-e POSTGRESQL_ENCODING=ISO885915
876+
-v ${data_dir}:/var/lib/pgsql/data:Z" create_container $name
877+
878+
wait_ready "$name"
879+
880+
# LATIN9 is a alias for ISO885915
881+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
882+
SHOW SERVER_ENCODING;
883+
EOSQL" | grep LATIN9
884+
885+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
886+
SHOW LC_COLLATE;
887+
EOSQL" | grep -vi LC_COLLA | grep en_GB
888+
889+
docker stop "$(get_cid "$name")"
890+
891+
name=pg-test-locales-2
892+
# create a dir for data
893+
create_volume_dir
894+
data_dir="${volume_dir}"
895+
896+
DOCKER_ARGS="-e POSTGRESQL_ADMIN_PASSWORD=Natoar23ae
897+
-e POSTGRESQL_LOCALE=C
898+
-e POSTGRESQL_ENCODING=UTF8
899+
-v ${data_dir}:/var/lib/pgsql/data:Z" create_container $name
900+
901+
wait_ready "$name"
902+
903+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
904+
SHOW SERVER_ENCODING;
905+
EOSQL" | grep UTF8
906+
907+
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
908+
SHOW LC_COLLATE;
909+
EOSQL" | grep -vi LC_COLLA | grep C
910+
911+
docker stop "$(get_cid "$name")"
912+
913+
}
914+
866915
# configuration defaults
867916
POSTGRESQL_MAX_CONNECTIONS=100
868917
POSTGRESQL_MAX_PREPARED_TRANSACTIONS=0

0 commit comments

Comments
 (0)