You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PostgreSQL未加-Fc参数的压缩备份文件恢复失败求助

问题:PostgreSQL备份文件恢复失败

备份背景

使用PostgreSQL 14.5执行备份命令:

pg_dump -Z6 -h localhost test_db -f test_db.tar -p port

原本计划添加-Fc(自定义归档格式)参数,但实际遗漏。

恢复报错情况

使用psql恢复的错误

执行psql恢复时出现无效命令及UTF8编码错误,具体日志:

psql:test_db.sql:117359: error: invalid command \gg<CB>t<E4>Iƣ<AC>=<AF>*ESC^TDuV^P^Ov<B6><B0>
psql:test_db.sql:117362: error: invalid command \a>@
psql:test_db.sql:117363: error: invalid command \"<99>k<86>
psql:test_db.sql:117372: error: invalid command \<9B>遥<FD><FE><BA>j
psql:test_db.sql:117406: ERROR:  invalid byte sequence for encoding "UTF8": 0x8a

使用pg_restore恢复的错误

执行pg_restore时提示:

pg_restore: error: input file does not appear to be a valid archive

已尝试的无效操作

  • cat test_db.tar | psql test_db -U postgres -h localhost -p port
  • 在psql终端执行\i testdb.tar
  • 将备份文件重命名为test_db.dump后尝试恢复

解决方案

你的备份文件是gzip压缩的纯SQL文本格式(仅用-Z6启用压缩但未指定-Fc归档格式),因此不能直接用pg_restore,也不能直接用psql读取压缩文件,必须先解压再恢复。

具体步骤

  1. 解压备份文件
    因-Z6采用gzip压缩,执行解压命令:

    gzip -d test_db.tar
    

    解压后会得到无后缀的文件,可重命名为.sql方便识别:

    mv test_db test_db.sql
    
  2. 用psql执行解压后的SQL文件
    运行以下命令完成恢复:

    psql -h localhost -p port -U postgres -d test_db -f test_db.sql
    

额外排查点

如果解压时提示不是gzip格式,先执行file test_db.tar查看文件实际类型,再用对应工具解压(比如bzip2压缩用bunzip2,lzma用unlzma)。

内容的提问来源于stack exchange,提问作者Santhosh Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 16:51:37