PostgreSQLsql檔案編碼引起的資料匯入亂碼或查詢字符集異常報錯(invalidbytesequence)
背景
當使用者客戶端字符集與服務端字符集不匹配時,寫入的多位元組字元(例如中文)可能出現亂碼。
例子
資料庫字符集為sql_ascii,允許儲存任意編碼字元。
digoal@pg11-320tb-zfs-> psql
psql (11beta4)
Type “help” for help.
postgres=# l+
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description |
---|---|---|---|---|---|---|---|---|
postgres | postgres | SQL_ASCII | C | en_US.utf8 | 140 TB | pg_default | default administrative connection database | |
template0 | postgres | SQL_ASCII | C | en_US.utf8 | =c/postgres + | 15 MB | pg_default | unmodifiable empty database |
postgres=CTc/postgres | ||||||||
template1 | postgres | SQL_ASCII | C | en_US.utf8 | =c/postgres + | 15 MB | pg_default | default template for new databases |
(3 rows)
客戶端為utf8編碼
digoal@pg11-320tb-zfs-> echo $LANG
en_US.utf8
編輯一個檔案,以UTF8編碼
vi test.sql
insert into tbl values (1, `你好`);
內容如下
digoal@pg11-320tb-zfs-> cat test.sql
insert into tbl values (1, `你好`);
編碼如下
digoal@pg11-320tb-zfs-> file test.sql
test.sql: UTF-8 Unicode text
轉換為GBK,寫入資料庫
digoal@pg11-320tb-zfs-> iconv –help
Usage: iconv [OPTION…] [FILE…]
Convert encoding of given files from one encoding to another.
Input/Output format specification:
-f, –from-code=NAME encoding of original text
-t, –to-code=NAME encoding for output
Information:
-l, –list list all known coded character sets
Output control:
-c omit invalid characters from output
-o, –output=FILE output file
-s, –silent suppress warnings
--verbose print progress information
-?, –help Give this help list
--usage Give a short usage message
-V, –version Print program version
Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.
For bug reporting instructions, please see:
http://www.gnu.org/software/libc/bugs.html.
digoal@pg11-320tb-zfs-> iconv -f UTF8 -t GBK test.sql|psql -f –
INSERT 0 1
或者這樣寫入(client_encoding和server都設定為sql_ascii時,不檢查編碼合法性,直接存入資料庫)
digoal@pg11-320tb-zfs-> iconv -f UTF8 -t GBK test.sql -o test.sql.gbk
digoal@pg11-320tb-zfs-> psql
psql (11beta4)
Type “help” for help.
postgres=# set client_encoding =sql_ascii;
SET
postgres=# i ./test.sql.gbk
INSERT 0 1
當設定客戶端client_encoding為utf8編碼時,由於存入的資料編碼不合法,導致查詢異常
digoal@pg11-320tb-zfs-> psql
psql (11beta4)
Type “help” for help.
postgres=# set client_encoding =utf8;
SET
postgres=# select * from tbl;
ERROR: invalid byte sequence for encoding “UTF8”: 0xc4 0xe3
當client_encoding設定為GBK編碼,查詢為亂碼
postgres=# set client_encoding =gbk;
SET
postgres=# select * from tbl;
id | info |
---|---|
1 | ?oí |
參考
《PostgreSQL 多位元組字符集合法性檢測》
《[轉] SqlServe到PG遷移錯誤:無效的編碼序列”UTF8″: 0x00》
《PostgreSQL UTF8 和 GB18030編碼map檔案不完整的問題》
《PostgreSQL WHY ERROR: invalid byte sequence for encoding “UTF8″》
《PostgreSQL SQL_ASCII encoding introduce》
《PostgreSQL Server Encoding sql_ascii attention》
轉自阿里雲德哥
相關文章
- mysql匯入資料亂碼錯誤一則MySql
- 匯入sql檔案出現亂碼SQL
- 字符集例子-同一字元不同字符集編碼不同及匯入匯出的亂碼字元
- office for Mac Excel匯入csv檔案亂碼MacExcel
- MySQL資料匯入匯出亂碼問題MySql
- BW資料匯入亂碼問題
- 資料檔案Resize引起的ORA-03297報錯
- 查詢dmp檔案(exp方式匯出) 字符集
- 管理工具匯入CSV檔案,中文資料亂碼的解決辦法。
- MySQL匯入資料亂碼、出錯等問題的解決辦法MySql
- Mysql資料庫使用Navicat Mysql匯入sql檔案報錯MySql資料庫
- sqlldr匯入資料中文亂碼SQL
- 同事編寫的sql指令碼檔案上傳到oracle 上顯示亂碼_字符集SQL指令碼Oracle
- java程式碼實現excel檔案資料匯入JavaExcel
- 檔案格式引起的指令碼執行錯誤指令碼
- Sublime Text 查詢時排除指定的資料夾或檔案
- Sqoop匯入資料異常處理OOP
- 演示字符集不同導致插入,查詢產生亂碼的過程
- 檔案系統異常引起的資料庫open失敗的解決過程資料庫
- Windows轉到linux中,檔案亂碼,檔案編碼轉換WindowsLinux
- Oracle -- 字符集編碼'GBK'庫資料匯入到'UFT-8'庫中 大量報錯 ORA-12899 解決方案Oracle
- 解決Excel資料匯入sqlite中的中文亂碼問題ExcelSQLite
- gvim如何制定開啟檔案的編碼字符集?
- MYSQL資料庫匯入資料時出現亂碼的解決辦法MySql資料庫
- MYSQL資料檔案匯入MySql
- 乾貨:blob匯出檔案亂碼解決方案
- Linux下刪除亂碼或特殊字元檔案Linux字元
- mysql匯入文字或excel檔案MySqlExcel
- c#匯出檔案,檔名中文亂碼解決方法C#
- 「Oracle」資料庫字符集編碼修改Oracle資料庫
- 求助:hibernate查詢出來是亂麻?但是插入到資料庫的資料卻不是亂碼?資料庫
- Loadrunner+引數化檔案編碼格式+獲取請求報文發生異常
- Linux查詢哪個程式佔用檔案或資料夾Linux
- ORACLE11G中us7ascii 字符集匯出檔案匯入zhs16gbk 庫中亂碼問題處理OracleASCII
- [Linux] linux 刪除亂碼的檔案&資料夾Linux
- hibernate異常之--count查詢異常
- 解決PHP匯出CSV檔案中文亂碼問題PHP
- TOMCAT 請求資料編碼亂碼 問題Tomcat