<!-- $Header: /cvsroot/pgsql/doc/src/sgml/charset.sgml,v 2.25 2002/07/24 05:51:56 ishii Exp $ -->

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

 <abstract>
  <para>
   Describes the available localization features from the point of
   view of the administrator.
  </para>
 </abstract>

  <para>
   <productname>PostgreSQL</productname> supports localization with
   three 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>
      Using explicit multiple-byte character sets defined in the
      <productname>PostgreSQL</productname> server to support languages
      that require more characters than will fit into a single byte,
      and to provide character set recoding between client and server.
      The number of supported character sets is fixed at the time the
      server is compiled, and internal operations such as string
      comparisons require expansion of each character into a 32-bit
      word.
     </para>
    </listitem>

    <listitem>
     <para>
      Single byte character recoding provides a more light-weight
      solution for users of multiple, yet single-byte character sets.
     </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>-like 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; 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 tell
    <command>initdb</command> exactly which locale you want with the
    option <option>--locale</option>.  For example:
<screen>
<prompt>$ </><userinput>initdb --locale=sv_SE</>
</screen>
   </para>

   <para>
    This example 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</> (Canada,
    French).  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.
   </para>

   <para>
    Occasionally it is useful to mix rules from several locales, e.g.,
    use U.S. 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? The 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 for the lifetime of a database cluster.  That is, once
    <command>initdb</command> has run, you cannot change them anymore.
    <literal>LC_COLLATE</literal> and <literal>LC_CTYPE</literal> are
    those categories.  They affect the sort order of indexes, so they
    must be kept fixed, or indexes on text columns will become corrupt.
    <productname>PostgreSQL</productname> enforces this by recording
    the values of <envar>LC_COLLATE</> and <envar>LC_CTYPE</> that are
    seen by <command>initdb</>.  The server automatically adopts
    those two values when it is started.
   </para>

   <para>
    The other locale categories can be changed as desired whenever the
    server is started by setting the run-time configuration variables
    that have the same name as the locale categories (see <xref
    linkend="runtime-config"> for details).  The defaults 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 the
    assignments from <filename>postgresql.conf</filename> then the
    server will inherit the settings from the 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 to different locales, messages may
    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>
     (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
     <citerefentry><refentrytitle>gettext</><manvolnum>3</></> manual
     page, for more information.
    </para>
   </note>

   <para>
    To enable messages translated to the user's preferred language,
    the <option>--enable-nls</option> option must be used.  This
    option is independent of the other locale support.
   </para>
  </sect2>

  <sect2>
   <title>Benefits</>

   <para>
    Locale support influences in particular the following features:

    <itemizedlist>
     <listitem>
      <para>
       Sort order in <command>ORDER BY</> queries.
       <indexterm><primary>ORDER BY</></>
      </para>
     </listitem>

     <listitem>
      <para>
       The <function>to_char</> family of functions
      </para>
     </listitem>

     <listitem>
      <para>
       The <literal>LIKE</> and <literal>~</> operators for pattern
       matching
      </para>
     </listitem>
    </itemizedlist>
   </para>

   <para>
    The only severe drawback of using the locale support in
    <productname>PostgreSQL</> is its speed.  So use locale only if you
    actually need it.  It should be noted in particular that selecting
    a non-C locale disables index optimizations for <literal>LIKE</> and
    <literal>~</> operators, which can make a huge difference in the
    speed of searches that use those operators.
   </para>
  </sect2>

  <sect2>
   <title>Problems</>

   <para>
    If locale support doesn't work in spite of the explanation above,
    check that the locale support in your operating system is correctly configured.
    To check whether a given locale is installed and functional you
    can use <application>Perl</>, for example.  Perl has also support
    for locales and if a locale is broken <command>perl -v</> will
    complain something like this:
<screen>
<prompt>$</> <userinput>export LC_CTYPE='not_exist'</>
<prompt>$</> <userinput>perl -v</>
<computeroutput>
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LC_ALL = (unset),
LC_CTYPE = "not_exist",
LANG = (unset)
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
</computeroutput>
</screen>
   </para>

   <para>
    Check that your locale files are in the right location.  Possible
    locations include: <filename>/usr/lib/locale</filename> (<systemitem class="osname">Linux</>,
    <systemitem class="osname">Solaris</>), <filename>/usr/share/locale</filename> (<systemitem class="osname">Linux</>),
    <filename>/usr/lib/nls/loc</filename> (<systemitem class="osname">DUX 4.0</>).  Check the locale
    man page of your system if you are not sure.
   </para>

   <para>
    Check that <productname>PostgreSQL</> is actually using the locale that
    you think it is.  <envar>LC_COLLATE</> and <envar>LC_CTYPE</> settings are
    determined at <application>initdb</> time and cannot be changed without
    repeating <application>initdb</>.  Other locale settings including
    <envar>LC_MESSAGES</> and <envar>LC_MONETARY</> are determined by the 
    environment the postmaster is started in, and can be changed with a simple
    postmaster restart.  You can check the <envar>LC_COLLATE</> and
    <envar>LC_CTYPE</> settings of
    a database with the <filename>contrib/pg_controldata</> utility program.
   </para>

   <para>
    The directory <filename>src/test/locale</> 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.  If you create such
    an application you need to devise a plan to cope with this
    situation.  The embedded SQL interface (<application>ecpg</>) is
    also affected by this problem.  It is currently recommended that
    servers interfacing with <application>ecpg</> applications be
    configured to send messages in English.
   </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 is currently not available or fully
    translated, your assistance would be appreciated.  If you want to
    help, refer to the <citetitle>Developer's Guide</> or write to the
    developers' mailing list.
   </para>
  </sect2>
 </sect1>


  <sect1 id="multibyte">
   <title>Multibyte Support</title>

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

   <note>
    <title>Author</title>

    <para>
     Tatsuo Ishii (<email>ishii@postgresql.org</email>),
     last updated 2002-07-24.
     Check <ulink
      url="http://www.sra.co.jp/people/t-ishii/PostgreSQL/">Tatsuo's
      web site</ulink> for more information.
    </para>
   </note>

   <para>
    Multibyte (<acronym>MB</acronym>) support is intended to allow
    <productname>PostgreSQL</productname> to handle
    multiple-byte character sets such as <acronym>EUC</> (Extended Unix Code), Unicode, and
    Mule internal code. With <acronym>MB</acronym> enabled you can use multibyte
    character sets in regular expressions (regexp), LIKE, and some
    other functions. The default
    encoding system is selected while initializing your
    <productname>PostgreSQL</productname> installation using
    <application>initdb</application>. Note that this can be
    overridden when you create a database using
    <application>createdb</application> or by using the SQL command
    <command>CREATE DATABASE</>. So you can have multiple databases each with
    a different encoding system. Note that <acronym>MB</acronym> can
    handle single byte characters sets such as ISO-8859-1.
   </para>

    <para>
    Multibyte support is enabled by default since PostgreSQL version 7.3.
    </para>

   <sect2>
    <title>Supported character set encodings</title>

    <para>
    Following encoding can be used as database encoding.

     <table tocentry="1">
      <title>Character Set Encodings</title>
      <titleabbrev>Encodings</titleabbrev>
      <tgroup cols="2">
       <thead>
	<row>
	 <entry>Encoding</entry>
	 <entry>Description</entry>
	</row>
       </thead>
       <tbody>
	<row>
	 <entry><literal>SQL_ASCII</literal></entry>
	 <entry><acronym>ASCII</acronym></entry>
	</row>
	<row>
	 <entry><literal>EUC_JP</literal></entry>
	 <entry>Japanese <acronym>EUC</></entry>
	</row>
	<row>
	 <entry><literal>EUC_CN</literal></entry>
	 <entry>Chinese <acronym>EUC</></entry>
	</row>
	<row>
	 <entry><literal>EUC_KR</literal></entry>
	 <entry>Korean <acronym>EUC</></entry>
	</row>
	<row>
	 <entry><literal>JOHAB</literal></entry>
	 <entry>Korean <acronym>EUC</> (Hangle base)</entry>
	</row>
	<row>
	 <entry><literal>EUC_TW</literal></entry>
	 <entry>Taiwan <acronym>EUC</acronym></entry>
	</row>
	<row>
	 <entry><literal>UNICODE</literal></entry>
	 <entry>Unicode (<acronym>UTF</acronym>-8)</entry>
	</row>
	<row>
	 <entry><literal>MULE_INTERNAL</literal></entry>
	 <entry>Mule internal code</entry>
	</row>
	<row>
	 <entry><literal>LATIN1</literal></entry>
	 <entry>ISO 8859-1 ECMA-94 Latin Alphabet No.1</entry>
	</row>
	<row>
	 <entry><literal>LATIN2</literal></entry>
	 <entry>ISO 8859-2 ECMA-94 Latin Alphabet No.2</entry>
	</row>
	<row>
	 <entry><literal>LATIN3</literal></entry>
	 <entry>ISO 8859-3 ECMA-94 Latin Alphabet No.3</entry>
	</row>
	<row>
	 <entry><literal>LATIN4</literal></entry>
	 <entry>ISO 8859-4 ECMA-94 Latin Alphabet No.4</entry>
	</row>
	<row>
	 <entry><literal>LATIN5</literal></entry>
	 <entry>ISO 8859-9 ECMA-128 Latin Alphabet No.5</entry>
	</row>
	<row>
	 <entry><literal>LATIN6</literal></entry>
	 <entry>ISO 8859-10 ECMA-144 Latin Alphabet No.6</entry>
	</row>
	<row>
	 <entry><literal>LATIN7</literal></entry>
	 <entry>ISO 8859-13 Latin Alphabet No.7</entry>
	</row>
	<row>
	 <entry><literal>LATIN8</literal></entry>
	 <entry>ISO 8859-14 Latin Alphabet No.8</entry>
	</row>
	<row>
	 <entry><literal>LATIN9</literal></entry>
	 <entry>ISO 8859-15 Latin Alphabet No.9</entry>
	</row>
	<row>
	 <entry><literal>LATIN10</literal></entry>
	 <entry>ISO 8859-16 ASRO SR 14111 Latin Alphabet No.10</entry>
	</row>
	<row>
	 <entry><literal>ISO-8859-5</literal></entry>
	 <entry>ECMA-113 Latin/Cyrillic</entry>
	</row>
	<row>
	 <entry><literal>ISO-8859-6</literal></entry>
	 <entry>ECMA-114 Latin/Arabic</entry>
	</row>
	<row>
	 <entry><literal>ISO-8859-7</literal></entry>
	 <entry>ECMA-118 Latin/Greek</entry>
	</row>
	<row>
	 <entry><literal>ISO-8859-8</literal></entry>
	 <entry>ECMA-121 Latin/Hebrew</entry>
	</row>
	<row>
	 <entry><literal>KOI8</literal></entry>
	 <entry><acronym>KOI</acronym>8-R(U)</entry>
	</row>
	<row>
	 <entry><literal>WIN</literal></entry>
	 <entry>Windows CP1251</entry>
	</row>
	<row>
	 <entry><literal>ALT</literal></entry>
	 <entry>Windows CP866</entry>
	</row>
	<row>
	 <entry><literal>WIN1256</literal></entry>
	 <entry>Arabic Windows CP1256</entry>
	</row>
	<row>
	 <entry><literal>TCVN</literal></entry>
	 <entry>Vietnamese TCVN-5712(Windows CP1258)</entry>
	</row>
	<row>
	 <entry><literal>WIN874</literal></entry>
	 <entry>Thai Windows CP874</entry>
	</row>
       </tbody>
      </tgroup>
     </table>
    </para>

    <important>
     <para>
      Before <productname>PostgreSQL</>7.2, <literal>LATIN5</> mistakenly
      meant ISO 8859-5.  From 7.2 on,
      <literal>LATIN5</> means ISO 8859-9. If you have a <literal>LATIN5</>
      database created on 7.1 or earlier and want to migrate to 7.2 (or
      later), you should be very careful about this change.
     </para>
    </important>

    <important>
     <para>
      Not all APIs supports all the encodings listed above. For example, the
      <productname>PostgreSQL</>
      JDBC driver does not support <literal>MULE_INTERNAL</>, <literal>LATIN6</>,
      <literal>LATIN8</>, and <literal>LATIN10</>.
     </para>
    </important>
    </sect2>
    
   <sect2>
    <title>Setting the Encoding</title>

    <para>
     <application>initdb</application> defines the default encoding
     for a <productname>PostgreSQL</productname> installation. For example:

<screen>
$ <userinput>initdb -E EUC_JP</>
</screen>

     sets the default encoding to <literal>EUC_JP</literal> (Extended Unix Code for Japanese).
     Note that 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, SQL_ASCII is used.
    </para>

    <para>
     You can create a database with a different encoding:

<screen>
$ <userinput>createdb -E EUC_KR korean</>
</screen>

     will create a database named <database>korean</database> with <literal>EUC_KR</literal> encoding.
     Another way to accomplish this is to use a SQL command:

<programlisting>
CREATE DATABASE korean WITH ENCODING = 'EUC_KR';
</programlisting>

     The encoding for a database is represented as an
     <firstterm>encoding column</firstterm>  in the
     <literal>pg_database</literal> system catalog.
     You can see that 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
   Database    |  Owner  |   Encoding    
---------------+---------+---------------
 euc_cn        | t-ishii | EUC_CN
 euc_jp        | t-ishii | EUC_JP
 euc_kr        | t-ishii | EUC_KR
 euc_tw        | t-ishii | EUC_TW
 mule_internal | t-ishii | MULE_INTERNAL
 regression    | t-ishii | SQL_ASCII
 template1     | t-ishii | EUC_JP
 test          | t-ishii | EUC_JP
 unicode       | t-ishii | UNICODE
(9 rows)
</screen>
    </para>
   </sect2>

   <sect2>
    <title>Automatic encoding conversion between server and
     client</title>

    <para>
     <productname>PostgreSQL</productname> supports an automatic
     encoding conversion between server and client for some
     encodings. The conversion info is stored in pg_converson system
     catalog. You can create a new conversion by using <command>CREATE
     CONVERSION</command>. PostgreSQL comes with some predefined
     conversions. They are listed in <xref
     linkend="multibyte-translation-table">.
    </para>

     <table tocentry="1" id="multibyte-translation-table">
      <title>Client/Server Character Set Encodings</title>
      <titleabbrev>Communication Encodings</titleabbrev>
      <tgroup cols="2">
       <thead>
	<row>
	 <entry>Server Encoding</entry>
	 <entry>Available Client Encodings</entry>
	</row>
       </thead>
       <tbody>
	<row>
	 <entry><literal>SQL_ASCII</literal></entry>
	 <entry><literal>SQL_ASCII</literal>, <literal>UNICODE</literal>, <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>EUC_JP</literal></entry>
	 <entry><literal>EUC_JP</literal>, <literal>SJIS</literal>,
	 <literal>UNICODE</literal>, <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>EUC_CN</literal></entry>
	 <entry><literal>EUC_CN</literal>, <literal>UNICODE</literal>, <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>EUC_KR</literal></entry>
	 <entry><literal>EUC_KR</literal>, <literal>UNICODE</literal>, <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>JOHAB</literal></entry>
	 <entry><literal>JOHAB</literal>, <literal>UNICODE</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>EUC_TW</literal></entry>
	 <entry><literal>EUC_TW</literal>, <literal>BIG5</literal>,
	 <literal>UNICODE</literal>, <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>  
	 <entry><literal>LATIN1</literal></entry>
	 <entry><literal>LATIN1</literal>, <literal>UNICODE</literal>
	 <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>  
	 <entry><literal>LATIN2</literal></entry>
	 <entry><literal>LATIN2</literal>, <literal>WIN1250</literal>,
	 <literal>UNICODE</literal>,
	 <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>  
	 <entry><literal>LATIN3</literal></entry>
	 <entry><literal>LATIN3</literal>, <literal>UNICODE</literal>
	 <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>  
	 <entry><literal>LATIN4</literal></entry>
	 <entry><literal>LATIN4</literal>, <literal>UNICODE</literal>
	 <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>  
	 <entry><literal>LATIN5</literal></entry>
	 <entry><literal>LATIN5</literal>, <literal>UNICODE</literal>
	 <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>  
	 <entry><literal>LATIN6</literal></entry>
	 <entry><literal>LATIN6</literal>, <literal>UNICODE</literal>
	 <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>  
	 <entry><literal>LATIN7</literal></entry>
	 <entry><literal>LATIN7</literal>, <literal>UNICODE</literal>
	 <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>  
	 <entry><literal>LATIN8</literal></entry>
	 <entry><literal>LATIN8</literal>, <literal>UNICODE</literal>
	 <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>  
	 <entry><literal>LATIN9</literal></entry>
	 <entry><literal>LATIN9</literal>, <literal>UNICODE</literal>
	 <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>  
	 <entry><literal>LATIN10</literal></entry>
	 <entry><literal>LATIN10</literal>, <literal>UNICODE</literal>
	 <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>ISO_8859_5</literal></entry>
	 <entry><literal>ISO_8859_5</literal>,
	 <literal>UNICODE</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>ISO_8859_6</literal></entry>
	 <entry><literal>ISO_8859_6</literal>,
	 <literal>UNICODE</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>ISO_8859_7</literal></entry>
	 <entry><literal>ISO_8859_7</literal>,
	 <literal>UNICODE</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>ISO_8859_8</literal></entry>
	 <entry><literal>ISO_8859_8</literal>,
	 <literal>UNICODE</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>ISO_8859_9</literal></entry>
	 <entry><literal>ISO_8859_9</literal>, <literal>WIN</literal>, 
	 <literal>ALT</literal>, <literal>KOI8R</literal>,
	 <literal>UNICODE</literal>, <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>UNICODE</literal></entry>
	 <entry>
	 <literal>EUC_JP</literal>, <literal>SJIS</literal>, 
	 <literal>EUC_KR</literal>, <literal>UHC</literal>, <literal>JOHAB</literal>,
	 <literal>EUC_CN</literal>, <literal>GBK</literal>,
	 <literal>EUC_TW</literal>, <literal>BIG5</literal>, 
	 <literal>LATIN1</literal> to <literal>LATIN10</literal>, 
	 <literal>ISO_8859_5</literal>, 
	 <literal>ISO_8859_6</literal>,
 	 <literal>ISO_8859_7</literal>, 
	 <literal>ISO_8859_8</literal>, 
	 <literal>WIN</literal>, <literal>ALT</literal>, 
	 <literal>KOI8</literal>, 
	 <literal>WIN1256</literal>,
	 <literal>TCVN</literal>,
	 <literal>WIN874</literal>,
	 </entry>
	</row>
	<row>
	 <entry><literal>MULE_INTERNAL</literal></entry>
	 <entry><literal>EUC_JP</literal>, <literal>SJIS</literal>, <literal>EUC_KR</literal>, <literal>EUC_CN</literal>, 
	  <literal>EUC_TW</literal>, <literal>BIG5</literal>, <literal>LATIN1</literal> to <literal>LATIN5</literal>, 
	  <literal>WIN</literal>, <literal>ALT</literal>, <literal>WIN1250</literal></entry>
	</row>
	<row>
	 <entry><literal>KOI8</literal></entry>
	 <entry><literal>ISO_8859_9</literal>, <literal>WIN</literal>, 
	 <literal>ALT</literal>, <literal>KOI8</literal>,
	 <literal>UNICODE</literal>, <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>WIN</literal></entry>
	 <entry><literal>ISO_8859_9</literal>, <literal>WIN</literal>, 
	 <literal>ALT</literal>, <literal>KOI8</literal>,
	 <literal>UNICODE</literal>, <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>ALT</literal></entry>
	 <entry><literal>ISO_8859_9</literal>, <literal>WIN</literal>, 
	 <literal>ALT</literal>, <literal>KOI8</literal>,
	 <literal>UNICODE</literal>, <literal>MULE_INTERNAL</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>WIN1256</literal></entry>
	 <entry><literal>WIN1256</literal>,
	 <literal>UNICODE</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>TCVN</literal></entry>
	 <entry><literal>TCVN</literal>,
	 <literal>UNICODE</literal>
	 </entry>
	</row>
	<row>
	 <entry><literal>WIN874</literal></entry>
	 <entry><literal>WIN874</literal>,
	 <literal>UNICODE</literal>
	 </entry>
	</row>
       </tbody>
      </tgroup>
     </table>

    <para>
     To enable the automatic encoding translation, you have to tell
     <productname>PostgreSQL</productname> the 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>
	Using <application>libpq</> functions.
	<command>\encoding</command> actually calls
	<function>PQsetClientEncoding()</function> for its purpose.

<synopsis>
int PQsetClientEncoding(PGconn *<replaceable>conn</replaceable>, const char *<replaceable>encoding</replaceable>)
</synopsis>

	where <replaceable>conn</replaceable> is a connection to the server,
	and <replaceable>encoding</replaceable> is an encoding you
	want to use. If it successfully sets the encoding, it returns 0,
	otherwise -1. The current encoding for this connection can be shown by
	using:

<synopsis>
int PQclientEncoding(const PGconn *<replaceable>conn</replaceable>)
</synopsis>

	Note that it returns the encoding ID, not a symbolic string
	such as <literal>EUC_JP</literal>. To convert an encoding ID to an encoding name, you
	can use:

<synopsis>
char *pg_encoding_to_char(int <replaceable>encoding_id</replaceable>)
</synopsis>
       </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 'encoding';
</programlisting>

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

<programlisting>
SET NAMES 'encoding';
</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 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 client_encoding variable.

      If client_encoding variable in postgresql.conf 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>
   </sect2>

   <sect2>
    <title>About Unicode</title>

    <indexterm><primary>Unicode</></>

    <para>
     An automatic encoding translation between Unicode and other
     encodings has been supported since <productname>PostgreSQL</> 7.1.
     For 7.1 it was not enabled by default.
     To enable this feature, run configure with the
     <option>--enable-unicode-conversion</option> option. Note that this requires
     the <option>--enable-multibyte</option> option also.
    </para>
    <para>
    For 7.2, <option>--enable-unicode-conversion</option> is not necessary.
    The Unicode conversion functionality is automatically enabled
    if <option>--enable-multibyte</option> is specified.
    </para>
    <para>
    For 7.3, <option>--enable-unicode-conversion</option> nor
    <option>--enable-multibyte</option> is needed.
    </para>
   </sect2>

   <sect2>
    <title>What happens if the translation is not possible?</title>

    <para>
     Suppose you choose <literal>EUC_JP</literal> for the server
     and <literal>LATIN1</literal> for the client,
     then some Japanese characters cannot be translated into <literal>LATIN1</literal>. In
     this case, a letter that cannot be represented in the <literal>LATIN1</literal> character set
     would be transformed as:

<synopsis>
(HEXA DECIMAL)
</synopsis>
    </para>
   </sect2>

   <sect2>
    <title>References</title>

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

     <variablelist>
      <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 2044</term>

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

   <sect2>
    <title>History</title>

<literallayout class="monospaced">
Dec 7, 2000
	* An automatic encoding translation between Unicode and other
	  encodings are implemented
	* Changes above will appear in 7.1

May 20, 2000
	* SJIS UDC (NEC selection IBM kanji) support contributed
	  by Eiji Tokuya
	* Changes above will appear in 7.0.1

Mar 22, 2000
	* Add new libpq functions PQsetClientEncoding, PQclientEncoding
	* ./configure --with-mb=EUC_JP
	  now deprecated. use 
	  ./configure --enable-multibyte=EUC_JP
	  instead
  	* Add SQL_ASCII regression test case
	* Add SJIS User Defined Character (UDC) support
	* All of above will appear in 7.0

July 11, 1999
	* Add support for WIN1250 (Windows Czech) as a client encoding
	  (contributed by Pavel Behal)
	* fix some compiler warnings (contributed by Tomoaki Nishiyama)

Mar 23, 1999
	* Add support for KOI8(KOI8-R), WIN(CP1251), ALT(CP866)
	  (thanks Oleg Broytmann for testing)
	* Fix problem with MB and locale

Jan 26, 1999
	* Add support for Big5 for frontend encoding
	  (you need to create a database with EUC_TW to use Big5)
	* Add regression test case for EUC_TW
	  (contributed by Jonah Kuo <email>jonahkuo@mail.ttn.com.tw</email>)

Dec 15, 1998
	* Bugs related to SQL_ASCII support fixed

Nov 5, 1998
	* 6.4 release. In this version, pg_database has "encoding"
	  column that represents the database encoding

Jul 22, 1998
	* determine encoding at initdb/createdb rather than compile time
	* support for PGCLIENTENCODING when issuing COPY command
	* support for SQL92 syntax "SET NAMES"
	* support for LATIN2-5
	* add UNICODE regression test case
	* new test suite for MB
	* clean up source files

Jun 5, 1998
	* add support for the encoding translation between the backend
	  and the frontend
	* new command SET CLIENT_ENCODING etc. added
	* add support for LATIN1 character set
	* enhance 8-bit cleanliness

April 21, 1998 some enhancements/fixes
	* character_length(), position(), substring() are now aware of 
	  multi-byte characters
	* add octet_length()
	* add --with-mb option to configure
	* new regression tests for EUC_KR
  	  (contributed by Soonmyung Hong)
	* add some test cases to the EUC_JP regression test
	* fix problem in regress/regress.sh in case of System V
	* fix toupper(), tolower() to handle 8bit chars

Mar 25, 1998 MB PL2 is incorporated into <productname>PostgreSQL</> 6.3.1

Mar 10, 1998 PL2 released
	* add regression test for EUC_JP, EUC_CN and MULE_INTERNAL
	* add an English document (this file)
	* fix problems concerning 8-bit single byte characters

Mar 1, 1998 PL1 released
</literallayout>
   </sect2>

   <sect2>
    <title>WIN1250 on Windows/ODBC</title>

    <para>
<!--
[Here is a good documentation explaining how to use WIN1250 on
Windows/ODBC from Pavel Behal]

Version: 0.91 for PgSQL 6.5
Author: Pavel Behal
Revised by: Tatsuo Ishii
Email: behal@opf.slu.cz
License: The Same as <productname>PostgreSQL</>

Sorry for my Eglish and C code, I'm not native :-)

!!!!!!!!!!!!!!!!!!!!!!!!! NO WARRANTY !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-->

     The WIN1250 character set on Windows client platforms can be used
     with <productname>PostgreSQL</productname> with locale support
     enabled.
    </para>

    <para>
     The following should be kept in mind:

     <itemizedlist>
      <listitem>
       <para>
	Success depends on proper system locales. This has been tested
	with <systemitem class="osname">Red Hat 6.0</> and <systemitem
	class="osname">Slackware 3.6</>, with the
        <literal>cs_CZ.iso8859-2</literal> locale.
       </para>
      </listitem>

      <listitem>
       <para>
	Never try to set the server's database encoding to WIN1250.
	Always use LATIN2 instead since there is no WIN1250 locale
	in Unix.
       </para>
      </listitem>

      <listitem>
       <para>
	The WIN1250 encoding is usable only for Windows ODBC clients. The
	characters are recoded on the fly, to be displayed and stored
	back properly.
       </para>
      </listitem>
     </itemizedlist>
    </para>

    <procedure>
     <title>WIN1250 on Windows/ODBC</title>
     <step>
      <para>
       Compile <productname>PostgreSQL</productname> with locale enabled
       and the server-side encoding set to <literal>LATIN2</literal>.
      </para>
     </step>

     <step>
      <para>
       Set up your installation. Do not forget to create locale
       variables in your environment. For example (this may
       not be correct for <emphasis>your</emphasis> environment):

<programlisting>
LC_ALL=cs_CZ.ISO8859-2
</programlisting>
      </para>
     </step>

     <step>
      <para>
       You have to start the server with locales set!
      </para>
     </step>

     <step>
      <para>
       Try it with the Czech language, and have it sort on a query.
      </para>
     </step>

     <step>
      <para>
       Install ODBC driver for <productname>PostgreSQL</productname> on your Windows machine.
      </para>
     </step>

     <step>
      <para>
       Set up your data source properly. Include this line in your ODBC
       configuration dialog in the field <guilabel>Connect Settings</guilabel>:

<programlisting>
SET CLIENT_ENCODING = 'WIN1250';
</programlisting>
      </para>
     </step>

     <step>
      <para>
       Now try it again, but in Windows with ODBC.
      </para>
     </step>
    </procedure>
   </sect2>
  </sect1>


 <sect1 id="recode">
  <title>Single-byte character set recoding</>
<!-- formerly in README.charsets, by Josef Balatka, <balatka@email.cz> -->

  <para>
   You can set up this feature with the <option>--enable-recode</> option
   to <filename>configure</>. This option was formerly described as
   <quote>Cyrillic recode support</> which doesn't express all its
   power. It can be used for <emphasis>any</> single-byte character
   set recoding.
  </para>

  <para>
   This method uses a file <filename>charset.conf</> file located in
   the database directory (<envar>PGDATA</>).  It's a typical
   configuration text file where spaces and newlines separate items
   and records and # specifies comments.  Three keywords with the
   following syntax are recognized here:
<synopsis>
BaseCharset      <replaceable>server_charset</>
RecodeTable      <replaceable>from_charset</> <replaceable>to_charset</> <replaceable>file_name</>
HostCharset      <replaceable>host_spec</>    <replaceable>host_charset</>
</synopsis>
  </para>

  <para>
   <token>BaseCharset</> defines the encoding of the database server.
   All character set names are only used for mapping inside of
   <filename>charset.conf</> so you can freely use typing-friendly
   names.
  </para>

  <para>
   <token>RecodeTable</> records specify translation tables between
   server and client.  The file name is relative to the
   <envar>PGDATA</> directory.  The table file format is very
   simple. There are no keywords and characters are represented by a
   pair of decimal or hexadecimal (0x prefixed) values on single
   lines:
<synopsis>
<replaceable>char_value</>   <replaceable>translated_char_value</>
</synopsis>
  </para>

  <para>
   <token>HostCharset</> records define the client character set by IP
   address. You can use a single IP address, an IP mask range starting
   from the given address or an IP interval (e.g., 127.0.0.1,
   192.168.1.100/24, 192.168.1.20-192.168.1.40).
  </para>

  <para>
   The <filename>charset.conf</> file is always processed up to the
   end, so you can easily specify exceptions from the previous
   rules. In the <filename>src/data/</> directory you will find an
   example <filename>charset.conf</> and a few recoding tables.
  </para>

  <para>
   As this solution is based on the client's IP address and character
   set mapping there are obviously some restrictions as well. You
   cannot use different encodings on the same host at the same
   time. It is also inconvenient when you boot your client hosts into
   multiple operating systems.  Nevertheless, when these restrictions are
   not limiting and you do not need multibyte characters then it is a
   simple and effective solution.
  </para>
 </sect1>

</chapter>

<!-- Keep this comment at the end of the file
Local variables:
mode:sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"./reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:("/usr/lib/sgml/catalog")
sgml-local-ecat-files:nil
End:
-->
