PostgreSQL如何导入SJIS字符集的文件

磨砺技术珠矶,践行数据之道,追求卓越价值 
回到上一级页面: PostgreSQL杂记页     回到顶级页面:PostgreSQL索引页 
[作者 高健@博客园  luckyjackgao@gmail.com]

 

外部文件来自于Windows,日语SJIS字符集。

由于SJIS字符集和标准的POSIX有冲突,故Linux平台一般是不支持此字符集的。而PostgreSQL的服务器端也不支持它。

但是它支持客户端使用SJIS,因此导入时,可以用PostgreSQL的自动变换功能。

具体如下:

-----------------------------
Linux Encoding:
[root@pg200 data]# echo $LANG
ja_JP.UTF-8
[root@pg200 data]# 


-----------------------------
PostgreSQL Encoding:
postgres=# x
Expanded display is on.
postgres=# l
List of databases
-[ RECORD 1 ]-----+----------------------
Name              | postgres
Owner             | postgres
Encoding          | UTF8
Collate           | ja_JP.UTF-8
Ctype             | ja_JP.UTF-8
Access privileges |
-[ RECORD 2 ]-----+----------------------
Name              | template0
Owner             | postgres
Encoding          | UTF8
Collate           | ja_JP.UTF-8
Ctype             | ja_JP.UTF-8
Access privileges | =c/postgres
                  | postgres=CTc/postgres -[ RECORD 3 ]-----+----------------------
Name              | template1
Owner             | postgres
Encoding          | UTF8
Collate           | ja_JP.UTF-8
Ctype             | ja_JP.UTF-8
Access privileges | =c/postgres
                  | postgres=CTc/postgres

postgres=# 


-----------------------------
Windows side File:(sjistest.txt)
ああああ
いいいい
うううう
ええええ
おおおお


-----------------------------
Directly Show it in Linux:
[root@pg200 data]# cat sjistest.txt
        
        
        
        
        


-----------------------------
In PostgreSQL、directly COPY:
[postgres@pg200 pgsql]$ ./bin/psql
psql (9.2.4)
Type "help" for help.

postgres=# copy t1 from '/usr/local/pgsql/data/sjistest.txt';
ERROR:  invalid byte sequence for encoding "UTF8": 0x82
CONTEXT:  COPY t1, line 1
postgres=# 


-----------------------------
PostgreSQL automatically changing:

[postgres@pg200 pgsql]$ ./bin/psql
psql (9.2.4)
Type "help" for help.

postgres=# set client_encoding to 'SJIS'; SET postgres=# copy t1 from '/usr/local/pgsql/data/sjistest.txt';
COPY 5
postgres=# set client_encoding to 'UTF8'; SET postgres=# select * from t1;
   val    
----------
 ああああ
 いいいい
 うううう
 ええええ
 おおおお
(5 rows)

postgres=# 

 

[作者 高健@博客园  luckyjackgao@gmail.com]
回到上一级页面: PostgreSQL杂记页     回到顶级页面:PostgreSQL索引页 
磨砺技术珠矶,践行数据之道,追求卓越价值 

原文地址:https://www.cnblogs.com/gaojian/p/3351237.html