2014-07-15 13 views
8

我有一個postgresql數據庫服務器,它有多個數據庫。如何獲取pg_dump -s以包含CREATE DATABASE命令?

postgres=# \list 
           List of databases 
    Name  | Owner | Encoding | Collate | Ctype | Access privileges 
--------------+----------+-----------+---------+-------+----------------------- 
test1  | postgres | UTF8  | C  | C  | 
test2  | postgres | SQL_ASCII | C  | C  | 
test3  | postgres | SQL_ASCII | C  | C  | 
test4  | postgres | SQL_ASCII | C  | C  | 
template0 | postgres | SQL_ASCII | C  | C  | =c/postgres   + 
       |   |   |   |  | postgres=CTc/postgres 
template1 | postgres | SQL_ASCII | C  | C  | postgres=CTc/postgres+ 
       |   |   |   |  | =c/postgres 
(6 rows) 

我需要一種方法來創建一個包含架構轉儲文件 - 包括CREATE DATABASE語句 - 數據庫我想傾倒。

到目前爲止,我已經想通了,:

pg_dump -s -U postgres -d test1 > test1_only.sql 

將創建模式只是爲test1數據庫,但不包括CREATE DATABASE命令。 我能得到CREATE DATABASE命令出現的唯一辦法是要做到:

pg_dumpall -s -U postgres > /schema_alldatabases.sql 

將轉儲所有數據庫的架構。但是這當然包括所有數據庫的所有模式。在我要恢復這個文件的服務器上,我已經有了test3和test4 ...並且我不想覆蓋它們,因爲模式不同。

有沒有辦法讓pg_dump -s命令包含CREATE DATABASE命令? 或者,我應該只使用pg_dumpall並控制我恢復的內容?如果是這樣,你能告訴我如何從dump文件恢復test1嗎?

謝謝。

回答

10

使用--create-C選項

pg_dump --create -s -U postgres -d test1 > test1_only.sql 

開始輸出與命令來創建數據庫本身並重新連接到創建的數據庫。 (對於這種形式的腳本,在運行腳本之前連接到的目標安裝中的哪個數據庫無關緊要)。如果還指定了--clean,則腳本將在重新連接之前刪除並重新創建目標數據庫。

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