1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
|
<!-- doc/src/sgml/charset.sgml -->
<chapter id="charset">
<title>Localization</title>
<para>
This chapter describes the available localization features from the
point of view of the administrator.
<productname>PostgreSQL</productname> supports two localization
facilities:
<itemizedlist>
<listitem>
<para>
Using the locale features of the operating system to provide
locale-specific collation order, number formatting, translated
messages, and other aspects.
This is covered in <xref linkend="locale"> and
<xref linkend="collation">.
</para>
</listitem>
<listitem>
<para>
Providing a number of different character sets to support storing text
in all kinds of languages, and providing character set translation
between client and server.
This is covered in <xref linkend="multibyte">.
</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> locale facilities provided by the server operating
system. For additional information refer to the documentation of your
system.
</para>
<sect2>
<title>Overview</title>
<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>
<para>
This example for Unix systems sets the locale to Swedish
(<literal>sv</>) as spoken
in Sweden (<literal>SE</>). Other possibilities might include
<literal>en_US</> (U.S. English) and <literal>fr_CA</> (French
Canadian). If more than one character set can be used for a
locale then the specifications can take the form
<replaceable>language_territory.codeset</>. For example,
<literal>fr_BE.UTF-8</> represents the French language (fr) as
spoken in Belgium (BE), with a <acronym>UTF-8</> character set
encoding.
</para>
<para>
What locales are available on your
system under what names depends on what was provided by the operating
system vendor and what was installed. On most Unix systems, the command
<literal>locale -a</> will provide a list of available locales.
Windows uses more verbose locale names, such as <literal>German_Germany</>
or <literal>Swedish_Sweden.1252</>, but the principles are the same.
</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 certain
aspects 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? 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 name <literal>C</>, or equivalently
<literal>POSIX</>.
</para>
<para>
Some locale categories must have their values
fixed when the database is created. You can use different settings
for different databases, but once a database is created, you cannot
change them for that database anymore. <literal>LC_COLLATE</literal>
and <literal>LC_CTYPE</literal> are these categories. They affect
the sort order of indexes, so they must be kept fixed, or indexes on
text columns would become corrupt.
(But you can alleviate this restriction using collations, as discussed
in <xref linkend="collation">.)
The default values for these
categories are determined when <command>initdb</command> is run, and
those values are used when new databases are created, unless
specified otherwise in the <command>CREATE DATABASE</command> command.
</para>
<para>
The other locale categories can be changed whenever desired
by setting the server configuration parameters
that have the same name as the locale categories (see <xref
linkend="runtime-config-client-format"> for details). The values
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 remove these
assignments from <filename>postgresql.conf</filename> then the
server will inherit the settings from its 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 in different locales, messages might
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>
(or 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</>.
</para>
</note>
<para>
To enable messages to be translated to the user's preferred language,
<acronym>NLS</acronym> must have been selected at build time
(<literal>configure --enable-nls</>). All other locale support is
built in automatically.
</para>
</sect2>
<sect2>
<title>Behavior</title>
<para>
The locale settings influence the following SQL features:
<itemizedlist>
<listitem>
<para>
Sort order in queries using <literal>ORDER BY</> or the standard
comparison operators on textual data
<indexterm><primary>ORDER BY</><secondary>and locales</></indexterm>
</para>
</listitem>
<listitem>
<para>
The <function>upper</>, <function>lower</>, and <function>initcap</>
functions
<indexterm><primary>upper</><secondary>and locales</></indexterm>
<indexterm><primary>lower</><secondary>and locales</></indexterm>
</para>
</listitem>
<listitem>
<para>
Pattern matching operators (<literal>LIKE</>, <literal>SIMILAR TO</>,
and POSIX-style regular expressions); locales affect both case
insensitive matching and the classification of characters by
character-class regular expressions
<indexterm><primary>LIKE</><secondary>and locales</></indexterm>
<indexterm><primary>regular expressions</><secondary>and locales</></indexterm>
</para>
</listitem>
<listitem>
<para>
The <function>to_char</> family of functions
<indexterm><primary>to_char</><secondary>and locales</></indexterm>
</para>
</listitem>
<listitem>
<para>
The ability to use indexes with <literal>LIKE</> clauses
</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>
<para>
As a workaround to allow <productname>PostgreSQL</> to use indexes
with <literal>LIKE</> clauses under a non-C locale, several custom
operator classes exist. These allow the creation of an index that
performs a strict character-by-character comparison, ignoring
locale comparison rules. Refer to <xref linkend="indexes-opclass">
for more information. Another approach is to create indexes using
the <literal>C</> collation, as discussed in
<xref linkend="collation">.
</para>
</sect2>
<sect2>
<title>Problems</title>
<para>
If locale support doesn't work according to 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 can 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. The <envar>LC_COLLATE</> and <envar>LC_CTYPE</>
settings are determined when a database is created, and cannot be
changed except by creating a new database. Other locale
settings including <envar>LC_MESSAGES</> and <envar>LC_MONETARY</>
are initially determined by the environment the server is started
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>
<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. Authors of such
applications are advised to make use of the error code scheme
instead.
</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 are currently not available or not fully
translated, your assistance would be appreciated. If you want to
help, refer to <xref linkend="nls"> or write to the developers'
mailing list.
</para>
</sect2>
</sect1>
<sect1 id="collation">
<title>Collation Support</title>
<indexterm zone="collation"><primary>collation</></>
<para>
The collation feature allows specifying the sort order and character
classification behavior of data per-column, or even per-operation.
This alleviates the restriction that the
<symbol>LC_COLLATE</symbol> and <symbol>LC_CTYPE</symbol> settings
of a database cannot be changed after its creation.
</para>
<sect2>
<title>Concepts</title>
<para>
Conceptually, every expression of a collatable data type has a
collation. (The built-in collatable data types are
<type>text</type>, <type>varchar</type>, and <type>char</type>.
User-defined base types can also be marked collatable, and of course
a domain over a collatable data type is collatable.) If the
expression is a column reference, the collation of the expression is the
defined collation of the column. If the expression is a constant, the
collation is the default collation of the data type of the
constant. The collation of a more complex expression is derived
from the collations of its inputs, as described below.
</para>
<para>
The collation of an expression can be the <quote>default</quote>
collation, which means the locale settings defined for the
database. It is also possible for an expression's collation to be
indeterminate. In such cases, ordering operations and other
operations that need to know the collation will fail.
</para>
<para>
When the database system has to perform an ordering or a character
classification, it uses the collation of the input expression. This
happens, for example, with <literal>ORDER BY</literal> clauses
and function or operator calls such as <literal><</literal>.
The collation to apply for an <literal>ORDER BY</literal> clause
is simply the collation of the sort key. The collation to apply for a
function or operator call is derived from the arguments, as described
below. In addition to comparison operators, collations are taken into
account by functions that convert between lower and upper case
letters, such as <function>lower</>, <function>upper</>, and
<function>initcap</>; by pattern matching operators; and by
<function>to_char</> and related functions.
</para>
<para>
For a function or operator call, the collation that is derived by
examining the argument collations is used at run time for performing
the specified operation. If the result of the function or operator
call is of a collatable data type, the collation is also used at parse
time as the defined collation of the function or operator expression,
in case there is a surrounding expression that requires knowledge of
its collation.
</para>
<para>
The <firstterm>collation derivation</firstterm> of an expression can be
implicit or explicit. This distinction affects how collations are
combined when multiple different collations appear in an
expression. An explicit collation derivation occurs when a
<literal>COLLATE</literal> clause is used; all other collation
derivations are implicit. When multiple collations need to be
combined, for example in a function call, the following rules are
used:
<orderedlist>
<listitem>
<para>
If any input expression has an explicit collation derivation, then
all explicitly derived collations among the input expressions must be
the same, otherwise an error is raised. If any explicitly
derived collation is present, that is the result of the
collation combination.
</para>
</listitem>
<listitem>
<para>
Otherwise, all input expressions must have the same implicit
collation derivation or the default collation. If any non-default
collation is present, that is the result of the collation combination.
Otherwise, the result is the default collation.
</para>
</listitem>
<listitem>
<para>
If there are conflicting non-default implicit collations among the
input expressions, then the combination is deemed to have indeterminate
collation. This is not an error condition unless the particular
function being invoked requires knowledge of the collation it should
apply. If it does, an error will be raised at run-time.
</para>
</listitem>
</orderedlist>
For example, consider this table definition:
<programlisting>
CREATE TABLE test1 (
a text COLLATE "de_DE",
b text COLLATE "es_ES",
...
);
</programlisting>
Then in
<programlisting>
SELECT a < 'foo' FROM test1;
</programlisting>
the <literal><</literal> comparison is performed according to
<literal>de_DE</literal> rules, because the expression combines an
implicitly derived collation with the default collation. But in
<programlisting>
SELECT a < ('foo' COLLATE "fr_FR") FROM test1;
</programlisting>
the comparison is performed using <literal>fr_FR</literal> rules,
because the explicit collation derivation overrides the implicit one.
Furthermore, given
<programlisting>
SELECT a < b FROM test1;
</programlisting>
the parser cannot determine which collation to apply, since the
<structfield>a</> and <structfield>b</> columns have conflicting
implicit collations. Since the <literal><</literal> operator
does need to know which collation to use, this will result in an
error. The error can be resolved by attaching an explicit collation
specifier to either input expression, thus:
<programlisting>
SELECT a < b COLLATE "de_DE" FROM test1;
</programlisting>
or equivalently
<programlisting>
SELECT a COLLATE "de_DE" < b FROM test1;
</programlisting>
On the other hand, the structurally similar case
<programlisting>
SELECT a || b FROM test1;
</programlisting>
does not result in an error, because the <literal>||</> operator
does not care about collations: its result is the same regardless
of the collation.
</para>
<para>
The collation assigned to a function or operator's combined input
expressions is also considered to apply to the function or operator's
result, if the function or operator delivers a result of a collatable
data type. So, in
<programlisting>
SELECT * FROM test1 ORDER BY a || 'foo';
</programlisting>
the ordering will be done according to <literal>de_DE</literal> rules.
But this query:
<programlisting>
SELECT * FROM test1 ORDER BY a || b;
</programlisting>
results in an error, because even though the <literal>||</> operator
doesn't need to know a collation, the <literal>ORDER BY</> clause does.
As before, the conflict can be resolved with an explicit collation
specifier:
<programlisting>
SELECT * FROM test1 ORDER BY a || b COLLATE "fr_FR";
</programlisting>
</para>
</sect2>
<sect2>
<title>Managing Collations</title>
<para>
A collation is an SQL schema object that maps an SQL name to
operating system locales. In particular, it maps to a combination
of <symbol>LC_COLLATE</symbol> and <symbol>LC_CTYPE</symbol>. (As
the name would suggest, the main purpose of a collation is to set
<symbol>LC_COLLATE</symbol>, which controls the sort order. But
it is rarely necessary in practice to have an
<symbol>LC_CTYPE</symbol> setting that is different from
<symbol>LC_COLLATE</symbol>, so it is more convenient to collect
these under one concept than to create another infrastructure for
setting <symbol>LC_CTYPE</symbol> per expression.) Also, a collation
is tied to a character set encoding (see <xref linkend="multibyte">).
The same collation name may exist for different encodings.
</para>
<para>
On all platforms, the collations named <literal>default</>,
<literal>C</>, and <literal>POSIX</> are available. Additional
collations may be available depending on operating system support.
The <literal>default</> collation selects the <symbol>LC_COLLATE</symbol>
and <symbol>LC_CTYPE</symbol> values specified at database creation time.
The <literal>C</> and <literal>POSIX</> collations both specify
<quote>traditional C</> behavior, in which only the ASCII letters
<quote><literal>A</></quote> through <quote><literal>Z</></quote>
are treated as letters, and sorting is done strictly by character
code byte values.
</para>
<para>
If the operating system provides support for using multiple locales
within a single program (<function>newlocale</> and related functions),
then when a database cluster is initialized, <command>initdb</command>
populates the system catalog <literal>pg_collation</literal> with
collations based on all the locales it finds on the operating
system at the time. For example, the operating system might
provide a locale named <literal>de_DE.utf8</literal>.
<command>initdb</command> would then create a collation named
<literal>de_DE.utf8</literal> for encoding <literal>UTF8</literal>
that has both <symbol>LC_COLLATE</symbol> and
<symbol>LC_CTYPE</symbol> set to <literal>de_DE.utf8</literal>.
It will also create a collation with the <literal>.utf8</literal>
tag stripped off the name. So you could also use the collation
under the name <literal>de_DE</literal>, which is less cumbersome
to write and makes the name less encoding-dependent. Note that,
nevertheless, the initial set of collation names is
platform-dependent.
</para>
<para>
In case a collation is needed that has different values for
<symbol>LC_COLLATE</symbol> and <symbol>LC_CTYPE</symbol>, a new
collation may be created using
the <xref linkend="sql-createcollation"> command. That command
can also be used to create a new collation from an existing
collation, which can be useful to be able to use
operating-system-independent collation names in applications.
</para>
<para>
Within any particular database, only collations that use that
database's encoding are of interest. Other entries in
<literal>pg_collation</literal> are ignored. Thus, a stripped collation
name such as <literal>de_DE</literal> can be considered unique
within a given database even though it would not be unique globally.
Use of the stripped collation names is recommendable, since it will
make one less thing you need to change if you decide to change to
another database encoding. Note however that the <literal>default</>,
<literal>C</>, and <literal>POSIX</> collations can be used
regardless of the database encoding.
</para>
<para>
<productname>PostgreSQL</productname> considers distinct collation
objects to be incompatible even when they have identical properties.
Thus for example,
<programlisting>
SELECT a COLLATE "C" < b COLLATE "POSIX" FROM test1;
</programlisting>
will draw an error even though the <literal>C</> and <literal>POSIX</>
collations have identical behaviors. Mixing stripped and non-stripped
collation names is therefore not recommended.
</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 (also called
encodings), including
single-byte character sets such as the ISO 8859 series and
multiple-byte character sets such as <acronym>EUC</> (Extended Unix
Code), UTF-8, and Mule internal code. All supported character sets
can be used transparently by clients, but a few are not supported
for use within the server (that is, as a server-side encoding).
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, so you can have multiple
databases each with a different character set.
</para>
<para>
An important restriction, however, is that each database's character set
must be compatible with the database's <envar>LC_CTYPE</> (character
classification) and <envar>LC_COLLATE</> (string sort order) locale
settings. For <literal>C</> or
<literal>POSIX</> locale, any character set is allowed, but for other
locales there is only one character set that will work correctly.
(On Windows, however, UTF-8 encoding can be used with any locale.)
</para>
<sect2 id="multibyte-charset-supported">
<title>Supported Character Sets</title>
<para>
<xref linkend="charset-table"> shows the character sets available
for use in <productname>PostgreSQL</productname>.
</para>
<table id="charset-table">
<title><productname>PostgreSQL</productname> Character Sets</title>
<tgroup cols="6">
<thead>
<row>
<entry>Name</entry>
<entry>Description</entry>
<entry>Language</entry>
<entry>Server?</entry>
<!--
The Bytes/Char field is populated by looking at the values returned
by pg_wchar_table.mblen function for each encoding.
-->
<entry>Bytes/Char</entry>
<entry>Aliases</entry>
</row>
</thead>
<tbody>
<row>
<entry><literal>BIG5</literal></entry>
<entry>Big Five</entry>
<entry>Traditional Chinese</entry>
<entry>No</entry>
<entry>1-2</entry>
<entry><literal>WIN950</>, <literal>Windows950</></entry>
</row>
<row>
<entry><literal>EUC_CN</literal></entry>
<entry>Extended UNIX Code-CN</entry>
<entry>Simplified Chinese</entry>
<entry>Yes</entry>
<entry>1-3</entry>
<entry></entry>
</row>
<row>
<entry><literal>EUC_JP</literal></entry>
<entry>Extended UNIX Code-JP</entry>
<entry>Japanese</entry>
<entry>Yes</entry>
<entry>1-3</entry>
<entry></entry>
</row>
<row>
<entry><literal>EUC_JIS_2004</literal></entry>
<entry>Extended UNIX Code-JP, JIS X 0213</entry>
<entry>Japanese</entry>
<entry>Yes</entry>
<entry>1-3</entry>
<entry></entry>
</row>
<row>
<entry><literal>EUC_KR</literal></entry>
<entry>Extended UNIX Code-KR</entry>
<entry>Korean</entry>
<entry>Yes</entry>
<entry>1-3</entry>
<entry></entry>
</row>
<row>
<entry><literal>EUC_TW</literal></entry>
<entry>Extended UNIX Code-TW</entry>
<entry>Traditional Chinese, Taiwanese</entry>
<entry>Yes</entry>
<entry>1-3</entry>
<entry></entry>
</row>
<row>
<entry><literal>GB18030</literal></entry>
<entry>National Standard</entry>
<entry>Chinese</entry>
<entry>No</entry>
<entry>1-4</entry>
<entry></entry>
</row>
<row>
<entry><literal>GBK</literal></entry>
<entry>Extended National Standard</entry>
<entry>Simplified Chinese</entry>
<entry>No</entry>
<entry>1-2</entry>
<entry><literal>WIN936</>, <literal>Windows936</></entry>
</row>
<row>
<entry><literal>ISO_8859_5</literal></entry>
<entry>ISO 8859-5, <acronym>ECMA</> 113</entry>
<entry>Latin/Cyrillic</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>ISO_8859_6</literal></entry>
<entry>ISO 8859-6, <acronym>ECMA</> 114</entry>
<entry>Latin/Arabic</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>ISO_8859_7</literal></entry>
<entry>ISO 8859-7, <acronym>ECMA</> 118</entry>
<entry>Latin/Greek</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>ISO_8859_8</literal></entry>
<entry>ISO 8859-8, <acronym>ECMA</> 121</entry>
<entry>Latin/Hebrew</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>JOHAB</literal></entry>
<entry><acronym>JOHAB</></entry>
<entry>Korean (Hangul)</entry>
<entry>No</entry>
<entry>1-3</entry>
<entry></entry>
</row>
<row>
<entry><literal>KOI8R</literal></entry>
<entry><acronym>KOI</acronym>8-R</entry>
<entry>Cyrillic (Russian)</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>KOI8</></entry>
</row>
<row>
<entry><literal>KOI8U</literal></entry>
<entry><acronym>KOI</acronym>8-U</entry>
<entry>Cyrillic (Ukrainian)</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>LATIN1</literal></entry>
<entry>ISO 8859-1, <acronym>ECMA</> 94</entry>
<entry>Western European</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>ISO88591</></entry>
</row>
<row>
<entry><literal>LATIN2</literal></entry>
<entry>ISO 8859-2, <acronym>ECMA</> 94</entry>
<entry>Central European</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>ISO88592</></entry>
</row>
<row>
<entry><literal>LATIN3</literal></entry>
<entry>ISO 8859-3, <acronym>ECMA</> 94</entry>
<entry>South European</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>ISO88593</></entry>
</row>
<row>
<entry><literal>LATIN4</literal></entry>
<entry>ISO 8859-4, <acronym>ECMA</> 94</entry>
<entry>North European</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>ISO88594</></entry>
</row>
<row>
<entry><literal>LATIN5</literal></entry>
<entry>ISO 8859-9, <acronym>ECMA</> 128</entry>
<entry>Turkish</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>ISO88599</></entry>
</row>
<row>
<entry><literal>LATIN6</literal></entry>
<entry>ISO 8859-10, <acronym>ECMA</> 144</entry>
<entry>Nordic</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>ISO885910</></entry>
</row>
<row>
<entry><literal>LATIN7</literal></entry>
<entry>ISO 8859-13</entry>
<entry>Baltic</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>ISO885913</></entry>
</row>
<row>
<entry><literal>LATIN8</literal></entry>
<entry>ISO 8859-14</entry>
<entry>Celtic</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>ISO885914</></entry>
</row>
<row>
<entry><literal>LATIN9</literal></entry>
<entry>ISO 8859-15</entry>
<entry>LATIN1 with Euro and accents</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>ISO885915</></entry>
</row>
<row>
<entry><literal>LATIN10</literal></entry>
<entry>ISO 8859-16, <acronym>ASRO</> SR 14111</entry>
<entry>Romanian</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>ISO885916</></entry>
</row>
<row>
<entry><literal>MULE_INTERNAL</literal></entry>
<entry>Mule internal code</entry>
<entry>Multilingual Emacs</entry>
<entry>Yes</entry>
<entry>1-4</entry>
<entry></entry>
</row>
<row>
<entry><literal>SJIS</literal></entry>
<entry>Shift JIS</entry>
<entry>Japanese</entry>
<entry>No</entry>
<entry>1-2</entry>
<entry><literal>Mskanji</>, <literal>ShiftJIS</>, <literal>WIN932</>, <literal>Windows932</></entry>
</row>
<row>
<entry><literal>SHIFT_JIS_2004</literal></entry>
<entry>Shift JIS, JIS X 0213</entry>
<entry>Japanese</entry>
<entry>No</entry>
<entry>1-2</entry>
<entry></entry>
</row>
<row>
<entry><literal>SQL_ASCII</literal></entry>
<entry>unspecified (see text)</entry>
<entry><emphasis>any</></entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>UHC</literal></entry>
<entry>Unified Hangul Code</entry>
<entry>Korean</entry>
<entry>No</entry>
<entry>1-2</entry>
<entry><literal>WIN949</>, <literal>Windows949</></entry>
</row>
<row>
<entry><literal>UTF8</literal></entry>
<entry>Unicode, 8-bit</entry>
<entry><emphasis>all</></entry>
<entry>Yes</entry>
<entry>1-4</entry>
<entry><literal>Unicode</></entry>
</row>
<row>
<entry><literal>WIN866</literal></entry>
<entry>Windows CP866</entry>
<entry>Cyrillic</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>ALT</></entry>
</row>
<row>
<entry><literal>WIN874</literal></entry>
<entry>Windows CP874</entry>
<entry>Thai</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>WIN1250</literal></entry>
<entry>Windows CP1250</entry>
<entry>Central European</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>WIN1251</literal></entry>
<entry>Windows CP1251</entry>
<entry>Cyrillic</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>WIN</></entry>
</row>
<row>
<entry><literal>WIN1252</literal></entry>
<entry>Windows CP1252</entry>
<entry>Western European</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>WIN1253</literal></entry>
<entry>Windows CP1253</entry>
<entry>Greek</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>WIN1254</literal></entry>
<entry>Windows CP1254</entry>
<entry>Turkish</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>WIN1255</literal></entry>
<entry>Windows CP1255</entry>
<entry>Hebrew</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>WIN1256</literal></entry>
<entry>Windows CP1256</entry>
<entry>Arabic</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>WIN1257</literal></entry>
<entry>Windows CP1257</entry>
<entry>Baltic</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry></entry>
</row>
<row>
<entry><literal>WIN1258</literal></entry>
<entry>Windows CP1258</entry>
<entry>Vietnamese</entry>
<entry>Yes</entry>
<entry>1</entry>
<entry><literal>ABC</>, <literal>TCVN</>, <literal>TCVN5712</>, <literal>VSCII</></entry>
</row>
</tbody>
</tgroup>
</table>
<para>
Not all client <acronym>API</>s support all the listed character sets. For example, the
<productname>PostgreSQL</>
JDBC driver does not support <literal>MULE_INTERNAL</>, <literal>LATIN6</>,
<literal>LATIN8</>, and <literal>LATIN10</>.
</para>
<para>
The <literal>SQL_ASCII</> setting behaves considerably differently
from the other settings. When the server character set is
<literal>SQL_ASCII</>, the server interprets byte values 0-127
according to the ASCII standard, while byte values 128-255 are taken
as uninterpreted characters. No encoding conversion will be done when
the setting is <literal>SQL_ASCII</>. Thus, this setting is not so
much a declaration that a specific encoding is in use, as a declaration
of ignorance about the encoding. In most cases, if you are
working with any non-ASCII data, it is unwise to use the
<literal>SQL_ASCII</> setting because
<productname>PostgreSQL</productname> will be unable to help you by
converting or validating non-ASCII characters.
</para>
</sect2>
<sect2>
<title>Setting the Character Set</title>
<para>
<command>initdb</> defines the default character set (encoding)
for a <productname>PostgreSQL</productname> cluster. For example,
<screen>
initdb -E EUC_JP
</screen>
sets the default character set to
<literal>EUC_JP</literal> (Extended Unix Code for Japanese). You
can use <option>--encoding</option> instead of
<option>-E</option> if you prefer longer option strings.
If no <option>-E</> or <option>--encoding</option> option is
given, <command>initdb</> attempts to determine the appropriate
encoding to use based on the specified or default locale.
</para>
<para>
You can specify a non-default encoding at database creation time,
provided that the encoding is compatible with the selected locale:
<screen>
createdb -E EUC_KR -T template0 --lc-collate=ko_KR.euckr --lc-ctype=ko_KR.euckr korean
</screen>
This will create a database named <literal>korean</literal> that
uses the character set <literal>EUC_KR</literal>, and locale <literal>ko_KR</literal>.
Another way to accomplish this is to use this SQL command:
<programlisting>
CREATE DATABASE korean WITH ENCODING 'EUC_KR' LC_COLLATE='ko_KR.euckr' LC_CTYPE='ko_KR.euckr' TEMPLATE=template0;
</programlisting>
Notice that the above commands specify copying the <literal>template0</>
database. When copying any other database, the encoding and locale
settings cannot be changed from those of the source database, because
that might result in corrupt data. For more information see
<xref linkend="manage-ag-templatedbs">.
</para>
<para>
The encoding for a database is stored in the system catalog
<literal>pg_database</literal>. You can see it by using the
<command>psql</command> <option>-l</option> option or the
<command>\l</command> command.
<screen>
$ <userinput>psql -l</userinput>
List of databases
Name | Owner | Encoding | Collation | Ctype | Access Privileges
-----------+----------+-----------+-------------+-------------+-------------------------------------
clocaledb | hlinnaka | SQL_ASCII | C | C |
englishdb | hlinnaka | UTF8 | en_GB.UTF8 | en_GB.UTF8 |
japanese | hlinnaka | UTF8 | ja_JP.UTF8 | ja_JP.UTF8 |
korean | hlinnaka | EUC_KR | ko_KR.euckr | ko_KR.euckr |
postgres | hlinnaka | UTF8 | fi_FI.UTF8 | fi_FI.UTF8 |
template0 | hlinnaka | UTF8 | fi_FI.UTF8 | fi_FI.UTF8 | {=c/hlinnaka,hlinnaka=CTc/hlinnaka}
template1 | hlinnaka | UTF8 | fi_FI.UTF8 | fi_FI.UTF8 | {=c/hlinnaka,hlinnaka=CTc/hlinnaka}
(7 rows)
</screen>
</para>
<important>
<para>
On most modern operating systems, <productname>PostgreSQL</productname>
can determine which character set is implied by the <envar>LC_CTYPE</>
setting, and it will enforce that only the matching database encoding is
used. On older systems it is your responsibility to ensure that you use
the encoding expected by the locale you have selected. A mistake in
this area is likely to lead to strange behavior of locale-dependent
operations such as sorting.
</para>
<para>
<productname>PostgreSQL</productname> will allow superusers to create
databases with <literal>SQL_ASCII</> encoding even when
<envar>LC_CTYPE</> is not <literal>C</> or <literal>POSIX</>. As noted
above, <literal>SQL_ASCII</> does not enforce that the data stored in
the database has any particular encoding, and so this choice poses risks
of locale-dependent misbehavior. Using this combination of settings is
deprecated and may someday be forbidden altogether.
</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 set combinations. The conversion information is stored in the
<literal>pg_conversion</> system catalog. <productname>PostgreSQL</>
comes with some predefined conversions, as shown in <xref
linkend="multibyte-translation-table">. You can create a new
conversion using the SQL command <command>CREATE CONVERSION</command>.
</para>
<table id="multibyte-translation-table">
<title>Client/Server Character Set Conversions</title>
<tgroup cols="2">
<thead>
<row>
<entry>Server Character Set</entry>
<entry>Available Client Character Sets</entry>
</row>
</thead>
<tbody>
<row>
<entry><literal>BIG5</literal></entry>
<entry><emphasis>not supported as a server encoding</emphasis>
</entry>
</row>
<row>
<entry><literal>EUC_CN</literal></entry>
<entry><emphasis>EUC_CN</emphasis>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>EUC_JP</literal></entry>
<entry><emphasis>EUC_JP</emphasis>,
<literal>MULE_INTERNAL</literal>,
<literal>SJIS</literal>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>EUC_KR</literal></entry>
<entry><emphasis>EUC_KR</emphasis>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>EUC_TW</literal></entry>
<entry><emphasis>EUC_TW</emphasis>,
<literal>BIG5</literal>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>GB18030</literal></entry>
<entry><emphasis>not supported as a server encoding</emphasis>
</entry>
</row>
<row>
<entry><literal>GBK</literal></entry>
<entry><emphasis>not supported as a server encoding</emphasis>
</entry>
</row>
<row>
<entry><literal>ISO_8859_5</literal></entry>
<entry><emphasis>ISO_8859_5</emphasis>,
<literal>KOI8R</literal>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>,
<literal>WIN866</literal>,
<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>
</entry>
</row>
<row>
<entry><literal>JOHAB</literal></entry>
<entry><emphasis>JOHAB</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>KOI8R</literal></entry>
<entry><emphasis>KOI8R</emphasis>,
<literal>ISO_8859_5</literal>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>,
<literal>WIN866</literal>,
<literal>WIN1251</literal>
</entry>
</row>
<row>
<entry><literal>KOI8U</literal></entry>
<entry><emphasis>KOI8U</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>LATIN1</literal></entry>
<entry><emphasis>LATIN1</emphasis>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>LATIN2</literal></entry>
<entry><emphasis>LATIN2</emphasis>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>,
<literal>WIN1250</literal>
</entry>
</row>
<row>
<entry><literal>LATIN3</literal></entry>
<entry><emphasis>LATIN3</emphasis>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>LATIN4</literal></entry>
<entry><emphasis>LATIN4</emphasis>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>LATIN5</literal></entry>
<entry><emphasis>LATIN5</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>LATIN6</literal></entry>
<entry><emphasis>LATIN6</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>LATIN7</literal></entry>
<entry><emphasis>LATIN7</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>LATIN8</literal></entry>
<entry><emphasis>LATIN8</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>LATIN9</literal></entry>
<entry><emphasis>LATIN9</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>LATIN10</literal></entry>
<entry><emphasis>LATIN10</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>MULE_INTERNAL</literal></entry>
<entry><emphasis>MULE_INTERNAL</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>KOI8R</literal>,
<literal>LATIN1</literal> to <literal>LATIN4</literal>,
<literal>SJIS</literal>,
<literal>WIN866</literal>,
<literal>WIN1250</literal>,
<literal>WIN1251</literal>
</entry>
</row>
<row>
<entry><literal>SJIS</literal></entry>
<entry><emphasis>not supported as a server encoding</emphasis>
</entry>
</row>
<row>
<entry><literal>SQL_ASCII</literal></entry>
<entry><emphasis>any (no conversion will be performed)</emphasis>
</entry>
</row>
<row>
<entry><literal>UHC</literal></entry>
<entry><emphasis>not supported as a server encoding</emphasis>
</entry>
</row>
<row>
<entry><literal>UTF8</literal></entry>
<entry><emphasis>all supported encodings</emphasis>
</entry>
</row>
<row>
<entry><literal>WIN866</literal></entry>
<entry><emphasis>WIN866</emphasis>,
<literal>ISO_8859_5</literal>,
<literal>KOI8R</literal>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>,
<literal>WIN1251</literal>
</entry>
</row>
<row>
<entry><literal>WIN874</literal></entry>
<entry><emphasis>WIN874</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>WIN1250</literal></entry>
<entry><emphasis>WIN1250</emphasis>,
<literal>LATIN2</literal>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>WIN1251</literal></entry>
<entry><emphasis>WIN1251</emphasis>,
<literal>ISO_8859_5</literal>,
<literal>KOI8R</literal>,
<literal>MULE_INTERNAL</literal>,
<literal>UTF8</literal>,
<literal>WIN866</literal>
</entry>
</row>
<row>
<entry><literal>WIN1252</literal></entry>
<entry><emphasis>WIN1252</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>WIN1253</literal></entry>
<entry><emphasis>WIN1253</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>WIN1254</literal></entry>
<entry><emphasis>WIN1254</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>WIN1255</literal></entry>
<entry><emphasis>WIN1255</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>WIN1256</literal></entry>
<entry><emphasis>WIN1256</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>WIN1257</literal></entry>
<entry><emphasis>WIN1257</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
<row>
<entry><literal>WIN1258</literal></entry>
<entry><emphasis>WIN1258</emphasis>,
<literal>UTF8</literal>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
To enable 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>
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>
<application>libpq</> (<xref linkend="libpq-control">) has functions to control the client encoding.
</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 '<replaceable>value</>';
</programlisting>
Also you can use the standard SQL syntax <literal>SET NAMES</literal>
for this purpose:
<programlisting>
SET NAMES '<replaceable>value</>';
</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 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
— suppose you chose <literal>EUC_JP</literal> for the
server and <literal>LATIN1</literal> for the client, and some
Japanese characters are returned that do not have a representation in
<literal>LATIN1</literal> — an error is reported.
</para>
<para>
If the client character set is defined as <literal>SQL_ASCII</>,
encoding conversion is disabled, regardless of the server's character
set. Just as for the server, use of <literal>SQL_ASCII</> is unwise
unless you are working with all-ASCII data.
</para>
</sect2>
<sect2>
<title>Further Reading</title>
<para>
These are good sources to start learning about various kinds of encoding
systems.
<variablelist>
<varlistentry>
<term><citetitle>CJKV Information Processing: Chinese, Japanese, Korean & Vietnamese Computing</citetitle></term>
<listitem>
<para>
Contains detailed explanations of <literal>EUC_JP</literal>,
<literal>EUC_CN</literal>, <literal>EUC_KR</literal>,
<literal>EUC_TW</literal>.
</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 3629</term>
<listitem>
<para>
<acronym>UTF</acronym>-8 (8-bit UCS/Unicode Transformation
Format) is defined here.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</sect2>
</sect1>
</chapter>
|