如何更改 PostgreSql 数据库的所有者?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4313323/
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
How to change owner of PostgreSql database?
提问by Jayashri
I need to change the owner of PostgreSql database.
我需要更改 PostgreSql 数据库的所有者。
How to change owner of PostgreSql database in phppgadmin?
如何在 phppgadmin 中更改 PostgreSql 数据库的所有者?
回答by Frank Heikens
ALTER DATABASE name OWNER TO new_owner;
See the Postgresql manual's entry on thisfor more details.
有关更多详细信息,请参阅Postgresql 手册的条目。
回答by Antwane
Frank Heikens answer will only update database ownership. Often, you also want to update ownership of contained objects (including tables). Starting with Postgres 8.2, REASSIGN OWNEDis available to simplify this task.
Frank Heikens 的回答只会更新数据库所有权。通常,您还希望更新所包含对象(包括表)的所有权。从 Postgres 8.2 开始,可以使用REASSIGN OWNED来简化此任务。
IMPORTANT EDIT!
重要编辑!
Never use REASSIGN OWNED
when the original role is postgres
, this could damage your entire DB instance. The command will update all objects with a new owner, including system resources (postgres0, postgres1, etc.)
切勿REASSIGN OWNED
在原始角色为 时使用postgres
,这可能会损坏您的整个数据库实例。该命令将使用新的所有者更新所有对象,包括系统资源(postgres0、postgres1 等)
First, connect to admin database and update DB ownership:
首先,连接到管理数据库并更新数据库所有权:
psql
postgres=# REASSIGN OWNED BY old_name TO new_name;
This is a global equivalent of ALTER DATABASE
command provided in Frank's answer, but instead of updating a particular DB, it change ownership of all DBs owned by 'old_name'.
这是ALTER DATABASE
Frank 的答案中提供的命令的全局等效项,但它不会更新特定的数据库,而是更改“old_name”拥有的所有数据库的所有权。
The next step is to update tables ownership for each database:
下一步是更新每个数据库的表所有权:
psql old_name_db
old_name_db=# REASSIGN OWNED BY old_name TO new_name;
This must be performed on each DB owned by 'old_name'. The command will update ownership of all tables in the DB.
这必须在“old_name”拥有的每个数据库上执行。该命令将更新数据库中所有表的所有权。