Postgres REASSIGN OWNED 仅用于 1 个数据库

Car*_*uez 5 database postgresql alter

我想使用REASSIGN OWNED查询将 1 个数据库中的所有对象从所有者 A 更改为所有者 B。

假设我有以下数据库:

 postgres              | postgres        | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0             | postgres        | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                       |                 |          |             |             | postgres=CTc/postgres
 template1             | postgres        | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                       |                 |          |             |             | postgres=CTc/postgres
 db1                   | user1           | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 db2                   | user1           | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 db3                   | user2           | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
Run Code Online (Sandbox Code Playgroud)

我想要 db1 和里面的所有对象,所以它们归 user2 所有。我跑:

postgres=# \c db1
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
You are now connected to database "db1" as user "postgres".
db1=# REASSIGN OWNED BY user1 TO user2;
REASSIGN OWNED
Run Code Online (Sandbox Code Playgroud)

db1 及其所有对象的所有者已更改。但是该命令也更改了 db2 的所有者。不是 db2 中的对象,只是数据库(如 ALTER DATABASE 语句):

 postgres              | postgres        | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0             | postgres        | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                       |                 |          |             |             | postgres=CTc/postgres
 template1             | postgres        | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                       |                 |          |             |             | postgres=CTc/postgres
 db1                   | user2           | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 db2                   | user2           | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 db3                   | user2           | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
Run Code Online (Sandbox Code Playgroud)

这是正常行为吗?如何在REASSIGN OWNED不改变其他数据库的情况下运行?

alv*_*rre 1

Daniel V\xc3\xa9rit\xc3\xa9 的评论中引用的文档指出:

\n\n
old_role\n\nThe name of a role. The ownership of all the objects within the current database, and of all shared objects (databases, tablespaces), owned by this role will be reassigned to new_role.\n
Run Code Online (Sandbox Code Playgroud)\n\n

所以这是根据规格。如果这不是您想要的,我认为您需要更全面地陈述您的用例。

\n

  • 但是之前的版本9.2,有[这个](http://www.postgresql.org/docs/9.2/static/sql-reassign-owned.html):“_REASSIGN OWNED命令不会影响拥有的任何数据库的所有权通过角色。使用 ALTER DATABASE 重新分配所有权_”,这**与当前行为相反** (9.3)。令人惊讶的是,它是不可避免的副作用,而不是新的非默认选项。 (2认同)
  • 由于[此错误报告](http:// www.postgresql.org/message-id/flat/E1ThhcE-0001nJ-1v@wrigleys.postgresql.org#E1ThhcE-0001nJ-1v@wrigleys.postgresql.org)。 (2认同)