学习目标
学习openGauss数据库、用户和模式的关系和访问方式,理解模式是在数据库层面,用户是在实例层面
课程作业
1.查看当前数据库下有哪些模式
注意的是只能查看当前数据库下的schema,原因是pg的数据库之前是强隔离,互相无法访问
两种方法:
gsql元命令
openGauss=# \dn
List of schemas
Name | Owner
-----------------+--------
blockchain | omm
cstore | omm
db4ai | omm
dbe_perf | omm
dbe_pldebugger | omm
dbe_pldeveloper | omm
dbe_sql_util | omm
pkg_service | omm
public | omm
snapshot | omm
sqladvisor | omm
user1 | omm
user10 | user10
user2 | user2
user3 | user3
(15 rows)
数据字典
--当前库
openGauss=# SELECT catalog_name, schema_name, schema_owner FROM information_schema.schemata;
catalog_name | schema_name | schema_owner
--------------+--------------------+--------------
postgres | pg_toast | omm
postgres | cstore | omm
postgres | pkg_service | omm
postgres | dbe_perf | omm
postgres | snapshot | omm
postgres | blockchain | omm
postgres | pg_catalog | omm
postgres | public | omm
postgres | sqladvisor | omm
postgres | dbe_pldebugger | omm
postgres | dbe_pldeveloper | omm
postgres | dbe_sql_util | omm
postgres | information_schema | omm
postgres | db4ai | omm
postgres | user2 | user2
postgres | user3 | user3
postgres | user10 | user10
postgres | user1 | omm
(18 rows)
--musicdb库
openGauss=# \c musicdb
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb" as user "omm".
musicdb=# SELECT catalog_name, schema_name, schema_owner
FROM information_schema.schemata;
catalog_name | schema_name | schema_owner
--------------+--------------------+--------------
musicdb | pg_toast | omm
musicdb | cstore | omm
musicdb | pkg_service | omm
musicdb | dbe_perf | omm
musicdb | snapshot | omm
musicdb | blockchain | omm
musicdb | pg_catalog | omm
musicdb | public | omm
musicdb | sqladvisor | omm
musicdb | dbe_pldebugger | omm
musicdb | dbe_pldeveloper | omm
musicdb | dbe_sql_util | omm
musicdb | information_schema | omm
musicdb | db4ai | omm
musicdb | user1 | user1
(15 rows)
2.然后为数据库musicdb创建4个模式,名称自定义
--当前库的schema和用户
openGauss=# \dn
List of schemas
Name | Owner
-----------------+--------
blockchain | omm
cstore | omm
db4ai | omm
dbe_perf | omm
dbe_pldebugger | omm
dbe_pldeveloper | omm
dbe_sql_util | omm
pkg_service | omm
public | omm
snapshot | omm
sqladvisor | omm
user1 | omm
user10 | user10
user2 | user2
user3 | user3
(15 rows)
--创建4个schema,owner是user10
openGauss=# create schema schm1 AUTHORIZATION user10;
CREATE SCHEMA
openGauss=# create schema schm2 AUTHORIZATION user10;
CREATE SCHEMA
openGauss=# create schema schm3 AUTHORIZATION user10;
CREATE SCHEMA
openGauss=# create schema schm4 AUTHORIZATION user10;
CREATE SCHEMA
openGauss=#
openGauss=# \dn
List of schemas
Name | Owner
-----------------+--------
blockchain | omm
cstore | omm
db4ai | omm
dbe_perf | omm
dbe_pldebugger | omm
dbe_pldeveloper | omm
dbe_sql_util | omm
pkg_service | omm
public | omm
schm1 | user10
schm2 | user10
schm3 | user10
schm4 | user10
snapshot | omm
sqladvisor | omm
user1 | omm
user10 | user10
user2 | user2
user3 | user3
(19 rows)
3.在数据库musicdb的不同的模式下创建同名的表
--在4个schema下分别创建ttt表,并插入数据
openGauss=# create table schm1.ttt(col varchar(100));
CREATE TABLE
openGauss=# create table schm2.ttt(col varchar(100));
CREATE TABLE
openGauss=# create table schm3.ttt(col varchar(100));
CREATE TABLE
openGauss=# create table schm4.ttt(col varchar(100));
CREATE TABLE
openGauss=#
openGauss=#
openGauss=# insert into schm1.ttt values('Hello! from schema schm1 11111');
INSERT 0 1
openGauss=# insert into schm2.ttt values('Hello! from schema schm2 22222');
INSERT 0 1
openGauss=# insert into schm3.ttt values('Hello! from schema schm3 33333');
INSERT 0 1
openGauss=# insert into schm4.ttt values('Hello! from schema schm4 44444');
INSERT 0 1
4.访问musicdb数据库下不同模式的同名表
--通过参数search_path切换查找的模式名
openGauss=# show search_path ;
search_path
-----------------
"$user", public
(1 row)
openGauss=# set search_path = "$user",public,schm1;
SET
openGauss=# \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+--------------+-------+-------+----------------------------------
public | customer_new | table | omm | {orientation=row,compression=no}
public | customer_t | table | omm | {orientation=row,compression=no}
schm1 | ttt | table | omm | {orientation=row,compression=no}
(3 rows)
openGauss=# set search_path = "$user",public,schm2;
SET
openGauss=# \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+--------------+-------+-------+----------------------------------
public | customer_new | table | omm | {orientation=row,compression=no}
public | customer_t | table | omm | {orientation=row,compression=no}
schm2 | t2 | table | omm | {orientation=row,compression=no}
schm2 | ttt | table | omm | {orientation=row,compression=no}
(4 rows)
--通过带schema名去访问不同的schema下的ttt表
openGauss=# select * from schm1.ttt;
col
----------------------------------
Hello! from schema schm1 11111
(1 row)
openGauss=# select * from schm2.ttt;
col
----------------------------------
Hello! from schema schm2 22222
(1 row)
--通过information_schema.tables 视图查找当前库下所有的表
openGauss=# select table_catalog, table_schema, table_name, table_type from information_schema.tables where table_schema not in ('pg_catalog', 'information_schema','dbe_perf');
table_catalog | table_schema | table_name | table_type
---------------+-----------------+--------------+------------
postgres | db4ai | snapshot | BASE TABLE
postgres | dbe_pldeveloper | gs_errors | BASE TABLE
postgres | dbe_pldeveloper | gs_source | BASE TABLE
postgres | schm4 | ttt | BASE TABLE
postgres | schm3 | ttt | BASE TABLE
postgres | schm2 | ttt | BASE TABLE
postgres | schm1 | ttt | BASE TABLE
postgres | user10 | test1 | BASE TABLE
postgres | schm2 | t2 | BASE TABLE
postgres | public | customer_new | BASE TABLE
postgres | public | customer_t | BASE TABLE
(11 rows)
这个和pg_tables差不多的感觉
openGauss=# select * from pg_tables where schemaname not in ('pg_catalog', 'information_schema','dbe_perf');
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | tablecreator | created | last_ddl_time
-----------------+--------------+------------+------------+------------+----------+-------------+--------------+-------------------------------+-------------------------------
public | customer_t | omm | | f | f | f | omm | 2022-11-28 23:13:41.681056+08 | 2022-11-28 23:13:41.681056+08
public | customer_new | omm | | f | f | f | omm | 2022-11-28 23:13:54.090321+08 | 2022-11-28 23:13:54.090321+08
schm2 | t2 | omm | | f | f | f | omm | 2022-11-30 21:45:03.405095+08 | 2022-11-30 21:45:03.405095+08
user10 | test1 | user10 | | f | f | f | user10 | 2022-11-28 23:57:40.120535+08 | 2022-11-28 23:57:40.120535+08
schm1 | ttt | omm | | f | f | f | omm | 2022-11-30 21:04:33.111881+08 | 2022-11-30 21:04:33.111881+08
schm2 | ttt | omm | | f | f | f | omm | 2022-11-30 21:04:33.216989+08 | 2022-11-30 21:04:33.216989+08
schm3 | ttt | omm | | f | f | f | omm | 2022-11-30 21:04:33.25975+08 | 2022-11-30 21:04:33.25975+08
schm4 | ttt | omm | | f | f | f | omm | 2022-11-30 21:04:33.27375+08 | 2022-11-30 21:04:33.27375+08
dbe_pldeveloper | gs_source | omm | | t | f | f | | |
dbe_pldeveloper | gs_errors | omm | | t | f | f | | |
db4ai | snapshot | omm | | t | f | f | | |
(11 rows)
5.实验理解:模式是在数据库层面,用户是在实例层面
--分别在postgres库和 musicdb库查看用户和模式
可以看出,一个实例下两个库的用户是一样的,但是每个库除了默认的dbe、public等schema外,其他schema是在各自库中分别创建出来的
--postgres
openGauss=# \c postgres
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "postgres" as user "omm".
openGauss=# \dn
List of schemas
Name | Owner
-----------------+--------
blockchain | omm
cstore | omm
db4ai | omm
dbe_perf | omm
dbe_pldebugger | omm
dbe_pldeveloper | omm
dbe_sql_util | omm
pkg_service | omm
public | omm
schm1 | user10
schm2 | user10
schm3 | user10
schm4 | user10
snapshot | omm
sqladvisor | omm
user1 | omm
user10 | user10
user2 | user2
user3 | user3
(19 rows)
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 | {}
user10 | Sysadmin | {}
user2 | Sysadmin | {}
user3 | Sysadmin
openGauss=# \c musicdb
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb" as user "omm".
musicdb=# \dn
List of schemas
Name | Owner
-----------------+-------
blockchain | omm
cstore | omm
db4ai | omm
dbe_perf | omm
dbe_pldebugger | omm
dbe_pldeveloper | omm
dbe_sql_util | omm
pkg_service | omm
public | omm
snapshot | omm
sqladvisor | omm
user1 | user1
(12 rows)
--musicdb
musicdb=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------------------------------------------------------------+-----------
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}
user1 | Sysadmin | {}
user10 | Sysadmin | {}
user2 | Sysadmin | {}
user3 | Sysadmin
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




