Multibyte Support

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

Multibyte support also fixes some problems concerning 8-bit single byte character sets, including ISO8859.

Enabling Multibyte Support

Multibyte support is enabled by default in Red Hat Database.

If you have a non-default system and now want to enable multibyte support, run configure with the multibyte option:

% ./configure --enable-multibyte[=encoding_system]

where encoding_system can be one of the values in the following table:

Important

Before PostgreSQL7.2, LATIN5 mistakenly meant ISO 8859-5. From 7.2 on, LATIN5 means ISO 8859-9. If you have a 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.

Not all APIs supports all the encodings listed above. For example, the PostgreSQL JDBC driver does not support MULE_INTERNAL, LATIN6, LATIN8, or LATIN10.

Here is an example of configuring PostgreSQL to use a Japanese encoding by default:
$ ./configure --enable-multibyte=EUC_JP

If the encoding system is omitted (./configure --enable-multibyte), SQL_ASCII is assumed.

Setting the Encoding

initdb defines the default encoding for a PostgreSQL installation. For example:
$ initdb -E EUC_JP
sets the default encoding to EUC_JP (Extended UNIX Code for Japanese). Note that you can use --encoding instead of -E. If no -E or --encoding option is given, the encoding specified at configure time is used.

You can create a database with a different encoding:
$ createdb -E EUC_KR korean
will create a database named korean with EUC_KR encoding. Another way to accomplish this is to use a SQL command:
CREATE DATABASE korean WITH ENCODING = 'EUC_KR';
The encoding for a database is represented as an encoding column in the pg_database system catalog. You can see that by using the -l option or the \l command of psql.
$ psql -l
            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)

Automatic Encoding Translation Between Server and Client

PostgreSQL supports an automatic encoding translation between server and client for some encodings. The available combinations are listed in Table 3-2.

To enable the automatic encoding translation, you have to tell PostgreSQL the encoding you would like to use in the frontend. There are several ways to accomplish this:

About Unicode

An automatic encoding translation between Unicode and other encodings has been supported since PostgreSQL 7.1. For 7.1 it was not enabled by default. To enable this feature, run configure with the --enable-unicode-conversion option. Note that this requires the --enable-multibyte option also.

For 7.2, --enable-unicode-conversion is not necessary. The Unicode conversion functionality is automatically enabled if --enable-multibyte is specified.

When Translation Is Not Possible

Suppose you choose EUC_JP for the backend, LATIN1 for the frontend, then some Japanese characters could not be translated into LATIN1. In this case, a letter that cannot be represented in the LATIN1 character set would be transformed as:
(HEXA DECIMAL)