我需要在同一臺服務器上克隆一個數據庫(只有模式)。 我想使用來自pg_dump的輸入並將其傳遞給pg_restore。我知道這可以用psql完成,但psql沒有「-c clean」選項。使用pg_dump結果作爲pg_restore的輸入
這是可能的,但與pg_restore?
pg_dump --schema public dbName | psql dbNameTest
我需要在同一臺服務器上克隆一個數據庫(只有模式)。 我想使用來自pg_dump的輸入並將其傳遞給pg_restore。我知道這可以用psql完成,但psql沒有「-c clean」選項。使用pg_dump結果作爲pg_restore的輸入
這是可能的,但與pg_restore?
pg_dump --schema public dbName | psql dbNameTest
http://www.postgresql.org/docs/9.1/static/app-pgdump.html
您需要使用-F與pg_dump的-c,-d或-t選項組合以和pg_restore使用它。您不能將pg_restore用於純文本SQL轉儲。
我相信正確的選項是'--format = t' –
下接近:
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"
你試過'pg_dump --format = c ... | pg_restore ...' –
使用'pg_dump --schema-only(-c -C ...更多選項...)databasename> myfile.sql',並在使用psql提交之前編輯輸出文件。 --schema-only的輸出並不那麼大。 – wildplasser
使用psql有什麼問題? –