最近对SQL Server到PostgreSQL的数据迁移时出现了问题,返回的错误为:invalid byte sequence for encoding "UTF8": 0x00。
经查证pg源代码,该问题引起的原因是sql server的字符类型字段中含有空字符\0,该字符在pg中不支持。
问题重现:
1、PG客户端:
postgres=# create table text_test (id int,info text);
CREATE TABLE
postgres=# insert into text_test values (1,E'\0x00');
ERROR: invalid byte sequence for encoding "UTF8": 0x00
2、SQL Server产生数据
create table test_varchar(id int,name varchar(20));
insert into test_varchar values (1, 'name' + char(0));
insert into test_varchar values (1, 'name' + ''); 然后通过java程序进行获取数据并插入到PG,同样会得到错误信息:
invalid byte sequence for encoding "UTF8": 0x00
首先我们认为此为gb2312转化到UTF8时,发生了无法转化的错误。经查UTF8是变长的, 1-6个字节。他的编码规则如下:
Bits
Last code point
Byte 1<