1
<!-- doc/src/sgml/charset.sgml -->
4
<title>Localization</title>
7
This chapter describes the available localization features from the
8
point of view of the administrator.
9
<productname>PostgreSQL</productname> supports two localization
15
Using the locale features of the operating system to provide
16
locale-specific collation order, number formatting, translated
17
messages, and other aspects.
18
This is covered in <xref linkend="locale"> and
19
<xref linkend="collation">.
25
Providing a number of different character sets to support storing text
26
in all kinds of languages, and providing character set translation
27
between client and server.
28
This is covered in <xref linkend="multibyte">.
36
<title>Locale Support</title>
38
<indexterm zone="locale"><primary>locale</></>
41
<firstterm>Locale</> support refers to an application respecting
42
cultural preferences regarding alphabets, sorting, number
43
formatting, etc. <productname>PostgreSQL</> uses the standard ISO
44
C and <acronym>POSIX</acronym> locale facilities provided by the server operating
45
system. For additional information refer to the documentation of your
50
<title>Overview</title>
53
Locale support is automatically initialized when a database
54
cluster is created using <command>initdb</command>.
55
<command>initdb</command> will initialize the database cluster
56
with the locale setting of its execution environment by default,
57
so if your system is already set to use the locale that you want
58
in your database cluster then there is nothing else you need to
59
do. If you want to use a different locale (or you are not sure
60
which locale your system is set to), you can instruct
61
<command>initdb</command> exactly which locale to use by
62
specifying the <option>--locale</option> option. For example:
69
This example for Unix systems sets the locale to Swedish
70
(<literal>sv</>) as spoken
71
in Sweden (<literal>SE</>). Other possibilities might include
72
<literal>en_US</> (U.S. English) and <literal>fr_CA</> (French
73
Canadian). If more than one character set can be used for a
74
locale then the specifications can take the form
75
<replaceable>language_territory.codeset</>. For example,
76
<literal>fr_BE.UTF-8</> represents the French language (fr) as
77
spoken in Belgium (BE), with a <acronym>UTF-8</> character set
82
What locales are available on your
83
system under what names depends on what was provided by the operating
84
system vendor and what was installed. On most Unix systems, the command
85
<literal>locale -a</> will provide a list of available locales.
86
Windows uses more verbose locale names, such as <literal>German_Germany</>
87
or <literal>Swedish_Sweden.1252</>, but the principles are the same.
91
Occasionally it is useful to mix rules from several locales, e.g.,
92
use English collation rules but Spanish messages. To support that, a
93
set of locale subcategories exist that control only certain
94
aspects of the localization rules:
100
<entry><envar>LC_COLLATE</></>
101
<entry>String sort order</>
104
<entry><envar>LC_CTYPE</></>
105
<entry>Character classification (What is a letter? Its upper-case equivalent?)</>
108
<entry><envar>LC_MESSAGES</></>
109
<entry>Language of messages</>
112
<entry><envar>LC_MONETARY</></>
113
<entry>Formatting of currency amounts</>
116
<entry><envar>LC_NUMERIC</></>
117
<entry>Formatting of numbers</>
120
<entry><envar>LC_TIME</></>
121
<entry>Formatting of dates and times</>
127
The category names translate into names of
128
<command>initdb</command> options to override the locale choice
129
for a specific category. For instance, to set the locale to
130
French Canadian, but use U.S. rules for formatting currency, use
131
<literal>initdb --locale=fr_CA --lc-monetary=en_US</literal>.
135
If you want the system to behave as if it had no locale support,
136
use the special locale name <literal>C</>, or equivalently
141
Some locale categories must have their values
142
fixed when the database is created. You can use different settings
143
for different databases, but once a database is created, you cannot
144
change them for that database anymore. <literal>LC_COLLATE</literal>
145
and <literal>LC_CTYPE</literal> are these categories. They affect
146
the sort order of indexes, so they must be kept fixed, or indexes on
147
text columns would become corrupt.
148
(But you can alleviate this restriction using collations, as discussed
149
in <xref linkend="collation">.)
150
The default values for these
151
categories are determined when <command>initdb</command> is run, and
152
those values are used when new databases are created, unless
153
specified otherwise in the <command>CREATE DATABASE</command> command.
157
The other locale categories can be changed whenever desired
158
by setting the server configuration parameters
159
that have the same name as the locale categories (see <xref
160
linkend="runtime-config-client-format"> for details). The values
161
that are chosen by <command>initdb</command> are actually only written
162
into the configuration file <filename>postgresql.conf</filename> to
163
serve as defaults when the server is started. If you remove these
164
assignments from <filename>postgresql.conf</filename> then the
165
server will inherit the settings from its execution environment.
169
Note that the locale behavior of the server is determined by the
170
environment variables seen by the server, not by the environment
171
of any client. Therefore, be careful to configure the correct locale settings
172
before starting the server. A consequence of this is that if
173
client and server are set up in different locales, messages might
174
appear in different languages depending on where they originated.
179
When we speak of inheriting the locale from the execution
180
environment, this means the following on most operating systems:
181
For a given locale category, say the collation, the following
182
environment variables are consulted in this order until one is
183
found to be set: <envar>LC_ALL</envar>, <envar>LC_COLLATE</envar>
184
(or the variable corresponding to the respective category),
185
<envar>LANG</envar>. If none of these environment variables are
186
set then the locale defaults to <literal>C</literal>.
190
Some message localization libraries also look at the environment
191
variable <envar>LANGUAGE</envar> which overrides all other locale
192
settings for the purpose of setting the language of messages. If
193
in doubt, please refer to the documentation of your operating
194
system, in particular the documentation about
195
<application>gettext</>.
200
To enable messages to be translated to the user's preferred language,
201
<acronym>NLS</acronym> must have been selected at build time
202
(<literal>configure --enable-nls</>). All other locale support is
203
built in automatically.
208
<title>Behavior</title>
211
The locale settings influence the following SQL features:
216
Sort order in queries using <literal>ORDER BY</> or the standard
217
comparison operators on textual data
218
<indexterm><primary>ORDER BY</><secondary>and locales</></indexterm>
224
The <function>upper</>, <function>lower</>, and <function>initcap</>
226
<indexterm><primary>upper</><secondary>and locales</></indexterm>
227
<indexterm><primary>lower</><secondary>and locales</></indexterm>
233
Pattern matching operators (<literal>LIKE</>, <literal>SIMILAR TO</>,
234
and POSIX-style regular expressions); locales affect both case
235
insensitive matching and the classification of characters by
236
character-class regular expressions
237
<indexterm><primary>LIKE</><secondary>and locales</></indexterm>
238
<indexterm><primary>regular expressions</><secondary>and locales</></indexterm>
244
The <function>to_char</> family of functions
245
<indexterm><primary>to_char</><secondary>and locales</></indexterm>
251
The ability to use indexes with <literal>LIKE</> clauses
258
The drawback of using locales other than <literal>C</> or
259
<literal>POSIX</> in <productname>PostgreSQL</> is its performance
260
impact. It slows character handling and prevents ordinary indexes
261
from being used by <literal>LIKE</>. For this reason use locales
262
only if you actually need them.
266
As a workaround to allow <productname>PostgreSQL</> to use indexes
267
with <literal>LIKE</> clauses under a non-C locale, several custom
268
operator classes exist. These allow the creation of an index that
269
performs a strict character-by-character comparison, ignoring
270
locale comparison rules. Refer to <xref linkend="indexes-opclass">
271
for more information. Another approach is to create indexes using
272
the <literal>C</> collation, as discussed in
273
<xref linkend="collation">.
278
<title>Problems</title>
281
If locale support doesn't work according to the explanation above,
282
check that the locale support in your operating system is
283
correctly configured. To check what locales are installed on your
284
system, you can use the command <literal>locale -a</literal> if
285
your operating system provides it.
289
Check that <productname>PostgreSQL</> is actually using the locale
290
that you think it is. The <envar>LC_COLLATE</> and <envar>LC_CTYPE</>
291
settings are determined when a database is created, and cannot be
292
changed except by creating a new database. Other locale
293
settings including <envar>LC_MESSAGES</> and <envar>LC_MONETARY</>
294
are initially determined by the environment the server is started
295
in, but can be changed on-the-fly. You can check the active locale
296
settings using the <command>SHOW</> command.
300
The directory <filename>src/test/locale</> in the source
301
distribution contains a test suite for
302
<productname>PostgreSQL</>'s locale support.
306
Client applications that handle server-side errors by parsing the
307
text of the error message will obviously have problems when the
308
server's messages are in a different language. Authors of such
309
applications are advised to make use of the error code scheme
314
Maintaining catalogs of message translations requires the on-going
315
efforts of many volunteers that want to see
316
<productname>PostgreSQL</> speak their preferred language well.
317
If messages in your language are currently not available or not fully
318
translated, your assistance would be appreciated. If you want to
319
help, refer to <xref linkend="nls"> or write to the developers'
326
<sect1 id="collation">
327
<title>Collation Support</title>
329
<indexterm zone="collation"><primary>collation</></>
332
The collation feature allows specifying the sort order and character
333
classification behavior of data per-column, or even per-operation.
334
This alleviates the restriction that the
335
<symbol>LC_COLLATE</symbol> and <symbol>LC_CTYPE</symbol> settings
336
of a database cannot be changed after its creation.
340
<title>Concepts</title>
343
Conceptually, every expression of a collatable data type has a
344
collation. (The built-in collatable data types are
345
<type>text</type>, <type>varchar</type>, and <type>char</type>.
346
User-defined base types can also be marked collatable, and of course
347
a domain over a collatable data type is collatable.) If the
348
expression is a column reference, the collation of the expression is the
349
defined collation of the column. If the expression is a constant, the
350
collation is the default collation of the data type of the
351
constant. The collation of a more complex expression is derived
352
from the collations of its inputs, as described below.
356
The collation of an expression can be the <quote>default</quote>
357
collation, which means the locale settings defined for the
358
database. It is also possible for an expression's collation to be
359
indeterminate. In such cases, ordering operations and other
360
operations that need to know the collation will fail.
364
When the database system has to perform an ordering or a character
365
classification, it uses the collation of the input expression. This
366
happens, for example, with <literal>ORDER BY</literal> clauses
367
and function or operator calls such as <literal><</literal>.
368
The collation to apply for an <literal>ORDER BY</literal> clause
369
is simply the collation of the sort key. The collation to apply for a
370
function or operator call is derived from the arguments, as described
371
below. In addition to comparison operators, collations are taken into
372
account by functions that convert between lower and upper case
373
letters, such as <function>lower</>, <function>upper</>, and
374
<function>initcap</>; by pattern matching operators; and by
375
<function>to_char</> and related functions.
379
For a function or operator call, the collation that is derived by
380
examining the argument collations is used at run time for performing
381
the specified operation. If the result of the function or operator
382
call is of a collatable data type, the collation is also used at parse
383
time as the defined collation of the function or operator expression,
384
in case there is a surrounding expression that requires knowledge of
389
The <firstterm>collation derivation</firstterm> of an expression can be
390
implicit or explicit. This distinction affects how collations are
391
combined when multiple different collations appear in an
392
expression. An explicit collation derivation occurs when a
393
<literal>COLLATE</literal> clause is used; all other collation
394
derivations are implicit. When multiple collations need to be
395
combined, for example in a function call, the following rules are
401
If any input expression has an explicit collation derivation, then
402
all explicitly derived collations among the input expressions must be
403
the same, otherwise an error is raised. If any explicitly
404
derived collation is present, that is the result of the
405
collation combination.
411
Otherwise, all input expressions must have the same implicit
412
collation derivation or the default collation. If any non-default
413
collation is present, that is the result of the collation combination.
414
Otherwise, the result is the default collation.
420
If there are conflicting non-default implicit collations among the
421
input expressions, then the combination is deemed to have indeterminate
422
collation. This is not an error condition unless the particular
423
function being invoked requires knowledge of the collation it should
424
apply. If it does, an error will be raised at run-time.
429
For example, consider this table definition:
432
a text COLLATE "de_DE",
433
b text COLLATE "es_ES",
440
SELECT a < 'foo' FROM test1;
442
the <literal><</literal> comparison is performed according to
443
<literal>de_DE</literal> rules, because the expression combines an
444
implicitly derived collation with the default collation. But in
446
SELECT a < ('foo' COLLATE "fr_FR") FROM test1;
448
the comparison is performed using <literal>fr_FR</literal> rules,
449
because the explicit collation derivation overrides the implicit one.
452
SELECT a < b FROM test1;
454
the parser cannot determine which collation to apply, since the
455
<structfield>a</> and <structfield>b</> columns have conflicting
456
implicit collations. Since the <literal><</literal> operator
457
does need to know which collation to use, this will result in an
458
error. The error can be resolved by attaching an explicit collation
459
specifier to either input expression, thus:
461
SELECT a < b COLLATE "de_DE" FROM test1;
465
SELECT a COLLATE "de_DE" < b FROM test1;
467
On the other hand, the structurally similar case
469
SELECT a || b FROM test1;
471
does not result in an error, because the <literal>||</> operator
472
does not care about collations: its result is the same regardless
477
The collation assigned to a function or operator's combined input
478
expressions is also considered to apply to the function or operator's
479
result, if the function or operator delivers a result of a collatable
482
SELECT * FROM test1 ORDER BY a || 'foo';
484
the ordering will be done according to <literal>de_DE</literal> rules.
487
SELECT * FROM test1 ORDER BY a || b;
489
results in an error, because even though the <literal>||</> operator
490
doesn't need to know a collation, the <literal>ORDER BY</> clause does.
491
As before, the conflict can be resolved with an explicit collation
494
SELECT * FROM test1 ORDER BY a || b COLLATE "fr_FR";
500
<title>Managing Collations</title>
503
A collation is an SQL schema object that maps an SQL name to
504
operating system locales. In particular, it maps to a combination
505
of <symbol>LC_COLLATE</symbol> and <symbol>LC_CTYPE</symbol>. (As
506
the name would suggest, the main purpose of a collation is to set
507
<symbol>LC_COLLATE</symbol>, which controls the sort order. But
508
it is rarely necessary in practice to have an
509
<symbol>LC_CTYPE</symbol> setting that is different from
510
<symbol>LC_COLLATE</symbol>, so it is more convenient to collect
511
these under one concept than to create another infrastructure for
512
setting <symbol>LC_CTYPE</symbol> per expression.) Also, a collation
513
is tied to a character set encoding (see <xref linkend="multibyte">).
514
The same collation name may exist for different encodings.
518
On all platforms, the collations named <literal>default</>,
519
<literal>C</>, and <literal>POSIX</> are available. Additional
520
collations may be available depending on operating system support.
521
The <literal>default</> collation selects the <symbol>LC_COLLATE</symbol>
522
and <symbol>LC_CTYPE</symbol> values specified at database creation time.
523
The <literal>C</> and <literal>POSIX</> collations both specify
524
<quote>traditional C</> behavior, in which only the ASCII letters
525
<quote><literal>A</></quote> through <quote><literal>Z</></quote>
526
are treated as letters, and sorting is done strictly by character
531
If the operating system provides support for using multiple locales
532
within a single program (<function>newlocale</> and related functions),
533
then when a database cluster is initialized, <command>initdb</command>
534
populates the system catalog <literal>pg_collation</literal> with
535
collations based on all the locales it finds on the operating
536
system at the time. For example, the operating system might
537
provide a locale named <literal>de_DE.utf8</literal>.
538
<command>initdb</command> would then create a collation named
539
<literal>de_DE.utf8</literal> for encoding <literal>UTF8</literal>
540
that has both <symbol>LC_COLLATE</symbol> and
541
<symbol>LC_CTYPE</symbol> set to <literal>de_DE.utf8</literal>.
542
It will also create a collation with the <literal>.utf8</literal>
543
tag stripped off the name. So you could also use the collation
544
under the name <literal>de_DE</literal>, which is less cumbersome
545
to write and makes the name less encoding-dependent. Note that,
546
nevertheless, the initial set of collation names is
551
In case a collation is needed that has different values for
552
<symbol>LC_COLLATE</symbol> and <symbol>LC_CTYPE</symbol>, a new
553
collation may be created using
554
the <xref linkend="sql-createcollation"> command. That command
555
can also be used to create a new collation from an existing
556
collation, which can be useful to be able to use
557
operating-system-independent collation names in applications.
561
Within any particular database, only collations that use that
562
database's encoding are of interest. Other entries in
563
<literal>pg_collation</literal> are ignored. Thus, a stripped collation
564
name such as <literal>de_DE</literal> can be considered unique
565
within a given database even though it would not be unique globally.
566
Use of the stripped collation names is recommendable, since it will
567
make one less thing you need to change if you decide to change to
568
another database encoding. Note however that the <literal>default</>,
569
<literal>C</>, and <literal>POSIX</> collations can be used
570
regardless of the database encoding.
574
<productname>PostgreSQL</productname> considers distinct collation
575
objects to be incompatible even when they have identical properties.
578
SELECT a COLLATE "C" < b COLLATE "POSIX" FROM test1;
580
will draw an error even though the <literal>C</> and <literal>POSIX</>
581
collations have identical behaviors. Mixing stripped and non-stripped
582
collation names is therefore not recommended.
587
<sect1 id="multibyte">
588
<title>Character Set Support</title>
590
<indexterm zone="multibyte"><primary>character set</></>
593
The character set support in <productname>PostgreSQL</productname>
594
allows you to store text in a variety of character sets (also called
595
encodings), including
596
single-byte character sets such as the ISO 8859 series and
597
multiple-byte character sets such as <acronym>EUC</> (Extended Unix
598
Code), UTF-8, and Mule internal code. All supported character sets
599
can be used transparently by clients, but a few are not supported
600
for use within the server (that is, as a server-side encoding).
601
The default character set is selected while
602
initializing your <productname>PostgreSQL</productname> database
603
cluster using <command>initdb</>. It can be overridden when you
604
create a database, so you can have multiple
605
databases each with a different character set.
609
An important restriction, however, is that each database's character set
610
must be compatible with the database's <envar>LC_CTYPE</> (character
611
classification) and <envar>LC_COLLATE</> (string sort order) locale
612
settings. For <literal>C</> or
613
<literal>POSIX</> locale, any character set is allowed, but for other
614
locales there is only one character set that will work correctly.
615
(On Windows, however, UTF-8 encoding can be used with any locale.)
618
<sect2 id="multibyte-charset-supported">
619
<title>Supported Character Sets</title>
622
<xref linkend="charset-table"> shows the character sets available
623
for use in <productname>PostgreSQL</productname>.
626
<table id="charset-table">
627
<title><productname>PostgreSQL</productname> Character Sets</title>
632
<entry>Description</entry>
633
<entry>Language</entry>
634
<entry>Server?</entry>
636
The Bytes/Char field is populated by looking at the values returned
637
by pg_wchar_table.mblen function for each encoding.
639
<entry>Bytes/Char</entry>
640
<entry>Aliases</entry>
645
<entry><literal>BIG5</literal></entry>
646
<entry>Big Five</entry>
647
<entry>Traditional Chinese</entry>
650
<entry><literal>WIN950</>, <literal>Windows950</></entry>
653
<entry><literal>EUC_CN</literal></entry>
654
<entry>Extended UNIX Code-CN</entry>
655
<entry>Simplified Chinese</entry>
661
<entry><literal>EUC_JP</literal></entry>
662
<entry>Extended UNIX Code-JP</entry>
663
<entry>Japanese</entry>
669
<entry><literal>EUC_JIS_2004</literal></entry>
670
<entry>Extended UNIX Code-JP, JIS X 0213</entry>
671
<entry>Japanese</entry>
677
<entry><literal>EUC_KR</literal></entry>
678
<entry>Extended UNIX Code-KR</entry>
679
<entry>Korean</entry>
685
<entry><literal>EUC_TW</literal></entry>
686
<entry>Extended UNIX Code-TW</entry>
687
<entry>Traditional Chinese, Taiwanese</entry>
693
<entry><literal>GB18030</literal></entry>
694
<entry>National Standard</entry>
695
<entry>Chinese</entry>
701
<entry><literal>GBK</literal></entry>
702
<entry>Extended National Standard</entry>
703
<entry>Simplified Chinese</entry>
706
<entry><literal>WIN936</>, <literal>Windows936</></entry>
709
<entry><literal>ISO_8859_5</literal></entry>
710
<entry>ISO 8859-5, <acronym>ECMA</> 113</entry>
711
<entry>Latin/Cyrillic</entry>
717
<entry><literal>ISO_8859_6</literal></entry>
718
<entry>ISO 8859-6, <acronym>ECMA</> 114</entry>
719
<entry>Latin/Arabic</entry>
725
<entry><literal>ISO_8859_7</literal></entry>
726
<entry>ISO 8859-7, <acronym>ECMA</> 118</entry>
727
<entry>Latin/Greek</entry>
733
<entry><literal>ISO_8859_8</literal></entry>
734
<entry>ISO 8859-8, <acronym>ECMA</> 121</entry>
735
<entry>Latin/Hebrew</entry>
741
<entry><literal>JOHAB</literal></entry>
742
<entry><acronym>JOHAB</></entry>
743
<entry>Korean (Hangul)</entry>
749
<entry><literal>KOI8R</literal></entry>
750
<entry><acronym>KOI</acronym>8-R</entry>
751
<entry>Cyrillic (Russian)</entry>
754
<entry><literal>KOI8</></entry>
757
<entry><literal>KOI8U</literal></entry>
758
<entry><acronym>KOI</acronym>8-U</entry>
759
<entry>Cyrillic (Ukrainian)</entry>
765
<entry><literal>LATIN1</literal></entry>
766
<entry>ISO 8859-1, <acronym>ECMA</> 94</entry>
767
<entry>Western European</entry>
770
<entry><literal>ISO88591</></entry>
773
<entry><literal>LATIN2</literal></entry>
774
<entry>ISO 8859-2, <acronym>ECMA</> 94</entry>
775
<entry>Central European</entry>
778
<entry><literal>ISO88592</></entry>
781
<entry><literal>LATIN3</literal></entry>
782
<entry>ISO 8859-3, <acronym>ECMA</> 94</entry>
783
<entry>South European</entry>
786
<entry><literal>ISO88593</></entry>
789
<entry><literal>LATIN4</literal></entry>
790
<entry>ISO 8859-4, <acronym>ECMA</> 94</entry>
791
<entry>North European</entry>
794
<entry><literal>ISO88594</></entry>
797
<entry><literal>LATIN5</literal></entry>
798
<entry>ISO 8859-9, <acronym>ECMA</> 128</entry>
799
<entry>Turkish</entry>
802
<entry><literal>ISO88599</></entry>
805
<entry><literal>LATIN6</literal></entry>
806
<entry>ISO 8859-10, <acronym>ECMA</> 144</entry>
807
<entry>Nordic</entry>
810
<entry><literal>ISO885910</></entry>
813
<entry><literal>LATIN7</literal></entry>
814
<entry>ISO 8859-13</entry>
815
<entry>Baltic</entry>
818
<entry><literal>ISO885913</></entry>
821
<entry><literal>LATIN8</literal></entry>
822
<entry>ISO 8859-14</entry>
823
<entry>Celtic</entry>
826
<entry><literal>ISO885914</></entry>
829
<entry><literal>LATIN9</literal></entry>
830
<entry>ISO 8859-15</entry>
831
<entry>LATIN1 with Euro and accents</entry>
834
<entry><literal>ISO885915</></entry>
837
<entry><literal>LATIN10</literal></entry>
838
<entry>ISO 8859-16, <acronym>ASRO</> SR 14111</entry>
839
<entry>Romanian</entry>
842
<entry><literal>ISO885916</></entry>
845
<entry><literal>MULE_INTERNAL</literal></entry>
846
<entry>Mule internal code</entry>
847
<entry>Multilingual Emacs</entry>
853
<entry><literal>SJIS</literal></entry>
854
<entry>Shift JIS</entry>
855
<entry>Japanese</entry>
858
<entry><literal>Mskanji</>, <literal>ShiftJIS</>, <literal>WIN932</>, <literal>Windows932</></entry>
861
<entry><literal>SHIFT_JIS_2004</literal></entry>
862
<entry>Shift JIS, JIS X 0213</entry>
863
<entry>Japanese</entry>
869
<entry><literal>SQL_ASCII</literal></entry>
870
<entry>unspecified (see text)</entry>
871
<entry><emphasis>any</></entry>
877
<entry><literal>UHC</literal></entry>
878
<entry>Unified Hangul Code</entry>
879
<entry>Korean</entry>
882
<entry><literal>WIN949</>, <literal>Windows949</></entry>
885
<entry><literal>UTF8</literal></entry>
886
<entry>Unicode, 8-bit</entry>
887
<entry><emphasis>all</></entry>
890
<entry><literal>Unicode</></entry>
893
<entry><literal>WIN866</literal></entry>
894
<entry>Windows CP866</entry>
895
<entry>Cyrillic</entry>
898
<entry><literal>ALT</></entry>
901
<entry><literal>WIN874</literal></entry>
902
<entry>Windows CP874</entry>
909
<entry><literal>WIN1250</literal></entry>
910
<entry>Windows CP1250</entry>
911
<entry>Central European</entry>
917
<entry><literal>WIN1251</literal></entry>
918
<entry>Windows CP1251</entry>
919
<entry>Cyrillic</entry>
922
<entry><literal>WIN</></entry>
925
<entry><literal>WIN1252</literal></entry>
926
<entry>Windows CP1252</entry>
927
<entry>Western European</entry>
933
<entry><literal>WIN1253</literal></entry>
934
<entry>Windows CP1253</entry>
941
<entry><literal>WIN1254</literal></entry>
942
<entry>Windows CP1254</entry>
943
<entry>Turkish</entry>
949
<entry><literal>WIN1255</literal></entry>
950
<entry>Windows CP1255</entry>
951
<entry>Hebrew</entry>
957
<entry><literal>WIN1256</literal></entry>
958
<entry>Windows CP1256</entry>
959
<entry>Arabic</entry>
965
<entry><literal>WIN1257</literal></entry>
966
<entry>Windows CP1257</entry>
967
<entry>Baltic</entry>
973
<entry><literal>WIN1258</literal></entry>
974
<entry>Windows CP1258</entry>
975
<entry>Vietnamese</entry>
978
<entry><literal>ABC</>, <literal>TCVN</>, <literal>TCVN5712</>, <literal>VSCII</></entry>
985
Not all client <acronym>API</>s support all the listed character sets. For example, the
986
<productname>PostgreSQL</>
987
JDBC driver does not support <literal>MULE_INTERNAL</>, <literal>LATIN6</>,
988
<literal>LATIN8</>, and <literal>LATIN10</>.
992
The <literal>SQL_ASCII</> setting behaves considerably differently
993
from the other settings. When the server character set is
994
<literal>SQL_ASCII</>, the server interprets byte values 0-127
995
according to the ASCII standard, while byte values 128-255 are taken
996
as uninterpreted characters. No encoding conversion will be done when
997
the setting is <literal>SQL_ASCII</>. Thus, this setting is not so
998
much a declaration that a specific encoding is in use, as a declaration
999
of ignorance about the encoding. In most cases, if you are
1000
working with any non-ASCII data, it is unwise to use the
1001
<literal>SQL_ASCII</> setting because
1002
<productname>PostgreSQL</productname> will be unable to help you by
1003
converting or validating non-ASCII characters.
1008
<title>Setting the Character Set</title>
1011
<command>initdb</> defines the default character set (encoding)
1012
for a <productname>PostgreSQL</productname> cluster. For example,
1018
sets the default character set to
1019
<literal>EUC_JP</literal> (Extended Unix Code for Japanese). You
1020
can use <option>--encoding</option> instead of
1021
<option>-E</option> if you prefer longer option strings.
1022
If no <option>-E</> or <option>--encoding</option> option is
1023
given, <command>initdb</> attempts to determine the appropriate
1024
encoding to use based on the specified or default locale.
1028
You can specify a non-default encoding at database creation time,
1029
provided that the encoding is compatible with the selected locale:
1032
createdb -E EUC_KR -T template0 --lc-collate=ko_KR.euckr --lc-ctype=ko_KR.euckr korean
1035
This will create a database named <literal>korean</literal> that
1036
uses the character set <literal>EUC_KR</literal>, and locale <literal>ko_KR</literal>.
1037
Another way to accomplish this is to use this SQL command:
1040
CREATE DATABASE korean WITH ENCODING 'EUC_KR' LC_COLLATE='ko_KR.euckr' LC_CTYPE='ko_KR.euckr' TEMPLATE=template0;
1043
Notice that the above commands specify copying the <literal>template0</>
1044
database. When copying any other database, the encoding and locale
1045
settings cannot be changed from those of the source database, because
1046
that might result in corrupt data. For more information see
1047
<xref linkend="manage-ag-templatedbs">.
1051
The encoding for a database is stored in the system catalog
1052
<literal>pg_database</literal>. You can see it by using the
1053
<command>psql</command> <option>-l</option> option or the
1054
<command>\l</command> command.
1057
$ <userinput>psql -l</userinput>
1059
Name | Owner | Encoding | Collation | Ctype | Access Privileges
1060
-----------+----------+-----------+-------------+-------------+-------------------------------------
1061
clocaledb | hlinnaka | SQL_ASCII | C | C |
1062
englishdb | hlinnaka | UTF8 | en_GB.UTF8 | en_GB.UTF8 |
1063
japanese | hlinnaka | UTF8 | ja_JP.UTF8 | ja_JP.UTF8 |
1064
korean | hlinnaka | EUC_KR | ko_KR.euckr | ko_KR.euckr |
1065
postgres | hlinnaka | UTF8 | fi_FI.UTF8 | fi_FI.UTF8 |
1066
template0 | hlinnaka | UTF8 | fi_FI.UTF8 | fi_FI.UTF8 | {=c/hlinnaka,hlinnaka=CTc/hlinnaka}
1067
template1 | hlinnaka | UTF8 | fi_FI.UTF8 | fi_FI.UTF8 | {=c/hlinnaka,hlinnaka=CTc/hlinnaka}
1074
On most modern operating systems, <productname>PostgreSQL</productname>
1075
can determine which character set is implied by the <envar>LC_CTYPE</>
1076
setting, and it will enforce that only the matching database encoding is
1077
used. On older systems it is your responsibility to ensure that you use
1078
the encoding expected by the locale you have selected. A mistake in
1079
this area is likely to lead to strange behavior of locale-dependent
1080
operations such as sorting.
1084
<productname>PostgreSQL</productname> will allow superusers to create
1085
databases with <literal>SQL_ASCII</> encoding even when
1086
<envar>LC_CTYPE</> is not <literal>C</> or <literal>POSIX</>. As noted
1087
above, <literal>SQL_ASCII</> does not enforce that the data stored in
1088
the database has any particular encoding, and so this choice poses risks
1089
of locale-dependent misbehavior. Using this combination of settings is
1090
deprecated and may someday be forbidden altogether.
1096
<title>Automatic Character Set Conversion Between Server and Client</title>
1099
<productname>PostgreSQL</productname> supports automatic
1100
character set conversion between server and client for certain
1101
character set combinations. The conversion information is stored in the
1102
<literal>pg_conversion</> system catalog. <productname>PostgreSQL</>
1103
comes with some predefined conversions, as shown in <xref
1104
linkend="multibyte-translation-table">. You can create a new
1105
conversion using the SQL command <command>CREATE CONVERSION</command>.
1108
<table id="multibyte-translation-table">
1109
<title>Client/Server Character Set Conversions</title>
1113
<entry>Server Character Set</entry>
1114
<entry>Available Client Character Sets</entry>
1119
<entry><literal>BIG5</literal></entry>
1120
<entry><emphasis>not supported as a server encoding</emphasis>
1124
<entry><literal>EUC_CN</literal></entry>
1125
<entry><emphasis>EUC_CN</emphasis>,
1126
<literal>MULE_INTERNAL</literal>,
1127
<literal>UTF8</literal>
1131
<entry><literal>EUC_JP</literal></entry>
1132
<entry><emphasis>EUC_JP</emphasis>,
1133
<literal>MULE_INTERNAL</literal>,
1134
<literal>SJIS</literal>,
1135
<literal>UTF8</literal>
1139
<entry><literal>EUC_KR</literal></entry>
1140
<entry><emphasis>EUC_KR</emphasis>,
1141
<literal>MULE_INTERNAL</literal>,
1142
<literal>UTF8</literal>
1146
<entry><literal>EUC_TW</literal></entry>
1147
<entry><emphasis>EUC_TW</emphasis>,
1148
<literal>BIG5</literal>,
1149
<literal>MULE_INTERNAL</literal>,
1150
<literal>UTF8</literal>
1154
<entry><literal>GB18030</literal></entry>
1155
<entry><emphasis>not supported as a server encoding</emphasis>
1159
<entry><literal>GBK</literal></entry>
1160
<entry><emphasis>not supported as a server encoding</emphasis>
1164
<entry><literal>ISO_8859_5</literal></entry>
1165
<entry><emphasis>ISO_8859_5</emphasis>,
1166
<literal>KOI8R</literal>,
1167
<literal>MULE_INTERNAL</literal>,
1168
<literal>UTF8</literal>,
1169
<literal>WIN866</literal>,
1170
<literal>WIN1251</literal>
1174
<entry><literal>ISO_8859_6</literal></entry>
1175
<entry><emphasis>ISO_8859_6</emphasis>,
1176
<literal>UTF8</literal>
1180
<entry><literal>ISO_8859_7</literal></entry>
1181
<entry><emphasis>ISO_8859_7</emphasis>,
1182
<literal>UTF8</literal>
1186
<entry><literal>ISO_8859_8</literal></entry>
1187
<entry><emphasis>ISO_8859_8</emphasis>,
1188
<literal>UTF8</literal>
1192
<entry><literal>JOHAB</literal></entry>
1193
<entry><emphasis>JOHAB</emphasis>,
1194
<literal>UTF8</literal>
1198
<entry><literal>KOI8R</literal></entry>
1199
<entry><emphasis>KOI8R</emphasis>,
1200
<literal>ISO_8859_5</literal>,
1201
<literal>MULE_INTERNAL</literal>,
1202
<literal>UTF8</literal>,
1203
<literal>WIN866</literal>,
1204
<literal>WIN1251</literal>
1208
<entry><literal>KOI8U</literal></entry>
1209
<entry><emphasis>KOI8U</emphasis>,
1210
<literal>UTF8</literal>
1214
<entry><literal>LATIN1</literal></entry>
1215
<entry><emphasis>LATIN1</emphasis>,
1216
<literal>MULE_INTERNAL</literal>,
1217
<literal>UTF8</literal>
1221
<entry><literal>LATIN2</literal></entry>
1222
<entry><emphasis>LATIN2</emphasis>,
1223
<literal>MULE_INTERNAL</literal>,
1224
<literal>UTF8</literal>,
1225
<literal>WIN1250</literal>
1229
<entry><literal>LATIN3</literal></entry>
1230
<entry><emphasis>LATIN3</emphasis>,
1231
<literal>MULE_INTERNAL</literal>,
1232
<literal>UTF8</literal>
1236
<entry><literal>LATIN4</literal></entry>
1237
<entry><emphasis>LATIN4</emphasis>,
1238
<literal>MULE_INTERNAL</literal>,
1239
<literal>UTF8</literal>
1243
<entry><literal>LATIN5</literal></entry>
1244
<entry><emphasis>LATIN5</emphasis>,
1245
<literal>UTF8</literal>
1249
<entry><literal>LATIN6</literal></entry>
1250
<entry><emphasis>LATIN6</emphasis>,
1251
<literal>UTF8</literal>
1255
<entry><literal>LATIN7</literal></entry>
1256
<entry><emphasis>LATIN7</emphasis>,
1257
<literal>UTF8</literal>
1261
<entry><literal>LATIN8</literal></entry>
1262
<entry><emphasis>LATIN8</emphasis>,
1263
<literal>UTF8</literal>
1267
<entry><literal>LATIN9</literal></entry>
1268
<entry><emphasis>LATIN9</emphasis>,
1269
<literal>UTF8</literal>
1273
<entry><literal>LATIN10</literal></entry>
1274
<entry><emphasis>LATIN10</emphasis>,
1275
<literal>UTF8</literal>
1279
<entry><literal>MULE_INTERNAL</literal></entry>
1280
<entry><emphasis>MULE_INTERNAL</emphasis>,
1281
<literal>BIG5</literal>,
1282
<literal>EUC_CN</literal>,
1283
<literal>EUC_JP</literal>,
1284
<literal>EUC_KR</literal>,
1285
<literal>EUC_TW</literal>,
1286
<literal>ISO_8859_5</literal>,
1287
<literal>KOI8R</literal>,
1288
<literal>LATIN1</literal> to <literal>LATIN4</literal>,
1289
<literal>SJIS</literal>,
1290
<literal>WIN866</literal>,
1291
<literal>WIN1250</literal>,
1292
<literal>WIN1251</literal>
1296
<entry><literal>SJIS</literal></entry>
1297
<entry><emphasis>not supported as a server encoding</emphasis>
1301
<entry><literal>SQL_ASCII</literal></entry>
1302
<entry><emphasis>any (no conversion will be performed)</emphasis>
1306
<entry><literal>UHC</literal></entry>
1307
<entry><emphasis>not supported as a server encoding</emphasis>
1311
<entry><literal>UTF8</literal></entry>
1312
<entry><emphasis>all supported encodings</emphasis>
1316
<entry><literal>WIN866</literal></entry>
1317
<entry><emphasis>WIN866</emphasis>,
1318
<literal>ISO_8859_5</literal>,
1319
<literal>KOI8R</literal>,
1320
<literal>MULE_INTERNAL</literal>,
1321
<literal>UTF8</literal>,
1322
<literal>WIN1251</literal>
1326
<entry><literal>WIN874</literal></entry>
1327
<entry><emphasis>WIN874</emphasis>,
1328
<literal>UTF8</literal>
1332
<entry><literal>WIN1250</literal></entry>
1333
<entry><emphasis>WIN1250</emphasis>,
1334
<literal>LATIN2</literal>,
1335
<literal>MULE_INTERNAL</literal>,
1336
<literal>UTF8</literal>
1340
<entry><literal>WIN1251</literal></entry>
1341
<entry><emphasis>WIN1251</emphasis>,
1342
<literal>ISO_8859_5</literal>,
1343
<literal>KOI8R</literal>,
1344
<literal>MULE_INTERNAL</literal>,
1345
<literal>UTF8</literal>,
1346
<literal>WIN866</literal>
1350
<entry><literal>WIN1252</literal></entry>
1351
<entry><emphasis>WIN1252</emphasis>,
1352
<literal>UTF8</literal>
1356
<entry><literal>WIN1253</literal></entry>
1357
<entry><emphasis>WIN1253</emphasis>,
1358
<literal>UTF8</literal>
1362
<entry><literal>WIN1254</literal></entry>
1363
<entry><emphasis>WIN1254</emphasis>,
1364
<literal>UTF8</literal>
1368
<entry><literal>WIN1255</literal></entry>
1369
<entry><emphasis>WIN1255</emphasis>,
1370
<literal>UTF8</literal>
1374
<entry><literal>WIN1256</literal></entry>
1375
<entry><emphasis>WIN1256</emphasis>,
1376
<literal>UTF8</literal>
1380
<entry><literal>WIN1257</literal></entry>
1381
<entry><emphasis>WIN1257</emphasis>,
1382
<literal>UTF8</literal>
1386
<entry><literal>WIN1258</literal></entry>
1387
<entry><emphasis>WIN1258</emphasis>,
1388
<literal>UTF8</literal>
1396
To enable automatic character set conversion, you have to
1397
tell <productname>PostgreSQL</productname> the character set
1398
(encoding) you would like to use in the client. There are several
1399
ways to accomplish this:
1404
Using the <command>\encoding</command> command in
1405
<application>psql</application>.
1406
<command>\encoding</command> allows you to change client
1407
encoding on the fly. For
1408
example, to change the encoding to <literal>SJIS</literal>, type:
1418
<application>libpq</> (<xref linkend="libpq-control">) has functions to control the client encoding.
1424
Using <command>SET client_encoding TO</command>.
1426
Setting the client encoding can be done with this SQL command:
1429
SET CLIENT_ENCODING TO '<replaceable>value</>';
1432
Also you can use the standard SQL syntax <literal>SET NAMES</literal>
1436
SET NAMES '<replaceable>value</>';
1439
To query the current client encoding:
1442
SHOW client_encoding;
1445
To return to the default encoding:
1448
RESET client_encoding;
1455
Using <envar>PGCLIENTENCODING</envar>. If the environment variable
1456
<envar>PGCLIENTENCODING</envar> is defined in the client's
1457
environment, that client encoding is automatically selected
1458
when a connection to the server is made. (This can
1459
subsequently be overridden using any of the other methods
1466
Using the configuration variable <xref
1467
linkend="guc-client-encoding">. If the
1468
<varname>client_encoding</> variable is set, that client
1469
encoding is automatically selected when a connection to the
1470
server is made. (This can subsequently be overridden using any
1471
of the other methods mentioned above.)
1479
If the conversion of a particular character is not possible
1480
— suppose you chose <literal>EUC_JP</literal> for the
1481
server and <literal>LATIN1</literal> for the client, and some
1482
Japanese characters are returned that do not have a representation in
1483
<literal>LATIN1</literal> — an error is reported.
1487
If the client character set is defined as <literal>SQL_ASCII</>,
1488
encoding conversion is disabled, regardless of the server's character
1489
set. Just as for the server, use of <literal>SQL_ASCII</> is unwise
1490
unless you are working with all-ASCII data.
1495
<title>Further Reading</title>
1498
These are good sources to start learning about various kinds of encoding
1503
<term><ulink url="http://www.i18ngurus.com/docs/984813247.html"></ulink></term>
1507
An extensive collection of documents about character sets, encodings,
1514
<term><citetitle>CJKV Information Processing: Chinese, Japanese, Korean & Vietnamese Computing</citetitle></term>
1518
Contains detailed explanations of <literal>EUC_JP</literal>,
1519
<literal>EUC_CN</literal>, <literal>EUC_KR</literal>,
1520
<literal>EUC_TW</literal>.
1526
<term><ulink url="http://www.unicode.org/"></ulink></term>
1530
The web site of the Unicode Consortium.
1536
<term>RFC 3629</term>
1540
<acronym>UTF</acronym>-8 (8-bit UCS/Unicode Transformation
1541
Format) is defined here.