学习内容:
使用create user创建的用户与使用create role创建的用户的区别在于,前者可以直接连接登录数据库,而使用create role创建的用户不能直接登录到数据库。必须添加LOGIN权限后,才能登录到数据库管理系统。
删除用户,首先需要将用户拥有的数据库对象转移或者删除。
登陆opengauss数据库
[root@enmoedu ~]# su - omm
Last login: Sun Dec 11 11:31:57 CST 2022 on pts/0
[omm@enmoedu ~]$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Normal
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip port instance state
----------------------------------------------------------------------------------------------
1 enmoedu 192.168.94.135 15400 6001 /opt/huawei/install/data/dn P Primary Normal
[omm@enmoedu ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:34 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# 1、环境准备
1.1)创建一个名叫test_tbs的表空间和一个名叫testdb的数据库
create tablespace test_tbs relative location 'tablespace/test_tbs1';
1.2)创建一个名叫testdb的数据库
create database testdb with tablespace = test_tbs;
2、使用create user创建用户
2.1)使用create user创建的用户,具有登录权限
- 创建一个名叫user1的数据库用户,其密码为enmoedu@1234
create user user1 identified by 'enmoedu@1234';
- 数据库testdb所有的权限都授予用户user1
grant all on database testdb to user1;
- 执行下面的gsql元命令,查看系统目前有哪些数据库
\l
- 执行下面的gsql元命令,查看系统目前有哪些用户
\du
- 用刚刚创建的数据库用户user1登录到数据库testdb
gsql -d testdb -U user1 -W enmoedu@1234 -p 15400 -r
新开启会话session,重新登陆opengauss数据库
[omm@enmoedu ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:34 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# 3、使用create role创建用户
3.1)使用create role命令创建的用户user2,没有登录权限
- 创建一个新的名叫user2的角色,其密码为enmoedu@1234
- 将数据库testdb所有的权限都授予用户user2【用户user2对数据库testdb具有的权限和用户user1一模一样】
create role user2 identified by 'enmoedu@1234';
grant all on database testdb to user2;
- 查看当前opengauss数据库集群由哪些用户
\du
- 使用用户user2尝试登录到数据库testdb
gsql -d testdb -U user2 -W enmoedu@1234 -p 15400 -r
原因:由于数据库用户user2是使用create role语句创建的,目前还不被允许登录到opengauss数据库管理系统。
新开启session会话,重新登陆opengauss
[omm@enmoedu ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:34 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# 4、授予用户user2登录权限后
授予用户user2登录权限后,可以登录数据库
alter user user2 login;
\du
现在我们已经授予了user2用户登录数据库的权限,可以正常登录数据库testdb
gsql -d testdb -U user2 -W enmoedu@1234 -r
删除用户和角色
删除用户,首先需要将用户拥有的数据库对象转移或者删除,回收权限。
授予用户user2数据库超级用户的权限
[omm@enmoedu ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:34 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# alter user user2 sysadmin;
使用用户user2登录到数据库testdb,创建表空间test_tbs1、数据库testdb、表test1和表test2
openGauss=# \c testdb user2
Password for user user2: 【enmoedu@1234】
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "testdb" as user "user2".
testdb=>

create tablespace test_tbs1 relative location 'tablespace/test_tbs11';
create database testdb with tablespace = test_tbs1;
create table test1(col int);
create table test2(col int);
执行如下的命令,删除用户user2
[omm@enmoedu ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:34 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# drop user user2;
ERROR: role "user2" cannot be dropped because some objects depend on it
DETAIL: owner of tablespace test_tbs1
privileges for database testdb
2 objects in database testdb结论,可以看出不能删除用户user2的原因是:
1)用户user2拥有数据库testdb。
2)用户user2拥有表空间对象test_tbs1。
3)用户user2对数据库testdb具有权限。
4)用户user2在数据库testdb中有两个对象。
要删除用户user2,必须先表空间对象和数据库对象的属主修改为其他的用户(如user1用户),或者干脆将其删除
alter database testdb owner to user1;
alter tablespace test_tbs1 owner to user1;
回收用户user2对testdb数据库的权限
revoke all on database testdb from user2;
在testdb中属于用户user2的数据库对象的处理方法可以是:假如该对象还有用,可以将该对象转移给其他用户;假如该对象没有用,可以直接删除掉该对象
1)假设表test1已经没有用了,我们可以删除表test1
openGauss=# \c testdb user2
Password for user user2: 【enmoedu@1234】
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "testdb" as user "user2".
testdb=> drop table test1;
DROP TABLE
testdb=> 2)假设表test2还有用将表test2转移给用户user1
reassign owned by user2 to user1;
\dt
执行删除用户的操作,可以删除user2用户
[omm@enmoedu ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:34 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# drop user user2;
DROP ROLE
openGauss=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------------------------------------------------------------+-----------
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}
user1 | Sysadmin | {}
openGauss=#




