恢复PostgreSQL数据库时出错"无效字节序列"

dil*_*ddy 2 postgresql database-restore database-backups postgresql-9.1 postgresql-8.4

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

WARNING: errors ignored on restore: 4 
Run Code Online (Sandbox Code Playgroud)

此外,经过调查,我发现所有表中有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
Run Code Online (Sandbox Code Playgroud)

我尝试在Google上研究我的问题,但没有结果.请帮助恢复这三个表没有任何错误.

Dan*_*ité 11

较旧版本的PostgreSQL在UTF-8合规性方面不如新版本严格.据推测,您正在尝试将包含无效UTF-8的数据从这样的旧版本恢复到较新版本.

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

  1. 将表中的内容从转储文件解压缩到SQL纯文本文件中:

    pg_restore --table=tablename --data-only dumpfile >plaintext.sql
    
    Run Code Online (Sandbox Code Playgroud)
  2. 删除文本编辑器中的无效字符或自动删除 iconv:

    iconv -c -f UTF-8 -t UTF-8 <plaintext.sql >plaintext-cleaned.sql
    
    Run Code Online (Sandbox Code Playgroud)
  3. 导入已清理的数据:

    psql dbname < plaintext-cleaned.sql
    
    Run Code Online (Sandbox Code Playgroud)