pg_restore: [archiver] 在文件头中没有找到魔法字符串

maj*_*aja 23 postgresql backup postgresql-9.1 pg-dump

我正在使用 PostgreSQL 9.1 并希望恢复使用pg_dump以下命令生成的备份文件:

sudo pg_dump -h 127.0.0.1 -U postgres --clean --inserts -E UTF8 -f out.sql database_name
Run Code Online (Sandbox Code Playgroud)

该命令生成一个有效的 sql 文件,首先删除任何现有的数据库对象,然后生成所有表、索引、序列等,最后插入数据。

当我尝试使用以下方法恢复生成的备份文件时:(添加换行符仅用于显示目的)

sudo pg_restore 
    -d database_name -h 127.0.0.1 -U postgres
    --format=c --clean --create out.sql
Run Code Online (Sandbox Code Playgroud)

它失败并打印:

pg_restore: [archiver] did not find magic string in file header
Run Code Online (Sandbox Code Playgroud)

这是什么原因?

Dan*_*ité 24

您正在恢复,pg_restore --format=c ...但未使用pg_dump完成--format=c,而是使用默认的 格式完成。

pg_dump联机帮助页:

  -F format, --format=format
       Selects the format of the output.  format can be one of the
       following:

       p, plain
           Output a plain-text SQL script file (the default).
Run Code Online (Sandbox Code Playgroud)

纯格式的转储应直接提供给psql命令行工具,pg_restore不知道它是什么,这就是此错误消息的原因:未在文件头中找到魔术字符串

您可以more out.sql在 shell 中直接查看转储文件,您将看到可读的 SQL 命令。用 恢复它psql -f out.sql [other options]。您可能希望首先创建目标数据库,因为调用中--create不存在该选项pg_dump

另一方面,您可以重新调用转储以添加--format=c其选项。那么这将是相反的:pg_restore必须用于解释自定义格式的转储文件。