学习目标
学习 openGauss 数据库、用户和模式的关系和访问方式,理解模式是在数据库层面,用户是在实例层面
课程学习
一个用户连接到数据库后,可以在这个数据库中创建多个模式。要访问这些模式,可以使用DatabaseName.SchemaName.TableName或者SchemaName.TableName,来访问某个模式下的一个表。
默认情况下访问public模式下的表,可以不用添加模式名前缀。
连接数据库
[root@opengauss ~]# su - omm
Last login: Fri Jan 31 11:19:22 CST 2025 on pts/0
[omm@opengauss ~]$ gsql -d postgres -p 5432
gsql ((openGauss 5.0.3 build 89d144c2) compiled at 2024-07-31 20:59:31 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
1.创建模式、查看模式
--进入数据库test,创建表空间、测试数据库
\set PROMPT1 '%n@%m %~%R%#'
CREATE TABLESPACE test_tbs RELATIVE LOCATION 'tablespace/test_ts1';
CREATE DATABASE testdb WITH TABLESPACE = test_tbs;
--执行下面的 SQL 语句,创建用户 user1:
CREATE USER user1 IDENTIFIED BY 'opengauss_1234';
--授予user1数据库系统的SYSADMIN权限:
ALTER USER user1 SYSADMIN;
--使用用户 user1 连接到数据库 testdb,首先查看当前数据库下有哪些模式;
\q
gsql -d testdb -U user1 -p 5432 -W opengauss_1234 -r
\dn
testdb=> \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
(11 rows)
2.然后为数据库 testdb 创建 4 个模式: gauss1、 gauss2、 gauss3、 gauss4
--用户 user1 在数据库 testdb 中,创建了 4 个模式:
create schema gauss1 AUTHORIZATION user1;
create schema gauss2 AUTHORIZATION user1;
create schema gauss3 AUTHORIZATION user1;
create schema gauss4 AUTHORIZATION user1;
–查看testdb数据库下有哪些模式:
\dn
--除了可以用 gsql 的元命令 \dn 来查看数据库有哪些模式,还可以执行下面的 SQL 语句,查看某个数据库下有哪些模式:
SELECT catalog_name, schema_name, schema_owner
FROM information_schema.schemata;
3.在数据库 testdb 的不同的模式下创建同名的表:
--在不同模式下,创建相同的表
create table gauss1.ttt(col varchar(100));
create table gauss2.ttt(col varchar(100));
create table gauss3.ttt(col varchar(100));
create table gauss4.ttt(col varchar(100));
--执行下面的SQL语句,往4个模式中的表ttt分别插入一条数据:
--在同一个数据库下,可以直接使用SchemaName.TableName来指定一个表,可以省略数据库名。
insert into gauss1.ttt values('Hello! from schema gauss1 11111');
insert into gauss2.ttt values('Hello! from schema gauss2 22222');
insert into gauss3.ttt values('Hello! from schema gauss3 33333');
insert into gauss4.ttt values('Hello! from schema gauss4 44444');
--执行下面的SQL语句,查看testdb数据库目前有哪些表
--创建视图:
create or replace view my_tables as
select table_catalog, table_schema, table_name, table_type
from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema','dbe_perf');
--查看视图:
select * from my_tables;
testdb=> create or replace view my_tables as
testdb-> select table_catalog, table_schema, table_name, table_type
testdb-> from information_schema.tables
testdb-> where table_schema not in ('pg_catalog', 'information_schema','dbe_perf');
4.查看用户在数据库中搜索模式的顺序:
--查看默认的搜索模式的顺序
show SEARCH_PATH;
--访问 testdb 数据库下其他模式的表,需要指定模式名前缀:
select * from gauss1.ttt;
select * from gauss2.ttt;
select * from gauss3.ttt;
select * from gauss4.ttt;
5.模式是在数据库层面,用户是在实例层面
--登录 testdb 数据库,查看用户和模式
gsql -d testdb -U user1 -p 5432 -W opengauss_1234 -r
\du
\dn
\q
--登录 omm 数据库,查看用户和模式
gsql -r -p 5432
\du
\dn
\q
课程作业
1.查看当前数据库下有哪些模式
testdb=> \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
gauss1 | user1
gauss2 | user1
gauss3 | user1
gauss4 | user1
pkg_service | omm
public | omm
snapshot | omm
sqladvisor | omm
(15 rows)
2.然后为数据库 testdb 创建 4 个模式,名称自定义
create schema gauss1;
create schema gauss2;
create schema gauss3;
create schema gauss4;
testdb=> create schema gauss4;
CREATE SCHEMA
testdb=> \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
gauss1 | user1
gauss2 | user1
gauss3 | user1
gauss4 | user1
pkg_service | omm
public | omm
snapshot | omm
sqladvisor | omm
(15 rows)
3.在数据库 testdb 的不同的模式下创建同名的表
--在不同模式下,创建相同的表
create table gauss1.ttt(col varchar(100));
create table gauss2.ttt(col varchar(100));
create table gauss3.ttt(col varchar(100));
create table gauss4.ttt(col varchar(100));
--执行下面的 SQL 语句,往4个模式中的表ttt分别插入一条数据:
--在同一个数据库下,可以直接使用 SchemaName.TableName 来指定一个表,可以省略数据库名。
insert into gauss1.ttt values('Hello! from schema gauss1 11111');
insert into gauss2.ttt values('Hello! from schema gauss2 22222');
insert into gauss3.ttt values('Hello! from schema gauss3 33333');
insert into gauss4.ttt values('Hello! from schema gauss4 44444');
4.访问 testdb 数据库下不同模式的同名表
select * from gauss1.ttt;
select * from gauss2.ttt;
select * from gauss3.ttt;
select * from gauss4.ttt;
5.实验理解:模式是在数据库层面,用户是在实例层面
如下,登录不同的数据库,看到的模式不一样,故模式是在数据库层面的对象,看到的用户是一样的,故用户是在实例层面的对象。
--登录 testdb 数据库,查看用户和模式
gsql -d testdb -U user1 -p 5432 -W opengauss_1234 -r
\du
\dn
\q
--登录 postgres 数据库,查看用户和模式
[omm@opengauss ~]$ gsql -d postgres -p 5432
gsql ((openGauss 5.0.3 build 89d144c2) compiled at 2024-07-31 20:59:31 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# \du
List of roles
Role name | Attributes | Memb
er of
-----------+------------------------------------------------------------------------------------------------------------------+-----
------
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}
openg | | {}
user1 | Sysadmin | {}
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
openg | openg
pkg_service | omm
public | omm
snapshot | omm
sqladvisor | omm
(12 rows)