Supported Versions: Current (17) / 16 / 15 / 14 / 13
Development Versions: devel
Unsupported versions: 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

20.2. Character Set Support

The character set support in PostgreSQL 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 EUC (Extended Unix Code), Unicode, 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 PostgreSQL database cluster using initdb. It 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 character set.

20.2.1. Supported Character Sets

Table 20-1 shows the character sets available for use in the server.

Table 20-1. Server Character Sets

Name Description
SQL_ASCII ASCII
EUC_JP Japanese EUC
EUC_CN Chinese EUC
EUC_KR Korean EUC
JOHAB Korean EUC (Hangle base)
EUC_TW Taiwan EUC
UNICODE Unicode (UTF-8)
MULE_INTERNAL Mule internal code
LATIN1 ISO 8859-1/ECMA 94 (Latin alphabet no.1)
LATIN2 ISO 8859-2/ECMA 94 (Latin alphabet no.2)
LATIN3 ISO 8859-3/ECMA 94 (Latin alphabet no.3)
LATIN4 ISO 8859-4/ECMA 94 (Latin alphabet no.4)
LATIN5 ISO 8859-9/ECMA 128 (Latin alphabet no.5)
LATIN6 ISO 8859-10/ECMA 144 (Latin alphabet no.6)
LATIN7 ISO 8859-13 (Latin alphabet no.7)
LATIN8 ISO 8859-14 (Latin alphabet no.8)
LATIN9 ISO 8859-15 (Latin alphabet no.9)
LATIN10 ISO 8859-16/ASRO SR 14111 (Latin alphabet no.10)
ISO_8859_5 ISO 8859-5/ECMA 113 (Latin/Cyrillic)
ISO_8859_6 ISO 8859-6/ECMA 114 (Latin/Arabic)
ISO_8859_7 ISO 8859-7/ECMA 118 (Latin/Greek)
ISO_8859_8 ISO 8859-8/ECMA 121 (Latin/Hebrew)
KOI8 KOI8-R(U)
ALT Windows CP866
WIN874 Windows CP874 (Thai)
WIN1250 Windows CP1250
WIN Windows CP1251
WIN1256 Windows CP1256 (Arabic)
TCVN TCVN-5712/Windows CP1258 (Vietnamese)

Important: Before PostgreSQL 7.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 careful about this change.

Not all APIs support all the listed character sets. For example, the PostgreSQL JDBC driver does not support MULE_INTERNAL, LATIN6, LATIN8, and LATIN10.

20.2.2. Setting the Character Set

initdb defines the default character set for a PostgreSQL cluster. For example,

initdb -E EUC_JP

sets the default character set (encoding) to EUC_JP (Extended Unix Code for Japanese). You can use --encoding instead of -E if you prefer to type longer option strings. If no -E or --encoding option is given, SQL_ASCII is used.

You can create a database with a different character set:

createdb -E EUC_KR korean

This will create a database named korean that uses the character set EUC_KR. Another way to accomplish this is to use this SQL command:

CREATE DATABASE korean WITH ENCODING 'EUC_KR';

The encoding for a database is stored in the system catalog pg_database. 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)

Important: 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 LC_COLLATE and LC_CTYPE settings imply a particular encoding, and locale-dependent operations (such as sorting) are likely to misinterpret data that is in an incompatible encoding.

Since these locale settings are frozen by 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 PostgreSQL.

One way to use multiple encodings safely is to set the locale to C or POSIX during initdb, thus disabling any real locale awareness.

20.2.3. Automatic Character Set Conversion Between Server and Client

PostgreSQL supports automatic character set conversion between server and client for certain character sets. The conversion information is stored in the pg_conversion system catalog. You can create a new conversion by using the SQL command CREATE CONVERSION. PostgreSQL comes with some predefined conversions. They are listed in Table 20-2.

Table 20-2. Client/Server Character Set Conversions

Server Character Set Available Client Character Sets
SQL_ASCII SQL_ASCII, UNICODE, MULE_INTERNAL
EUC_JP EUC_JP, SJIS, UNICODE, MULE_INTERNAL
EUC_CN EUC_CN, UNICODE, MULE_INTERNAL
EUC_KR EUC_KR, UNICODE, MULE_INTERNAL
JOHAB JOHAB, UNICODE
EUC_TW EUC_TW, BIG5, UNICODE, MULE_INTERNAL
LATIN1 LATIN1, UNICODE MULE_INTERNAL
LATIN2 LATIN2, WIN1250, UNICODE, MULE_INTERNAL
LATIN3 LATIN3, UNICODE, MULE_INTERNAL
LATIN4 LATIN4, UNICODE, MULE_INTERNAL
LATIN5 LATIN5, UNICODE
LATIN6 LATIN6, UNICODE, MULE_INTERNAL
LATIN7 LATIN7, UNICODE, MULE_INTERNAL
LATIN8 LATIN8, UNICODE, MULE_INTERNAL
LATIN9 LATIN9, UNICODE, MULE_INTERNAL
LATIN10 LATIN10, UNICODE, MULE_INTERNAL
ISO_8859_5 ISO_8859_5, UNICODE, MULE_INTERNAL, WIN, ALT, KOI8
ISO_8859_6 ISO_8859_6, UNICODE
ISO_8859_7 ISO_8859_7, UNICODE
ISO_8859_8 ISO_8859_8, UNICODE
UNICODE EUC_JP, SJIS, EUC_KR, UHC, JOHAB, EUC_CN, GBK, EUC_TW, BIG5, LATIN1 to LATIN10, ISO_8859_5, ISO_8859_6, ISO_8859_7, ISO_8859_8, WIN, ALT, KOI8, WIN1256, TCVN, WIN874, GB18030, WIN1250
MULE_INTERNAL EUC_JP, SJIS, EUC_KR, EUC_CN, EUC_TW, BIG5, LATIN1 to LATIN5, WIN, ALT, WIN1250, BIG5, ISO_8859_5, KOI8
KOI8 ISO_8859_5, WIN, ALT, KOI8, UNICODE, MULE_INTERNAL
ALT ISO_8859_5, WIN, ALT, KOI8, UNICODE, MULE_INTERNAL
WIN874 WIN874, UNICODE
WIN1250 LATIN2, WIN1250, UNICODE, MULE_INTERNAL
WIN ISO_8859_5, WIN, ALT, KOI8, UNICODE, MULE_INTERNAL
WIN1256 WIN1256, UNICODE
TCVN TCVN, UNICODE

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

  • Using the \encoding command in psql. \encoding allows you to change client encoding on the fly. For example, to change the encoding to SJIS, type:

    \encoding SJIS
    
  • Using libpq functions. \encoding actually calls PQsetClientEncoding() for its purpose.

    int PQsetClientEncoding(PGconn *conn, const char *encoding);
    

    where conn is a connection to the server, and encoding 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:

    int PQclientEncoding(const PGconn *conn);
    

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

    char *pg_encoding_to_char(int encoding_id);
    
  • Using SET client_encoding TO. Setting the client encoding can be done with this SQL command:

    SET CLIENT_ENCODING TO 'value';
    

    Also you can use the more standard SQL syntax SET NAMES for this purpose:

    SET NAMES 'value';
    

    To query the current client encoding:

    SHOW client_encoding;
    

    To return to the default encoding:

    RESET client_encoding;
    
  • Using PGCLIENTENCODING. If the environment variable PGCLIENTENCODING 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.)

  • Using the configuration variable client_encoding. If the 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.)

If the conversion of a particular character is not possible — suppose you chose EUC_JP for the server and LATIN1 for the client, then some Japanese characters cannot be converted to LATIN1 — it is transformed to its hexadecimal byte values in parentheses, e.g., (826C).

20.2.4. Further Reading

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

ftp://ftp.ora.com/pub/examples/nutshell/ujip/doc/cjk.inf

Detailed explanations of EUC_JP, EUC_CN, EUC_KR, EUC_TW appear in section 3.2.

http://www.unicode.org/

The web site of the Unicode Consortium

RFC 2044

UTF-8 is defined here.