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

openGauss每日一练第19天 | 用户和角色管理

原创 bingo_007 2022-12-13
649

学习目标

掌握openGauss的用户和角色管理。

使用create user创建的用户与使用create role创建的用户的区别在于,前者可以直接连接登录数据库,而使用create role创建的用户不能直接登录到数据库。必须添加LOGIN权限后,才能登录到数据库管理系统。
删除用户,首先需要将用户拥有的数据库对象转移或者删除。


课程作业

1、创建test10_tbs的表空间,在这个表空间中创建数据库testdb10

create tablespace test10_tbs relative location 'tablespace/test10_tbs1';
create database testdb10 with tablespace = test10_tbs;


2、使用create user创建用户user10,登录数据库testdb10,创建测试表t1和t2

-创建用户user10,并将数据库testdb所有的权限都授予用户user10:

create user user10 identified by 'robin@123';
grant all on database testdb10 to user10;
\l

--用刚刚创建的数据库用户user10登录到数据库testdb10

alter user user10 sysadmin;            ##若不授权,会提示无权限创建表
gsql -d testdb10 -U user10 -W robin@123 -r
create table t1(id int ,name char(10));
create table t2(id int ,name char(10));

3、使用create role创建角色role10,登录数据库testdb10

--创建角色role10

create role role10 identified by 'robin@123';
grant all on database testdb10 to user10;

--使用角色role10尝试登录到数据库testdb10

gsql -d testdb10 -U role10 -W robin@123 -r

若需角色登录登录数据库

alter user role10 LOGIN;
\du

再次测试登录数据库

gsql -d testdb10 -U role10 -W robin@123 -r

4、将表t1直接删除,将前面创建的表空间和数据库、表t2转给role10,删除用户user10

drop table t1;
ALTER USER role10 SYSADMIN; 

--使用角色role10登录到数据库testdb10

\c testdb10 role10
ALTER DATABASE testdb10 OWNER to role10;
ALTER tablespace test_tbs1 OWNER to role10;
ALTER table t2 OWNER to role10;


5、最后删除role10

revoke all on database testdb10 from user10;
drop user user10;

若用户下存在数据库、表空间/表等对象、未清理的权限时,无法删除用户。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论