使用pg_dump结果作为pg_restore的输入 [英] Use pg_dump result as input for pg_restore

查看:145
本文介绍了使用pg_dump结果作为pg_restore的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在同一服务器上克隆一个数据库(仅架构)。
我想使用pg_dump的输入并将其通过管道传输到pg_restore。我知道可以使用psql完成此操作,但是psql没有 -c clean选项。

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_restore吗?

Is this possible but with pg_restore ?

 pg_dump --schema public dbName | psql dbNameTest


推荐答案

以下内容接近:

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

如果 dbNameTest 还不存在,则无法使用。下面的工作(尽管它抱怨 dbNameTest 已经存在。我可以忍受)

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

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

A带有短选项的oneliner将是:

A oneliner with short options would be:

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

sh脚本 pg_copy_schema 会类似于:

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

这篇关于使用pg_dump结果作为pg_restore的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆