我想用不同的模式恢复数据库
我已经使用以下命令对名为 temp1
的数据库进行了转储
$ pg_dump -i -h localhost -U postgres -F c -b -v -f pub.backup temp1
现在我想在名为“db_temp”的不同数据库中恢复转储,但我只希望所有表都应该在“db_temp”数据库中的“temp_schema”(不是 fms temp1 数据库中的默认模式)中创建。
有没有办法使用 pg_restore 命令来做到这一点?
任何其他方法也将受到赞赏!
I have taken a dump of a database named temp1
, by using the follwing command
$ pg_dump -i -h localhost -U postgres -F c -b -v -f pub.backup temp1
Now I want to restore the dump in a different database called "db_temp" , but in that I just want that all the tables should be created in a "temp_schema" ( not the default schema which is in the fms temp1 database ) which is in the "db_temp" database.
Is there any way to do this using pg_restore
command?
Any other method also be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
一种快速而肮脏的方法:
1)重命名默认模式:
2)创建新模式作为默认模式:
3)恢复数据
4)根据需要重命名模式:
A quick and dirty way:
1) rename default schema:
2) create new schema as default schema:
3) restore data
4) rename schemas according to need:
有一个简单的解决方案:
--format=p
或-F p
格式“p”)现在您可以使用以下命令恢复备份转储
set search_path to
命令会将 myschema 设置为默认值,以便在此架构中创建新表和其他对象,独立于他们之前居住的“默认”模式。There is a simple solution:
--format=p
or-F p
)Now you can restore your backup dump with the command
The
set search_path to <schema>
command will set myschema as the default, so that new tables and other objects are created in this schema, independently of the "default" schema where they lived before.pg_restore 本身没有办法。您可以做的是使用 pg_restore 生成 SQL 输出,然后通过 sed 脚本等发送该输出来更改它。不过,您需要小心编写 sed 脚本的方式,因此它不会匹配并更改数据内的内容。
There's no way in pg_restore itself. What you can do is use pg_restore to generate SQL output, and then send this through for example a sed script to change it. You need to be careful about how you write that sed script though, so it doesn't match and change things inside your data.
也许最简单的方法是在恢复后简单地重命名架构,即使用以下 SQL:
我相信,因为您使用压缩存档格式作为 pg_dump 的输出,所以您无法在恢复之前更改它。该选项是使用默认输出并对架构名称进行搜索和替换,但这会有风险,如果您不小心,可能会导致数据损坏。
Probably the easiest method would be to simply rename the schema after restore, ie with the following SQL:
I believe that because you're using the compressed archive format for the output of pg_dump you can't alter it before restoring. The option would be to use the default output and do a search and replace on the schema name, but that would be risky and could perhaps cause data to be corrupted if you were not careful.
如果您只有几个表,那么您可以一次恢复一个表,当您指定
-t tablename
时,pg_restore
接受-d数据库
。当然,您必须在恢复表之前设置架构,然后在完成恢复表后整理索引和约束。或者,在不同的端口上设置另一台服务器,使用新的 PostgreSQL 服务器进行恢复,重命名架构,转储它,然后恢复到原始数据库中。当然,这有点麻烦,但它可以完成工作。
如果您喜欢冒险,您也许可以使用十六进制编辑器更改转储文件中的数据库名称。我认为它只在转储中的一处提到,只要新旧数据库名称相同,它就应该起作用。 YMMV,不要在生产环境中做这样的事情,如果这会炸毁并夷平你的家乡,请不要怪我,以及所有其他常见的免责声明。
If you only have a few tables then you can restore one table at a time,
pg_restore
accepts-d database
when you specify-t tablename
. Of course, you'll have to set up the schema before restoring the tables and then sort out the indexes and constraints when you're done restoring the tables.Alternatively, set up another server on a different port, restore using the new PostgreSQL server, rename the schema, dump it, and restore into your original database. This is a bit of a kludge of course but it will get the job done.
If you're adventurous you might be able to change the database name in the dump file using a hex editor. I think it is only mentioned in one place in the dump and as long as the new and old database names are the same it should work. YMMV, don't do anything like this in a production environment, don't blame me if this blows up and levels your home town, and all the rest of the usual disclaimers.
重命名临时数据库中的架构。
导出架构:
创建一个新数据库。恢复导出:(
删除数据库)
数据只需修改顶部设置的
search_path
即可。Rename the schema in a temporary database.
Export the schema:
Create a new database. Restore the export:
(delete the database)
For the data you can just modify the set
search_path
at the top.如前所述,pg_dump、psql 或 pg_restore 不直接支持在转储/恢复过程中更改模式名称。但使用“普通”格式导出然后修改 .sql 文件相当简单。这个 Bash 脚本完成了基础工作:
用法:
As noted, there's no direct support in pg_dump, psql or pg_restore to change the schema name during a dump/restore process. But it's fairly straightforward to export using "plain" format then modify the .sql file. This Bash script does the basics:
Usage:
这个问题很老了,但也许可以帮助某人。
将
pg_restore
的输出流式传输到sed
并替换架构名称,以便将转储导入到不同的架构。例如:
v.15.6 之前的 Pg 恢复
从 v.15.6 开始的 Pg 恢复
The question is pretty old, but maybe can help some one.
Streaming the output of
pg_restore
tosed
and replace the schema name in order to import the dump to a different schema.Something like:
Pg restore before v.15.6
Pg restore from v.15.6