如何让pg_dump -s包含CREATE DATABASE命令?

dot*_*dot 10 postgresql backup postgresql-9.2

我有一个postgresql数据库服务器,其中包含多个数据库.

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

我需要一种方法来创建一个转储文件,其中包含我正在尝试转储的数据库的模式 - 包括CREATE DATABASE语句.

到目前为止,我已经发现:

pg_dump -s -U postgres -d test1 > test1_only.sql
Run Code Online (Sandbox Code Playgroud)

将仅为test1数据库创建模式,但它不包括CREATE DATABASE命令.我能够获得CREATE DATABASE命令的唯一方法是:

pg_dumpall -s -U postgres > /schema_alldatabases.sql 
Run Code Online (Sandbox Code Playgroud)

将转储所有数据库的架构.但是,这当然包括所有数据库的所有模式.在我要恢复此文件的服务器上,我已经有test3和test4 ......我不想覆盖它们,因为模式不同.

有没有让pg_dump -s命令包含CREATE DATABASE命令?或者,我应该只使用pg_dumpall并控制我恢复的内容?如果是这样,你能告诉我如何从转储文件中恢复test1吗?

谢谢.

Clo*_*eto 13

使用--create-C选项

pg_dump --create -s -U postgres -d test1 > test1_only.sql
Run Code Online (Sandbox Code Playgroud)

使用命令开始输出以创建数据库本身并重新连接到创建的数据库.(对于此表单的脚本,在运行脚本之前连接到的目标安装中的哪个数据库无关紧要.)如果还指定了--clean,则脚本将在重新连接之前删除并重新创建目标数据库.

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