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.

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.

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.

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.

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
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 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.)

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).