openGauss中一个数据库中可以创建多个模式 :重要概念:模式是在数据库层面,用户是在实例层面
opengauss数据字典表: information_schema.tables
1 查看当前数据库下有哪些模式
openGauss=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------------------------------------------------------------+-----------
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}
user10 | Sysadmin | {}
user3 | Sysadmin | {}
zyf | 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
pkg_service | omm
public | omm
snapshot | omm
sqladvisor | omm
user10 | user10
(12 rows)
2 数据库musicdb创建4个模式
musicdb1-> \conninfo
You are connected to database "musicdb1" as user "user10" via socket in "/opt/huawei/tmp" at port "26000".
musicdb1->
create schema schm1 AUTHORIZATION user10;
create schema schm2 AUTHORIZATION user10;
create schema schm3 AUTHORIZATION user10;
create schema schm4 AUTHORIZATION user10;
musicdb1=> \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
(15 rows)
3 在数据库musicdb的不同的模式下创建同名的表
create table schm1.ttt(col varchar(100));
create table schm2.ttt(col varchar(100));
create table schm3.ttt(col varchar(100));
create table schm4.ttt(col varchar(100));
insert into schm1.ttt values('Hello! from schema schm1 11111');
insert into schm2.ttt values('Hello! from schema schm2 22222');
insert into schm3.ttt values('Hello! from schema schm3 33333');
insert into schm4.ttt values('Hello! from schema schm4 44444');
4 查看musicdb数据库目前有哪些表!!!
create or replace view v_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');
musicdb1=> select *from v_my_t
musicdb1=> select *from v_my_tables;
table_catalog | table_schema | table_name | table_type
---------------+-----------------+-------------+------------
musicdb1 | db4ai | snapshot | BASE TABLE
musicdb1 | dbe_pldeveloper | gs_errors | BASE TABLE
musicdb1 | dbe_pldeveloper | gs_source | BASE TABLE
musicdb1 | public | v_my_tables | VIEW
musicdb1 | schm4 | ttt | BASE TABLE
musicdb1 | schm3 | ttt | BASE TABLE
musicdb1 | schm2 | ttt | BASE TABLE
musicdb1 | schm1 | ttt | BASE TABLE
(8 rows)
select *
from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema','dbe_perf');
5 访问musicdb数据库下不同模式的同名表
musicdb1=> select *from musicdb1.schm1.ttt;
col
----------------------------------
Hello! from schema schm1 11111
(1 row)
musicdb1=> select *from schm1.ttt;
col
----------------------------------
Hello! from schema schm1 11111
(1 row)
musicdb1=> select *from schm2.ttt;
col
----------------------------------
Hello! from schema schm2 22222
(1 row)
musicdb1=> select *from schm3.ttt;
col
----------------------------------
Hello! from schema schm3 33333
(1 row)
musicdb1=> select *from schm4.ttt;
col
----------------------------------
Hello! from schema schm4 44444
(1 row)
musicdb1=>
6 模式是在数据库层面,用户是在实例层面
可以看到 musicdb1 中的 schm1-4 在 postgres数据库中不存在,说明了 模式是在数据库层面。
用 omm用户登录 postgres 数据库,\du 可以看到用户 user10等
用 user10 用户登录 music10 数据库 ,\du 也可以看到用户 user10等。
说明 用户是在实例层面,不属于某个数据库。
musicdb1=> \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------------------------------------------------------------+-----------
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}
user10 | Sysadmin | {}
user3 | Sysadmin | {}
zyf | Sysadmin | {}
musicdb1=> \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 | |
dbe_sql_util | omm | omm=UC/omm +| sql util schema | f
| | =U/omm | |
pkg_service | omm | | pkg_service schema | f
public | omm | omm=UC/omm +| standard public schema | f
| | =U/omm | |
schm1 | user10 | | | f
schm2 | user10 | | | f
schm3 | user10 | | | f
schm4 | user10 | | | f
snapshot | omm | | snapshot schema | f
sqladvisor | omm | omm=UC/omm +| sqladvisor schema | f
| | =U/omm | |
(15 rows)
musicdb1=> \q
[omm@node1 ~]$ gsql -p 26000 -d postgres -r
gsql ((openGauss 3.1.0 build 4e931f9a) compiled at 2022-09-29 14:19:24 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
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
user10 | user10
(12 rows)
openGauss=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------------------------------------------------------------+-----------
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}
user10 | Sysadmin | {}
user3 | Sysadmin | {}
zyf | Sysadmin | {}
openGauss=#
最后修改时间:2022-11-30 11:02:49
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




