postgresql/doc/src/sgml/charset.sgml

1111 lines
36 KiB
Plaintext
Raw Normal View History

<!-- $PostgreSQL: pgsql/doc/src/sgml/charset.sgml,v 2.65 2005/03/13 22:04:29 momjian 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
2001-11-18 21:33:32 +01:00
locale-specific collation order, number formatting, translated
messages, and other aspects.
</para>
</listitem>
<listitem>
<para>
Providing a number of different character sets defined in the
<productname>PostgreSQL</productname> server, including
multiple-byte 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>
2001-11-12 20:19:39 +01:00
<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>
2001-11-18 21:33:32 +01:00
<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</> (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
2004-12-27 23:30:10 +01:00
system vendor and what was installed. (On most systems, the command
<literal>locale -a</> will provide a list of available locales.)
</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
2004-12-27 23:30:10 +01:00
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</></>
2004-12-27 23:30:10 +01:00
<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 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 running by setting the run-time configuration variables
that have the same name as the locale categories (see <xref
linkend="runtime-config-client-format"> 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>
2001-11-18 21:33:32 +01:00
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
2001-11-18 21:33:32 +01:00
before starting the server. A consequence of this is that if
2004-12-27 23:30:10 +01:00
client and server are set up in different locales, messages may
2001-11-18 21:33:32 +01:00
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 documentation about
<application>gettext</>, for more information.
</para>
</note>
<para>
2004-12-27 23:30:10 +01:00
To enable messages to be translated to the user's preferred language,
<acronym>NLS</acronym> must have been enabled at build time. This
choice is independent of the other locale support.
</para>
</sect2>
<sect2>
<title>Behavior</>
<para>
Locale support influences the following features:
<itemizedlist>
<listitem>
<para>
Sort order in queries using <literal>ORDER BY</>
2003-08-31 19:32:24 +02:00
<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>to_char</> family of functions
</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>
</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 what locales are installed on your
system, you may 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. <envar>LC_COLLATE</> and <envar>LC_CTYPE</>
settings are determined at <command>initdb</> time and cannot be
changed without repeating <command>initdb</>. Other locale
settings including <envar>LC_MESSAGES</> and <envar>LC_MONETARY</>
are initially determined by the environment the server is started
2004-12-27 23:30:10 +01:00
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>
2001-11-18 21:33:32 +01:00
<para>
Client applications that handle server-side errors by parsing the
text of the error message will obviously have problems when the
2003-11-04 10:55:39 +01:00
server's messages are in a different language. Authors of such
applications are advised to make use of the error code scheme
instead.
2001-11-18 21:33:32 +01:00
</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.
2004-12-27 23:30:10 +01:00
If messages in your language are currently not available or not fully
2001-11-18 21:33:32 +01:00
translated, your assistance would be appreciated. If you want to
2004-12-27 23:30:10 +01:00
help, refer to <xref linkend="nls"> or write to the developers'
mailing list.
2001-11-18 21:33:32 +01:00
</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, including
single-byte character sets such as the ISO 8859 series and
multiple-byte character sets such as <acronym>EUC</> (Extended Unix
Code), UTF8, and Mule internal code. All character sets can be
used transparently throughout the server. (If you use extension
functions from other sources, it depends on whether they wrote
their code correctly.) 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 using <command>createdb</command> or by using the
SQL command <command>CREATE DATABASE</>. So you can have multiple
databases each with a different character set.
</para>
<sect2 id="multibyte-charset-supported">
<title>Supported Character Sets</title>
<para>
<xref linkend="charset-table"> shows the character sets available
for use in the server.
</para>
<table id="charset-table">
<title>Server Character Sets</title>
<tgroup cols="2">
<thead>
2004-12-27 23:30:10 +01:00
<row>
<entry>Name</entry>
<entry>Description</entry>
<entry>Language</entry>
2005-03-13 03:20:50 +01:00
<entry>Aliases</entry>
2004-12-27 23:30:10 +01:00
</row>
</thead>
<tbody>
<row>
<entry><literal>BIG5</literal></entry>
2005-03-13 05:10:23 +01:00
<entry>Big Five</entry>
<entry>Traditional Chinese</entry>
2005-03-13 03:20:50 +01:00
<entry>WIN950, Windows950</entry>
</row>
2004-12-27 23:30:10 +01:00
<row>
<entry><literal>EUC_CN</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Extended UNIX Code-CN</entry>
2005-03-13 05:10:23 +01:00
<entry>Simplified Chinese</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>EUC_JP</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Extended UNIX Code-JP</entry>
<entry>Japanese</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>EUC_KR</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Extended UNIX Code-KR</entry>
<entry>Korean</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>EUC_TW</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Extended UNIX Code-TW</entry>
2005-03-13 05:10:23 +01:00
<entry>Traditional Chinese, Taiwanese</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>GB18030</literal></entry>
2005-03-13 05:10:23 +01:00
<entry>National Standard</entry>
2005-03-13 03:54:34 +01:00
<entry>Chinese</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
</row>
<row>
<entry><literal>GBK</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Extended National Standard</entry>
2005-03-13 05:10:23 +01:00
<entry>Simplified Chinese</entry>
2005-03-13 03:20:50 +01:00
<entry>WIN936, Windows936</entry>
</row>
<row>
<entry><literal>ISO_8859_5</literal></entry>
2005-03-13 05:35:06 +01:00
<entry>ISO 8859-5, <acronym>ECMA</> 113</entry>
2005-03-13 03:54:34 +01:00
<entry>Latin/Cyrillic</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
</row>
2004-12-27 23:30:10 +01:00
<row>
<entry><literal>ISO_8859_6</literal></entry>
2005-03-13 05:35:06 +01:00
<entry>ISO 8859-6, <acronym>ECMA</> 114</entry>
2005-03-13 03:54:34 +01:00
<entry>Latin/Arabic</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>ISO_8859_7</literal></entry>
2005-03-13 05:35:06 +01:00
<entry>ISO 8859-7, <acronym>ECMA</> 118</entry>
2005-03-13 03:54:34 +01:00
<entry>Latin/Greek</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
</row>
<row>
<entry><literal>ISO_8859_8</literal></entry>
2005-03-13 05:35:06 +01:00
<entry>ISO 8859-8, <acronym>ECMA</> 121</entry>
2005-03-13 03:54:34 +01:00
<entry>Latin/Hebrew</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
</row>
<row>
<entry><literal>JOHAB</literal></entry>
2005-03-13 05:10:23 +01:00
<entry><acronym>JOHAB</></entry>
2005-03-13 05:35:06 +01:00
<entry>Korean (Hangul)</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
</row>
<row>
<entry><literal>KOI8</literal></entry>
2005-03-13 03:54:34 +01:00
<entry><acronym>KOI</acronym>8-R(U)</entry>
<entry>Cyrillic</entry>
2005-03-13 05:10:23 +01:00
<entry>KOI8R</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>LATIN1</literal></entry>
2005-03-13 05:35:06 +01:00
<entry>ISO 8859-1, <acronym>ECMA</> 94</entry>
2005-03-13 03:54:34 +01:00
<entry>Western European</entry>
2005-03-13 03:20:50 +01:00
<entry>ISO88591</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>LATIN2</literal></entry>
2005-03-13 05:35:06 +01:00
<entry>ISO 8859-2, <acronym>ECMA</> 94</entry>
2005-03-13 03:54:34 +01:00
<entry>Central European</entry>
2005-03-13 03:20:50 +01:00
<entry>ISO88592</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>LATIN3</literal></entry>
2005-03-13 05:35:06 +01:00
<entry>ISO 8859-3, <acronym>ECMA</> 94</entry>
2005-03-13 03:54:34 +01:00
<entry>South European</entry>
2005-03-13 03:20:50 +01:00
<entry>ISO88593</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>LATIN4</literal></entry>
2005-03-13 05:35:06 +01:00
<entry>ISO 8859-4, <acronym>ECMA</> 94</entry>
2005-03-13 03:54:34 +01:00
<entry>North European</entry>
2005-03-13 03:20:50 +01:00
<entry>ISO88594</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>LATIN5</literal></entry>
2005-03-13 05:35:06 +01:00
<entry>ISO 8859-9, <acronym>ECMA</> 128</entry>
2005-03-13 03:54:34 +01:00
<entry>Turkish</entry>
2005-03-13 03:20:50 +01:00
<entry>ISO88599</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>LATIN6</literal></entry>
2005-03-13 05:35:06 +01:00
<entry>ISO 8859-10, <acronym>ECMA</> 144</entry>
2005-03-13 03:54:34 +01:00
<entry>Nordic</entry>
2005-03-13 03:20:50 +01:00
<entry>ISO885910</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>LATIN7</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>ISO 8859-13</entry>
<entry>Baltic</entry>
2005-03-13 03:20:50 +01:00
<entry>ISO885913</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>LATIN8</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>ISO 8859-14</entry>
<entry>Celtic</entry>
2005-03-13 03:20:50 +01:00
<entry>ISO885914</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>LATIN9</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>ISO 8859-15</entry>
<entry>LATIN1 with Euro and accents</entry>
2005-03-13 03:20:50 +01:00
<entry>ISO885915</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>LATIN10</literal></entry>
2005-03-13 05:35:06 +01:00
<entry>ISO 8859-16, <acronym>ASRO</> SR 14111</entry>
2005-03-13 03:54:34 +01:00
<entry>Romanian</entry>
2005-03-13 03:20:50 +01:00
<entry>ISO885916</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>MULE_INTERNAL</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Mule internal code</entry>
<entry>Multi-lingual Emacs</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
</row>
<row>
<entry><literal>SJIS</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Shift JIS</entry>
<entry>Japanese</entry>
2005-03-13 03:20:50 +01:00
<entry>Mskanji, ShiftJIS, WIN932, Windows932</entry>
</row>
<row>
<entry><literal>SQL_ASCII</literal></entry>
<entry><acronym>ASCII</acronym></entry>
2005-03-13 04:44:51 +01:00
<entry>English</entry>
2005-03-13 03:54:34 +01:00
<entry></entry>
</row>
<row>
<entry><literal>UHC</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Unified Hangul Code</entry>
<entry>Korean</entry>
2005-03-13 03:20:50 +01:00
<entry>WIN949, Windows949</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>UTF8</literal></entry>
2005-03-13 04:44:51 +01:00
<entry>Unicode, 8-bit</entry>
<entry><emphasis>all</></entry>
2005-03-13 03:20:50 +01:00
<entry>Unicode</entry>
</row>
2004-12-27 23:30:10 +01:00
<row>
<entry><literal>WIN866</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Windows CP866</entry>
<entry>Cyrillic</entry>
2005-03-13 03:20:50 +01:00
<entry>ALT</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>WIN874</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Windows CP874</entry>
<entry>Thai</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>WIN1250</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Windows CP1250</entry>
<entry>Central European</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>WIN1251</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Windows CP1251</entry>
<entry>Cyrillic</entry>
2005-03-13 03:20:50 +01:00
<entry>WIN</entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>WIN1256</literal></entry>
2005-03-13 03:54:34 +01:00
<entry>Windows CP1256</entry>
<entry>Arabic</entry>
2005-03-13 03:33:03 +01:00
<entry></entry>
2004-12-27 23:30:10 +01:00
</row>
<row>
<entry><literal>WIN1258</literal></entry>
2005-03-13 05:35:06 +01:00
<entry>Windows CP1258</entry>
2005-03-13 03:54:34 +01:00
<entry>Vietnamese</entry>
2005-03-13 03:20:50 +01:00
<entry>ABC, TCVN, TCVN5712, VSCII</entry>
2004-12-27 23:30:10 +01:00
</row>
</tbody>
</tgroup>
</table>
2001-11-28 21:49:10 +01:00
<para>
Not all <acronym>API</>s support all the listed character sets. For example, the
2001-11-28 21:49:10 +01:00
<productname>PostgreSQL</>
JDBC driver does not support <literal>MULE_INTERNAL</>, <literal>LATIN6</>,
<literal>LATIN8</>, and <literal>LATIN10</>.
</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,
2001-11-28 21:49:10 +01:00
<screen>
initdb -E EUC_JP
2001-11-28 21:49:10 +01:00
</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, <literal>SQL_ASCII</> is used.
</para>
<para>
You can create a database with a different character set:
2001-11-28 21:49:10 +01:00
<screen>
createdb -E EUC_KR korean
2001-11-28 21:49:10 +01:00
</screen>
This will create a database named <literal>korean</literal> that
uses the character set <literal>EUC_KR</literal>. Another way to
accomplish this is to use this SQL command:
2001-11-28 21:49:10 +01:00
<programlisting>
CREATE DATABASE korean WITH ENCODING 'EUC_KR';
2001-11-28 21:49:10 +01:00
</programlisting>
The encoding for a database is stored in the system catalog
<literal>pg_database</literal>. You can see that by using the
<option>-l</option> option or the <command>\l</command> command
of <command>psql</command>.
2001-11-28 21:49:10 +01:00
<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
utf8 | t-ishii | UTF8
(9 rows)
2001-11-28 21:49:10 +01:00
</screen>
</para>
2004-12-27 23:30:10 +01:00
<important>
<para>
Although you can specify any encoding you want for a database, it is
unwise to choose an encoding that is not what is expected by the locale
you have selected. The <literal>LC_COLLATE</literal> and
<literal>LC_CTYPE</literal> settings imply a particular encoding,
and locale-dependent operations (such as sorting) are likely to
misinterpret data that is in an incompatible encoding.
</para>
<para>
Since these locale settings are frozen by <command>initdb</>, the
apparent flexibility to use different encodings in different databases
of a cluster is more theoretical than real. It is likely that these
mechanisms will be revisited in future versions of
<productname>PostgreSQL</productname>.
</para>
<para>
One way to use multiple encodings safely is to set the locale to
<literal>C</> or <literal>POSIX</> during <command>initdb</>, thus
disabling any real locale awareness.
</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 sets. The conversion information is stored in the
<literal>pg_conversion</> system catalog. You can create a new
conversion by using the SQL command <command>CREATE
CONVERSION</command>. <productname>PostgreSQL</> comes with some
predefined conversions. They are listed in <xref
linkend="multibyte-translation-table">.
2001-11-28 21:49:10 +01:00
</para>
<table id="multibyte-translation-table">
<title>Client/Server Character Set Conversions</title>
<tgroup cols="2">
<thead>
2004-12-27 23:30:10 +01:00
<row>
<entry>Server Character Set</entry>
<entry>Available Client Character Sets</entry>
</row>
</thead>
<tbody>
2004-12-27 23:30:10 +01:00
<row>
<entry><literal>BIG5</literal></entry>
<entry><emphasis>BIG5</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>EUC_TW</literal>
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>EUC_CN</literal></entry>
<entry><emphasis>EUC_CN</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>EUC_JP</literal></entry>
<entry><emphasis>EUC_JP</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>MULE_INTERNAL</literal>,
<literal>SJIS</literal>,
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>EUC_KR</literal></entry>
<entry><emphasis>EUC_KR</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>EUC_TW</literal></entry>
<entry><emphasis>EUC_TW</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>BIG5</literal>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>GB18030</literal></entry>
<entry><emphasis>GB18030</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>GBK</literal></entry>
<entry><emphasis>GBK</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>ISO_8859_5</literal></entry>
<entry><emphasis>ISO_8859_5</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>KOI8</literal>,
<literal>MULE_INTERNAL</literal>,
2005-03-13 06:11:49 +01:00
<literal>UTF8</literal>,
<literal>WIN866</literal>,
2005-03-13 06:11:49 +01:00
<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>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>JOHAB</literal></entry>
<entry><emphasis>JOHAB</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>KOI8</literal></entry>
<entry><emphasis>KOI8</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>ISO_8859_5</literal>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>,
<literal>WIN866</literal>,
<literal>WIN1251</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
2005-03-13 06:11:49 +01:00
<row>
2004-12-27 23:30:10 +01:00
<entry><literal>LATIN1</literal></entry>
<entry><emphasis>LATIN1</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
2005-03-13 06:11:49 +01:00
<row>
2004-12-27 23:30:10 +01:00
<entry><literal>LATIN2</literal></entry>
<entry><emphasis>LATIN2</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>,
2005-03-13 06:11:49 +01:00
<literal>WIN1250</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
2005-03-13 06:11:49 +01:00
<row>
2004-12-27 23:30:10 +01:00
<entry><literal>LATIN3</literal></entry>
<entry><emphasis>LATIN3</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
2005-03-13 06:11:49 +01:00
<row>
2004-12-27 23:30:10 +01:00
<entry><literal>LATIN4</literal></entry>
<entry><emphasis>LATIN4</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
2005-03-13 06:11:49 +01:00
<row>
2004-12-27 23:30:10 +01:00
<entry><literal>LATIN5</literal></entry>
<entry><emphasis>LATIN5</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
2005-03-13 06:11:49 +01:00
<row>
2004-12-27 23:30:10 +01:00
<entry><literal>LATIN6</literal></entry>
<entry><emphasis>LATIN6</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
2005-03-13 06:11:49 +01:00
<row>
2004-12-27 23:30:10 +01:00
<entry><literal>LATIN7</literal></entry>
<entry><emphasis>LATIN7</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
2005-03-13 06:11:49 +01:00
<row>
2004-12-27 23:30:10 +01:00
<entry><literal>LATIN8</literal></entry>
<entry><emphasis>LATIN8</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
2005-03-13 06:11:49 +01:00
<row>
2004-12-27 23:30:10 +01:00
<entry><literal>LATIN9</literal></entry>
<entry><emphasis>LATIN9</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
2005-03-13 06:11:49 +01:00
<row>
2004-12-27 23:30:10 +01:00
<entry><literal>LATIN10</literal></entry>
<entry><emphasis>LATIN10</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>MULE_INTERNAL</literal></entry>
<entry><emphasis>MULE_INTERNAL</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>BIG5</literal>,
<literal>EUC_CN</literal>,
<literal>EUC_JP</literal>,
<literal>EUC_KR</literal>,
<literal>EUC_TW</literal>,
<literal>ISO_8859_5</literal>,
2005-03-13 06:16:33 +01:00
<literal>KOI8</literal>,
<literal>LATIN1</literal> to <literal>LATIN4</literal>,
<literal>LATIN2</literal>,
<literal>LATIN3</literal>,
<literal>LATIN4</literal>,
2005-03-13 06:11:49 +01:00
<literal>SJIS</literal>,
<literal>WIN866</literal>,
<literal>WIN1250</literal>,
<literal>WIN1251</literal>
2005-03-13 06:16:33 +01:00
</entry>
2005-03-13 06:11:49 +01:00
</row>
<row>
<entry><literal>SJIS</literal></entry>
<entry><emphasis>SJIS</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>EUC_JP</literal>,
<literal>MULE_INTERNAL</literal>,
2005-03-13 06:11:49 +01:00
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>SQL_ASCII</literal></entry>
<entry><emphasis>SQL_ASCII</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>UHC</literal></entry>
<entry><emphasis>UHC</emphasis>,
2005-03-13 06:11:49 +01:00
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>UTF8</literal></entry>
<entry><emphasis>UTF8</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>ISO_8859_6</literal>,
<literal>ISO_8859_7</literal>,
<literal>ISO_8859_8</literal>,
<literal>GB18030</literal>,
<literal>GBK</literal>,
<literal>JOHAB</literal>,
<literal>KOI8</literal>,
<literal>LATIN1</literal> to <literal>LATIN10</literal>,
<literal>SJIS</literal>,
<literal>UHC</literal>,
<literal>WIN866</literal>,
<literal>WIN874</literal>,
<literal>WIN1250</literal>,
<literal>WIN1251</literal>,
<literal>WIN1256</literal>,
<literal>WIN1258</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>WIN866</literal></entry>
<entry><emphasis>WIN866</emphasis>,
2005-03-13 06:31:04 +01:00
<literal>ISO_8859_5</literal>,
2005-03-13 06:11:49 +01:00
<literal>KOI8</literal>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>,
<literal>WIN1251</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>WIN874</literal></entry>
<entry><emphasis>WIN874</emphasis>,
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>WIN1250</literal></entry>
<entry><emphasis>WIN1250</emphasis>,
2005-03-13 06:31:04 +01:00
<literal>LATIN2</literal>,
2005-03-13 06:11:49 +01:00
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>WIN1251</literal></entry>
<entry><emphasis>WIN1251</emphasis>,
2005-03-13 06:31:04 +01:00
<literal>ISO_8859_5</literal>,
2005-03-13 06:11:49 +01:00
<literal>KOI8</literal>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>,
<literal>WIN866</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>WIN1256</literal></entry>
<entry><emphasis>WIN1256</emphasis>,
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
<row>
<entry><literal>WIN1258</literal></entry>
<entry><emphasis>WIN1258</emphasis>,
<literal>UTF8</literal>
2004-12-27 23:30:10 +01:00
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
To enable the 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>
2004-12-27 23:30:10 +01:00
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:
2001-11-28 21:49:10 +01:00
<programlisting>
\encoding SJIS
2001-11-28 21:49:10 +01:00
</programlisting>
</para>
</listitem>
<listitem>
<para>
2004-12-27 23:30:10 +01:00
Using <application>libpq</> functions.
<command>\encoding</command> actually calls
<function>PQsetClientEncoding()</function> for its purpose.
2001-11-28 21:49:10 +01:00
<synopsis>
int PQsetClientEncoding(PGconn *<replaceable>conn</replaceable>, const char *<replaceable>encoding</replaceable>);
2001-11-28 21:49:10 +01:00
</synopsis>
2004-12-27 23:30:10 +01:00
where <replaceable>conn</replaceable> is a connection to the server,
and <replaceable>encoding</replaceable> is the encoding you
want to use. If the function successfully sets the encoding, it returns 0,
otherwise -1. The current encoding for this connection can be determined by
using:
2001-11-28 21:49:10 +01:00
<synopsis>
int PQclientEncoding(const PGconn *<replaceable>conn</replaceable>);
2001-11-28 21:49:10 +01:00
</synopsis>
2004-12-27 23:30:10 +01:00
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:
2001-11-28 21:49:10 +01:00
<synopsis>
char *pg_encoding_to_char(int <replaceable>encoding_id</replaceable>);
2001-11-28 21:49:10 +01:00
</synopsis>
</para>
</listitem>
<listitem>
<para>
2004-12-27 23:30:10 +01:00
Using <command>SET client_encoding TO</command>.
2004-12-27 23:30:10 +01:00
Setting the client encoding can be done with this SQL command:
2001-11-28 21:49:10 +01:00
<programlisting>
SET CLIENT_ENCODING TO '<replaceable>value</>';
2001-11-28 21:49:10 +01:00
</programlisting>
2004-12-27 23:30:10 +01:00
Also you can use the more standard SQL syntax <literal>SET NAMES</literal> for this purpose:
2001-11-28 21:49:10 +01:00
<programlisting>
SET NAMES '<replaceable>value</>';
2001-11-28 21:49:10 +01:00
</programlisting>
2004-12-27 23:30:10 +01:00
To query the current client encoding:
2001-11-28 21:49:10 +01:00
<programlisting>
SHOW client_encoding;
2001-11-28 21:49:10 +01:00
</programlisting>
2004-12-27 23:30:10 +01:00
To return to the default encoding:
2001-11-28 21:49:10 +01:00
<programlisting>
RESET client_encoding;
2001-11-28 21:49:10 +01:00
</programlisting>
</para>
</listitem>
<listitem>
<para>
2004-12-27 23:30:10 +01:00
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 cannot be converted to
<literal>LATIN1</literal> &mdash; it is transformed to its
hexadecimal byte values in parentheses, e.g.,
<literal>(826C)</literal>.
</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>
<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>
2004-12-27 23:30:10 +01:00
<acronym>UTF</acronym>-8 is defined here.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</sect2>
</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:
-->