PostgreSQL copy 时提示:ERROR: invalid byte sequence for encoding "UTF8": 0xb3

时间:2022-11-28 11:46:18

测试时使用三种文件格式:

ISO-8859

Netpbm PBM image

ASCII

if [ $(file $filename|grep -c "ISO-8859") -gt 0 ]
then
   echo "ISO-8859" psql -c "copy $schemaname.$tbname from '$dirname/$filename' with(format 'csv', delimiter ', encoding 'ISO-8859-1')" $dbname elif [ $(file $filename|grep -c "ASCII") -gt 0 ]
then
   echo $format psql -c "copy $schemaname.$tbname from '$dirname/$filename' with(format 'csv', delimiter ', encoding 'UTF-8')" $dbname else psql -c "copy $schemaname.$tbname from '$dirname/$filename' with(format 'csv', delimiter ', encoding 'ISO-8859-1')" $dbname

这种处理方式在文件格式为ASCII时,copy中途仍然会出现编码错误提示。

ERROR: invalid byte sequence for encoding "UTF8": 0xb3 

 

最后摸索发现不管什么格式,都指定为ISO-8859-1就能处理。

 

全包容的编码格式,都能处理:

psql -c "copy $schemaname.$tbname from '$dirname/$filename' with(format 'csv', delimiter ', encoding 'ISO-8859-1')" $dbname