还原 PostgreSQL 数据库时出错"invalid byte sequence"



今天早些时候,我试图使用pgAdmin III从生产中恢复我的PostgreSQL(8.1.22)数据库。然而,在恢复过程完成后,它开始抛出错误,如:

WARNING: errors ignored on restore: 4 

此外,经过调查,我发现在所有表中有3个表没有被恢复(包含0行)。当我检查日志时,我在3个表附近发现了以下错误:

pg_restore: [archiver (db)] Error from TOC entry 5390; 0 442375 TABLE DATA tablename postgres
pg_restore: [archiver (db)] COPY failed: ERROR:  invalid byte sequence for encoding "UTF8": 0xea0942
HINT:  This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
CONTEXT:  COPY tablename , line 7875

我试着在谷歌上研究我的问题,但没有结果。请帮助恢复这三张表,不要出现任何错误。

旧版本的PostgreSQL在UTF-8兼容方面不如新版本严格。可能您正试图将包含无效UTF-8的数据从旧版本恢复到新版本。

必须清除无效字符串。您可以对由于以下错误而未导入的每个表执行该过程:

  1. 将表的内容从转储文件提取到SQL纯文本文件中:

    pg_restore --table=tablename --data-only dumpfile >plaintext.sql
    
  2. 删除文本编辑器中的无效字符或使用iconv:自动删除无效字符

    iconv -c -f UTF-8 -t UTF-8 <plaintext.sql >plaintext-cleaned.sql
    
  3. 导入经过净化的数据:

    psql dbname < plaintext-cleaned.sql
    

相关内容

最新更新