Localization
This chapter describes the available localization features from the
point of view of the administrator.
PostgreSQL supports two localization
facilities:
Using the locale features of the operating system to provide
locale-specific collation order, number formatting, translated
messages, and other aspects.
Providing a number of different character sets to support storing text
in all kinds of languages, and providing character set translation
between client and server.
Locale Supportlocale>>
Locale> support refers to an application respecting
cultural preferences regarding alphabets, sorting, number
formatting, etc. PostgreSQL> uses the standard ISO
C and POSIX locale facilities provided by the server operating
system. For additional information refer to the documentation of your
system.
Overview
Locale support is automatically initialized when a database
cluster is created using initdb.
initdb will initialize the database cluster
with the locale setting of its execution environment by default,
so if your system is already set to use the locale that you want
in your database cluster then there is nothing else you need to
do. If you want to use a different locale (or you are not sure
which locale your system is set to), you can instruct
initdb exactly which locale to use by
specifying the option. For example:
initdb --locale=sv_SE
This example for Unix systems sets the locale to Swedish
(sv>) as spoken
in Sweden (SE>). Other possibilities might be
en_US> (U.S. English) and fr_CA> (French
Canadian). If more than one character set can be used for a
locale then the specifications can take the form
language_territory.codeset>. For example,
fr_BE.UTF-8> represents the French language (fr) as
spoken in Belgium (BE), with a UTF-8> character set
encoding.
What locales are available on your
system under what names depends on what was provided by the operating
system vendor and what was installed. On most Unix systems, the command
locale -a> will provide a list of available locales.
Windows uses more verbose locale names, such as German_Germany>
or Swedish_Sweden.1252>, but the principles are the same.
Occasionally it is useful to mix rules from several locales, e.g.,
use English collation rules but Spanish messages. To support that, a
set of locale subcategories exist that control only certain
aspects of the localization rules:
LC_COLLATE>>
String sort order>
LC_CTYPE>>
Character classification (What is a letter? Its upper-case equivalent?)>
LC_MESSAGES>>
Language of messages>
LC_MONETARY>>
Formatting of currency amounts>
LC_NUMERIC>>
Formatting of numbers>
LC_TIME>>
Formatting of dates and times>
The category names translate into names of
initdb options to override the locale choice
for a specific category. For instance, to set the locale to
French Canadian, but use U.S. rules for formatting currency, use
initdb --locale=fr_CA --lc-monetary=en_US.
If you want the system to behave as if it had no locale support,
use the special locale C> or POSIX>.
Some locale categories must have their values
fixed when the database is created. You can use different settings
for different databases, but once a database is created, you cannot
change them for that database anymore. LC_COLLATE
and LC_CTYPE are these type of categories. They affect
the sort order of indexes, so they must be kept fixed, or indexes on
text columns would become corrupt. The default values for these
categories are determined when initdb is run, and
those values are used when new databases are created, unless
specified otherwise in the CREATE DATABASE command.
The other locale categories can be changed whenever desired
by setting the server configuration parameters
that have the same name as the locale categories (see for details). The values
that are chosen by initdb are actually only written
into the configuration file postgresql.conf to
serve as defaults when the server is started. If you disable these
assignments from postgresql.conf then the
server will inherit the settings from its execution environment.
Note that the locale behavior of the server is determined by the
environment variables seen by the server, not by the environment
of any client. Therefore, be careful to configure the correct locale settings
before starting the server. A consequence of this is that if
client and server are set up in different locales, messages might
appear in different languages depending on where they originated.
When we speak of inheriting the locale from the execution
environment, this means the following on most operating systems:
For a given locale category, say the collation, the following
environment variables are consulted in this order until one is
found to be set: LC_ALL, LC_COLLATE
(or the variable corresponding to the respective category),
LANG. If none of these environment variables are
set then the locale defaults to C.
Some message localization libraries also look at the environment
variable LANGUAGE which overrides all other locale
settings for the purpose of setting the language of messages. If
in doubt, please refer to the documentation of your operating
system, in particular the documentation about
gettext>.
To enable messages to be translated to the user's preferred language,
NLS must have been selected at build time
(configure --enable-nls>). All other locale support is
built in automatically.
Behavior
The locale settings influence the following SQL features:
Sort order in queries using ORDER BY> or the standard
comparison operators on textual data
ORDER BY>and locales>
The ability to use indexes with LIKE> clauses
LIKE>and locales>
The upper>, lower>, and initcap>
functions
upper>and locales>lower>and locales>
The to_char> family of functions
to_char>and locales>
The drawback of using locales other than C> or
POSIX> in PostgreSQL> is its performance
impact. It slows character handling and prevents ordinary indexes
from being used by LIKE>. For this reason use locales
only if you actually need them.
As a workaround to allow PostgreSQL> to use indexes
with LIKE> clauses under a non-C locale, several custom
operator classes exist. These allow the creation of an index that
performs a strict character-by-character comparison, ignoring
locale comparison rules. Refer to
for more information.
Problems
If locale support doesn't work according to the explanation above,
check that the locale support in your operating system is
correctly configured. To check what locales are installed on your
system, you can use the command locale -a if
your operating system provides it.
Check that PostgreSQL> is actually using the locale
that you think it is. The LC_COLLATE> and LC_CTYPE>
settings are determined when a database is created, and cannot be
changed except by creating a new database. Other locale
settings including LC_MESSAGES> and LC_MONETARY>
are initially determined by the environment the server is started
in, but can be changed on-the-fly. You can check the active locale
settings using the SHOW> command.
The directory src/test/locale> in the source
distribution contains a test suite for
PostgreSQL>'s locale support.
Client applications that handle server-side errors by parsing the
text of the error message will obviously have problems when the
server's messages are in a different language. Authors of such
applications are advised to make use of the error code scheme
instead.
Maintaining catalogs of message translations requires the on-going
efforts of many volunteers that want to see
PostgreSQL> speak their preferred language well.
If messages in your language are currently not available or not fully
translated, your assistance would be appreciated. If you want to
help, refer to or write to the developers'
mailing list.
Character Set Supportcharacter set>>
The character set support in PostgreSQL
allows you to store text in a variety of character sets (also called
encodings), including
single-byte character sets such as the ISO 8859 series and
multiple-byte character sets such as EUC> (Extended Unix
Code), UTF-8, and Mule internal code. All supported character sets
can be used transparently by clients, but a few are not supported
for use within the server (that is, as a server-side encoding).
The default character set is selected while
initializing your PostgreSQL database
cluster using initdb>. It can be overridden when you
create a database, so you can have multiple
databases each with a different character set.
An important restriction, however, is that each database's character set
must be compatible with the database's LC_CTYPE> (character
classification) and LC_COLLATE> (string sort order) locale
settings. For C> or
POSIX> locale, any character set is allowed, but for other
locales there is only one character set that will work correctly.
(On Windows, however, UTF-8 encoding can be used with any locale.)
Supported Character Sets shows the character sets available
for use in PostgreSQL.
PostgreSQL Character SetsNameDescriptionLanguageServer?Bytes/CharAliasesBIG5Big FiveTraditional ChineseNo1-2WIN950>, Windows950>EUC_CNExtended UNIX Code-CNSimplified ChineseYes1-3EUC_JPExtended UNIX Code-JPJapaneseYes1-3EUC_JIS_2004Extended UNIX Code-JP, JIS X 0213JapaneseYes1-3EUC_KRExtended UNIX Code-KRKoreanYes1-3EUC_TWExtended UNIX Code-TWTraditional Chinese, TaiwaneseYes1-3GB18030National StandardChineseNo1-2GBKExtended National StandardSimplified ChineseNo1-2WIN936>, Windows936>ISO_8859_5ISO 8859-5, ECMA> 113Latin/CyrillicYes1ISO_8859_6ISO 8859-6, ECMA> 114Latin/ArabicYes1ISO_8859_7ISO 8859-7, ECMA> 118Latin/GreekYes1ISO_8859_8ISO 8859-8, ECMA> 121Latin/HebrewYes1JOHABJOHAB>Korean (Hangul)No1-3KOI8RKOI8-RCyrillic (Russian)Yes1KOI8>KOI8UKOI8-UCyrillic (Ukrainian)Yes1LATIN1ISO 8859-1, ECMA> 94Western EuropeanYes1ISO88591>LATIN2ISO 8859-2, ECMA> 94Central EuropeanYes1ISO88592>LATIN3ISO 8859-3, ECMA> 94South EuropeanYes1ISO88593>LATIN4ISO 8859-4, ECMA> 94North EuropeanYes1ISO88594>LATIN5ISO 8859-9, ECMA> 128TurkishYes1ISO88599>LATIN6ISO 8859-10, ECMA> 144NordicYes1ISO885910>LATIN7ISO 8859-13BalticYes1ISO885913>LATIN8ISO 8859-14CelticYes1ISO885914>LATIN9ISO 8859-15LATIN1 with Euro and accentsYes1ISO885915>LATIN10ISO 8859-16, ASRO> SR 14111RomanianYes1ISO885916>MULE_INTERNALMule internal codeMultilingual EmacsYes1-4SJISShift JISJapaneseNo1-2Mskanji>, ShiftJIS>, WIN932>, Windows932>SHIFT_JIS_2004Shift JIS, JIS X 0213JapaneseNo1-2SQL_ASCIIunspecified (see text)any>Yes1UHCUnified Hangul CodeKoreanNo1-2WIN949>, Windows949>UTF8Unicode, 8-bitall>Yes1-4Unicode>WIN866Windows CP866CyrillicYes1ALT>WIN874Windows CP874ThaiYes1WIN1250Windows CP1250Central EuropeanYes1WIN1251Windows CP1251CyrillicYes1WIN>WIN1252Windows CP1252Western EuropeanYes1WIN1253Windows CP1253GreekYes1WIN1254Windows CP1254TurkishYes1WIN1255Windows CP1255HebrewYes1WIN1256Windows CP1256ArabicYes1WIN1257Windows CP1257BalticYes1WIN1258Windows CP1258VietnameseYes1ABC>, TCVN>, TCVN5712>, VSCII>
Not all client API>s support all the listed character sets. For example, the
PostgreSQL>
JDBC driver does not support MULE_INTERNAL>, LATIN6>,
LATIN8>, and LATIN10>.
The SQL_ASCII> setting behaves considerably differently
from the other settings. When the server character set is
SQL_ASCII>, the server interprets byte values 0-127
according to the ASCII standard, while byte values 128-255 are taken
as uninterpreted characters. No encoding conversion will be done when
the setting is SQL_ASCII>. Thus, this setting is not so
much a declaration that a specific encoding is in use, as a declaration
of ignorance about the encoding. In most cases, if you are
working with any non-ASCII data, it is unwise to use the
SQL_ASCII> setting because
PostgreSQL will be unable to help you by
converting or validating non-ASCII characters.
Setting the Character Setinitdb> defines the default character set (encoding)
for a PostgreSQL cluster. For example,
initdb -E EUC_JP
sets the default character set to
EUC_JP (Extended Unix Code for Japanese). You
can use instead of
if you prefer longer option strings.
If no option is
given, initdb> attempts to determine the appropriate
encoding to use based on the specified or default locale.
You can specify a non-default encoding at database creation time,
provided that the encoding is compatible with the selected locale:
createdb -E EUC_KR -T template0 --lc-collate=ko_KR.euckr --lc-ctype=ko_KR.euckr korean
This will create a database named korean that
uses the character set EUC_KR, and locale ko_KR.
Another way to accomplish this is to use this SQL command:
CREATE DATABASE korean WITH ENCODING 'EUC_KR' LC_COLLATE='ko_KR.euckr' LC_CTYPE='ko_KR.euckr' TEMPLATE=template0;
Notice that the above commands specify copying the template0>
database. When copying any other database, the encoding and locale
settings cannot be changed from those of the source database, because
that might result in corrupt data. For more information see
.
The encoding for a database is stored in the system catalog
pg_database. You can see it by using the
psql option or the
\l command.
$ psql -l
List of databases
Name | Owner | Encoding | Collation | Ctype | Access Privileges
-----------+----------+-----------+-------------+-------------+-------------------------------------
clocaledb | hlinnaka | SQL_ASCII | C | C |
englishdb | hlinnaka | UTF8 | en_GB.UTF8 | en_GB.UTF8 |
japanese | hlinnaka | UTF8 | ja_JP.UTF8 | ja_JP.UTF8 |
korean | hlinnaka | EUC_KR | ko_KR.euckr | ko_KR.euckr |
postgres | hlinnaka | UTF8 | fi_FI.UTF8 | fi_FI.UTF8 |
template0 | hlinnaka | UTF8 | fi_FI.UTF8 | fi_FI.UTF8 | {=c/hlinnaka,hlinnaka=CTc/hlinnaka}
template1 | hlinnaka | UTF8 | fi_FI.UTF8 | fi_FI.UTF8 | {=c/hlinnaka,hlinnaka=CTc/hlinnaka}
(7 rows)
On most modern operating systems, PostgreSQL
can determine which character set is implied by the LC_CTYPE>
setting, and it will enforce that only the matching database encoding is
used. On older systems it is your responsibility to ensure that you use
the encoding expected by the locale you have selected. A mistake in
this area is likely to lead to strange behavior of locale-dependent
operations such as sorting.
PostgreSQL will allow superusers to create
databases with SQL_ASCII> encoding even when
LC_CTYPE> is not C> or POSIX>. As noted
above, SQL_ASCII> does not enforce that the data stored in
the database has any particular encoding, and so this choice poses risks
of locale-dependent misbehavior. Using this combination of settings is
deprecated and may someday be forbidden altogether.
Automatic Character Set Conversion Between Server and ClientPostgreSQL supports automatic
character set conversion between server and client for certain
character set combinations. The conversion information is stored in the
pg_conversion> system catalog. PostgreSQL>
comes with some predefined conversions, as shown in . You can create a new
conversion using the SQL command CREATE CONVERSION.
Client/Server Character Set ConversionsServer Character SetAvailable Client Character SetsBIG5not supported as a server encodingEUC_CNEUC_CN,
MULE_INTERNAL,
UTF8EUC_JPEUC_JP,
MULE_INTERNAL,
SJIS,
UTF8EUC_KREUC_KR,
MULE_INTERNAL,
UTF8EUC_TWEUC_TW,
BIG5,
MULE_INTERNAL,
UTF8GB18030not supported as a server encodingGBKnot supported as a server encodingISO_8859_5ISO_8859_5,
KOI8R,
MULE_INTERNAL,
UTF8,
WIN866,
WIN1251ISO_8859_6ISO_8859_6,
UTF8ISO_8859_7ISO_8859_7,
UTF8ISO_8859_8ISO_8859_8,
UTF8JOHABJOHAB,
UTF8KOI8RKOI8R,
ISO_8859_5,
MULE_INTERNAL,
UTF8,
WIN866,
WIN1251KOI8UKOI8U,
UTF8LATIN1LATIN1,
MULE_INTERNAL,
UTF8LATIN2LATIN2,
MULE_INTERNAL,
UTF8,
WIN1250LATIN3LATIN3,
MULE_INTERNAL,
UTF8LATIN4LATIN4,
MULE_INTERNAL,
UTF8LATIN5LATIN5,
UTF8LATIN6LATIN6,
UTF8LATIN7LATIN7,
UTF8LATIN8LATIN8,
UTF8LATIN9LATIN9,
UTF8LATIN10LATIN10,
UTF8MULE_INTERNALMULE_INTERNAL,
BIG5,
EUC_CN,
EUC_JP,
EUC_KR,
EUC_TW,
ISO_8859_5,
KOI8R,
LATIN1 to LATIN4,
SJIS,
WIN866,
WIN1250,
WIN1251SJISnot supported as a server encodingSQL_ASCIIany (no conversion will be performed)UHCnot supported as a server encodingUTF8all supported encodingsWIN866WIN866,
ISO_8859_5,
KOI8R,
MULE_INTERNAL,
UTF8,
WIN1251WIN874WIN874,
UTF8WIN1250WIN1250,
LATIN2,
MULE_INTERNAL,
UTF8WIN1251WIN1251,
ISO_8859_5,
KOI8R,
MULE_INTERNAL,
UTF8,
WIN866WIN1252WIN1252,
UTF8WIN1253WIN1253,
UTF8WIN1254WIN1254,
UTF8WIN1255WIN1255,
UTF8WIN1256WIN1256,
UTF8WIN1257WIN1257,
UTF8WIN1258WIN1258,
UTF8
To enable automatic character set conversion, you have to
tell PostgreSQL the character set
(encoding) you would like to use in the client. There are several
ways to accomplish this:
Using the \encoding command in
psql.
\encoding allows you to change client
encoding on the fly. For
example, to change the encoding to SJIS, type:
\encoding SJIS
libpq> () has functions to control the client encoding.
Using SET client_encoding TO.
Setting the client encoding can be done with this SQL command:
SET CLIENT_ENCODING TO 'value>';
Also you can use the standard SQL syntax SET NAMES
for this purpose:
SET NAMES 'value>';
To query the current client encoding:
SHOW client_encoding;
To return to the default encoding:
RESET client_encoding;
Using PGCLIENTENCODING. If the environment variable
PGCLIENTENCODING is defined in the client's
environment, that client encoding is automatically selected
when a connection to the server is made. (This can
subsequently be overridden using any of the other methods
mentioned above.)
Using the configuration variable . If the
client_encoding> variable is set, that client
encoding is automatically selected when a connection to the
server is made. (This can subsequently be overridden using any
of the other methods mentioned above.)
If the conversion of a particular character is not possible
— suppose you chose EUC_JP for the
server and LATIN1 for the client, and some
Japanese characters are returned that do not have a representation in
LATIN1 — an error is reported.
If the client character set is defined as SQL_ASCII>,
encoding conversion is disabled, regardless of the server's character
set. Just as for the server, use of SQL_ASCII> is unwise
unless you are working with all-ASCII data.
Further Reading
These are good sources to start learning about various kinds of encoding
systems.
An extensive collection of documents about character sets, encodings,
and code pages.
CJKV Information Processing: Chinese, Japanese, Korean & Vietnamese Computing
Contains detailed explanations of EUC_JP,
EUC_CN, EUC_KR,
EUC_TW.
The web site of the Unicode Consortium.
RFC 3629
UTF-8 (8-bit UCS/Unicode Transformation
Format) is defined here.