Postgresql – Thai and English in a postgresql

collationencodingpostgresql

I have a postgresql DB with Thai and English values/strings in several tables.

Currently I have the following settings

| Encoding |   Collate   |    Ctype |
| UTF8     | en_US.UTF-8 | en_US.UTF-8  |

If I order by a column with Thai values, the sort order is not correct.

What settings should I change to have postgresql sort correctly?

Best Answer

On PostgreSQL 9.1 and newer, you can use the COLLATE qualifier on an operation to override the database's default collation. See the manual for information on collation support.

E.g.

SELECT a, b FROM mytable ORDER BY c COLLATE 'th_TH.UTF-8';

Note that PostgreSQL can't mix different collations, using a dynamic collation based on detected language. It doesn't work like that.

On prior versions you must use a single database-wide collation. So you'd have to dump your database, CREATE DATABASE ... ENCODING 'UTF-8' LC_COLLATE 'th_TH.UTF-8', and re-load.