如何从自定义格式的转储文件中恢复PostgreSQL数据库?

3

我遇到了一个问题,使用pg_restore进行数据库还原时无法正常工作。我在我的Mac上安装了Postgres 12.2,并运行High Sierra (10.13.6),它是通过Brew安装的。

我创建了一个简单的名为"foo"的数据库,其中包含一个名为"foo"的表,以测试此功能:

Nates-MacBook-Pro:k8s natereed$ psql -d foo
psql (12.2, server 12.1)
Type "help" for help.

foo=# SELECT table_schema,table_name
FROM information_schema.tables 
WHERE table_schema='public' ORDER BY table_schema,table_name;
table_schema | table_name 
--------------+------------
public       | foo
(1 row)

生成转储文件:

pg_dump -Fc foo -f foo.backup

当我尝试还原时,没有任何反应。pg_restore只是卡住了:

pg_restore -h localhost -p 5432 -U postgres -v -Fc -f foo.backup

我尝试了这个命令的各种排列组合,但每次它都会停在那里。在活动监视器中,我可以看到这个进程,但它并没有使用任何CPU。


“-f” 指定的是 输出 文件,而不是输入文件。从命令行中删除 “-f” - 最后一个参数(没有“开关”)指定了输入文件(如手册中所述):pg_restore -h localhost -p 5432 -U postgres -v -Fc foo.backup - user330315
1个回答

4

在线帮助说明:

pg_restore restores a PostgreSQL database from an archive created by pg_dump.

Usage:
  pg_restore [OPTION]... [FILE]

General options:
  -d, --dbname=NAME        connect to database name
  -f, --file=FILENAME      output file name
  -F, --format=c|d|t       backup file format (should be automatic)
  -l, --list               print summarized TOC of the archive
  -v, --verbose            verbose mode
  -V, --version            output version information, then exit
  -?, --help               show this help, then exit

对于 pg_restore,-f 是输出文件的参数,而不是输入文件的参数。在你的例子中去掉 -f:

pg_restore -h localhost -p 5432 -U postgres -v -Fc foo.backup

Nates-MacBook-Pro:k8s natereed$ pg_restore -h localhost -p 5432 -U natereed -v -Fc pg_restore: 错误:必须指定 -d/--dbname 和 -f/--file 中的一个 - Nate Reed
假设我想从备份中创建数据库。必须指定要还原的文件或数据库之一。指定要从中还原的备份文件的正确方法是什么?输出文件名(-f)不是指我们正在还原的文件吗? - Nate Reed
@NateReed:来自手册:"-f 指定生成脚本的输出文件,或与 -l 一起使用时用于列表的输出文件"。 - user330315
尝试使用以下命令:pg_restore -h localhost -p 5432 -U postgres -v -C -d <dbname> -Fc foo.backup(其中-C表示创建数据库)。 - pifor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接