用户和角色有什么区别?
user 和 role的唯一区别是user是具有login用户属性的,而role默认不带logins属性,可以通过alter role方式添加,在其他方面二者没有区别。
学习openGauss体系结构,使用多个用户访问同一个数据库
创建user1、user2、user3用户,验证数据库musicdb可以被用户user1、user2、user3访问(分别在数据库中创建了一张表、插入数据、进行查询)。即一个数据库可以被多个用户访问。
--测试环境准备
su - omm
gsql -r
--进入数据库omm,创建表空间、测试数据库
drop DATABASE IF EXISTS musicdb;
drop DATABASE IF EXISTS musicdb1;
drop DATABASE IF EXISTS musicdb2;
drop DATABASE IF EXISTS musicdb3;
drop tablespace IF EXISTS music_tbs;
CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
CREATE DATABASE musicdb WITH TABLESPACE = music_tbs;
实训环境示例:
root@modb:~# su - omm
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=# drop DATABASE IF EXISTS musicdb;
DROP DATABASE
omm=# drop DATABASE IF EXISTS musicdb1;
DROP DATABASE
omm=# drop DATABASE IF EXISTS musicdb2;
DROP DATABASE
omm=# drop DATABASE IF EXISTS musicdb3;
DROP DATABASE
omm=# drop tablespace IF EXISTS music_tbs;
DROP TABLESPACE
omm=# CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
CREATE DATABASE musicdb WITH TABLESPACE = music_tbs;CREATE TABLESPACE
omm=#
CREATE DATABASE
omm=#
--执行下面的SQL语句,创建用户user1、user2、user3:
CREATE USER user1 IDENTIFIED BY 'kunpeng@1234';
CREATE USER user2 IDENTIFIED BY 'kunpeng@1234';
CREATE USER user3 IDENTIFIED BY 'kunpeng@1234';
--授予user1、user2、user3数据库系统的SYSADMIN权限:
ALTER USER user1 SYSADMIN;
ALTER USER user2 SYSADMIN;
ALTER USER user3 SYSADMIN;
--执行下面的命令,查看有哪些用户:
\du
omm=# CREATE USER user1 IDENTIFIED BY 'kunpeng@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# CREATE USER user2 IDENTIFIED BY 'kunpeng@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# CREATE USER user3 IDENTIFIED BY 'kunpeng@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=#
omm=# ALTER USER user1 SYSADMIN;
ALTER ROLE
omm=# ALTER USER user2 SYSADMIN;
ALTER ROLE
omm=# ALTER USER user3 SYSADMIN;
ALTER ROLE
omm=# \du
List of roles
Role name | Attributes
| Member of
-----------+-----------------------------------------------------------------------------------
-------------------------------+-----------
gaussdb | Sysadmin
| {}
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Ope
user2 | Sysadmin
| {}
user3 | Sysadmin
| {}
ratoradmin, Policyadmin, UseFT | {}
user1 | Sysadmin
| {}
omm=#
表名分别为: products1、 products2、 products3
字段名 | 数据类型 | 含义 |
---|---|---|
product_id | INTEGER | 产品编号 |
product_name | Char(20) | 产品名 |
category | Char(30) | 种类 |
向表中插入数据: | ||
product_id | product_name | category |
- | - | - |
1502 | olympus camera | electrncs |
1601 | lamaze | toys |
1700 | wait interface | Books |
1666 | harry potter | toys |
--以用户user1的身份在数据库musicdb2中创建表t1,并插入一条数据:
\c musicdb2 user1
create table products1(product_id integer,product_name char(20),category char(30));
insert into products1(product_id,product_name,category) values(1502,'olympus camera','electrncs'),
(1601,'lamaze','toys'),
(1700,'wait interface','Books'),
(1666,'harry potter','toys');
--以用户user2的身份在数据库musicdb2中创建表t2,并插入一条数据:
\c musicdb2 user2
create table products2(product_id integer,product_name char(20),category char(30));
insert into products2(product_id,product_name,category) values(1502,'olympus camera','electrncs'),
(1601,'lamaze','toys'),
(1700,'wait interface','Books'),
(1666,'harry potter','toys');
--以用户user3的身份在数据库musicdb2中创建表t3,并插入一条数据:
\c musicdb2 user3
create table products3(product_id integer,product_name char(20),category char(30));
insert into products3(product_id,product_name,category) values(1502,'olympus camera','electrncs'),
(1601,'lamaze','toys'),
(1700,'wait interface','Books'),
(1666,'harry potter','toys');
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=# \c musicdb2 user1
Password for user user1:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb2" as user "user1".
musicdb2=> create table products1(product_id integer,product_name char(20),category char(30));
CREATE TABLE
musicdb2=> insert into products1(product_id,product_name,category) values(1502,'olympus camera','electrncs'),
musicdb2-> (1601,'lamaze','toys'),
musicdb2-> (1700,'wait interface','Books'),
musicdb2-> (1666,'harry potter','toys');
INSERT 0 4
musicdb2=> \c musicdb2 user2
Password for user user2:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb2" as user "user2".
musicdb2=> create table products2(product_id integer,product_name char(20),category char(30));
CREATE TABLE
musicdb2=> insert into products2(product_id,product_name,category) values(1502,'olympus camera','electrncs'),
musicdb2-> (1601,'lamaze','toys'),
musicdb2-> (1700,'wait interface','Books'),
musicdb2-> (1666,'harry potter','toys');
INSERT 0 4
musicdb2=> \c musicdb2 user3
Password for user user3:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb2" as user "user3".
musicdb2=> create table products3(product_id integer,product_name char(20),category char(30));
CREATE TABLE
musicdb2=> insert into products3(product_id,product_name,category) values(1502,'olympus camera','electrncs'),
musicdb2-> (1601,'lamaze','toys'),
musicdb2-> (1700,'wait interface','Books'),
musicdb2-> (1666,'harry potter','toys');
INSERT 0 4
musicdb2=>
omm=# \c musicdb2 user3
Password for user user3:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb2" as user "user3".
musicdb2=> \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+-----------+-------+-------+----------------------------------
public | products1 | table | user1 | {orientation=row,compression=no}
public | products2 | table | user2 | {orientation=row,compression=no}
public | products3 | table | user3 | {orientation=row,compression=no}
(3 rows)
musicdb2=>