加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 16:19:04 所属栏目:百科 来源:网络整理
导读:我有一个 postgresql数据库服务器,其中包含多个数据库. postgres=# list List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges --------------+----------+-----------+---------+-------+----------------------- test1 | po
我有一个 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数据库创建模式,但它不包括CREATE DATABASE命令.
我能够获得CREATE DATABASE命令的唯一方法是:

pg_dumpall -s -U postgres > /schema_alldatabases.sql

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

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

谢谢.

使用–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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读