gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:16:59 25 4
gpt4 key购买 nike

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

如果使用 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 已经存在它会提示。我可以接受)

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

具有短选项的单线将是:

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"

关于postgresql - 使用 pg_dump 结果作为 pg_restore 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11103279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com