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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24760286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:32:41  来源:igfitidea点击:

How to get a pg_dump -s to include the CREATE DATABASE command?

postgresqlbackuppostgresql-9.2

提问by dot

I have a postgresql db server that has multiple database in it.

我有一个 postgresql db 服务器,里面有多个数据库。

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)

I need a way to create a dump file that contains the schema - including a CREATE DATABASE statement - for the database I'm trying to dump.

我需要一种方法来为我试图转储的数据库创建一个包含模式的转储文件 - 包括一个 CREATE DATABASE 语句。

So far, I've figured out that:

到目前为止,我已经弄清楚了:

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

will create the schema just for the test1 database, but it doesn't include the CREATE DATABASE command. The only way I was able to get the CREATE DATABASE command to appear was to do:

将为 test1 数据库创建模式,但它不包括 CREATE DATABASE 命令。我能够让 CREATE DATABASE 命令出现的唯一方法是:

pg_dumpall -s -U postgres > /schema_alldatabases.sql 

will dump the schema for all databases. But this then ofcourse include all the schemas for all databases. On the server that I'm going to restore this file on, I already have test3 and test4 ... and I don't want to overwrite them, as the schemas are different.

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

Is there anyway to have the pg_dump -s command include the CREATE DATABASE command? Or, am I supposed to just use the pg_dumpall and control what I restore? if so, can you show me how I can just restore test1 from the dump file?

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

Thanks.

谢谢。

回答by Clodoaldo Neto

Use --createor -Coption

使用--create-C选择

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

Begin the output with a command to create the database itself and reconnect to the created database. (With a script of this form, it doesn't matter which database in the destination installation you connect to before running the script.) If --clean is also specified, the script drops and recreates the target database before reconnecting to it.

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

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

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