我有一个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)
我需要一种方法来创建一个包含模式(包括一个CREATE DATABASE语句)的转储文件,以便于对我正在转储的数据库进行还原。
到目前为止,我已经弄清楚:
pg_dump -s -U postgres -d test1 > test1_only.sql
这个操作只会为test1数据库创建模式(schema),但不包括CREATE DATABASE命令。唯一让CREATE DATABASE命令出现的方法是执行以下操作:
pg_dumpall -s -U postgres > /schema_alldatabases.sql
将会为所有数据库转储模式。但这当然包括所有数据库的模式。在我要恢复此文件的服务器上,我已经有了test3和test4……而且我不想覆盖它们,因为模式不同。
是否有办法让pg_dump -s命令包括CREATE DATABASE命令?或者,我应该只使用pg_dumpall并控制我要恢复的内容?如果是这样,请问如何只从转储文件中还原test1?
谢谢。