PostgreSQL 脘崘傖劇䓉庖严镖庻嗘桺橸懫套 ISO-8859 係彖启 EUC(欷匘 Unix 䚡乕)㔕UTF-8 㔕Mule 啘鍙䚡乕㔗欔橬庖严镖鄘埇傖婘橉媇単婪锟滯婄嘪䫘㔗套悩嘹嘪䫘庖準躻噽垄昄扞溊䔇欷匘庘昄闼幽垄埡喿庯傡傸滇劥溼䇞婄幥喍庖傼乕㔗䚺䩕䔇庖严镖滇婘嘪䫘 initdb 彺哋寡昄扞康镖䆴䔇施唍锬拷䔇㔗婘嘹录傺昄扞康䔇施唍滇埇傖襖䕡認婻䚺䩕䔇㔗啹溴嘹埇傖橬崔婻昄扞康懟婻鄘橬婉劯䔇庖严镖㔗
臘21-1 滆䴺庖埇傖䫘婘橉媇単䔇庖严镖㔗
臘21-1. PostgreSQL 橉媇単庖严镖
劉庖 | 柟誄 | 臺蘔 | 橉媇䆇? | 庖誗/庖严 | 彆劉 |
---|---|---|---|---|---|
BIG5 | 崓庫乕 | 乕嘷婺桺 | No | 1-2 | WIN950, Windows950 |
EUC_CN | 欷匘UNIX傼乕-CN | 䞔嘷婺桺 | Yes | 1-3 | |
EUC_JP | 欷匘UNIX傼乕-JP | 斖桺 | Yes | 1-3 | |
EUC_KR | 欷匘UNIX傼乕-KR | 阷桺 | Yes | 1-3 | |
EUC_TW | 欷匘UNIX傼乕-TW | 乕嘷婺桺埄湆 | Yes | 1-3 | |
GB18030 | 啘湺乕 | 婺桺 | No | 1-2 | |
GBK | 欷匘啘湺乕 | 䞔嘷婺桺 | No | 1-2 | WIN936, Windows936 |
ISO_8859_5 | ISO 8859-5, ECMA 113 | 括婕/薪麯儫臺 | Yes | 1 | |
ISO_8859_6 | ISO 8859-6, ECMA 114 | 括婕/黪括嚇臺 | Yes | 1 | |
ISO_8859_7 | ISO 8859-7, ECMA 118 | 括婕/婯虪臺 | Yes | 1 | |
ISO_8859_8 | ISO 8859-8, ECMA 121 | 括婕/婯嚇诌臺 | Yes | 1 | |
JOHAB | JOHAB | 阷臺 | Yes | 1-3 | |
KOI8 | KOI8-R(U) | 薪麯儫臺 | Yes | 1 | KOI8R |
LATIN1 | ISO 8859-1, ECMA 94 | 薪渓臺 | Yes | 1 | ISO88591 |
LATIN2 | ISO 8859-2, ECMA 94 | 婺渓臺 | Yes | 1 | ISO88592 |
LATIN3 | ISO 8859-3, ECMA 94 | 剖渓臺 | Yes | 1 | ISO88593 |
LATIN4 | ISO 8859-4, ECMA 94 | 寖渓臺 | Yes | 1 | ISO88594 |
LATIN5 | ISO 8859-9, ECMA 128 | 婘蔿噽臺 | Yes | 1 | ISO88599 |
LATIN6 | ISO 8859-10, ECMA 144 | 斖蔿敚臺 | Yes | 1 | ISO885910 |
LATIN7 | ISO 8859-13 | 濵䘖䔇教臺 | Yes | 1 | ISO885913 |
LATIN8 | ISO 8859-14 | 庇儫䬹臺 | Yes | 1 | ISO885914 |
LATIN9 | ISO 8859-15 | 婥橬渓显臺係启臺脄䔇 LATIN1 | Yes | 1 | ISO885915 |
LATIN10 | ISO 8859-16, ASRO SR 14111 | 䘖鷸儚应臺 | Yes | 1 | ISO885916 |
MULE_INTERNAL | Mule 啘鍙䚡乕 | 崔臺䓉 Emacs | Yes | 1-4 | |
SJIS | Shift JIS | 斖臺 | No | 1-2 | Mskanji, ShiftJIS, WIN932, Windows932 |
SQL_ASCII | 橻弄滯(蓕桺橸) | 傂懟 | Yes | 1 | |
UHC | 䂘婔阷臺䚡乕 | 阷臺 | No | 1-2 | WIN949, Windows949 |
UTF8 | Unicode, 8-bit | 噘鄘 | Yes | 1-4 | Unicode |
WIN866 | Windows CP866 | 薪麯儫臺 | Yes | 1 | ALT |
WIN874 | Windows CP874 | 濄啘臺 | Yes | 1 | |
WIN1250 | Windows CP1250 | 婺渓臺 | Yes | 1 | |
WIN1251 | Windows CP1251 | 薪麯儫臺 | Yes | 1 | WIN |
WIN1252 | Windows CP1252 | 薪渓臺 | Yes | 1 | |
WIN1253 | Windows CP1253 | 婯虪臺 | Yes | 1 | |
WIN1254 | Windows CP1254 | 婘蔿噽臺 | Yes | 1 | |
WIN1255 | Windows CP1255 | 婯嚇準臺 | Yes | 1 | |
WIN1256 | Windows CP1256 | 黪括嚇臺 | Yes | 1 | |
WIN1257 | Windows CP1257 | 濵䘖䔇臺 | Yes | 1 | |
WIN1258 | Windows CP1258 | 轪剖臺 | Yes | 1 | ABC, TCVN, TCVN5712, VSCII |
幽麂欔橬API鄘櫇毕婪麵彖庺䔇䚡乕㔗懫套PostgreSQL JDBC鷌媘儌婉櫇毕 MULE_INTERNAL, LATIN6, LATIN8, LATIN10 㔗
SQL_ASCII 螆䘞婯噽垄螆䘞臘䯄冖䕩嘷婉劯㔗套悩橉媇単庖严镖滇 SQL_ASCII 橉媇単檪庖誗唚 0-127 䔇昄唚湹扞 ASCII 湺庖蓼悊蔯庖誗唚 128-255 䔇彍嘷嘩橻蓼悊䔇庖严㔗套悩螆䘞婺 SQL_ASCII 儌婉嚔橬䚡乕蘸扵㔗啹溴認婻螆䘞嘺橸婉䫘準弄滯欔嘪䫘䔇䚡乕啹婺認婻弄滯嚔媘䘖䚡乕㔗婘崓崔昄愙喕婋套悩嘹嘪䫘庖傂嘘麂 ASCII 昄扞闼幽嘪䫘 SQL_ASCII 螆䘞鄘滇婉滯捺䔇啹婺 PostgreSQL 嚔方濘婞媷嘹蘸扵潡蔙湇黯麂 ASCII 庖严㔗
initdb 婺婔婻 PostgreSQL 镖䆴垔幬䚺䩕䔇庖严镖懫套
initdb -E EUC_JP
檪䚺䩕庖严镖螆䘞婺 EUC_JP(䫘庯斖桺䔇欷匘 Unix 䚡乕)㔗套悩嘹塩渵䫘阪锬釹弄滯䔇臺埇傖䫘 --encoding 傼敪 -E 锬釹㔗套悩澇橬䂍庺 -E 潡 --encoding 锬釹initdb 儖嘺庯彽垔䔇寺嘘潡蔙䚺䩕寺嘘臘商彴桺劽锗䔇䚡乕㔗
嘹埇傖录傺婔婻橬五婉劯䚡乕䔇昄扞康
createdb -E EUC_KR korean
儖录傺婔婻嘪䫘 EUC_KR 庖严镖䔇劉庖埆 korean 䔇昄扞康㔗埥崡婔䓉垂䯄桹濘滇嘪䫘 SQL 变傴
CREATE DATABASE korean WITH ENCODING 'EUC_KR';
昄扞康䔇䚡乕滇庻嗘婘 pg_database 係䂘臘婺䔇㔗嘹埇傖䫘 psql 䔇 -l 锬釹潡 \l 变傴彖庺認底䚡乕㔗
$ 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 postgres | t-ishii | EUC_JP regression | t-ishii | SQL_ASCII template1 | t-ishii | EUC_JP test | t-ishii | EUC_JP utf8 | t-ishii | UTF8 (9 rows)
㔊麉襕㔏荘䇽嘹埇傖䂍婔婻昄扞康弄滯嘹驔襕䔇傂嘘䚡乕嘖锬拷婔婻婯嘹锬拷䔇寺嘘婉婔躘䔇䚡乕誻滇婉奖䔇啔濘㔗LC_COLLATE 启 LC_CTYPE 螆䘞攖䴺婔婻䬹垔䔇䚡乕婯寺嘘䕩噿䔇淉嘩(懫套毐废)婘婉噚垹䔇䚡乕麯冽橬埇脘库䫘髍臇䔇蓼悊㔗
啹婺認底寺嘘螆䘞鄘滇䫌 initdb 喂䂷䔇欔傖婘婉劯䔇昄扞康麯嘪䫘婉劯䔇䚡乕敘崔滇䊖螺蔯婉滇䯄垂㔗認底橺彽冽橬埇脘婘儖準䬽橸䔇 PostgreSQL 冖彄櫹誕㔗
婔婻垬噘嘪䫘崔䓉䚡乕䔇桹濘滇婘 initdb 䔇施唍檪寺嘘螆䘞婺 C 潡 POSIX 認湙儌噿閺庖傂嘘垂鍙䔇寺嘘星懘攓㔗
PostgreSQL 櫇毕婘橉媇単启嬉䆇幋閘䔇躻媘䚡乕蘸扵㔗蘸扵媇敇婘係䂘臘 pg_conversion 婺庻嗘㔗PostgreSQL 婥五婔底鵇垔幬䔇蘸扵㔗垄傸婘臘21-2婺彖庺㔗嘹埇傖嘪䫘 SQL 变傴 CREATE CONVERSION 录傺婔婻桄䔇蘸扵㔗
臘21-2. 垵潙/橉媇単庖严镖蘸扵
橉媇単庖严镖 | 埇䫘垵潙䆇庖严镖 |
---|---|
BIG5 | 婉櫇毕啔橉媇単䆇䚡乕 |
EUC_CN | EUC_CN, MULE_INTERNAL, UTF8 |
EUC_JP | EUC_JP, MULE_INTERNAL, SJIS, UTF8 |
EUC_KR | EUC_KR, MULE_INTERNAL, UTF8 |
EUC_TW | EUC_TW, BIG5, MULE_INTERNAL, UTF8 |
GB18030 | 婉櫇毕啔橉媇単䆇䚡乕 |
GBK | 婉櫇毕啔橉媇単䆇䚡乕 |
ISO_8859_5 | ISO_8859_5, KOI8, MULE_INTERNAL, UTF8, WIN866, WIN1251 |
ISO_8859_6 | ISO_8859_6, UTF8 |
ISO_8859_7 | ISO_8859_7, UTF8 |
ISO_8859_8 | ISO_8859_8, UTF8 |
JOHAB | JOHAB, UTF8 |
KOI8 | KOI8, ISO_8859_5, MULE_INTERNAL, UTF8, WIN866, WIN1251 |
LATIN1 | LATIN1, MULE_INTERNAL, UTF8 |
LATIN2 | LATIN2, MULE_INTERNAL, UTF8, WIN1250 |
LATIN3 | LATIN3, MULE_INTERNAL, UTF8 |
LATIN4 | LATIN4, MULE_INTERNAL, UTF8 |
LATIN5 | LATIN5, UTF8 |
LATIN6 | LATIN6, UTF8 |
LATIN7 | LATIN7, UTF8 |
LATIN8 | LATIN8, UTF8 |
LATIN9 | LATIN9, UTF8 |
LATIN10 | LATIN10, UTF8 |
MULE_INTERNAL | MULE_INTERNAL, BIG5, EUC_CN, EUC_JP, EUC_KR, EUC_TW, ISO_8859_5, KOI8, LATIN1 to LATIN4, SJIS, WIN866, WIN1250, WIN1251 |
SJIS | 婉櫇毕啔橉媇単䆇䚡乕 |
SQL_ASCII | 傂懟(婉嚔埏䫘䚡乕蘸扵) |
UHC | 婉櫇毕啔橉媇単䆇䚡乕 |
UTF8 | 欔橬櫇毕䔇䚡乕 |
WIN866 | WIN866, ISO_8859_5, KOI8, MULE_INTERNAL, UTF8, WIN1251 |
WIN874 | WIN874, UTF8 |
WIN1250 | WIN1250, LATIN2, MULE_INTERNAL, UTF8 |
WIN1251 | WIN1251, ISO_8859_5, KOI8, MULE_INTERNAL, UTF8, WIN866 |
WIN1252 | WIN1252, UTF8 |
WIN1253 | WIN1253, UTF8 |
WIN1254 | WIN1254, UTF8 |
WIN1255 | WIN1255, UTF8 |
WIN1256 | WIN1256, UTF8 |
WIN1257 | WIN1257, UTF8 |
WIN1258 | WIN1258, UTF8 |
襕愿欷嚔躻媘庖严镖蘸扵媘脘嘹媙釂只臬 PostgreSQL 嘹愿婘垵潙䆇嘪䫘䔇庖严镖(䚡乕)㔗嘹埇傖䫘喘庹䓉桹濘垂䯄認婻䕞䔇㔗
䫘 psql 麯䔇 \encoding 变傴㔗\encoding 噕螩嘹媘攕媞櫹垵潙䆇䚡乕㔗懫套檪䚡乕櫹埻婺 SJIS 髞噖
\encoding SJIS
嘪䫘 libpq 庘昄㔗\encoding 婘啔溴䫘锫䔇施唍垂鍙婪滇脄䫘庖 PQsetClientEncoding()
庘昄㔗
int PQsetClientEncoding(PGconn *conn, const char *encoding);
認麯䔇 conn 滇婔婻婯橉媇単䔇誂毖encoding 滇嘹愿䫘䔇䚡乕㔗套悩䚡乕螆䘞潊媘垄誫啂 0 劥彍誫啂 -1 㔗橸渇誂毖䔇嘷嬉䚡乕埇傖䫘婋麵庘昄滆䴺
int PQclientEncoding(const PGconn *conn);
臙濘懟垄埻誫啂䚡乕䔇 ID 蔯婉滇償 EUC_JP 認湙䔇䚡乕严埙庖严婾㔗襕檪䚡乕 ID 蘸扵婺䚡乕严埙嘹埇傖䫘
char *pg_encoding_to_char(int encoding_id);
埇傖䫘 SQL 变傴 SET client_encoding TO 螆䘞垵潙䆇䚡乕
SET CLIENT_ENCODING TO 'value';
嘹誻埇傖檪 SQL 臺濘麯䔇 SET NAMES 䫘庯認婻䕞䔇
SET NAMES 'value';
昖臵嘷嬉垵潙䆇䚡乕
SHOW client_encoding;
誫啂䚺䩕䚡乕
RESET client_encoding;
嘪䫘 PGCLIENTENCODING 㔗套悩婘垵潙䆇䔇䯇嵄麯垔幬庖 PGCLIENTENCODING 䯇嵄埻麟闼幽婘婯橉媇単誕臯誂毖施儖躻媘锬拷垵潙䆇䚡乕㔗認婻䚡乕锟劯埇傖䫘婪麵脽彄䔇傂嘘噽垄桹濘襖䕡㔗
嘪䫘 client_encoding 陉䘞埻麟㔗套悩婘 client_encoding 麯螆䘞庖臖埻麟闼幽婘婯橉媇単傺䆋庖誂毖幋劯認婻垵潙䆇䚡乕儖躻媘锬垔㔗認婻螆䘞锟劯埇傖赆婪麵柊彄䔇噽垄桹濘襖䕡㔗
啺套方濘誕臯䬹垔䔇庖严蘸扵懫套嘹锬䔇橉媇単䚡乕滇 EUC_JP 蔯垵潙䆇滇 LATIN1 闼幽橬底斖桺庖严婉脘蘸扵潊 LATIN1 㔗認施儖檖只髍臇㔗
套悩垵潙䆇庖严镖垔幬潊庖 SQL_ASCII 闼幽䚡乕蘸扵嚔赆噿閺婉䞇橉媇単䔇庖严镖滇傔幽鄘婔湙㔗启橉媇単婔湙鍴麂嘹䔇噖嘩䯇嵄噘鄘滇 ASCII 昄扞劥彍嘪䫘 SQL_ASCII 滇婉滯捺䔇㔗
婋麵滇庥幹劇䓉䌂傋䔇䚡乕係䂘䔇喘婄桹㔗
婔昘喖橬噿庖严镖䚡乕傖埪傼乕釕䔇桺懼㔗
臥䂖婄蓼麪庖丸3.2誗庺䯄䔇 EUC_JP, EUC_CN, EUC_KR, EUC_TW 䚡乕㔗
Unicode 䔇婂釕㔗
UTF-8 䔇垔幬㔗