2012-06-19 17 views
4

我需要在同一臺服務器上克隆一個數據庫(只有模式)。 我想使用來自pg_dump的輸入並將其傳遞給pg_restore。我知道這可以用psql完成,但psql沒有「-c clean」選項。使用pg_dump結果作爲pg_restore的輸入

這是可能的,但與pg_restore?

pg_dump --schema public dbName | psql dbNameTest 
+1

你試過'pg_dump --format = c ... | pg_restore ...' –

+1

使用'pg_dump --schema-only(-c -C ...更多選項...)databasename> myfile.sql',並在使用psql提交之前編輯輸出文件。 --schema-only的輸出並不那麼大。 – wildplasser

+0

使用psql有什麼問題? –

回答

6

下接近:

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 

短選項的oneliner是:

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" 
相關問題