~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/charset.sgml

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!-- doc/src/sgml/charset.sgml -->
 
2
 
 
3
<chapter id="charset">
 
4
 <title>Localization</title>
 
5
 
 
6
 <para>
 
7
  This chapter describes the available localization features from the
 
8
  point of view of the administrator.
 
9
  <productname>PostgreSQL</productname> supports two localization
 
10
  facilities:
 
11
 
 
12
   <itemizedlist>
 
13
    <listitem>
 
14
     <para>
 
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">.
 
20
     </para>
 
21
    </listitem>
 
22
 
 
23
    <listitem>
 
24
     <para>
 
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">.
 
29
     </para>
 
30
    </listitem>
 
31
   </itemizedlist>
 
32
  </para>
 
33
 
 
34
 
 
35
 <sect1 id="locale">
 
36
  <title>Locale Support</title>
 
37
 
 
38
  <indexterm zone="locale"><primary>locale</></>
 
39
 
 
40
  <para>
 
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
 
46
   system.
 
47
  </para>
 
48
 
 
49
  <sect2>
 
50
   <title>Overview</title>
 
51
 
 
52
   <para>
 
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:
 
63
<screen>
 
64
initdb --locale=sv_SE
 
65
</screen>
 
66
   </para>
 
67
 
 
68
   <para>
 
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
 
78
    encoding.
 
79
   </para>
 
80
 
 
81
   <para>
 
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.
 
88
   </para>
 
89
 
 
90
   <para>
 
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:
 
95
 
 
96
    <informaltable>
 
97
     <tgroup cols="2">
 
98
      <tbody>
 
99
       <row>
 
100
        <entry><envar>LC_COLLATE</></>
 
101
        <entry>String sort order</>
 
102
       </row>
 
103
       <row>
 
104
        <entry><envar>LC_CTYPE</></>
 
105
        <entry>Character classification (What is a letter? Its upper-case equivalent?)</>
 
106
       </row>
 
107
       <row>
 
108
        <entry><envar>LC_MESSAGES</></>
 
109
        <entry>Language of messages</>
 
110
       </row>
 
111
       <row>
 
112
        <entry><envar>LC_MONETARY</></>
 
113
        <entry>Formatting of currency amounts</>
 
114
       </row>
 
115
       <row>
 
116
        <entry><envar>LC_NUMERIC</></>
 
117
        <entry>Formatting of numbers</>
 
118
       </row>
 
119
       <row>
 
120
        <entry><envar>LC_TIME</></>
 
121
        <entry>Formatting of dates and times</>
 
122
       </row>
 
123
      </tbody>
 
124
     </tgroup>
 
125
    </informaltable>
 
126
 
 
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>.
 
132
   </para>
 
133
 
 
134
   <para>
 
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
 
137
    <literal>POSIX</>.
 
138
   </para>
 
139
 
 
140
   <para>
 
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.
 
154
   </para>
 
155
 
 
156
   <para>
 
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.
 
166
   </para>
 
167
 
 
168
   <para>
 
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.
 
175
   </para>
 
176
 
 
177
   <note>
 
178
    <para>
 
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>.
 
187
    </para>
 
188
 
 
189
    <para>
 
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</>.
 
196
    </para>
 
197
   </note>
 
198
 
 
199
   <para>
 
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.
 
204
   </para>
 
205
  </sect2>
 
206
 
 
207
  <sect2>
 
208
   <title>Behavior</title>
 
209
 
 
210
   <para>
 
211
    The locale settings influence the following SQL features:
 
212
 
 
213
    <itemizedlist>
 
214
     <listitem>
 
215
      <para>
 
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>
 
219
      </para>
 
220
     </listitem>
 
221
 
 
222
     <listitem>
 
223
      <para>
 
224
       The <function>upper</>, <function>lower</>, and <function>initcap</>
 
225
       functions
 
226
       <indexterm><primary>upper</><secondary>and locales</></indexterm>
 
227
       <indexterm><primary>lower</><secondary>and locales</></indexterm>
 
228
      </para>
 
229
     </listitem>
 
230
 
 
231
     <listitem>
 
232
      <para>
 
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>
 
239
      </para>
 
240
     </listitem>
 
241
 
 
242
     <listitem>
 
243
      <para>
 
244
       The <function>to_char</> family of functions
 
245
       <indexterm><primary>to_char</><secondary>and locales</></indexterm>
 
246
      </para>
 
247
     </listitem>
 
248
 
 
249
     <listitem>
 
250
      <para>
 
251
       The ability to use indexes with <literal>LIKE</> clauses
 
252
      </para>
 
253
     </listitem>
 
254
    </itemizedlist>
 
255
   </para>
 
256
 
 
257
   <para>
 
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.
 
263
   </para>
 
264
 
 
265
   <para>
 
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">.
 
274
   </para>
 
275
  </sect2>
 
276
 
 
277
  <sect2>
 
278
   <title>Problems</title>
 
279
 
 
280
   <para>
 
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.
 
286
   </para>
 
287
 
 
288
   <para>
 
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.
 
297
   </para>
 
298
 
 
299
   <para>
 
300
    The directory <filename>src/test/locale</> in the source
 
301
    distribution contains a test suite for
 
302
    <productname>PostgreSQL</>'s locale support.
 
303
   </para>
 
304
 
 
305
   <para>
 
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
 
310
    instead.
 
311
   </para>
 
312
 
 
313
   <para>
 
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'
 
320
    mailing list.
 
321
   </para>
 
322
  </sect2>
 
323
 </sect1>
 
324
 
 
325
 
 
326
 <sect1 id="collation">
 
327
  <title>Collation Support</title>
 
328
 
 
329
  <indexterm zone="collation"><primary>collation</></>
 
330
 
 
331
  <para>
 
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.
 
337
  </para>
 
338
 
 
339
  <sect2>
 
340
   <title>Concepts</title>
 
341
 
 
342
   <para>
 
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.
 
353
   </para>
 
354
 
 
355
   <para>
 
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.
 
361
   </para>
 
362
 
 
363
   <para>
 
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>&lt;</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.
 
376
   </para>
 
377
 
 
378
   <para>
 
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
 
385
    its collation.
 
386
   </para>
 
387
 
 
388
   <para>
 
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
 
396
    used:
 
397
 
 
398
    <orderedlist>
 
399
     <listitem>
 
400
      <para>
 
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.
 
406
      </para>
 
407
     </listitem>
 
408
 
 
409
     <listitem>
 
410
      <para>
 
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.
 
415
      </para>
 
416
     </listitem>
 
417
 
 
418
     <listitem>
 
419
      <para>
 
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.
 
425
      </para>
 
426
     </listitem>
 
427
    </orderedlist>
 
428
 
 
429
    For example, consider this table definition:
 
430
<programlisting>
 
431
CREATE TABLE test1 (
 
432
    a text COLLATE "de_DE",
 
433
    b text COLLATE "es_ES",
 
434
    ...
 
435
);
 
436
</programlisting>
 
437
 
 
438
    Then in
 
439
<programlisting>
 
440
SELECT a &lt; 'foo' FROM test1;
 
441
</programlisting>
 
442
    the <literal>&lt;</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
 
445
<programlisting>
 
446
SELECT a &lt; ('foo' COLLATE "fr_FR") FROM test1;
 
447
</programlisting>
 
448
    the comparison is performed using <literal>fr_FR</literal> rules,
 
449
    because the explicit collation derivation overrides the implicit one.
 
450
    Furthermore, given
 
451
<programlisting>
 
452
SELECT a &lt; b FROM test1;
 
453
</programlisting>
 
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>&lt;</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:
 
460
<programlisting>
 
461
SELECT a &lt; b COLLATE "de_DE" FROM test1;
 
462
</programlisting>
 
463
    or equivalently
 
464
<programlisting>
 
465
SELECT a COLLATE "de_DE" &lt; b FROM test1;
 
466
</programlisting>
 
467
    On the other hand, the structurally similar case
 
468
<programlisting>
 
469
SELECT a || b FROM test1;
 
470
</programlisting>
 
471
    does not result in an error, because the <literal>||</> operator
 
472
    does not care about collations: its result is the same regardless
 
473
    of the collation.
 
474
   </para>
 
475
 
 
476
   <para>
 
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
 
480
    data type.  So, in
 
481
<programlisting>
 
482
SELECT * FROM test1 ORDER BY a || 'foo';
 
483
</programlisting>
 
484
    the ordering will be done according to <literal>de_DE</literal> rules.
 
485
    But this query:
 
486
<programlisting>
 
487
SELECT * FROM test1 ORDER BY a || b;
 
488
</programlisting>
 
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
 
492
    specifier:
 
493
<programlisting>
 
494
SELECT * FROM test1 ORDER BY a || b COLLATE "fr_FR";
 
495
</programlisting>
 
496
   </para>
 
497
  </sect2>
 
498
 
 
499
  <sect2>
 
500
   <title>Managing Collations</title>
 
501
 
 
502
   <para>
 
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.
 
515
   </para>
 
516
 
 
517
   <para>
 
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
 
527
    code byte values.
 
528
   </para>
 
529
 
 
530
   <para>
 
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
 
547
    platform-dependent.
 
548
   </para>
 
549
 
 
550
   <para>
 
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.
 
558
   </para>
 
559
 
 
560
   <para>
 
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.
 
571
   </para>
 
572
 
 
573
   <para>
 
574
    <productname>PostgreSQL</productname> considers distinct collation
 
575
    objects to be incompatible even when they have identical properties.
 
576
    Thus for example,
 
577
<programlisting>
 
578
SELECT a COLLATE "C" &lt; b COLLATE "POSIX" FROM test1;
 
579
</programlisting>
 
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.
 
583
   </para>
 
584
  </sect2>
 
585
 </sect1>
 
586
 
 
587
 <sect1 id="multibyte">
 
588
  <title>Character Set Support</title>
 
589
 
 
590
  <indexterm zone="multibyte"><primary>character set</></>
 
591
 
 
592
  <para>
 
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.
 
606
  </para>
 
607
 
 
608
  <para>
 
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.)
 
616
  </para>
 
617
 
 
618
   <sect2 id="multibyte-charset-supported">
 
619
    <title>Supported Character Sets</title>
 
620
 
 
621
    <para>
 
622
     <xref linkend="charset-table"> shows the character sets available
 
623
     for use in <productname>PostgreSQL</productname>.
 
624
    </para>
 
625
 
 
626
     <table id="charset-table">
 
627
      <title><productname>PostgreSQL</productname> Character Sets</title>
 
628
      <tgroup cols="6">
 
629
       <thead>
 
630
        <row>
 
631
         <entry>Name</entry>
 
632
         <entry>Description</entry>
 
633
         <entry>Language</entry>
 
634
         <entry>Server?</entry>
 
635
         <!--
 
636
          The Bytes/Char field is populated by looking at the values returned
 
637
          by pg_wchar_table.mblen function for each encoding.
 
638
         -->
 
639
         <entry>Bytes/Char</entry>
 
640
         <entry>Aliases</entry>
 
641
        </row>
 
642
       </thead>
 
643
       <tbody>
 
644
        <row>
 
645
         <entry><literal>BIG5</literal></entry>
 
646
         <entry>Big Five</entry>
 
647
         <entry>Traditional Chinese</entry>
 
648
         <entry>No</entry>
 
649
         <entry>1-2</entry>
 
650
         <entry><literal>WIN950</>, <literal>Windows950</></entry>
 
651
        </row>
 
652
        <row>
 
653
         <entry><literal>EUC_CN</literal></entry>
 
654
         <entry>Extended UNIX Code-CN</entry>
 
655
         <entry>Simplified Chinese</entry>
 
656
         <entry>Yes</entry>
 
657
         <entry>1-3</entry>
 
658
         <entry></entry>
 
659
        </row>
 
660
        <row>
 
661
         <entry><literal>EUC_JP</literal></entry>
 
662
         <entry>Extended UNIX Code-JP</entry>
 
663
         <entry>Japanese</entry>
 
664
         <entry>Yes</entry>
 
665
         <entry>1-3</entry>
 
666
         <entry></entry>
 
667
        </row>
 
668
        <row>
 
669
         <entry><literal>EUC_JIS_2004</literal></entry>
 
670
         <entry>Extended UNIX Code-JP, JIS X 0213</entry>
 
671
         <entry>Japanese</entry>
 
672
         <entry>Yes</entry>
 
673
         <entry>1-3</entry>
 
674
         <entry></entry>
 
675
        </row>
 
676
        <row>
 
677
         <entry><literal>EUC_KR</literal></entry>
 
678
         <entry>Extended UNIX Code-KR</entry>
 
679
         <entry>Korean</entry>
 
680
         <entry>Yes</entry>
 
681
         <entry>1-3</entry>
 
682
         <entry></entry>
 
683
        </row>
 
684
        <row>
 
685
         <entry><literal>EUC_TW</literal></entry>
 
686
         <entry>Extended UNIX Code-TW</entry>
 
687
         <entry>Traditional Chinese, Taiwanese</entry>
 
688
         <entry>Yes</entry>
 
689
         <entry>1-3</entry>
 
690
         <entry></entry>
 
691
        </row>
 
692
        <row>
 
693
         <entry><literal>GB18030</literal></entry>
 
694
         <entry>National Standard</entry>
 
695
         <entry>Chinese</entry>
 
696
         <entry>No</entry>
 
697
         <entry>1-2</entry>
 
698
         <entry></entry>
 
699
        </row>
 
700
        <row>
 
701
         <entry><literal>GBK</literal></entry>
 
702
         <entry>Extended National Standard</entry>
 
703
         <entry>Simplified Chinese</entry>
 
704
         <entry>No</entry>
 
705
         <entry>1-2</entry>
 
706
         <entry><literal>WIN936</>, <literal>Windows936</></entry>
 
707
        </row>
 
708
        <row>
 
709
         <entry><literal>ISO_8859_5</literal></entry>
 
710
         <entry>ISO 8859-5, <acronym>ECMA</> 113</entry>
 
711
         <entry>Latin/Cyrillic</entry>
 
712
         <entry>Yes</entry>
 
713
         <entry>1</entry>
 
714
         <entry></entry>
 
715
        </row>
 
716
        <row>
 
717
         <entry><literal>ISO_8859_6</literal></entry>
 
718
         <entry>ISO 8859-6, <acronym>ECMA</> 114</entry>
 
719
         <entry>Latin/Arabic</entry>
 
720
         <entry>Yes</entry>
 
721
         <entry>1</entry>
 
722
         <entry></entry>
 
723
        </row>
 
724
        <row>
 
725
         <entry><literal>ISO_8859_7</literal></entry>
 
726
         <entry>ISO 8859-7, <acronym>ECMA</> 118</entry>
 
727
         <entry>Latin/Greek</entry>
 
728
         <entry>Yes</entry>
 
729
         <entry>1</entry>
 
730
         <entry></entry>
 
731
        </row>
 
732
        <row>
 
733
         <entry><literal>ISO_8859_8</literal></entry>
 
734
         <entry>ISO 8859-8, <acronym>ECMA</> 121</entry>
 
735
         <entry>Latin/Hebrew</entry>
 
736
         <entry>Yes</entry>
 
737
         <entry>1</entry>
 
738
         <entry></entry>
 
739
        </row>
 
740
        <row>
 
741
         <entry><literal>JOHAB</literal></entry>
 
742
         <entry><acronym>JOHAB</></entry>
 
743
         <entry>Korean (Hangul)</entry>
 
744
         <entry>No</entry>
 
745
         <entry>1-3</entry>
 
746
         <entry></entry>
 
747
        </row>
 
748
        <row>
 
749
         <entry><literal>KOI8R</literal></entry>
 
750
         <entry><acronym>KOI</acronym>8-R</entry>
 
751
         <entry>Cyrillic (Russian)</entry>
 
752
         <entry>Yes</entry>
 
753
         <entry>1</entry>
 
754
         <entry><literal>KOI8</></entry>
 
755
        </row>
 
756
        <row>
 
757
         <entry><literal>KOI8U</literal></entry>
 
758
         <entry><acronym>KOI</acronym>8-U</entry>
 
759
         <entry>Cyrillic (Ukrainian)</entry>
 
760
         <entry>Yes</entry>
 
761
         <entry>1</entry>
 
762
         <entry></entry>
 
763
        </row>
 
764
        <row>
 
765
         <entry><literal>LATIN1</literal></entry>
 
766
         <entry>ISO 8859-1, <acronym>ECMA</> 94</entry>
 
767
         <entry>Western European</entry>
 
768
         <entry>Yes</entry>
 
769
         <entry>1</entry>
 
770
         <entry><literal>ISO88591</></entry>
 
771
        </row>
 
772
        <row>
 
773
         <entry><literal>LATIN2</literal></entry>
 
774
         <entry>ISO 8859-2, <acronym>ECMA</> 94</entry>
 
775
         <entry>Central European</entry>
 
776
         <entry>Yes</entry>
 
777
         <entry>1</entry>
 
778
         <entry><literal>ISO88592</></entry>
 
779
        </row>
 
780
        <row>
 
781
         <entry><literal>LATIN3</literal></entry>
 
782
         <entry>ISO 8859-3, <acronym>ECMA</> 94</entry>
 
783
         <entry>South European</entry>
 
784
         <entry>Yes</entry>
 
785
         <entry>1</entry>
 
786
         <entry><literal>ISO88593</></entry>
 
787
        </row>
 
788
        <row>
 
789
         <entry><literal>LATIN4</literal></entry>
 
790
         <entry>ISO 8859-4, <acronym>ECMA</> 94</entry>
 
791
         <entry>North European</entry>
 
792
         <entry>Yes</entry>
 
793
         <entry>1</entry>
 
794
         <entry><literal>ISO88594</></entry>
 
795
        </row>
 
796
        <row>
 
797
         <entry><literal>LATIN5</literal></entry>
 
798
         <entry>ISO 8859-9, <acronym>ECMA</> 128</entry>
 
799
         <entry>Turkish</entry>
 
800
         <entry>Yes</entry>
 
801
         <entry>1</entry>
 
802
         <entry><literal>ISO88599</></entry>
 
803
        </row>
 
804
        <row>
 
805
         <entry><literal>LATIN6</literal></entry>
 
806
         <entry>ISO 8859-10, <acronym>ECMA</> 144</entry>
 
807
         <entry>Nordic</entry>
 
808
         <entry>Yes</entry>
 
809
         <entry>1</entry>
 
810
         <entry><literal>ISO885910</></entry>
 
811
        </row>
 
812
        <row>
 
813
         <entry><literal>LATIN7</literal></entry>
 
814
         <entry>ISO 8859-13</entry>
 
815
         <entry>Baltic</entry>
 
816
         <entry>Yes</entry>
 
817
         <entry>1</entry>
 
818
         <entry><literal>ISO885913</></entry>
 
819
        </row>
 
820
        <row>
 
821
         <entry><literal>LATIN8</literal></entry>
 
822
         <entry>ISO 8859-14</entry>
 
823
         <entry>Celtic</entry>
 
824
         <entry>Yes</entry>
 
825
         <entry>1</entry>
 
826
         <entry><literal>ISO885914</></entry>
 
827
        </row>
 
828
        <row>
 
829
         <entry><literal>LATIN9</literal></entry>
 
830
         <entry>ISO 8859-15</entry>
 
831
         <entry>LATIN1 with Euro and accents</entry>
 
832
         <entry>Yes</entry>
 
833
         <entry>1</entry>
 
834
         <entry><literal>ISO885915</></entry>
 
835
        </row>
 
836
        <row>
 
837
         <entry><literal>LATIN10</literal></entry>
 
838
         <entry>ISO 8859-16, <acronym>ASRO</> SR 14111</entry>
 
839
         <entry>Romanian</entry>
 
840
         <entry>Yes</entry>
 
841
         <entry>1</entry>
 
842
         <entry><literal>ISO885916</></entry>
 
843
        </row>
 
844
        <row>
 
845
         <entry><literal>MULE_INTERNAL</literal></entry>
 
846
         <entry>Mule internal code</entry>
 
847
         <entry>Multilingual Emacs</entry>
 
848
         <entry>Yes</entry>
 
849
         <entry>1-4</entry>
 
850
         <entry></entry>
 
851
        </row>
 
852
        <row>
 
853
         <entry><literal>SJIS</literal></entry>
 
854
         <entry>Shift JIS</entry>
 
855
         <entry>Japanese</entry>
 
856
         <entry>No</entry>
 
857
         <entry>1-2</entry>
 
858
         <entry><literal>Mskanji</>, <literal>ShiftJIS</>, <literal>WIN932</>, <literal>Windows932</></entry>
 
859
        </row>
 
860
        <row>
 
861
         <entry><literal>SHIFT_JIS_2004</literal></entry>
 
862
         <entry>Shift JIS, JIS X 0213</entry>
 
863
         <entry>Japanese</entry>
 
864
         <entry>No</entry>
 
865
         <entry>1-2</entry>
 
866
         <entry></entry>
 
867
        </row>
 
868
        <row>
 
869
         <entry><literal>SQL_ASCII</literal></entry>
 
870
         <entry>unspecified (see text)</entry>
 
871
         <entry><emphasis>any</></entry>
 
872
         <entry>Yes</entry>
 
873
         <entry>1</entry>
 
874
         <entry></entry>
 
875
        </row>
 
876
        <row>
 
877
         <entry><literal>UHC</literal></entry>
 
878
         <entry>Unified Hangul Code</entry>
 
879
         <entry>Korean</entry>
 
880
         <entry>No</entry>
 
881
         <entry>1-2</entry>
 
882
         <entry><literal>WIN949</>, <literal>Windows949</></entry>
 
883
        </row>
 
884
        <row>
 
885
         <entry><literal>UTF8</literal></entry>
 
886
         <entry>Unicode, 8-bit</entry>
 
887
         <entry><emphasis>all</></entry>
 
888
         <entry>Yes</entry>
 
889
         <entry>1-4</entry>
 
890
         <entry><literal>Unicode</></entry>
 
891
        </row>
 
892
        <row>
 
893
         <entry><literal>WIN866</literal></entry>
 
894
         <entry>Windows CP866</entry>
 
895
         <entry>Cyrillic</entry>
 
896
         <entry>Yes</entry>
 
897
         <entry>1</entry>
 
898
         <entry><literal>ALT</></entry>
 
899
        </row>
 
900
        <row>
 
901
         <entry><literal>WIN874</literal></entry>
 
902
         <entry>Windows CP874</entry>
 
903
         <entry>Thai</entry>
 
904
         <entry>Yes</entry>
 
905
         <entry>1</entry>
 
906
         <entry></entry>
 
907
        </row>
 
908
        <row>
 
909
         <entry><literal>WIN1250</literal></entry>
 
910
         <entry>Windows CP1250</entry>
 
911
         <entry>Central European</entry>
 
912
         <entry>Yes</entry>
 
913
         <entry>1</entry>
 
914
         <entry></entry>
 
915
        </row>
 
916
        <row>
 
917
         <entry><literal>WIN1251</literal></entry>
 
918
         <entry>Windows CP1251</entry>
 
919
         <entry>Cyrillic</entry>
 
920
         <entry>Yes</entry>
 
921
         <entry>1</entry>
 
922
         <entry><literal>WIN</></entry>
 
923
        </row>
 
924
        <row>
 
925
         <entry><literal>WIN1252</literal></entry>
 
926
         <entry>Windows CP1252</entry>
 
927
         <entry>Western European</entry>
 
928
         <entry>Yes</entry>
 
929
         <entry>1</entry>
 
930
         <entry></entry>
 
931
        </row>
 
932
        <row>
 
933
         <entry><literal>WIN1253</literal></entry>
 
934
         <entry>Windows CP1253</entry>
 
935
         <entry>Greek</entry>
 
936
         <entry>Yes</entry>
 
937
         <entry>1</entry>
 
938
         <entry></entry>
 
939
        </row>
 
940
        <row>
 
941
         <entry><literal>WIN1254</literal></entry>
 
942
         <entry>Windows CP1254</entry>
 
943
         <entry>Turkish</entry>
 
944
         <entry>Yes</entry>
 
945
         <entry>1</entry>
 
946
         <entry></entry>
 
947
        </row>
 
948
        <row>
 
949
         <entry><literal>WIN1255</literal></entry>
 
950
         <entry>Windows CP1255</entry>
 
951
         <entry>Hebrew</entry>
 
952
         <entry>Yes</entry>
 
953
         <entry>1</entry>
 
954
         <entry></entry>
 
955
        </row>
 
956
        <row>
 
957
         <entry><literal>WIN1256</literal></entry>
 
958
         <entry>Windows CP1256</entry>
 
959
         <entry>Arabic</entry>
 
960
         <entry>Yes</entry>
 
961
         <entry>1</entry>
 
962
         <entry></entry>
 
963
        </row>
 
964
        <row>
 
965
         <entry><literal>WIN1257</literal></entry>
 
966
         <entry>Windows CP1257</entry>
 
967
         <entry>Baltic</entry>
 
968
         <entry>Yes</entry>
 
969
         <entry>1</entry>
 
970
         <entry></entry>
 
971
        </row>
 
972
        <row>
 
973
         <entry><literal>WIN1258</literal></entry>
 
974
         <entry>Windows CP1258</entry>
 
975
         <entry>Vietnamese</entry>
 
976
         <entry>Yes</entry>
 
977
         <entry>1</entry>
 
978
         <entry><literal>ABC</>, <literal>TCVN</>, <literal>TCVN5712</>, <literal>VSCII</></entry>
 
979
        </row>
 
980
       </tbody>
 
981
      </tgroup>
 
982
     </table>
 
983
 
 
984
     <para>
 
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</>.
 
989
     </para>
 
990
 
 
991
     <para>
 
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.
 
1004
     </para>
 
1005
    </sect2>
 
1006
 
 
1007
   <sect2>
 
1008
    <title>Setting the Character Set</title>
 
1009
 
 
1010
    <para>
 
1011
     <command>initdb</> defines the default character set (encoding)
 
1012
     for a <productname>PostgreSQL</productname> cluster. For example,
 
1013
 
 
1014
<screen>
 
1015
initdb -E EUC_JP
 
1016
</screen>
 
1017
 
 
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.
 
1025
    </para>
 
1026
 
 
1027
    <para>
 
1028
     You can specify a non-default encoding at database creation time,
 
1029
     provided that the encoding is compatible with the selected locale:
 
1030
 
 
1031
<screen>
 
1032
createdb -E EUC_KR -T template0 --lc-collate=ko_KR.euckr --lc-ctype=ko_KR.euckr korean
 
1033
</screen>
 
1034
 
 
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:
 
1038
 
 
1039
<programlisting>
 
1040
CREATE DATABASE korean WITH ENCODING 'EUC_KR' LC_COLLATE='ko_KR.euckr' LC_CTYPE='ko_KR.euckr' TEMPLATE=template0;
 
1041
</programlisting>
 
1042
 
 
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">.
 
1048
    </para>
 
1049
 
 
1050
    <para>
 
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.
 
1055
 
 
1056
<screen>
 
1057
$ <userinput>psql -l</userinput>
 
1058
                                         List of databases
 
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}
 
1068
(7 rows)
 
1069
</screen>
 
1070
    </para>
 
1071
 
 
1072
    <important>
 
1073
     <para>
 
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.
 
1081
     </para>
 
1082
 
 
1083
     <para>
 
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.
 
1091
     </para>
 
1092
    </important>
 
1093
   </sect2>
 
1094
 
 
1095
   <sect2>
 
1096
    <title>Automatic Character Set Conversion Between Server and Client</title>
 
1097
 
 
1098
    <para>
 
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>.
 
1106
    </para>
 
1107
 
 
1108
     <table id="multibyte-translation-table">
 
1109
      <title>Client/Server Character Set Conversions</title>
 
1110
      <tgroup cols="2">
 
1111
       <thead>
 
1112
        <row>
 
1113
         <entry>Server Character Set</entry>
 
1114
         <entry>Available Client Character Sets</entry>
 
1115
        </row>
 
1116
       </thead>
 
1117
       <tbody>
 
1118
        <row>
 
1119
         <entry><literal>BIG5</literal></entry>
 
1120
         <entry><emphasis>not supported as a server encoding</emphasis>
 
1121
         </entry>
 
1122
        </row>
 
1123
        <row>
 
1124
         <entry><literal>EUC_CN</literal></entry>
 
1125
         <entry><emphasis>EUC_CN</emphasis>,
 
1126
         <literal>MULE_INTERNAL</literal>,
 
1127
         <literal>UTF8</literal>
 
1128
         </entry>
 
1129
        </row>
 
1130
        <row>
 
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>
 
1136
         </entry>
 
1137
        </row>
 
1138
        <row>
 
1139
         <entry><literal>EUC_KR</literal></entry>
 
1140
         <entry><emphasis>EUC_KR</emphasis>,
 
1141
         <literal>MULE_INTERNAL</literal>,
 
1142
         <literal>UTF8</literal>
 
1143
         </entry>
 
1144
        </row>
 
1145
        <row>
 
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>
 
1151
         </entry>
 
1152
        </row>
 
1153
        <row>
 
1154
         <entry><literal>GB18030</literal></entry>
 
1155
         <entry><emphasis>not supported as a server encoding</emphasis>
 
1156
         </entry>
 
1157
        </row>
 
1158
        <row>
 
1159
         <entry><literal>GBK</literal></entry>
 
1160
         <entry><emphasis>not supported as a server encoding</emphasis>
 
1161
         </entry>
 
1162
        </row>
 
1163
        <row>
 
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>
 
1171
         </entry>
 
1172
        </row>
 
1173
        <row>
 
1174
         <entry><literal>ISO_8859_6</literal></entry>
 
1175
         <entry><emphasis>ISO_8859_6</emphasis>,
 
1176
         <literal>UTF8</literal>
 
1177
         </entry>
 
1178
        </row>
 
1179
        <row>
 
1180
         <entry><literal>ISO_8859_7</literal></entry>
 
1181
         <entry><emphasis>ISO_8859_7</emphasis>,
 
1182
         <literal>UTF8</literal>
 
1183
         </entry>
 
1184
        </row>
 
1185
        <row>
 
1186
         <entry><literal>ISO_8859_8</literal></entry>
 
1187
         <entry><emphasis>ISO_8859_8</emphasis>,
 
1188
         <literal>UTF8</literal>
 
1189
         </entry>
 
1190
        </row>
 
1191
        <row>
 
1192
         <entry><literal>JOHAB</literal></entry>
 
1193
         <entry><emphasis>JOHAB</emphasis>,
 
1194
         <literal>UTF8</literal>
 
1195
         </entry>
 
1196
        </row>
 
1197
        <row>
 
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>
 
1205
         </entry>
 
1206
        </row>
 
1207
        <row>
 
1208
         <entry><literal>KOI8U</literal></entry>
 
1209
         <entry><emphasis>KOI8U</emphasis>,
 
1210
         <literal>UTF8</literal>
 
1211
         </entry>
 
1212
        </row>
 
1213
        <row>
 
1214
         <entry><literal>LATIN1</literal></entry>
 
1215
         <entry><emphasis>LATIN1</emphasis>,
 
1216
         <literal>MULE_INTERNAL</literal>,
 
1217
         <literal>UTF8</literal>
 
1218
         </entry>
 
1219
        </row>
 
1220
        <row>
 
1221
         <entry><literal>LATIN2</literal></entry>
 
1222
         <entry><emphasis>LATIN2</emphasis>,
 
1223
         <literal>MULE_INTERNAL</literal>,
 
1224
         <literal>UTF8</literal>,
 
1225
         <literal>WIN1250</literal>
 
1226
         </entry>
 
1227
        </row>
 
1228
        <row>
 
1229
         <entry><literal>LATIN3</literal></entry>
 
1230
         <entry><emphasis>LATIN3</emphasis>,
 
1231
         <literal>MULE_INTERNAL</literal>,
 
1232
         <literal>UTF8</literal>
 
1233
         </entry>
 
1234
        </row>
 
1235
        <row>
 
1236
         <entry><literal>LATIN4</literal></entry>
 
1237
         <entry><emphasis>LATIN4</emphasis>,
 
1238
         <literal>MULE_INTERNAL</literal>,
 
1239
         <literal>UTF8</literal>
 
1240
         </entry>
 
1241
        </row>
 
1242
        <row>
 
1243
         <entry><literal>LATIN5</literal></entry>
 
1244
         <entry><emphasis>LATIN5</emphasis>,
 
1245
         <literal>UTF8</literal>
 
1246
         </entry>
 
1247
        </row>
 
1248
        <row>
 
1249
         <entry><literal>LATIN6</literal></entry>
 
1250
         <entry><emphasis>LATIN6</emphasis>,
 
1251
         <literal>UTF8</literal>
 
1252
         </entry>
 
1253
        </row>
 
1254
        <row>
 
1255
         <entry><literal>LATIN7</literal></entry>
 
1256
         <entry><emphasis>LATIN7</emphasis>,
 
1257
         <literal>UTF8</literal>
 
1258
         </entry>
 
1259
        </row>
 
1260
        <row>
 
1261
         <entry><literal>LATIN8</literal></entry>
 
1262
         <entry><emphasis>LATIN8</emphasis>,
 
1263
         <literal>UTF8</literal>
 
1264
         </entry>
 
1265
        </row>
 
1266
        <row>
 
1267
         <entry><literal>LATIN9</literal></entry>
 
1268
         <entry><emphasis>LATIN9</emphasis>,
 
1269
         <literal>UTF8</literal>
 
1270
         </entry>
 
1271
        </row>
 
1272
        <row>
 
1273
         <entry><literal>LATIN10</literal></entry>
 
1274
         <entry><emphasis>LATIN10</emphasis>,
 
1275
         <literal>UTF8</literal>
 
1276
         </entry>
 
1277
        </row>
 
1278
        <row>
 
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>
 
1293
         </entry>
 
1294
        </row>
 
1295
        <row>
 
1296
         <entry><literal>SJIS</literal></entry>
 
1297
         <entry><emphasis>not supported as a server encoding</emphasis>
 
1298
         </entry>
 
1299
        </row>
 
1300
        <row>
 
1301
         <entry><literal>SQL_ASCII</literal></entry>
 
1302
         <entry><emphasis>any (no conversion will be performed)</emphasis>
 
1303
         </entry>
 
1304
        </row>
 
1305
        <row>
 
1306
         <entry><literal>UHC</literal></entry>
 
1307
         <entry><emphasis>not supported as a server encoding</emphasis>
 
1308
         </entry>
 
1309
        </row>
 
1310
        <row>
 
1311
         <entry><literal>UTF8</literal></entry>
 
1312
         <entry><emphasis>all supported encodings</emphasis>
 
1313
         </entry>
 
1314
        </row>
 
1315
        <row>
 
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>
 
1323
         </entry>
 
1324
        </row>
 
1325
        <row>
 
1326
         <entry><literal>WIN874</literal></entry>
 
1327
         <entry><emphasis>WIN874</emphasis>,
 
1328
         <literal>UTF8</literal>
 
1329
         </entry>
 
1330
        </row>
 
1331
        <row>
 
1332
         <entry><literal>WIN1250</literal></entry>
 
1333
         <entry><emphasis>WIN1250</emphasis>,
 
1334
          <literal>LATIN2</literal>,
 
1335
          <literal>MULE_INTERNAL</literal>,
 
1336
          <literal>UTF8</literal>
 
1337
         </entry>
 
1338
        </row>
 
1339
        <row>
 
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>
 
1347
         </entry>
 
1348
        </row>
 
1349
        <row>
 
1350
         <entry><literal>WIN1252</literal></entry>
 
1351
         <entry><emphasis>WIN1252</emphasis>,
 
1352
          <literal>UTF8</literal>
 
1353
         </entry>
 
1354
        </row>
 
1355
        <row>
 
1356
         <entry><literal>WIN1253</literal></entry>
 
1357
         <entry><emphasis>WIN1253</emphasis>,
 
1358
          <literal>UTF8</literal>
 
1359
         </entry>
 
1360
        </row>
 
1361
        <row>
 
1362
         <entry><literal>WIN1254</literal></entry>
 
1363
         <entry><emphasis>WIN1254</emphasis>,
 
1364
          <literal>UTF8</literal>
 
1365
         </entry>
 
1366
        </row>
 
1367
        <row>
 
1368
         <entry><literal>WIN1255</literal></entry>
 
1369
         <entry><emphasis>WIN1255</emphasis>,
 
1370
          <literal>UTF8</literal>
 
1371
         </entry>
 
1372
        </row>
 
1373
        <row>
 
1374
         <entry><literal>WIN1256</literal></entry>
 
1375
         <entry><emphasis>WIN1256</emphasis>,
 
1376
         <literal>UTF8</literal>
 
1377
         </entry>
 
1378
        </row>
 
1379
        <row>
 
1380
         <entry><literal>WIN1257</literal></entry>
 
1381
         <entry><emphasis>WIN1257</emphasis>,
 
1382
          <literal>UTF8</literal>
 
1383
         </entry>
 
1384
        </row>
 
1385
        <row>
 
1386
         <entry><literal>WIN1258</literal></entry>
 
1387
         <entry><emphasis>WIN1258</emphasis>,
 
1388
         <literal>UTF8</literal>
 
1389
         </entry>
 
1390
        </row>
 
1391
       </tbody>
 
1392
      </tgroup>
 
1393
     </table>
 
1394
 
 
1395
    <para>
 
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:
 
1400
 
 
1401
     <itemizedlist>
 
1402
      <listitem>
 
1403
       <para>
 
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:
 
1409
 
 
1410
<programlisting>
 
1411
\encoding SJIS
 
1412
</programlisting>
 
1413
       </para>
 
1414
      </listitem>
 
1415
 
 
1416
      <listitem>
 
1417
       <para>
 
1418
        <application>libpq</> (<xref linkend="libpq-control">) has functions to control the client encoding.
 
1419
       </para>
 
1420
      </listitem>
 
1421
 
 
1422
      <listitem>
 
1423
       <para>
 
1424
        Using <command>SET client_encoding TO</command>.
 
1425
 
 
1426
        Setting the client encoding can be done with this SQL command:
 
1427
 
 
1428
<programlisting>
 
1429
SET CLIENT_ENCODING TO '<replaceable>value</>';
 
1430
</programlisting>
 
1431
 
 
1432
        Also you can use the standard SQL syntax <literal>SET NAMES</literal>
 
1433
        for this purpose:
 
1434
 
 
1435
<programlisting>
 
1436
SET NAMES '<replaceable>value</>';
 
1437
</programlisting>
 
1438
 
 
1439
        To query the current client encoding:
 
1440
 
 
1441
<programlisting>
 
1442
SHOW client_encoding;
 
1443
</programlisting>
 
1444
 
 
1445
        To return to the default encoding:
 
1446
 
 
1447
<programlisting>
 
1448
RESET client_encoding;
 
1449
</programlisting>
 
1450
       </para>
 
1451
      </listitem>
 
1452
 
 
1453
      <listitem>
 
1454
       <para>
 
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
 
1460
        mentioned above.)
 
1461
       </para>
 
1462
      </listitem>
 
1463
 
 
1464
      <listitem>
 
1465
      <para>
 
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.)
 
1472
       </para>
 
1473
      </listitem>
 
1474
 
 
1475
     </itemizedlist>
 
1476
    </para>
 
1477
 
 
1478
    <para>
 
1479
     If the conversion of a particular character is not possible
 
1480
     &mdash; 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> &mdash; an error is reported.
 
1484
    </para>
 
1485
 
 
1486
    <para>
 
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.
 
1491
    </para>
 
1492
   </sect2>
 
1493
 
 
1494
   <sect2>
 
1495
    <title>Further Reading</title>
 
1496
 
 
1497
    <para>
 
1498
     These are good sources to start learning about various kinds of encoding
 
1499
     systems.
 
1500
 
 
1501
     <variablelist>
 
1502
      <varlistentry>
 
1503
       <term><ulink url="http://www.i18ngurus.com/docs/984813247.html"></ulink></term>
 
1504
 
 
1505
       <listitem>
 
1506
        <para>
 
1507
         An extensive collection of documents about character sets, encodings,
 
1508
         and code pages.
 
1509
        </para>
 
1510
       </listitem>
 
1511
      </varlistentry>
 
1512
 
 
1513
      <varlistentry>
 
1514
       <term><citetitle>CJKV Information Processing: Chinese, Japanese, Korean &amp; Vietnamese Computing</citetitle></term>
 
1515
 
 
1516
       <listitem>
 
1517
        <para>
 
1518
         Contains detailed explanations of <literal>EUC_JP</literal>,
 
1519
         <literal>EUC_CN</literal>, <literal>EUC_KR</literal>,
 
1520
         <literal>EUC_TW</literal>.
 
1521
        </para>
 
1522
       </listitem>
 
1523
      </varlistentry>
 
1524
 
 
1525
      <varlistentry>
 
1526
       <term><ulink url="http://www.unicode.org/"></ulink></term>
 
1527
 
 
1528
       <listitem>
 
1529
        <para>
 
1530
         The web site of the Unicode Consortium.
 
1531
        </para>
 
1532
       </listitem>
 
1533
      </varlistentry>
 
1534
 
 
1535
      <varlistentry>
 
1536
       <term>RFC 3629</term>
 
1537
 
 
1538
       <listitem>
 
1539
        <para>
 
1540
         <acronym>UTF</acronym>-8 (8-bit UCS/Unicode Transformation
 
1541
         Format) is defined here.
 
1542
        </para>
 
1543
       </listitem>
 
1544
      </varlistentry>
 
1545
     </variablelist>
 
1546
    </para>
 
1547
   </sect2>
 
1548
 
 
1549
  </sect1>
 
1550
 
 
1551
</chapter>