学习目标
模式管理包括为数据库创建模式、删除模式、查看和设置模式的搜索路径、查看模式中的信息。
课程学习
1、创建表空间及用户
omm=# CREATE TABLESPACE enmtbs RELATIVE LOCATION 'tablespace/enmtbs1'; CREATE TABLESPACE omm=# CREATE DATABASE enmdb WITH TABLESPACE = enmtbs; CREATE DATABASE omm=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+-------+----------+---------+-------+------------------- enmdb | 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-# \db List of tablespaces Name | Owner | Location ------------+-------+-------------------- enmtbs | omm | tablespace/enmtbs1 pg_default | omm | pg_global | omm | (3 rows)
复制
2、执行下面的gsql元命令,查看openGauss数据库上有哪些用户和模式
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 | {} omm-# \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 snapshot | omm sqladvisor | omm (10 rows)
复制
3、执行下面的SQL语句,创建一个数据库用户user1,其密码为kunpeng@1234,并授予数据库用户user1 SYSADMIN权限:
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 omm=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------------------------------------------------------------+----------- gaussdb | Sysadmin | {}omm=# omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {} user1 | Sysadmin | {} omm=# \dn+ List of schemas Name | Owner | Access privileges | Description | WithBlockChain -----------------+-------+-------------------+----------------------------------+---------------- blockchain | omm | | blockchain schema | f cstore | omm | | reserved schema for DELTA tables | f db4ai | omm | omm=UC/omm +| db4ai schema | f | | =U/omm | | dbe_perf | omm | | dbe_perf schema | f dbe_pldebugger | omm | omm=UC/omm +| dbe_pldebugger schema | f public | omm | omm=UC/omm +| standard public schema | f | | =U/omm | | snapshot | omm | | snapshot schema | f sqladvisor | omm | omm=UC/omm +| sqladvisor schema | f | | =U/omm | | user1 | user1 | | | f (11 rows) | | =U/omm | | dbe_pldeveloper | omm | omm=UC/omm +| dbe_pldeveloper schema | f | | =U/omm | | pkg_service | omm | | pkg_service schema | f
复制
4、PUBLIC模式
openGauss在创建一个新的数据库时,会自动创建一个public模式。当用户登录到该数据库时,如果没有特殊的指定,都是操作在public模式中的数据库对象。
默认情况下,用户新创建的表,位于public模式中
omm=# --执行下面的SQL语句,创建一个测试表test: omm=# omm=# create table test(col1 char(10)); CREATE TABLE omm=# omm=# --执行下面的gsql元命令\dt,查看 test表的Schema: omm=# omm=# \dt test List of relations Schema | Name | Type | Owner | Storage --------+------+-------+-------+---------------------------------- public | test | table | omm | {orientation=row,compression=no} (1 row)
复制
5、一个用户创建多个模式
omm=# --创建一个模式schm1,属主是用户user1,并再次查看当前连接的数据库下有哪些模式: omm=# omm=# create schema schm1 AUTHORIZATION user1; CREATE SCHEMA omm=# omm=# --继续在数据库enmdb中,创建模式schm2,模式schm3,属主是用户omm: omm=# omm=# create schema schm2; CREATE SCHEMA omm=# create schema schm3; CREATE SCHEMA omm=# \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 schm1 | user1 schm2 | omm schm3 | omm snapshot | omm sqladvisor | omm user1 | user1 (14 rows) omm=# \dn+ List of schemas Name | Owner | Access privileges | Description | WithBlockChain -----------------+-------+-------------------+----------------------------------+---------------- blockchain | omm | | blockchain schema | f cstore | omm | | reserved schema for DELTA tables | f db4ai | omm | omm=UC/omm +| db4ai schema | f | | =U/omm | | dbe_perf | omm | | dbe_perf schema | f dbe_pldebugger | omm | omm=UC/omm +| dbe_pldebugger schema | f | | =U/omm | | dbe_pldeveloper | omm | omm=UC/omm +| dbe_pldeveloper schema | f | | =U/omm | | pkg_service | omm | | pkg_service schema | f public | omm | omm=UC/omm +| standard public schema | f | | =U/omm | | schm1 | user1 | | | f schm2 | omm | | | f schm3 | omm | | | f snapshot | omm | | snapshot schema | f sqladvisor | omm | omm=UC/omm +| sqladvisor schema | f | | =U/omm | | user1 | user1 | | | f (14 rows)
复制
6、查看和设置模式的搜索路径
–会话级设置模式搜索顺序
在gsql客户端会话中,执行命令SET SEARCH_PATH TO schm1可以修改模式搜索路径,但只在gsql客户端会话的持续过程中起作用,一旦退出gsql客户端会话,这个设置就丢失了。重新登录gsql会话将模式搜索路径恢复为默认值"$user",public。
omm-# SET SEARCH_PATH TO schm1; ERROR: syntax error at or near "stty" LINE 1: stty cols 124 ^ omm=# SET SEARCH_PATH TO schm1; SET omm=# show SEARCH_PATH; search_path ------------- schm1 (1 row) omm=# \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=# show search_path; search_path ---------------- "$user",public (1 row)
复制
–用户级设置模式搜索顺序
–设置数据库的用户user1的模式搜索顺序为模式schm2:
omm=# ALTER USER user1 SET SEARCH_PATH TO schm2; ALTER ROLE omm=# \q omm@modb:~$ gsql -d enmdb -U user1 -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. enmdb=> show SEARCH_PATH; search_path ------------- schm2 (1 row)
复制
7、搜索顺序的优先级
–会话级模式搜索顺序的优先级最高,用户级模式搜索顺序的优先级第2,数据库级模式搜索顺序的优先级最低。
#在会话级修改模式搜索顺序为模式schm3:
enmdb=> SET SEARCH_PATH TO schm3; SET show SEARCH_PATH;enmdb=> search_path ------------- schm3 (1 row)
复制
8、查看模式有哪些表
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. --查看当前数据库中,public模式下有哪些表 omm=# omm=# select table_catalog,table_schema,table_name,table_type omm-# from information_schema.tables omm-# where table_schema = 'public'; table_catalog | table_schema | table_name | table_type ---------------+--------------+------------+------------ omm | public | test | BASE TABLE (1 row) omm=# omm=# --查看指定数据库中,public模式下有哪些表: omm=# omm=# select table_catalog,table_schema,table_name,table_type omm-# from information_schema.tables omm-# where table_catalog='omm' and table_schema = 'public'; table_catalog | table_schema | table_name | table_type ---------------+--------------+------------+------------ omm | public | test | BASE TABLE (1 row)
复制
学习总结
openGauss的模式是对数据库做一个逻辑分割。所有的数据库对象都建立在模式下面。openGauss的模式和用户是弱绑定的,所谓的弱绑定是指虽然创建用户的同时会自动创建一个同名模式,但用户也可以单独创建模式,并且为用户指定其他的模式。
在一个数据库中,可以有多个模式。模式可以把一组对象组织在一起。这样组织机构有多少个应用,我们可以将数据库对象组织成几个模式;组织机构有几个部门,也可以为该部门创建单独的模式。默认情况下,用户将访问数据库的public模式。今天学习内容还是比较多得,主要是理解模式跟用户的关系,以及对模式的管理。
评论

