<!-- $PostgreSQL: pgsql/doc/src/sgml/charset.sgml,v 2.95 2009/05/18 08:59:28 petere Exp $ -->

<chapter id="charset">
 <title>Localization</>

 <para>
  This chapter describes the available localization features from the
  point of view of the administrator.
  <productname>PostgreSQL</productname> supports localization with
  two approaches:

   <itemizedlist>
    <listitem>
     <para>
      Using the locale features of the operating system to provide
      locale-specific collation order, number formatting, translated
      messages, and other aspects.
     </para>
    </listitem>

    <listitem>
     <para>
      Providing a number of different character sets to support storing text
      in all kinds of languages, and providing character set translation
      between client and server.
     </para>
    </listitem>
   </itemizedlist>
  </para>


 <sect1 id="locale">
  <title>Locale Support</title>

  <indexterm zone="locale"><primary>locale</></>

  <para>
   <firstterm>Locale</> support refers to an application respecting
   cultural preferences regarding alphabets, sorting, number
   formatting, etc.  <productname>PostgreSQL</> uses the standard ISO
   C and <acronym>POSIX</acronym> locale facilities provided by the server operating
   system.  For additional information refer to the documentation of your
   system.
  </para>

  <sect2>
   <title>Overview</>

   <para>
    Locale support is automatically initialized when a database
    cluster is created using <command>initdb</command>.
    <command>initdb</command> will initialize the database cluster
    with the locale setting of its execution environment by default,
    so if your system is already set to use the locale that you want
    in your database cluster then there is nothing else you need to
    do.  If you want to use a different locale (or you are not sure
    which locale your system is set to), you can instruct
    <command>initdb</command> exactly which locale to use by
    specifying the <option>--locale</option> option. For example:
<screen>
initdb --locale=sv_SE
</screen>
   </para>

   <para>
    This example for Unix systems sets the locale to Swedish
    (<literal>sv</>) as spoken
    in Sweden (<literal>SE</>).  Other possibilities might be
    <literal>en_US</> (U.S. English) and <literal>fr_CA</> (French
    Canadian).  If more than one character set can be useful for a
    locale then the specifications look like this:
    <literal>cs_CZ.ISO8859-2</>. What locales are available under what
    names on your system depends on what was provided by the operating
    system vendor and what was installed.  On most Unix systems, the command
    <literal>locale -a</> will provide a list of available locales.
    Windows uses more verbose locale names, such as <literal>German_Germany</>
    or <literal>Swedish_Sweden.1252</>, but the principles are the same.
   </para>

   <para>
    Occasionally it is useful to mix rules from several locales, e.g.,
    use English collation rules but Spanish messages.  To support that, a
    set of locale subcategories exist that control only a certain
    aspect of the localization rules:

    <informaltable>
     <tgroup cols="2">
      <tbody>
       <row>
        <entry><envar>LC_COLLATE</></>
        <entry>String sort order</>
       </row>
       <row>
        <entry><envar>LC_CTYPE</></>
        <entry>Character classification (What is a letter? Its upper-case equivalent?)</>
       </row>
       <row>
        <entry><envar>LC_MESSAGES</></>
        <entry>Language of messages</>
       </row>
       <row>
        <entry><envar>LC_MONETARY</></>
        <entry>Formatting of currency amounts</>
       </row>
       <row>
        <entry><envar>LC_NUMERIC</></>
        <entry>Formatting of numbers</>
       </row>
       <row>
        <entry><envar>LC_TIME</></>
        <entry>Formatting of dates and times</>
       </row>
      </tbody>
     </tgroup>
    </informaltable>

    The category names translate into names of
    <command>initdb</command> options to override the locale choice
    for a specific category.  For instance, to set the locale to
    French Canadian, but use U.S. rules for formatting currency, use
    <literal>initdb --locale=fr_CA --lc-monetary=en_US</literal>.
   </para>

   <para>
    If you want the system to behave as if it had no locale support,
    use the special locale <literal>C</> or <literal>POSIX</>.
   </para>

   <para>
    The nature of some locale categories is that their value has to be
    fixed when the database is created.  You can use different settings
    for different databases, but once a database is created, you cannot
    change them for that database anymore. <literal>LC_COLLATE</literal>
    and <literal>LC_CTYPE</literal> are these categories.  They affect
    the sort order of indexes, so they must be kept fixed, or indexes on
    text columns will become corrupt.  The default values for these
    categories are determined when <command>initdb</command> is run, and
    those values are used when new databases are created, unless
    specified otherwise in the <command>CREATE DATABASE</command> command.
   </para>

   <para>
    The other locale categories can be changed whenever desired
    by setting the server configuration parameters
    that have the same name as the locale categories (see <xref
    linkend="runtime-config-client-format"> for details).  The values
    that are chosen by <command>initdb</command> are actually only written
    into the configuration file <filename>postgresql.conf</filename> to
    serve as defaults when the server is started.  If you delete these
    assignments from <filename>postgresql.conf</filename> then the
    server will inherit the settings from its execution environment.
   </para>

   <para>
    Note that the locale behavior of the server is determined by the
    environment variables seen by the server, not by the environment
    of any client.  Therefore, be careful to configure the correct locale settings
    before starting the server.  A consequence of this is that if
    client and server are set up in different locales, messages might
    appear in different languages depending on where they originated.
   </para>

   <note>
    <para>
     When we speak of inheriting the locale from the execution
     environment, this means the following on most operating systems:
     For a given locale category, say the collation, the following
     environment variables are consulted in this order until one is
     found to be set: <envar>LC_ALL</envar>, <envar>LC_COLLATE</envar>
     (or the variable corresponding to the respective category),
     <envar>LANG</envar>.  If none of these environment variables are
     set then the locale defaults to <literal>C</literal>.
    </para>

    <para>
     Some message localization libraries also look at the environment
     variable <envar>LANGUAGE</envar> which overrides all other locale
     settings for the purpose of setting the language of messages.  If
     in doubt, please refer to the documentation of your operating
     system, in particular the documentation about
     <application>gettext</>, for more information.
    </para>
   </note>

   <para>
    To enable messages to be translated to the user's preferred language,
    <acronym>NLS</acronym> must have been selected at build time
    (<literal>configure --enable-nls</>).  All other locale support is
    built in automatically.
   </para>
  </sect2>

  <sect2>
   <title>Behavior</>

   <para>
    The locale settings influence the following SQL features:

    <itemizedlist>
     <listitem>
      <para>
       Sort order in queries using <literal>ORDER BY</> or the standard
       comparison operators on textual data
       <indexterm><primary>ORDER BY</><secondary>and locales</></indexterm>
      </para>
     </listitem>

     <listitem>
      <para>
       The ability to use indexes with <literal>LIKE</> clauses
       <indexterm><primary>LIKE</><secondary>and locales</></indexterm>
      </para>
     </listitem>

     <listitem>
      <para>
       The <function>upper</>,  <function>lower</>,  and <function>initcap</>
       functions
       <indexterm><primary>upper</><secondary>and locales</></indexterm>
       <indexterm><primary>lower</><secondary>and locales</></indexterm>
      </para>
     </listitem>

     <listitem>
      <para>
       The <function>to_char</> family of functions
       <indexterm><primary>to_char</><secondary>and locales</></indexterm>
      </para>
     </listitem>
    </itemizedlist>
   </para>

   <para>
    The drawback of using locales other than <literal>C</> or
    <literal>POSIX</> in <productname>PostgreSQL</> is its performance
    impact. It slows character handling and prevents ordinary indexes
    from being used by <literal>LIKE</>. For this reason use locales
    only if you actually need them.
   </para>

   <para>
    As a workaround to allow <productname>PostgreSQL</> to use indexes
    with <literal>LIKE</> clauses under a non-C locale, several custom
    operator classes exist. These allow the creation of an index that
    performs a strict character-by-character comparison, ignoring
    locale comparison rules. Refer to <xref linkend="indexes-opclass">
    for more information.
   </para>
  </sect2>

  <sect2>
   <title>Problems</>

   <para>
    If locale support doesn't work according to the explanation above,
    check that the locale support in your operating system is
    correctly configured.  To check what locales are installed on your
    system, you can use the command <literal>locale -a</literal> if
    your operating system provides it.
   </para>

   <para>
    Check that <productname>PostgreSQL</> is actually using the locale
    that you think it is.  The <envar>LC_COLLATE</> and <envar>LC_CTYPE</>
    settings are determined when a database is created, and cannot be
    changed except by creating a new database.  Other locale
    settings including <envar>LC_MESSAGES</> and <envar>LC_MONETARY</>
    are initially determined by the environment the server is started
    in, but can be changed on-the-fly.  You can check the active locale
    settings using the <command>SHOW</> command.
   </para>

   <para>
    The directory <filename>src/test/locale</> in the source
    distribution contains a test suite for
    <productname>PostgreSQL</>'s locale support.
   </para>

   <para>
    Client applications that handle server-side errors by parsing the
    text of the error message will obviously have problems when the
    server's messages are in a different language.  Authors of such
    applications are advised to make use of the error code scheme
    instead.
   </para>

   <para>
    Maintaining catalogs of message translations requires the on-going
    efforts of many volunteers that want to see
    <productname>PostgreSQL</> speak their preferred language well.
    If messages in your language are currently not available or not fully
    translated, your assistance would be appreciated.  If you want to
    help, refer to <xref linkend="nls"> or write to the developers'
    mailing list.
   </para>
  </sect2>
 </sect1>


 <sect1 id="multibyte">
  <title>Character Set Support</title>

  <indexterm zone="multibyte"><primary>character set</></>

  <para>
   The character set support in <productname>PostgreSQL</productname>
   allows you to store text in a variety of character sets (also called
   encodings), including
   single-byte character sets such as the ISO 8859 series and
   multiple-byte character sets such as <acronym>EUC</> (Extended Unix
   Code), UTF-8, and Mule internal code.  All supported character sets
   can be used transparently by clients, but a few are not supported
   for use within the server (that is, as a server-side encoding).
   The default character set is selected while
   initializing your <productname>PostgreSQL</productname> database
   cluster using <command>initdb</>.  It can be overridden when you
   create a database, so you can have multiple
   databases each with a different character set.
  </para>

  <para>
   An important restriction, however, is that each database's character set
   must be compatible with the database's <envar>LC_CTYPE</> and
   <envar>LC_COLLATE</> locale settings. For <literal>C</> or
   <literal>POSIX</> locale, any character set is allowed, but for other
   locales there is only one character set that will work correctly.
   (On Windows, however, UTF-8 encoding can be used with any locale.)
  </para>

   <sect2 id="multibyte-charset-supported">
    <title>Supported Character Sets</title>

    <para>
     <xref linkend="charset-table"> shows the character sets available
     for use in <productname>PostgreSQL</productname>.
    </para>

     <table id="charset-table">
      <title><productname>PostgreSQL</productname> Character Sets</title>
      <tgroup cols="6">
       <thead>
        <row>
         <entry>Name</entry>
         <entry>Description</entry>
         <entry>Language</entry>
         <entry>Server?</entry>
         <!--
          The Bytes/Char field is populated by looking at the values returned
          by pg_wchar_table.mblen function for each encoding.
         -->
         <entry>Bytes/Char</entry>
         <entry>Aliases</entry>
        </row>
       </thead>
       <tbody>
        <row>
         <entry><literal>BIG5</literal></entry>
         <entry>Big Five</entry>
         <entry>Traditional Chinese</entry>
         <entry>No</entry>
         <entry>1-2</entry>
         <entry><literal>WIN950</>, <literal>Windows950</></entry>
        </row>
        <row>
         <entry><literal>EUC_CN</literal></entry>
         <entry>Extended UNIX Code-CN</entry>
         <entry>Simplified Chinese</entry>
         <entry>Yes</entry>
         <entry>1-3</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>EUC_JP</literal></entry>
         <entry>Extended UNIX Code-JP</entry>
         <entry>Japanese</entry>
         <entry>Yes</entry>
         <entry>1-3</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>EUC_JIS_2004</literal></entry>
         <entry>Extended UNIX Code-JP, JIS X 0213</entry>
         <entry>Japanese</entry>
         <entry>Yes</entry>
         <entry>1-3</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>EUC_KR</literal></entry>
         <entry>Extended UNIX Code-KR</entry>
         <entry>Korean</entry>
         <entry>Yes</entry>
         <entry>1-3</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>EUC_TW</literal></entry>
         <entry>Extended UNIX Code-TW</entry>
         <entry>Traditional Chinese, Taiwanese</entry>
         <entry>Yes</entry>
         <entry>1-3</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>GB18030</literal></entry>
         <entry>National Standard</entry>
         <entry>Chinese</entry>
         <entry>No</entry>
         <entry>1-2</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>GBK</literal></entry>
         <entry>Extended National Standard</entry>
         <entry>Simplified Chinese</entry>
         <entry>No</entry>
         <entry>1-2</entry>
         <entry><literal>WIN936</>, <literal>Windows936</></entry>
        </row>
        <row>
         <entry><literal>ISO_8859_5</literal></entry>
         <entry>ISO 8859-5, <acronym>ECMA</> 113</entry>
         <entry>Latin/Cyrillic</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>ISO_8859_6</literal></entry>
         <entry>ISO 8859-6, <acronym>ECMA</> 114</entry>
         <entry>Latin/Arabic</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>ISO_8859_7</literal></entry>
         <entry>ISO 8859-7, <acronym>ECMA</> 118</entry>
         <entry>Latin/Greek</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>ISO_8859_8</literal></entry>
         <entry>ISO 8859-8, <acronym>ECMA</> 121</entry>
         <entry>Latin/Hebrew</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>JOHAB</literal></entry>
         <entry><acronym>JOHAB</></entry>
         <entry>Korean (Hangul)</entry>
         <entry>No</entry>
         <entry>1-3</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>KOI8R</literal></entry>
         <entry><acronym>KOI</acronym>8-R</entry>
         <entry>Cyrillic (Russian)</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>KOI8</></entry>
        </row>
        <row>
         <entry><literal>KOI8U</literal></entry>
         <entry><acronym>KOI</acronym>8-U</entry>
         <entry>Cyrillic (Ukrainian)</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>LATIN1</literal></entry>
         <entry>ISO 8859-1, <acronym>ECMA</> 94</entry>
         <entry>Western European</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>ISO88591</></entry>
        </row>
        <row>
         <entry><literal>LATIN2</literal></entry>
         <entry>ISO 8859-2, <acronym>ECMA</> 94</entry>
         <entry>Central European</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>ISO88592</></entry>
        </row>
        <row>
         <entry><literal>LATIN3</literal></entry>
         <entry>ISO 8859-3, <acronym>ECMA</> 94</entry>
         <entry>South European</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>ISO88593</></entry>
        </row>
        <row>
         <entry><literal>LATIN4</literal></entry>
         <entry>ISO 8859-4, <acronym>ECMA</> 94</entry>
         <entry>North European</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>ISO88594</></entry>
        </row>
        <row>
         <entry><literal>LATIN5</literal></entry>
         <entry>ISO 8859-9, <acronym>ECMA</> 128</entry>
         <entry>Turkish</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>ISO88599</></entry>
        </row>
        <row>
         <entry><literal>LATIN6</literal></entry>
         <entry>ISO 8859-10, <acronym>ECMA</> 144</entry>
         <entry>Nordic</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>ISO885910</></entry>
        </row>
        <row>
         <entry><literal>LATIN7</literal></entry>
         <entry>ISO 8859-13</entry>
         <entry>Baltic</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>ISO885913</></entry>
        </row>
        <row>
         <entry><literal>LATIN8</literal></entry>
         <entry>ISO 8859-14</entry>
         <entry>Celtic</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>ISO885914</></entry>
        </row>
        <row>
         <entry><literal>LATIN9</literal></entry>
         <entry>ISO 8859-15</entry>
         <entry>LATIN1 with Euro and accents</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry>ISO885915</entry>
        </row>
        <row>
         <entry><literal>LATIN10</literal></entry>
         <entry>ISO 8859-16, <acronym>ASRO</> SR 14111</entry>
         <entry>Romanian</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>ISO885916</></entry>
        </row>
        <row>
         <entry><literal>MULE_INTERNAL</literal></entry>
         <entry>Mule internal code</entry>
         <entry>Multilingual Emacs</entry>
         <entry>Yes</entry>
         <entry>1-4</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>SJIS</literal></entry>
         <entry>Shift JIS</entry>
         <entry>Japanese</entry>
         <entry>No</entry>
         <entry>1-2</entry>
         <entry><literal>Mskanji</>, <literal>ShiftJIS</>, <literal>WIN932</>, <literal>Windows932</></entry>
        </row>
        <row>
         <entry><literal>SHIFT_JIS_2004</literal></entry>
         <entry>Shift JIS, JIS X 0213</entry>
         <entry>Japanese</entry>
         <entry>No</entry>
         <entry>1-2</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>SQL_ASCII</literal></entry>
         <entry>unspecified (see text)</entry>
         <entry><emphasis>any</></entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>UHC</literal></entry>
         <entry>Unified Hangul Code</entry>
         <entry>Korean</entry>
         <entry>No</entry>
         <entry>1-2</entry>
         <entry><literal>WIN949</>, <literal>Windows949</></entry>
        </row>
        <row>
         <entry><literal>UTF8</literal></entry>
         <entry>Unicode, 8-bit</entry>
         <entry><emphasis>all</></entry>
         <entry>Yes</entry>
         <entry>1-4</entry>
         <entry><literal>Unicode</></entry>
        </row>
        <row>
         <entry><literal>WIN866</literal></entry>
         <entry>Windows CP866</entry>
         <entry>Cyrillic</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>ALT</></entry>
        </row>
        <row>
         <entry><literal>WIN874</literal></entry>
         <entry>Windows CP874</entry>
         <entry>Thai</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>WIN1250</literal></entry>
         <entry>Windows CP1250</entry>
         <entry>Central European</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>WIN1251</literal></entry>
         <entry>Windows CP1251</entry>
         <entry>Cyrillic</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>WIN</></entry>
        </row>
        <row>
         <entry><literal>WIN1252</literal></entry>
         <entry>Windows CP1252</entry>
         <entry>Western European</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>WIN1253</literal></entry>
         <entry>Windows CP1253</entry>
         <entry>Greek</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>WIN1254</literal></entry>
         <entry>Windows CP1254</entry>
         <entry>Turkish</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>WIN1255</literal></entry>
         <entry>Windows CP1255</entry>
         <entry>Hebrew</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>WIN1256</literal></entry>
         <entry>Windows CP1256</entry>
         <entry>Arabic</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>WIN1257</literal></entry>
         <entry>Windows CP1257</entry>
         <entry>Baltic</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry></entry>
        </row>
        <row>
         <entry><literal>WIN1258</literal></entry>
         <entry>Windows CP1258</entry>
         <entry>Vietnamese</entry>
         <entry>Yes</entry>
         <entry>1</entry>
         <entry><literal>ABC</>, <literal>TCVN</>, <literal>TCVN5712</>, <literal>VSCII</></entry>
        </row>
       </tbody>
      </tgroup>
     </table>

     <para>
      Not all <acronym>API</>s support all the listed character sets. For example, the
      <productname>PostgreSQL</>
      JDBC driver does not support <literal>MULE_INTERNAL</>, <literal>LATIN6</>,
      <literal>LATIN8</>, and <literal>LATIN10</>.
     </para>

     <para>
      The <literal>SQL_ASCII</> setting behaves considerably differently
      from the other settings.  When the server character set is
      <literal>SQL_ASCII</>, the server interprets byte values 0-127
      according to the ASCII standard, while byte values 128-255 are taken
      as uninterpreted characters.  No encoding conversion will be done when
      the setting is <literal>SQL_ASCII</>.  Thus, this setting is not so
      much a declaration that a specific encoding is in use, as a declaration
      of ignorance about the encoding.  In most cases, if you are
      working with any non-ASCII data, it is unwise to use the
      <literal>SQL_ASCII</> setting, because
      <productname>PostgreSQL</productname> will be unable to help you by
      converting or validating non-ASCII characters.
     </para>
    </sect2>

   <sect2>
    <title>Setting the Character Set</title>

    <para>
     <command>initdb</> defines the default character set
     for a <productname>PostgreSQL</productname> cluster. For example,

<screen>
initdb -E EUC_JP
</screen>

     sets the default character set (encoding) to
     <literal>EUC_JP</literal> (Extended Unix Code for Japanese).  You
     can use <option>--encoding</option> instead of
     <option>-E</option> if you prefer to type longer option strings.
     If no <option>-E</> or <option>--encoding</option> option is
     given, <command>initdb</> attempts to determine the appropriate
     encoding to use based on the specified or default locale.
    </para>

    <para>
     You can specify a non-default encoding at database creation time,
     provided that the encoding is compatible with the selected locale:

<screen>
createdb -E EUC_KR -T template0 --lc-collate=ko_KR.euckr --lc-ctype=ko_KR.euckr korean
</screen>

     This will create a database named <literal>korean</literal> that
     uses the character set <literal>EUC_KR</literal>, and locale <literal>ko_KR</literal>.
     Another way to accomplish this is to use this SQL command:

<programlisting>
CREATE DATABASE korean WITH ENCODING 'EUC_KR' LC_COLLATE='ko_KR.euckr' LC_CTYPE='ko_KR.euckr' TEMPLATE=template0;
</programlisting>

     Notice that the above commands specify copying the <literal>template0</>
     database.  When copying any other database, the encoding and locale
     settings cannot be changed from those of the source database, because
     that might result in corrupt data.  For more information see
     <xref linkend="manage-ag-templatedbs">.
    </para>

    <para>
     The encoding for a database is stored in the system catalog
     <literal>pg_database</literal>.  You can see it by using the
     <option>-l</option> option or the <command>\l</command> command
     of <command>psql</command>.

<screen>
$ <userinput>psql -l</userinput>
                                         List of databases
   Name    |  Owner   | Encoding  |  Collation  |    Ctype    |          Access Privileges          
-----------+----------+-----------+-------------+-------------+-------------------------------------
 clocaledb | hlinnaka | SQL_ASCII | C           | C           | 
 englishdb | hlinnaka | UTF8      | en_GB.UTF8  | en_GB.UTF8  | 
 japanese  | hlinnaka | UTF8      | ja_JP.UTF8  | ja_JP.UTF8  | 
 korean    | hlinnaka | EUC_KR    | ko_KR.euckr | ko_KR.euckr | 
 postgres  | hlinnaka | UTF8      | fi_FI.UTF8  | fi_FI.UTF8  | 
 template0 | hlinnaka | UTF8      | fi_FI.UTF8  | fi_FI.UTF8  | {=c/hlinnaka,hlinnaka=CTc/hlinnaka}
 template1 | hlinnaka | UTF8      | fi_FI.UTF8  | fi_FI.UTF8  | {=c/hlinnaka,hlinnaka=CTc/hlinnaka}
(7 rows)
</screen>
    </para>

    <important>
     <para>
      On most modern operating systems, <productname>PostgreSQL</productname>
      can determine which character set is implied by an <envar>LC_CTYPE</>
      setting, and it will enforce that only the matching database encoding is
      used.  On older systems it is your responsibility to ensure that you use
      the encoding expected by the locale you have selected.  A mistake in
      this area is likely to lead to strange misbehavior of locale-dependent
      operations such as sorting.
     </para>

     <para>
      <productname>PostgreSQL</productname> will allow superusers to create
      databases with <literal>SQL_ASCII</> encoding even when
      <envar>LC_CTYPE</> is not <literal>C</> or <literal>POSIX</>.  As noted
      above, <literal>SQL_ASCII</> does not enforce that the data stored in
      the database has any particular encoding, and so this choice poses risks
      of locale-dependent misbehavior.  Using this combination of settings is
      deprecated and may someday be forbidden altogether.
     </para>
    </important>
   </sect2>

   <sect2>
    <title>Automatic Character Set Conversion Between Server and Client</title>

    <para>
     <productname>PostgreSQL</productname> supports automatic
     character set conversion between server and client for certain
     character set combinations. The conversion information is stored in the
     <literal>pg_conversion</> system catalog.  <productname>PostgreSQL</>
     comes with some predefined conversions, as shown in <xref
     linkend="multibyte-translation-table">. You can create a new
     conversion using the SQL command <command>CREATE CONVERSION</command>.
    </para>

     <table id="multibyte-translation-table">
      <title>Client/Server Character Set Conversions</title>
      <tgroup cols="2">
       <thead>
        <row>
         <entry>Server Character Set</entry>
         <entry>Available Client Character Sets</entry>
        </row>
       </thead>
       <tbody>
        <row>
         <entry><literal>BIG5</literal></entry>
         <entry><emphasis>not supported as a server encoding</emphasis>
         </entry>
        </row>
        <row>
         <entry><literal>EUC_CN</literal></entry>
         <entry><emphasis>EUC_CN</emphasis>,
         <literal>MULE_INTERNAL</literal>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>EUC_JP</literal></entry>
         <entry><emphasis>EUC_JP</emphasis>,
         <literal>MULE_INTERNAL</literal>,
         <literal>SJIS</literal>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>EUC_KR</literal></entry>
         <entry><emphasis>EUC_KR</emphasis>,
         <literal>MULE_INTERNAL</literal>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>EUC_TW</literal></entry>
         <entry><emphasis>EUC_TW</emphasis>,
         <literal>BIG5</literal>,
         <literal>MULE_INTERNAL</literal>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>GB18030</literal></entry>
         <entry><emphasis>not supported as a server encoding</emphasis>
         </entry>
        </row>
        <row>
         <entry><literal>GBK</literal></entry>
         <entry><emphasis>not supported as a server encoding</emphasis>
         </entry>
        </row>
        <row>
         <entry><literal>ISO_8859_5</literal></entry>
         <entry><emphasis>ISO_8859_5</emphasis>,
         <literal>KOI8R</literal>,
         <literal>MULE_INTERNAL</literal>,
         <literal>UTF8</literal>,
         <literal>WIN866</literal>,
         <literal>WIN1251</literal>
         </entry>
        </row>
        <row>
         <entry><literal>ISO_8859_6</literal></entry>
         <entry><emphasis>ISO_8859_6</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>ISO_8859_7</literal></entry>
         <entry><emphasis>ISO_8859_7</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>ISO_8859_8</literal></entry>
         <entry><emphasis>ISO_8859_8</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>JOHAB</literal></entry>
         <entry><emphasis>JOHAB</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>KOI8R</literal></entry>
         <entry><emphasis>KOI8R</emphasis>,
         <literal>ISO_8859_5</literal>,
         <literal>MULE_INTERNAL</literal>,
         <literal>UTF8</literal>,
         <literal>WIN866</literal>,
         <literal>WIN1251</literal>
         </entry>
        </row>
        <row>
         <entry><literal>KOI8U</literal></entry>
         <entry><emphasis>KOI8U</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>LATIN1</literal></entry>
         <entry><emphasis>LATIN1</emphasis>,
         <literal>MULE_INTERNAL</literal>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>LATIN2</literal></entry>
         <entry><emphasis>LATIN2</emphasis>,
         <literal>MULE_INTERNAL</literal>,
         <literal>UTF8</literal>,
         <literal>WIN1250</literal>
         </entry>
        </row>
        <row>
         <entry><literal>LATIN3</literal></entry>
         <entry><emphasis>LATIN3</emphasis>,
         <literal>MULE_INTERNAL</literal>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>LATIN4</literal></entry>
         <entry><emphasis>LATIN4</emphasis>,
         <literal>MULE_INTERNAL</literal>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>LATIN5</literal></entry>
         <entry><emphasis>LATIN5</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>LATIN6</literal></entry>
         <entry><emphasis>LATIN6</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>LATIN7</literal></entry>
         <entry><emphasis>LATIN7</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>LATIN8</literal></entry>
         <entry><emphasis>LATIN8</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>LATIN9</literal></entry>
         <entry><emphasis>LATIN9</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>LATIN10</literal></entry>
         <entry><emphasis>LATIN10</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>MULE_INTERNAL</literal></entry>
         <entry><emphasis>MULE_INTERNAL</emphasis>,
          <literal>BIG5</literal>,
          <literal>EUC_CN</literal>,
          <literal>EUC_JP</literal>,
          <literal>EUC_KR</literal>,
          <literal>EUC_TW</literal>,
          <literal>ISO_8859_5</literal>,
          <literal>KOI8R</literal>,
          <literal>LATIN1</literal> to <literal>LATIN4</literal>,
          <literal>SJIS</literal>,
          <literal>WIN866</literal>,
          <literal>WIN1250</literal>,
          <literal>WIN1251</literal>
         </entry>
        </row>
        <row>
         <entry><literal>SJIS</literal></entry>
         <entry><emphasis>not supported as a server encoding</emphasis>
         </entry>
        </row>
        <row>
         <entry><literal>SQL_ASCII</literal></entry>
         <entry><emphasis>any (no conversion will be performed)</emphasis>
         </entry>
        </row>
        <row>
         <entry><literal>UHC</literal></entry>
         <entry><emphasis>not supported as a server encoding</emphasis>
         </entry>
        </row>
        <row>
         <entry><literal>UTF8</literal></entry>
         <entry><emphasis>all supported encodings</emphasis>
         </entry>
        </row>
        <row>
         <entry><literal>WIN866</literal></entry>
         <entry><emphasis>WIN866</emphasis>,
          <literal>ISO_8859_5</literal>,
          <literal>KOI8R</literal>,
          <literal>MULE_INTERNAL</literal>,
          <literal>UTF8</literal>,
          <literal>WIN1251</literal>
         </entry>
        </row>
        <row>
         <entry><literal>WIN874</literal></entry>
         <entry><emphasis>WIN874</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>WIN1250</literal></entry>
         <entry><emphasis>WIN1250</emphasis>,
          <literal>LATIN2</literal>,
          <literal>MULE_INTERNAL</literal>,
          <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>WIN1251</literal></entry>
         <entry><emphasis>WIN1251</emphasis>,
          <literal>ISO_8859_5</literal>,
          <literal>KOI8R</literal>,
          <literal>MULE_INTERNAL</literal>,
          <literal>UTF8</literal>,
          <literal>WIN866</literal>
         </entry>
        </row>
        <row>
         <entry><literal>WIN1252</literal></entry>
         <entry><emphasis>WIN1252</emphasis>,
          <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>WIN1253</literal></entry>
         <entry><emphasis>WIN1253</emphasis>,
          <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>WIN1254</literal></entry>
         <entry><emphasis>WIN1254</emphasis>,
          <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>WIN1255</literal></entry>
         <entry><emphasis>WIN1255</emphasis>,
          <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>WIN1256</literal></entry>
         <entry><emphasis>WIN1256</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>WIN1257</literal></entry>
         <entry><emphasis>WIN1257</emphasis>,
          <literal>UTF8</literal>
         </entry>
        </row>
        <row>
         <entry><literal>WIN1258</literal></entry>
         <entry><emphasis>WIN1258</emphasis>,
         <literal>UTF8</literal>
         </entry>
        </row>
       </tbody>
      </tgroup>
     </table>

    <para>
     To enable automatic character set conversion, you have to
     tell <productname>PostgreSQL</productname> the character set
     (encoding) you would like to use in the client. There are several
     ways to accomplish this:

     <itemizedlist>
      <listitem>
       <para>
        Using the <command>\encoding</command> command in
        <application>psql</application>.
        <command>\encoding</command> allows you to change client
        encoding on the fly. For
        example, to change the encoding to <literal>SJIS</literal>, type:

<programlisting>
\encoding SJIS
</programlisting>
       </para>
      </listitem>

      <listitem>
       <para>
        <application>libpq</> (<xref linkend="libpq-control">) has functions to control the client encoding.
       </para>
      </listitem>

      <listitem>
       <para>
        Using <command>SET client_encoding TO</command>.

        Setting the client encoding can be done with this SQL command:

<programlisting>
SET CLIENT_ENCODING TO '<replaceable>value</>';
</programlisting>

        Also you can use the standard SQL syntax <literal>SET NAMES</literal>
        for this purpose:

<programlisting>
SET NAMES '<replaceable>value</>';
</programlisting>

        To query the current client encoding:

<programlisting>
SHOW client_encoding;
</programlisting>

        To return to the default encoding:

<programlisting>
RESET client_encoding;
</programlisting>
       </para>
      </listitem>

      <listitem>
       <para>
        Using <envar>PGCLIENTENCODING</envar>. If the environment variable
        <envar>PGCLIENTENCODING</envar> is defined in the client's
        environment, that client encoding is automatically selected
        when a connection to the server is made.  (This can
        subsequently be overridden using any of the other methods
        mentioned above.)
       </para>
      </listitem>

      <listitem>
      <para>
       Using the configuration variable <xref
       linkend="guc-client-encoding">. If the
       <varname>client_encoding</> variable is set, that client
       encoding is automatically selected when a connection to the
       server is made.  (This can subsequently be overridden using any
       of the other methods mentioned above.)
       </para>
      </listitem>

     </itemizedlist>
    </para>

    <para>
     If the conversion of a particular character is not possible
     &mdash; suppose you chose <literal>EUC_JP</literal> for the
     server and <literal>LATIN1</literal> for the client, then some
     Japanese characters do not have a representation in
     <literal>LATIN1</literal> &mdash; then an error is reported.
    </para>

    <para>
     If the client character set is defined as <literal>SQL_ASCII</>,
     encoding conversion is disabled, regardless of the server's character
     set.  Just as for the server, use of <literal>SQL_ASCII</> is unwise
     unless you are working with all-ASCII data.
    </para>
   </sect2>

   <sect2>
    <title>Further Reading</title>

    <para>
     These are good sources to start learning about various kinds of encoding
     systems.

     <variablelist>
      <varlistentry>
       <term><ulink url="http://www.i18ngurus.com/docs/984813247.html"></ulink></term>

       <listitem>
        <para>
         An extensive collection of documents about character sets, encodings,
         and code pages.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term><ulink url="ftp://ftp.ora.com/pub/examples/nutshell/ujip/doc/cjk.inf"></ulink></term>

       <listitem>
        <para>
         Detailed explanations of <literal>EUC_JP</literal>,
         <literal>EUC_CN</literal>, <literal>EUC_KR</literal>,
         <literal>EUC_TW</literal> appear in section 3.2.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term><ulink url="http://www.unicode.org/"></ulink></term>

       <listitem>
        <para>
         The web site of the Unicode Consortium.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>RFC 3629</term>

       <listitem>
        <para>
         <acronym>UTF</acronym>-8 is defined here.
        </para>
       </listitem>
      </varlistentry>
     </variablelist>
    </para>
   </sect2>

  </sect1>

</chapter>
