暂无图片
暂无图片
2
暂无图片
暂无图片
暂无图片

PostgresSQL 角色的系统权限管理

原创 张玉龙 2022-06-28
1330

备注:以下测试是基于 PostgreSQL 13.3,但是也会涉及到其他版本的功能和特性,当出现其他版本的特性时会具体标注。

1. 以默认方式创建角色,不具备登录权限

postgres=# create database ttdb; postgres=# create role ttrole1; -- \du 可以查看角色的信息,可以发现新创建的角色 ttrole1 没有登录权限(Cannot login) postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole1 | Cannot login | {} -- 验证登录,提示角色没有登录权限 [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole1 -d ttdb -c 'select current_database(), current_user' psql: error: FATAL: role "ttrole1" is not permitted to log in -- 如果想让其具备登录权限,可以使用 alter role 进行授权 postgres=# alter role ttrole1 LOGIN; postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole1 | | {}

2. 创建一个具备登录权限的角色

postgres=# drop role ttrole1; -- 具备了 LOGIN 权限的角色可以被认为是用户 postgres=# create role ttrole1 with LOGIN; postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole1 | | {} -- 同样也可以使用 alter role 回收其登录权限 postgres=# alter role ttrole1 NOLOGIN; postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole1 | Cannot login | {}

3. 角色的密码管理

  • 使用角色的密码登录数据库的前提是 pg_hba.conf 文件配置了密码验证方式。
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all trust # IPv4 local connections: host all all 127.0.0.1/32 trust -- 此处不使用 trust host all all 192.168.0.0/24 md5
  • 角色不管是否具备 LOGIN 权限,都可以配置密码,如果未指定密码,则密码将设置为NULL,并且该角色的密码验证将始终失败。
-- 上面创建的角色 ttrole1 没有指定密码,登录验证直接输入回车,报 no password supplied [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole1 -d ttdb -c 'select current_database(), current_user' Password for user ttrole1: psql: error: fe_sendauth: no password supplied -- 输入任何密码都将始终失败 [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole1 -d ttdb -c 'select current_database(), current_user' Password for user ttrole1: psql: error: FATAL: password authentication failed for user "ttrole1" -- 可以使用 alter role 或 /password 修改密码登录 postgres=# alter role ttrole1 with password 'ttrole1'; postgres=# alter role ttrole1 LOGIN; [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole1 -d ttdb -c 'select current_database(), current_user' Password for user ttrole1: current_database | current_user ------------------+-------------- ttdb | ttrole1
  • 角色的密码记录在系统表 pg_authid 中
postgres=# SELECT rolname,rolpassword FROM pg_authid where rolname='ttrole1'; rolname | rolpassword ---------+------------------------------------- ttrole1 | md5bc8ab00ad76e611a176ba4f73e52e864 -- 可以发现此处的密码是以 MD5 的方式加密,密码的加密方式由参数 password_encryption 控制。
  • 在 CREATE ROLE 或 ALTER ROLE 中指定密码时,password_encryption 参数确定用于加密密码的算法,默认值是 md5,因 md5 的加密算法安全性不高,从 PostgreSQL 14 开始,其默认值改为 scram-sha-256 。
postgres=# select version(); version --------------------------------------------------------------------------------------------------------- PostgreSQL 13.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit (1 row) postgres=# show password_encryption; password_encryption --------------------- md5 postgres=# select version(); version --------------------------------------------------------------------------------------------------------- PostgreSQL 14.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit (1 row) postgres=# show password_encryption; password_encryption --------------------- scram-sha-256 (1 row) postgres=# SELECT rolname,rolpassword FROM pg_authid where rolname='postgres'; rolname | rolpassword ----------+--------------------------------------------------------------------------------------------------------------------------------------- postgres | SCRAM-SHA-256$4096:uiGbjgNyAsy6q9fQ1St97w==$5DqBEGZSfCJLmqlk0h5RES8iOtUSou64d41DiQavjYI=:Qy4cA8V5Flt3NtL+7D1DZWL8+on6l73sErsn0sA/MTY= (1 row) -- 不允许关闭加密方式,必须选择 md5 或者 scram-sha-256 postgres=# alter system set password_encryption=off; ERROR: invalid value for parameter "password_encryption": "off" HINT: Available values: md5, scram-sha-256.
  • 注意明文密码,create role 配置或 alter role 修改角色密码时,如果命令中指定了未加密的密码,密码将以明文形式传输到服务器,也可能会记录在客户端的命令历史记录或服务器日志中。
postgres=# create role ttrole2 with login password 'ttrole2'; postgres=# alter role ttrole2 with password 'aaaa'; -- 当 log_statement 参数设置为 ddl 或 all 时,create role 和 alter role 配置的密码会被以明文的形式记录到数据库日志中,这可能导致潜在的安全风险。 2022-06-11 12:51:09.424 CSTLOG: statement: create role ttrole2 with login password 'ttrole2'; 2022-06-11 12:51:09.424 CSTLOG: duration: 0.410 ms 2022-06-11 12:51:46.080 CSTLOG: statement: alter role ttrole2 with password 'aaaa'; 2022-06-11 12:51:46.080 CSTLOG: duration: 0.508 ms
  • 在不使用插件的情况下有两种方式防止传输明文密码
-- 方式一,使用 \password 修改角色密码 postgres=# \password ttrole2 Enter new password: Enter it again: 2022-06-11 15:50:50.913 CSTLOG: statement: ALTER USER ttrole2 PASSWORD 'md5d9f8e6376265b7952fce5c5afdb7cbf4' 2022-06-11 15:50:50.914 CSTLOG: duration: 0.339 ms -- 方式二:在本地创建密码哈希值,然后在创建或更改角色密码时使用此哈希值。 [postgres@pg ~]$ username='ttrole2'; dbpass='bbbb'; echo -n "${dbpass}${username}" | md5sum | awk '{print "md5" $1}' md5a4cafee095cf2ab0e970ccf4e8b448f2 postgres=# alter role ttrole2 with password 'md5a4cafee095cf2ab0e970ccf4e8b448f2'; ALTER ROLE postgres=# SELECT rolname,rolpassword FROM pg_authid where rolname='ttrole2'; rolname | rolpassword ---------+------------------------------------- ttrole2 | md5a4cafee095cf2ab0e970ccf4e8b448f2 (1 row) 2022-06-11 16:01:07.660 CSTLOG: statement: alter role ttrole2 with password 'md5a4cafee095cf2ab0e970ccf4e8b448f2'; 2022-06-11 16:01:07.661 CSTLOG: duration: 0.598 ms -- 验证密码 [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole2 -d ttdb -c 'select current_database(), current_user' Password for user ttrole2: current_database | current_user ------------------+-------------- ttdb | ttrole2 (1 row)
  • 设置角色密码的有效期,默认情况下,密码永久有效,使用 VALID UNTIL 可以配置密码有效期
-- 配置密码的有效期 postgres=# alter role ttrole2 WITH VALID UNTIL '2022-06-11 16:12:00'; -- pg_authid 也可以查询密码的有效期,如果 rolvaliduntil 列为 NULL 表示没有配置过有效期时间,密码永久有效 postgres=# SELECT rolname,rolpassword,rolvaliduntil FROM pg_authid where rolname='ttrole2'; rolname | rolpassword | rolvaliduntil ---------+-------------------------------------+------------------------ ttrole2 | md5a4cafee095cf2ab0e970ccf4e8b448f2 | 2022-06-11 16:12:00+08 -- \du 元命令也可以显示密码的有效期 postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole1 | | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} -- 当密码过了有效期时间以后,登录将失败 [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole2 -d ttdb -c 'select current_database(), current_user' Password for user ttrole2: psql: error: FATAL: password authentication failed for user "ttrole2" -- 重新调整有效期后,还可能正常登录 postgres=# alter role ttrole2 WITH VALID UNTIL '2022-06-11 16:15:00'; [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole2 -d ttdb -c 'select current_database(), current_user' Password for user ttrole2: current_database | current_user ------------------+-------------- ttdb | ttrole2 -- 取消密码的有效期限制,使密码永久有效 postgres=# alter role ttrole2 WITH VALID UNTIL 'infinity'; ALTER ROLE postgres=# SELECT rolname,rolpassword,rolvaliduntil FROM pg_authid where rolname='ttrole2'; rolname | rolpassword | rolvaliduntil ---------+-------------------------------------+--------------- ttrole2 | md5af50eb20fc2959e98cfdf59ff79205fd | infinity
  • 取消密码,将密码置为NULL
postgres=# alter role ttrole2 with password null;

4. 角色的 SUPERUSER 权限

  • SUPERUSER 的权限非常大,所有要谨慎授权,但是 SUPERUSER 不具备 LOGIN 权限。
postgres=# create role ttrole3 with superuser password 'ttrole3'; postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole1 | | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} ttrole3 | Superuser, Cannot login | {} [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole3 -d ttdb -c 'select current_database(), current_user' Password for user ttrole3: psql: error: FATAL: role "ttrole3" is not permitted to log in
  • SUPERUSER 包含了 CREATEDB 和 CREATEROLE 的权限
postgres=# drop role ttrole3; postgres=# create role ttrole3 with login superuser password 'ttrole3'; -- 即使没有 CREATEDB 和 CREATEROLE 的权限,也可以完成相关任务 [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole3 -d ttdb Password for user ttrole3: ttdb=# create database test; CREATE DATABASE ttdb=# drop database test; DROP DATABASE ttdb=# create role test; CREATE ROLE ttdb=# drop role test; DROP ROLE ttdb=# SELECT * FROM pg_create_physical_replication_slot('my_rep_slot_1'); slot_name | lsn ---------------+----- my_rep_slot_1 | ttdb=# select pg_drop_replication_slot('my_rep_slot_1'); -- 如果没有权限会报出以下错误信息 ttdb=> create database test; ERROR: permission denied to create database ttdb=> create role test; ERROR: permission denied to create role ttdb=> SELECT * FROM pg_create_physical_replication_slot('my_rep_slot_1'); ERROR: must be superuser or replication role to use replication slots
  • alter role 也可以回收 SUPERUSER 权限
postgres=# alter role ttrole3 with nosuperuser;

5. 角色的 CREATEROLE 权限

  • 需要注意,具有 CREATEROLE 权限的角色虽然不能创建具有 SUPERUSER 和 REPLICATION 权限的角色,但是可以创建具有 CREATEDB 权限的角色,并且具有 CREATEROLE 权限角色几乎等同于 SUPERUSER,具备 SUPERUSER 的大部分功能,只是不能管理具有 SUPERUSER 权限的角色,所以 CREATEROLE 权限也需要谨慎使用。
postgres=# create role ttrole4 with login createrole password 'ttrole4'; CREATE ROLE postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole1 | | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} ttrole3 | | {} ttrole4 | Create role | {} [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole4 -d ttdb -- 只能 SUPERUSER 才有权限创建具有 SUPERUSER 和 REPLICATION 权限的角色 ttdb=> create role test with login superuser password 'test'; ERROR: must be superuser to create superusers ttdb=> create role test with login createrole createdb replication password 'test'; ERROR: must be superuser to create replication users -- 具有 CREATEROLE 权限的角色可以创建具有 CREATEDB 权限的角色 ttdb=> create role test with login createrole createdb password 'test'; CREATE ROLE
  • 具有 CREATEROLE 权限的角色不仅可以创建角色,同样也可以删除角色,但是不能删除具有 SUPERUSER 权限的角色。
ttdb=> \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} test | Superuser, Cannot login | {} test2 | Cannot login, Replication | {} ttrole1 | | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} ttrole4 | Create role | {} ttuser2 | Cannot login | {} -- 不能删除具有 SUPERUSER 权限的角色 ttdb=> drop role test; ERROR: must be superuser to drop superusers -- 其他角色均可以删除 ttdb=> drop role test2; DROP ROLE ttdb=> drop role ttuser2; DROP ROLE

6. 角色的 CREATEDB 权限

  • 具有 CREATEDB 权限的角色不仅可以创建数据库,同样也可以删除数据库,但是只能删除 OWNER 属于当前角色的数据库。
postgres=# create role ttrole5 with login createdb password 'ttrole5'; CREATE ROLE postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole1 | | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} ttrole4 | Create role | {} ttrole5 | Create DB | {} [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole5 -d ttdb ttdb=> create database test; CREATE DATABASE ttdb=> \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres test | ttrole5 | UTF8 | en_US.utf8 | en_US.utf8 | test2 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | -- 数据库 test 的 Owner 是 ttrole5,所以可以直接删除 ttdb=> drop database test; DROP DATABASE -- 数据库 test2 的 Owner 是 postgres,所以没有权限删除 ttdb=> drop database test2; ERROR: must be owner of database test2

7. 限制角色的并发连接数 – CONNECTION LIMIT

  • 默认不限制角色的并发连接数
-- 限制只能同时登录一个会话 postgres=# create role ttrole6 with login password 'ttrole6' connection limit 1; CREATE ROLE postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole1 | | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} ttrole4 | Create role | {} ttrole5 | Create DB | {} ttrole6 | 1 connection | {} -- 第一个连接会话可以正常登录 [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole6 -d ttdb Password for user ttrole6: psql (13.3) Type "help" for help. ttdb=> -- 第二个并发连接就会失败 [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole6 -d ttdb Password for user ttrole6: psql: error: FATAL: too many connections for role "ttrole6"
  • 不会限制具有 SUPERUSER 权限的角色
-- 创建一个具有 SUPERUSER 权限的角色 postgres=# drop role ttrole6; postgres=# create role ttrole6 with login superuser password 'ttrole6' connection limit 1; -- 第一个会话可以正常登录 [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole6 -d ttdb Password for user ttrole6: psql (13.3) Type "help" for help. ttdb=# -- 其他会话同时登录也不会受到限制 [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole6 -d ttdb Password for user ttrole6: psql (13.3) Type "help" for help. ttdb=#
  • 修改 CONNECTION LIMIT
-- 修改并发连接数 postgres=# alter role ttrole6 connection limit 2; ALTER ROLE postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole1 | | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} ttrole4 | Create role | {} ttrole5 | Create DB | {} ttrole6 | 2 connections | {} -- 取消并发连接数限制 postgres=# alter role ttrole6 connection limit -1; ALTER ROLE postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole1 | | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} ttrole4 | Create role | {} ttrole5 | Create DB | {} ttrole6 | | {}

8. 修改角色的名称

  • 具有 SUPERUSER 和 CREATEROLE 权限的角色可以修改其他角色的名称,但是具有 CREATEROLE 权限的角色不能修改具有 SUPERUSER 权限的角色名称。
postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole1 | | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} ttrole4 | Create role | {} ttrole5 | Create DB | {} ttrole6 | Superuser +| {} | 1 connection | -- 普通角色 ttrole1 没有权限修改其他角色的名称 [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole1 -d ttdb ttdb=> alter role ttrole5 rename to ttuser5; ERROR: permission denied to rename role -- 具有 CREATEROLE 权限的角色 ttrole4 可以修改非 SUPERUSER 的角色名称 [postgres@pg ~]$ psql -h 192.168.0.52 -U ttrole4 -d ttdb ttdb=> alter role ttrole1 rename to ttuser1; NOTICE: MD5 password cleared because of role rename ALTER ROLE ttdb=> \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} ttrole4 | Create role | {} ttrole5 | Create DB | {} ttrole6 | Superuser +| {} | 1 connection | ttuser1 | | {} ttdb=> alter role ttrole6 rename to ttuser6; ERROR: must be superuser to rename superusers
  • 对于使用 MD5 加密的角色,因其加密方式使用角色名称作为加密盐,所以修改角色名称后会清除其密码。
-- 例如上面修改 ttrole5 角色名称的示例也给出了提示信息 ttdb=> alter role ttrole1 rename to ttuser1; NOTICE: MD5 password cleared because of role rename ALTER ROLE -- 使用原密码登录会收到密码错误的提示 [postgres@pg ~]$ psql -h 192.168.0.52 -U ttuser1 -d ttdb Password for user ttuser1: psql: error: FATAL: password authentication failed for user "ttuser1" -- 对重名名后的角色修改密码后就可以登录 postgres=# \password ttuser1 Enter new password: Enter it again: [postgres@pg ~]$ psql -h 192.168.0.52 -U ttuser1 -d ttdb -c 'select current_database(), current_user' Password for user ttuser1: current_database | current_user ------------------+-------------- ttdb | ttuser1

9. 建立角色关系

  • 可以将一个角色赋权给另一个角色
postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} ttrole4 | Create role | {} ttrole5 | Create DB | {} ttrole6 | Superuser +| {} | 1 connection | ttuser1 | Cannot login | {} -- 通过 GRANT 语句达将角色 ttrole4 赋予角色 ttuser1,使得角色 ttuser1 成为角色 ttrole4 的成员 postgres=# grant ttrole4 to ttuser1; GRANT ROLE postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} ttrole4 | Create role | {} ttrole5 | Create DB | {} ttrole6 | Superuser +| {} | 1 connection | ttuser1 | Cannot login | {ttrole4}

10. 删除角色

  • 具有 SUPERUSER 和 CREATEROLE 权限的角色可以删除角色,但是具有 CREATEROLE 权限的角色不能删除具有 SUPERUSER 权限的角色。
  • 如果角色在集群的数据库中被引用,则不能删除该角色,在删除角色之前,必须删除它拥有的所有对象(或重新分配其所有权)并回收该角色已授予其他对象的权限。
postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} ttrole2 | Password valid until 2022-06-11 16:12:00+08 | {} ttrole4 | Create role | {} ttrole5 | Create DB | {} ttrole6 | Superuser +| {} | 1 connection | ttuser1 | | {} -- 创建一个数据库引用角色 ttuser1 postgres=# create database test with owner = ttuser1; CREATE DATABASE -- 此时删除角色将失败 postgres=# drop role ttuser1; ERROR: role "ttuser1" cannot be dropped because some objects depend on it DETAIL: owner of database test -- 删除了角色 ttuser1 拥有的数据库对象 test ,就可以成功删除角色 postgres=# drop database test; DROP DATABASE postgres=# drop role ttuser1; DROP ROLE -- 如果不想删除数据库对象,也可以将数据库对象 test 重新分配给其他角色后再删除角色 postgres=# alter database test owner to ttrole2; ALTER DATABASE postgres=# drop role ttuser1; DROP ROLE
  • REASSIGN OWNED,更改数据库角色拥有的数据库对象的所有权,就是将一个角色下的所有对象的所有权转移到其他角色上,此命令通常用于准备删除一个或多个角色。
postgres=# create role ttuser1; CREATE ROLE postgres=# create database test1 with owner = ttuser1; CREATE DATABASE postgres=# create database test2 with owner = ttuser1; CREATE DATABASE postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres test1 | ttuser1 | UTF8 | en_US.utf8 | en_US.utf8 | test2 | ttuser1 | UTF8 | en_US.utf8 | en_US.utf8 | postgres=# reassign owned by ttuser1 to ttrole2; REASSIGN OWNED postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres test1 | ttrole2 | UTF8 | en_US.utf8 | en_US.utf8 | test2 | ttrole2 | UTF8 | en_US.utf8 | en_US.utf8 | postgres=# drop role ttuser1; DROP ROLE
  • DROP OWNED,删除数据库角色拥有的数据库对象,它的替代方法是 REASSIGN OWNED,并且也不是很安全,这里不做演示。
DROP OWNED BY { name | CURRENT_USER | SESSION_USER } [, ...] [ CASCADE | RESTRICT ]
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论