学习openGauss体系结构,使用多个用户访问同一个数据库
创建user1、user2、user3用户,验证数据库musicdb可以被用户user1、user2、user3访问(分别在数据库中创建了一张表、插入数据、进行查询)。即一个数据库可以被多个用户访问。
创建表默认Schema对象为公共对象,该数据库下所有用户创建表使用所有用户都查询可见。
在作业部分中,创建表数据时出现了一个疑问,我使用复制命令的时候,没有复制成功查询命令;
仍然粘贴执行了insert插入数据,并且数据内容与上一条插入语句内容完全一致,导致该表出现了重复数据,是否在建表时需要对创建列字段做重复数据限制?这是应该在建表时限制,还是应该在做SQL插入时做复查更新?
1.测试环境准备
omm=# \l
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
omm=# drop DATABASE IF EXISTS musicdb;
NOTICE: database "musicdb" does not exist, skipping
omm=# DROP DATABASE
drop DATABASE IF EXISTS musicdb1;
NOTICE: database "musicdb1" does not exist, skipping
DROP DATABASE
omm=# drop DATABASE IF EXISTS musicdb2;
NOTICE: database "musicdb2" does not exist, skipping
DROP DATABASE
omm=# drop DATABASE IF EXISTS musicdb3;
NOTICE: database "musicdb3" does not exist, skipping
DROP DATABASE
omm=# drop tablespace IF EXISTS music_tbs;
NOTICE: Tablespace "music_tbs" does not exist, skipping.
omm=# DROP TABLESPACE
omm=# CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
CREATE TABLESPACE
omm=# CREATE DATABASE musicdb WITH TABLESPACE = music_tbs;
CREATE DATABASE
omm=# \l
musicdb | omm | UTF8 | C | C |
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
omm=#
2.创建用户user1、user2、user3
omm=# \du
gaussdb | Sysadmin | {}
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}
user1 | | {}
user2 | | {}
user3 | | {}
omm=# omm=#
omm=# ALTER USER user1 SYSADMIN;
ALTER ROLE
omm=# ALTER USER user2 SYSADMIN;
ALTER ROLE
omm=# ALTER USER user3 SYSADMIN;
ALTER ROLE
omm=#
omm=#
omm=#
omm=# \du
gaussdb | Sysadmin | {}
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}
user1 | Sysadmin | {}
user2 | Sysadmin | {}
user3 | Sysadmin
3.在终端中,分别使用user1、user2、user3用户访问数据库musicdb。
–以用户user1的身份在数据库musicdb中创建表t1,并插入一条数据:
omm=# \c musicdb user1
Password for user user1:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb" as user "user1".
musicdb=> create table t1(col1 char(20));
CREATE TABLE
musicdb=> insert into t1 values('Hello kunpeng 1');
INSERT 0 1
musicdb=> select * from t1;
Hello kunpeng 1
–以用户user2的身份在数据库musicdb中创建表t2,并插入一条数据:
musicdb=> \c musicdb user2
Password for user user2:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb" as user "user2".
musicdb=> create table t2(col1 char(20));
CREATE TABLE
musicdb=> insert into t2 values('Hello kunpeng 2');
INSERT 0 1
musicdb=> select * from t2;
Hello kunpeng 2
–以用户user3的身份在数据库musicdb中创建表t3,并插入一条数据:
musicdb=> \c musicdb user3
Password for user user3:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb" as user "user3".
musicdb=> create table t3(col1 char(20));
CREATE TABLE
musicdb=> insert into t3 values('Hello kunpeng 3');
INSERT 0 1
musicdb=> select * from t3;
Hello kunpeng 3
4.使用user1、user2、user3用户中的任何一个,执行如下命令,查看当前数据库musicdb有哪些表:
musicdb=> \dt
public | t1 | table | user1 | {orientation=row,compression=no}
public | t2 | table | user2 | {orientation=row,compression=no}
public | t3 | table | user3 | {orientation=row,compression=no}
musicdb=> \q
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=# \dt
No relations found.
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
musicdb | omm | UTF8 | C | C |
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
(5 rows)
omm=# \c musicdb
musicdb=# Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb" as user "omm".
musicdb=# \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+------+-------+-------+----------------------------------
public | t1 | table | user1 | {orientation=row,compression=no}
public | t2 | table | user2 | {orientation=row,compression=no}
public | t3 | table | user3 | {orientation=row,compression=no}
(3 rows)
作业记录:
omm-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
musicdb | omm | UTF8 | C | C |
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
(5 rows)
omm=# CREATE TABLESPACE music_tbs2 RELATIVE LOCATION 'tablespace2/test_ts2';
omm=# CREATE TABLESPACE
omm=# \db
List of tablespaces
Name | Owner | Location
------------+-------+----------------------
music_tbs | omm | tablespace/test_ts1
music_tbs2 | omm | tablespace2/test_ts2
pg_default | omm |
pg_global | omm |
(4 rows)
omm=# CREATE DATABASE musicdb2 WITH TABLESPACE = music_tbs2;
CREATE DATABASE
musicdb2=> product_id | product_name | category
------------+--------------+----------
(0 rows)
musicdb2=>
musicdb2=>
musicdb2=> insert into test1 values(1502,'olympus camera','electrncs'),(1601,'lamaze',ce','Books'),(1666,'harry potter','toys');
INSERT 0 4
musicdb2=> musicdb2=>
musicdb2=>
musicdb2=>
musicdb2=>
musicdb2=> insert into test1 values(1502,'olympus camera','electrncs'),(1601,'lamaze',ce','Books'),(1666,'harry potter','toys');
INSERT 0 4
musicdb2=>
musicdb2=> select * from test1;
product_id | product_name | category
------------+----------------------+--------------------------------
1502 | olympus camera | electrncs
1601 | lamaze | toys
1700 | wait interface | Books
1666 | harry potter | toys
1502 | olympus camera | electrncs
1601 | lamaze | toys
1700 | wait interface | Books
1666 | harry potter | toys
(8 rows)
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 test2(product_id INTEGER,product_name Char(20),category Char(30));
musicdb2=> CREATE TABLE
insert into test2 values(1502,'olympus camera','electrncs'),(1601,'lamaze','toys'),(17ce','Books'),(1666,'harry potter','toys');
INSERT 0 4
musicdb2=> select * from test2;
product_id | product_name | category
------------+----------------------+--------------------------------
1502 | olympus camera | electrncs
1601 | lamaze | toys
1700 | wait interface | Books
1666 | harry potter | toys
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 test3(product_id INTEGER,product_name Char(20),category Char(30));
CREATE TABLE
musicdb2=> insert into test3 values(1502,'olympus camera','electrncs'),(1601,'lamaze',ce','Books'),(1666,'harry potter','toys');
INSERT 0 4
musicdb2=> select * from test3;
product_id | product_name | category
------------+----------------------+--------------------------------
1502 | olympus camera | electrncs
1601 | lamaze | toys
1700 | wait interface | Books
1666 | harry potter | toys
(4 rows)
musicdb2=> \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+-------+-------+-------+----------------------------------
public | test1 | table | user1 | {orientation=row,compression=no}
public | test2 | table | user2 | {orientation=row,compression=no}
public | test3 | table | user3 | {orientation=row,compression=no}
(3 rows)
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




