Postgres supports localization with three approaches:
Using the locale features of the operating system to provide locale-specific collation order, number formatting, and other aspects.
Using explicit multiple-byte character sets defined in the Postgres server to support languages that require more characters than will fit into a single byte, and to provide character set recoding between client and server. The number of supported character sets is fixed at the time the server is compiled, and internal operations such as string comparisons require expansion of each character into a 32-bit word.
Single byte character recoding provides a more light-weight solution for users of multiple, yet single-byte character sets.
Locale support refers to an application respecting cultural preferences regarding alphabets, sorting, number formatting, etc. PostgreSQL uses the standard ISO C and POSIX-like locale facilities provided by the server operating system. For additional information refer to the documentation of your system.
Locale support is not built into PostgreSQL by default; to enable it, supply the --enable-locale option to the configure script:
The information about which particular cultural rules to use is determined by standard environment variables. If you are getting localized behavior from other programs you probably have them set up already. The simplest way to set the localization information is the LANG variable, for example:
export LANG=sv_SEThis sets the locale to Swedish (sv) as spoken in Sweden (SE). Other possibilities might be en_US (U.S. English) and fr_CA (Canada, French). If more than one character set can be useful for a locale then the specifications look like this: cs_CZ.ISO8859-2. What locales are available under what names on your system depends on what was provided by the operating system vendor and what was installed.
Occasionally it is useful to mix rules from several locales, e.g., use U.S. collation rules but Spanish messages. To do that a set of environment variables exist that override the default of LANG for a particular category:
|LC_COLLATE||String sort order|
|LC_CTYPE||Character classification (What is a letter? What is the upper-case equivalent of this letter?)|
|LC_MESSAGES||Language of messages|
|LC_MONETARY||Formatting of currency amounts|
|LC_NUMERIC||Formatting of numbers|
|LC_TIME||Formatting of dates and times|
If you want the system to behave as if it had no locale support, use the special locale C or POSIX, or simply unset all locale related variables.
Note that the locale behavior is determined by the environment variables seen by the server, not by the environment of any client. Therefore, be careful to set these variables before starting the postmaster.
The LC_COLLATE and LC_CTYPE variables affect the sort order of indexes. Therefore, these values must be kept fixed for any particular database cluster, or indexes on text columns will become corrupt. Postgres enforces this by recording the values of LC_COLLATE and LC_CTYPE that are seen by initdb. The server automatically adopts those two values when it is started; only the other LC_ categories can be set from the environment at server startup. In short, only one collation order can be used in a database cluster, and it is chosen at initdb time.
Locale support influences in particular the following features:
Sort order in ORDER BY queries.
The to_char family of functions
The LIKE and ~ operators for pattern matching
The only severe drawback of using the locale support in PostgreSQL is its speed. So use locale only if you actually need it. It should be noted in particular that selecting a non-C locale disables index optimizations for LIKE and ~ operators, which can make a huge difference in the speed of searches that use those operators.
If locale support doesn't work in spite of the explanation above, check that the locale support in your operating system is okay. To check whether a given locale is installed and functional you can use Perl, for example. Perl has also support for locales and if a locale is broken perl -v will complain something like this:
$ export LC_CTYPE='not_exist' $ perl -v perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LC_ALL = (unset), LC_CTYPE = "not_exist", LANG = (unset) are supported and installed on your system. perl: warning: Falling back to the standard locale ("C").
Check that your locale files are in the right location. Possible locations include: /usr/lib/locale (Linux, Solaris), /usr/share/locale (Linux), /usr/lib/nls/loc (DUX 4.0). Check the locale man page of your system if you are not sure.
The directory src/test/locale contains a test suite for PostgreSQL's locale support.