学习目标
掌握openGauss的用户和角色管理。
使用create user创建的用户与使用create role创建的用户的区别在于,前者可以直接连接登录数据库,而使用create role创建的用户不能直接登录到数据库。必须添加LOGIN权限后,才能登录到数据库管理系统。
删除用户,首先需要将用户拥有的数据库对象转移或者删除。
1、创建test10_tbs的表空间,在这个表空间中创建数据库testdb10
omm@modb:~$ gsql -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. omm=# create tablespace test10_tbs relative location 'tablespace/test10_tbs1'; CREATE TABLESPACE omm=# omm=# create database testdb10 with tablespace=test10_tbs; CREATE DATABASE omm=# omm=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+-------+-----------+---------+-------+------------------- omm | omm | SQL_ASCII | C | C | postgres | omm | SQL_ASCII | C | C | template0 | omm | SQL_ASCII | C | C | =c/omm + | | | | | omm=CTc/omm template1 | omm | SQL_ASCII | C | C | =c/omm + | | | | | omm=CTc/omm testdb10 | omm | SQL_ASCII | C | C | (5 rows)
复制
2、使用create user创建用户user10,登录数据库testdb10,创建测试表t1和t2
--创建一个名叫user10的数据库用户,登录数据库testdb10,创建测试表t1和t2 omm=# create user user10 identified by 'aabb@123'; CREATE ROLE omm=# grant all on database testdb10 to user10; GRANT omm=# grant all privileges to user10; --需要加授权 all privileges才可 ALTER ROLE --执行下面的gsql元命令,查看系统目前有哪些数据库: omm=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+-------+-----------+---------+-------+------------------- omm | omm | SQL_ASCII | C | C | postgres | omm | SQL_ASCII | C | C | template0 | omm | SQL_ASCII | C | C | =c/omm + | | | | | omm=CTc/omm template1 | omm | SQL_ASCII | C | C | =c/omm + | | | | | omm=CTc/omm testdb10 | omm | SQL_ASCII | C | C | =Tc/omm + | | | | | omm=CTc/omm + | | | | | user10=CTc/omm + | | | | | user10=APm/omm (5 rows) --执行下面的gsql元命令,查看系统目前有哪些用户: omm=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------------------------------------------------------------+----------- john | Sysadmin | {} omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {} user10 | Sysadmin | {} --用刚刚创建的数据库用户user10登录到数据库testdb10 omm=# \c testdb10 user10 Password for user user10: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "testdb10" as user "user10". testdb10=> create table t1(col int); CREATE TABLE testdb10=> create table t2(col int); CREATE TABLE testdb10=>
复制
3、使用create role创建角色role10,登录数据库testdb10
--创建一个新的名叫role10的角色,登录数据库testdb10: omm=# create role role10 identified by 'aabb@123'; CREATE ROLE omm=# grant all on database testdb10 to role10; GRANT --查看数据库用户,role10显示为Cannot login: omm=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------------------------------------------------------------+----------- john | Sysadmin | {} omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {} role10 | Cannot login | {} user10 | Sysadmin | {} --使用用户role10尝试登录到数据库testdb10,提示无法登录数据库: omm=# \c testdb10 role10 Password for user role10: FATAL: role "role10" is not permitted to login Previous connection kept omm=# -- 授予用户role10数据库超级用户的权限: omm=# alter user role10 login; ALTER ROLE omm=# --使用用户role10登录到数据库testdb omm=# \c testdb10 role10 Password for user role10: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "testdb10" as user "role10". testdb10=> testdb10=>
复制
4、将表t1直接删除,将前面创建的表空间和数据库、表t2转给role10,删除用户user10
--登录 user10,删除t1 omm=# \c testdb10 user10 Password for user user10: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "testdb10" as user "user10". testdb10=> \dt List of relations Schema | Name | Type | Owner | Storage --------+------+-------+--------+---------------------------------- public | t1 | table | user10 | {orientation=row,compression=no} public | t2 | table | user10 | {orientation=row,compression=no} (2 rows) testdb10=> drop table t1; DROP TABLE testdb10=> --将前面创建的表空间和数据库、表t2转给role10,删除用户user10 testdb10=> alter database testdb10 owner to role10; ALTER DATABASE testdb10=> alter tablespace test10_tbs owner to role10; ALTER TABLESPACE testdb10=> alter table t2 owner to role10; ALTER TABLE testdb10=> \dt List of relations Schema | Name | Type | Owner | Storage --------+------+-------+--------+---------------------------------- public | t2 | table | role10 | {orientation=row,compression=no} (1 row) testdb10=> \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+--------+-----------+---------+-------+------------------- omm | omm | SQL_ASCII | C | C | postgres | omm | SQL_ASCII | C | C | template0 | omm | SQL_ASCII | C | C | =c/omm + | | | | | omm=CTc/omm template1 | omm | SQL_ASCII | C | C | =c/omm + | | | | | omm=CTc/omm testdb10 | role10 | SQL_ASCII | C | C | =Tc/role10 + | | | | | role10=CTc/role10+ | | | | | user10=CTc/role10+ | | | | | user10=APm/role10+ | | | | | role10=APm/role10 (5 rows) testdb10=> \db List of tablespaces Name | Owner | Location ------------+--------+------------------------ app_ts | omm | tablespace/app_ts1 enmtbs | omm | tablespace/enmtbs_01 myindex_ts | omm | tablespace/myindex_ts1 pg_default | omm | pg_global | omm | test10_tbs | role10 | tablespace/test10_tbs1 (6 rows) testdb10=> \c omm omm Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "omm" as user "omm". omm=# drop user user10 cascade; --删除用户user10 DROP ROLE omm=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+--------+-----------+---------+-------+------------------- omm | omm | SQL_ASCII | C | C | postgres | omm | SQL_ASCII | C | C | template0 | omm | SQL_ASCII | C | C | =c/omm + | | | | | omm=CTc/omm template1 | omm | SQL_ASCII | C | C | =c/omm + | | | | | omm=CTc/omm testdb10 | role10 | SQL_ASCII | C | C | =Tc/role10 + | | | | | role10=CTc/role10+ | | | | | role10=APm/role10 (5 rows)
复制
5、最后删除role10
--执行删除用户的操作,可以删除role10用户 omm=# drop user role10; ERROR: role "role10" cannot be dropped because some objects depend on it DETAIL: owner of tablespace test10_tbs owner of database testdb10 1 object in database testdb10 直接删除有报错提示。 --依次删除表、数据库、表空间,再删除role10即可。 omm=# \c testdb10 role10 Password for user role10: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "testdb10" as user "role10". testdb10=> drop table t2; DROP TABLE testdb10=> \c omm omm Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "omm" as user "omm". omm=# drop database testdb10; DROP DATABASE omm=# drop tablespace test10_tbs; DROP TABLESPACE omm=# drop user role10; DROP ROLE omm=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------------------------------------------------------------+----------- omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {} omm=#
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论

2年前

评论
今天这和删除废了不少心思呀!各种卡卡
不过若是生产就开心 特安全的感觉
2年前

1
相关阅读
2025年3月国产数据库大事记
墨天轮编辑部
600次阅读
2025-04-03 15:21:16
内蒙古公司成功完成新一代BOSS云原生系统割接上线
openGauss
199次阅读
2025-03-24 09:40:40
第4期 openGauss 中级认证OGCP直播班招生中!3月30日开课
墨天轮小教习
162次阅读
2025-03-17 15:48:40
openGauss 7.0.0-RC1 版本正式发布!
Gauss松鼠会
146次阅读
2025-04-01 12:27:03
openGauss 7.0.0-RC1 版本体验:一主一备快速安装指南
孙莹
127次阅读
2025-04-01 10:30:07
从数据库源码比较 PostgreSql和OpenGauss的启动过程
maozicb
85次阅读
2025-03-24 15:55:04
一文快速上手openGauss
进击的CJR
80次阅读
2025-03-26 16:12:54
openGauss HASH JOIN原理
lbsswhu
61次阅读
2025-03-18 10:45:01
openGauss 学习之路:集群部署实战探索
openGauss
55次阅读
2025-03-21 10:34:13
openGauss问题记录:开启备机归档且备机stop情况下,执行gs_probackup失败
zym
45次阅读
2025-03-18 19:06:13