使用pg_dump结果作为pg_restore的输入

dan*_*car 9 postgresql

我需要在同一台服务器上克隆数据库(仅架构).我想使用pg_dump的输入并将其传递给pg_restore.我知道这可以用psql完成,但psql没有"-c clean"选项.

这有可能,但pg_restore?

 pg_dump --schema public dbName | psql dbNameTest
Run Code Online (Sandbox Code Playgroud)

art*_*rtm 14

接下来是:

pg_dump --schema-only --format c dbName | \
  pg_restore --schema-only --clean --dbname=dbNameTest
Run Code Online (Sandbox Code Playgroud)

除非它不dbNameTest存在,否则它不起作用.以下工作(尽管如果dbNameTest已经存在则会抱怨.我可以忍受)

createdb dbNameTest
pg_dump --schema-only --format c dbName | \
  pg_restore --schema-only --clean --dbname=dbNameTest
Run Code Online (Sandbox Code Playgroud)

短期选择的oneliner将是:

createdb dbNameTest ; pg_dump -s -F c dbName | pg_restore -s -c -d dbNameTest
Run Code Online (Sandbox Code Playgroud)

一个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"
Run Code Online (Sandbox Code Playgroud)

  • 该操作员只想复制数据库模式 (2认同)

Don*_*oma 2

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

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