学习目标
学习openGauss体系结构,使用多个用户访问同一个数据库
课程学习
创建user1、user2、user3用户,验证数据库musicdb可以被用户user1、user2、user3访问(分别在数据库中创建了一张表、插入数据、进行查询)。即一个数据库可以被多个用户访问。
作业练习
1.创建用户user1、user2、user3,授予user1、user2、user3数据库系统的SYSADMIN权限
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=# \db
List of tablespaces
Name | Owner | Location
------------+-------+----------
pg_default | omm |
pg_global | omm |
(2 rows)
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
omm | omm | UTF8 | C | C |
postgres | omm | UTF8 | C | C |
template0 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
(4 rows)
omm=# CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
CREATE TABLESPACE
omm=# CREATE DATABASE musicdb2 WITH TABLESPACE = music_tbs;
CREATE DATABASE
omm=# CREATE USER user1 IDENTIFIED BY 'xiaocx@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# CREATE USER user2 IDENTIFIED BY 'xiaocx@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# CREATE USER user3 IDENTIFIED BY 'xiaocx@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
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, Operatoradmin, Policyadmin, UseFT | {}
user1 | Sysadmin | {}
user2 | Sysadmin | {}
user3 | Sysadmin | {}
omm=# 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 |
-- 用户user1访问数据库musicdb2 ,然后创建表并插入数据musicdb=> \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=> musicdb2=> create table products1(product_id INTEGER,product_name char(20),category Char(30)); musicdb2=> CREATE TABLE musicdb2=> insert into products1 values(1502,'olympus camera','electrncs'); INSERT 0 1 musicdb2=> insert into products1 values(1601,'lamaze','toys'); INSERT 0 1 musicdb2=> insert into products1 values(1601,'wait interface','Books'); INSERT 0 1 musicdb2=> insert into products1 values(1601,'harry potter','toys'); INSERT 0 1 musicdb2=> select * from products1; product_id | product_name | category ------------+----------------------+-------------------------------- 1502 | olympus camera | electrncs 1601 | lamaze | toys 1601 | wait interface | Books 1601 | harry potter | toys (4 rows) musicdb2=>-- 用户user2访问数据库musicdb2 ,然后创建表并插入数据 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 values(1502,'olympus camera','electrncs'); INSERT 0 1 musicdb2=> insert into products2 values(1601,'lamaze','toys'); INSERT 0 1 musicdb2=> insert into products2 values(1601,'wait interface','Books'); INSERT 0 1 musicdb2=> insert into products2 values(1601,'harry potter','toys'); INSERT 0 1 musicdb2=> select * from products2; product_id | product_name | category ------------+----------------------+-------------------------------- 1502 | olympus camera | electrncs 1601 | lamaze | toys 1601 | wait interface | Books 1601 | harry potter | toys (4 rows) musicdb2=>-- 用户user3访问数据库musicdb2 ,然后创建表并插入数据 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 values(1502,'olympus camera','electrncs'); INSERT 0 1 musicdb2=> insert into products3 values(1601,'lamaze','toys'); INSERT 0 1 musicdb2=> insert into products3 values(1601,'wait interface','Books'); INSERT 0 1 musicdb2=> insert into products3 values(1601,'harry potter','toys'); INSERT 0 1 musicdb2=> select * from products3; product_id | product_name | category ------------+----------------------+-------------------------------- 1502 | olympus camera | electrncs 1601 | lamaze | toys 1601 | wait interface | Books 1601 | harry potter | toys (4 rows) musicdb2=>
3.使用user1、user2、user3用户中的任何一个,查看当前数据库musicdb2有哪些表
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)最后修改时间:2022-12-03 11:53:52
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




