database Postgresql - 在不同的所有者上备份数据库和还原?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31469008/
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-09-08 07:56:56  来源:igfitidea点击:

Postgresql - backup database and restore on different owner?

databasepostgresqldatabase-backupsdatabase-restoreowner

提问by Andrius

I did backup on database on different server and that has different role than I need, with this command:

我使用以下命令在不同服务器上的数据库上进行了备份,并且其角色与我需要的不同:

pg_dump -Fc db_name -f db_name.dump

Then I copied backup to another server where I need to restore the database, but there is no such owner that was used for that database. Let say database has owner owner1, but on different server I only have owner2and I need to restore that database and change owner.

然后我将备份复制到另一台需要恢复数据库的服务器,但没有用于该数据库的所有者。假设数据库有 owner owner1,但在不同的服务器上我只有owner2,我需要恢复该数据库并更改所有者。

What I did on another server when restoring:

恢复时我在另一台服务器上做了什么:

createdb -p 5433 -T template0 db_name 
pg_restore -p 5433 --role=owner2 -d db_name db_name.dump

But when restore is run I get these errors:

但是当恢复运行时,我收到这些错误:

pg_restore: [archiver (db)] could not execute query: ERROR:  role "owner1" does not exist

How can I specify it so it would change owner? Or is it impossible?

如何指定它以便更改所有者?或者是不可能的?

回答by Gary

You should use the --no-owneroption, this stops pg_restoretrying to set the ownership of the objects to the original owner. Instead the objects will be owned by the user specified by --role

您应该使用该--no-owner选项,这将停止pg_restore尝试将对象的所有权设置为原始所有者。相反,对象将由指定的用户拥有--role

createdb -p 5433 -T template0 db_name 
pg_restore -p 5433 --no-owner --role=owner2 -d db_name db_name.dump

pg_restore doc

pg_restore 文档