学习目标
学习openGauss体系结构,使用多个用户访问同一个数据库。
学习内容
本章节学习了使用多个用户访问一个数据库。
- 创建一个表空间,再创建一个数据库,修改此数据库的默认表空间
- 创建三个用户,并赋权sysadmin权限。
- 使用三个用户分别登录新建数据库,创建三张表并插入数据再查看。
- 最后,登录数据库使用\dt查看创建的三张表的信息,确认是否分别三个三个用户。
课后作业
1.创建用户user1、user2、user3,授予user1、user2、user3数据库系统的SYSADMIN权限
CREATE USER user11 IDENTIFIED BY 'test@1234';
CREATE USER user22 IDENTIFIED BY 'test@1234';
CREATE USER user33 IDENTIFIED BY 'test@1234';授权sysadmin权限
ALTER USER user11 SYSADMIN;
ALTER USER user22 SYSADMIN;
ALTER USER user33 SYSADMIN;查询结果
omm=# \du
List of roles
Role name | Attributes
| Member of
-----------+------------------------------------------------------------------------------------------------------------
user11 | Sysadmin
| {}
user22 | Sysadmin
| {}
user33 | Sysadmin
| {}
gaussdb | Sysadmin
| {}
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin,
UseFT | {}2.分别使用user1、user2、user3访问数据库musicdb2,创建各自的表,并插入数据。表名和数据如下:
表名分别为: 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 |
创建数据库
CREATE DATABASE musicdb2;用户1:创建表、插入数据查询
\c musicdb2 user11
create table products1(product_id integer,product_name char(20),category char(30));
insert into products1 values(1502,'olympus camera','electrncs'),(1601,'lamaze','toys'),(1700,'wait interface','Books'),(1666,'harry potter','toys');
select * from products1;执行结果
omm=# \c musicdb2 user11
Password for user user11:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb2" as user "user11".
musicdb2=> create table products1(product_id integer,product_name char(20),category char(30));
CREATE TABLE
musicdb2=> insert into products1 values(1502,'olympus camera','electrncs'),(1601,'lamaze','toys'),(1700,'wait interface','Books'),(1666,'harry potter','toys');
INSERT 0 4
musicdb2=> select * from products1;
product_id | product_name | category
------------+----------------------+--------------------------------
1502 | olympus camera | electrncs
1601 | lamaze | toys
1700 | wait interface | Books
1666 | harry potter | toys
(4 rows)用用户2:创建表、插入数据查询
\c musicdb2 user22
create table products2(product_id integer,product_name char(20),category char(30));
insert into products2 values(1502,'olympus camera','electrncs'),(1601,'lamaze','toys'),(1700,'wait interface','Books'),(1666,'harry potter','toys');
select * from products2;执行结果
omm=# \c musicdb2 user22
Password for user user22:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb2" as user "user22".
musicdb2=> create table products2(product_id integer,product_name char(20),category char(30));
CREATE TABLE
musicdb2=> insert into products2 values(1502,'olympus camera','electrncs'),(1601,'lamaze','toys'),(1700,'wait interface','Books'),(1666,'harry potter','toys');
INSERT 0 4
musicdb2=> select * from products2;
product_id | product_name | category
------------+----------------------+--------------------------------
1502 | olympus camera | electrncs
1601 | lamaze | toys
1700 | wait interface | Books
1666 | harry potter | toys
(4 rows)用用户3:创建表、插入数据查询
\c musicdb2 user33
create table products3(product_id integer,product_name char(20),category char(30));
insert into products3 values(1502,'olympus camera','electrncs'),(1601,'lamaze','toys'),(1700,'wait interface','Books'),(1666,'harry potter','toys');
select * from products3;执行结果
omm=# \c musicdb2 user33
Password for user user33:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb2" as user "user33".
musicdb2=> create table products3(product_id integer,product_name char(20),category char(30));
CREATE TABLE
musicdb2=> insert into products3 values(1502,'olympus camera','electrncs'),(1601,'lamaze','toys'),(1700,'wait interface','Books'),(1666,'harry potter','toys');
INSERT 0 4
musicdb2=> select * from products3;
product_id | product_name | category
------------+----------------------+--------------------------------
1502 | olympus camera | electrncs
1601 | lamaze | toys
1700 | wait interface | Books
1666 | harry potter | toys
(4 rows)3.使用user1、user2、user3用户中的任何一个,查看当前数据库musicdb2有哪些表
\c musicdb2
\dt执行结果
musicdb2=> \c musicdb2
Password for user user33:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb2" as user "user33".
musicdb2=> \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+-----------+-------+--------+----------------------------------
public | products1 | table | user11 | {orientation=row,compression=no}
public | products2 | table | user22 | {orientation=row,compression=no}
public | products3 | table | user33 | {orientation=row,compression=no}
(3 rows)最后修改时间:2022-11-27 11:11:11
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




