postgresql 使用 pg_dump 结果作为 pg_restore 的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11103279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:01:06  来源:igfitidea点击:

Use pg_dump result as input for pg_restore

postgresql

提问by danidacar

I need to clone a database (only schema) on the same server. I would like to use the input from pg_dump and pipe it to pg_restore. I know this can be done with psql but psql doesn't have a "-c clean" option.

我需要在同一台服务器上克隆一个数据库(仅模式)。我想使用来自 pg_dump 的输入并将其通过管道传输到 pg_restore。我知道这可以用 psql 来完成,但 psql 没有“-c clean”选项。

Is this possible but with pg_restore ?

这是可能的,但使用 pg_restore 吗?

 pg_dump --schema public dbName | psql dbNameTest

回答by artm

The following comes close:

以下接近:

pg_dump --schema-only --format c dbName | \
  pg_restore --schema-only --clean --dbname=dbNameTest

Except it doesn't work if the dbNameTestdoesn't exist yet. The following does the job (although it complains if the dbNameTestalready exists. I can live with that)

除非它不dbNameTest存在,否则它不起作用。下面的工作(虽然它会抱怨如果dbNameTest已经存在。我可以忍受)

createdb dbNameTest
pg_dump --schema-only --format c dbName | \
  pg_restore --schema-only --clean --dbname=dbNameTest

A oneliner with short options would be:

具有短选项的单线将是:

createdb dbNameTest ; pg_dump -s -F c dbName | pg_restore -s -c -d dbNameTest

A sh script pg_copy_schemawould go something like:

sh 脚本pg_copy_schema将类似于:

#!/bin/sh
if [ -z "" ] ; then echo "Usage: `basename ##代码##` original-db new-db" ; exit 1 ; fi
echo "Copying schema of  to "
createdb "" 2> /dev/null
pg_dump --schema-only --format c "" | pg_restore --schema-only --clean --dbname=""

回答by Dondi Michael Stroma

http://www.postgresql.org/docs/9.1/static/app-pgdump.html

http://www.postgresql.org/docs/9.1/static/app-pgdump.html

You need to use -F combined with -c , -d, or -t option with pg_dump in order to use it with pg_restore. You can't use pg_restore with a plain-text SQL dump.

您需要将 -F 与 -c 、 -d 或 -t 选项与 pg_dump 结合使用,以便将其与 pg_restore 一起使用。您不能将 pg_restore 与纯文本 SQL 转储一起使用。