[openGauss 学废系列]-用户和模式的关系以及访问方式

一、学习目标

本节课的学习目标是学习openGauss数据库、用户和模式的关系以及访问方式,理解模式是在数据库层面、用户是在实例层面。

此处借用恩墨培训讲义的一幅讲解openGauss逻辑结构的图:

从上面这幅图,我们能看到openGauss逻辑结构中数据库、模式以及用户的关系。

  • 表由多个行与列组成,被分组存储成为了数据库;
  • 一个由单个openGauss服务器实例管理的数据库集合组成一个数据库集簇(Database Cluster);
  • 一个数据库集簇由多个用户(Users)和多个数据库(Databases)组成,这些数据库和用户共用同一套数据库实例以及相关配置文件。

二、测试练习

2.1 创建数据库及表空间

复制代码
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=# \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 musicdb  WITH TABLESPACE = music_tbs;
CREATE DATABASE

2.2 创建用户并授权

复制代码
omm=# CREATE USER user1 IDENTIFIED BY 'kunpeng@1234';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# ALTER USER user1 SYSADMIN;
ALTER ROLE

2.3 使用创建用户连接数据库

复制代码
omm@modb:~$ gsql -d musicdb  -U user1 -p 5432 -W kunpeng@1234 -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.

musicdb=> \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)
musicdb=>            List of tablespaces
    Name    | Owner |      Location       
------------+-------+---------------------
 music_tbs  | omm   | tablespace/test_ts1
 pg_default | omm   | 

2.4 在musicdb库下创建四个schema

复制代码
musicdb=> create schema  schema2  AUTHORIZATION user1;
musicdb=> CREATE SCHEMA
musicdb=> create schema  schema3  AUTHORIZATION user1;
CREATE SCHEMA
musicdb=> create schema  schema4  AUTHORIZATION user1;
CREATE SCHEMA

2.5 查看musicdb库下所有模式

复制代码
musicdb=> \dn
     List of schemas
      Name       | Owner 
-----------------+-------
 blockchain      | omm
 cstore          | omm
 db4ai           | omm
 dbe_perf        | omm
 dbe_pldebugger  | omm
 dbe_pldeveloper | omm
 pkg_service     | omm
 public          | omm
 schema1         | user1
 schema2         | user1
 schema3         | user1
 schema4         | user1
 schm1           | user1
 snapshot        | omm
 sqladvisor      | omm
(15 rows)

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      | information_schema | omm
 musicdb      | db4ai              | omm
 musicdb      | schm1              | user1
 musicdb      | schema1            | user1
 musicdb      | schema2            | user1
 musicdb      | schema3            | user1
 musicdb      | schema4            | user1
(18 rows)

-- 通过以上两种方式查看数据库下的所有模式,可以发现查询到信息有些差异,\dn查询到的要少一些,相比语句查询少了pg_toast、snapshot、information_schema这三个模式。        

2.6 分别在musicdb库下不同模式创建同名表并插入数据

复制代码
musicdb=> create table schema1.tb01(content varchar(100));
CREATE TABLE
musicdb=> create table schema2.tb01(content varchar(100));
CREATE TABLE
musicdb=> create table schema3.tb01(content varchar(100));
CREATE TABLE
musicdb=> create table schema4.tb01(content varchar(100));
CREATE TABLE
musicdb=> 
musicdb=> insert into schema1.tb01 values('This is schema1');
INSERT 0 1
musicdb=> insert into schema2.tb01 values('This is schema2');
INSERT 0 1
musicdb=> insert into schema3.tb01 values('This is schema3');
INSERT 0 1
musicdb=> insert into schema4.tb01 values('This is schema4');
INSERT 0 1

2.7 创建并查看视图

复制代码
musicdb=> create or replace view show_tables as
musicdb->  select table_catalog, table_schema, table_name, table_type
musicdb-> from information_schema.tables
musicdb-> where table_schema not in ('pg_catalog', 'information_schema','dbe_perf');
CREATE VIEW
musicdb=> select * from show_tables;
 table_catalog |  table_schema   | table_name  | table_type 
---------------+-----------------+-------------+------------
 musicdb       | db4ai           | snapshot    | BASE TABLE
 musicdb       | dbe_pldeveloper | gs_errors   | BASE TABLE
 musicdb       | dbe_pldeveloper | gs_source   | BASE TABLE
 musicdb       | public          | show_tables | VIEW
 musicdb       | schema4         | tb01        | BASE TABLE
 musicdb       | schema3         | tb01        | BASE TABLE
 musicdb       | schema2         | tb01        | BASE TABLE
 musicdb       | schema1         | tb01        | BASE TABLE
(8 rows)

2.8 查看搜索模式顺序

复制代码
musicdb=> show SEARCH_PATH;
  search_path   
----------------
 "$user",public
(1 row)
-- 可以看到搜索模式的顺序是先搜索user,再搜索public

2.9 使用user1访问musicdb库下不同模式的同名表

复制代码
musicdb=> select * from schema1.tb01;  
musicdb=>      content     
-----------------
 This is schema1
(1 row)

musicdb=> select * from schema2.tb01; 
     content     
-----------------
 This is schema2
(1 row)

musicdb=> select * from schema3.tb01;
     content     
-----------------
 This is schema3
(1 row)

musicdb=> select * from schema4.tb01;
     content     
-----------------
 This is schema4
(1 row)

三、学习心得

通过openGauss官网等资料了解openGauss的逻辑结构,并通过实操更形象的去理解数据库、模式、表三者之间的关系。

相关推荐
lunz_fly19924 小时前
Oracle清理:如何安全删除trace, alert和archivelog文件?
oracle
薛定谔的算法5 小时前
phoneGPT:构建专业领域的检索增强型智能问答系统
前端·数据库·后端
Databend6 小时前
Databend 亮相 RustChinaConf 2025,分享基于 Rust 构建商业化数仓平台的探索
数据库
得物技术7 小时前
破解gh-ost变更导致MySQL表膨胀之谜|得物技术
数据库·后端·mysql
Raymond运维12 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
沢田纲吉12 小时前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger2 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化