openGauss中一个数据库中可以创建多个模式,课程记录:
1.创建基于表的视图;
2.理解schemas与数据库及全局用户之间的关系:schemas是在数据库层面,用户是在实例层面;
另外\dn 与 sql 查询information_schema.schemata表获得数据有差异:
information_schema、pg_toast、pg_catalog多了3个schema对象。
环境检查:
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-# \db
List of tablespaces
Name | Owner | Location
------------+-------+----------
pg_default | omm |
pg_global | omm |
(2 rows)
--进入数据库omm,创建表空间、测试数据库
omm=# CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
CREATE TABLESPACE
omm=# CREATE DATABASE musicdb WITH TABLESPACE = music_tbs;
CREATE DATABASE
omm=# \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)
omm=# \db
List of tablespaces
Name | Owner | Location
------------+-------+---------------------
music_tbs | omm | tablespace/test_ts1
pg_default | omm |
pg_global | omm |
(3 rows)
创建用户,并赋权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 | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin,
Operatoradmin, Policyadmin, UseFT | {}
user1 | Sysadmin
| {}
--使用用户user1连接到数据库musicdb,首先查看当前数据库下有哪些模式
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=> \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)
创建schema:
musicdb=> create schema schm1 AUTHORIZATION user1;
CREATE SCHEMA
musicdb=> create schema schm2 AUTHORIZATION user1;
CREATE SCHEMA
musicdb=> create schema schm3 AUTHORIZATION user1;
CREATE SCHEMA
musicdb=> create schema schm4 AUTHORIZATION user1;
CREATE SCHEMA
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
schm1 | user1
schm2 | user1
schm3 | user1
schm4 | user1
snapshot | omm
sqladvisor | omm
(14 rows)
通过sql查询schema
musicdb=> SELECT catalog_name, schema_name, schema_owner
musicdb-> 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 | schm2 | user1
musicdb | schm3 | user1
musicdb | schm4 | user1
(17 rows)
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
schm1 | user1
schm2 | user1
schm3 | user1
schm4 | user1
snapshot | omm
sqladvisor | omm
(14 rows)
查询对象不一致,使用sql会多3个;
新建表,插入数据,根据创建表创建视图:
musicdb=> create table schm1.ttt(col varchar(100));
CREATE TABLE
musicdb=> create table schm2.ttt(col varchar(100));
CREATE TABLE
musicdb=> create table schm3.ttt(col varchar(100));
CREATE TABLE
musicdb=> create table schm4.ttt(col varchar(100));
CREATE TABLE
musicdb=> insert into schm1.ttt values('Hello! from schema schm1 11111');
INSERT 0 1
musicdb=> insert into schm2.ttt values('Hello! from schema schm2 22222');
INSERT 0 1
musicdb=> insert into schm3.ttt values('Hello! from schema schm3 33333');
INSERT 0 1
musicdb=> insert into schm4.ttt values('Hello! from schema schm4 44444');
INSERT 0 1
musicdb=> create or replace view my_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=> musicdb=>
musicdb=>
musicdb=> select * from my_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 | my_tables | VIEW
musicdb | schm4 | ttt | BASE TABLE
musicdb | schm3 | ttt | BASE TABLE
musicdb | schm2 | ttt | BASE TABLE
musicdb | schm1 | ttt | BASE TABLE
(8 rows)
查看用户在数据库中搜索模式的顺序:
musicdb=> show SEARCH_PATH;
musicdb=> search_path
----------------
"$user",public
(1 row)
musicdb=> select * from schm1.ttt;
col
----------------------------------
Hello! from schema schm1 11111
(1 row)
musicdb=>
musicdb=> select * from schm2.ttt;
col
----------------------------------
Hello! from schema schm2 22222
(1 row)
musicdb=>
musicdb=> select * from schm3.ttt;
col
----------------------------------
Hello! from schema schm3 33333
(1 row)
musicdb=>
musicdb=> select * from schm4.ttt;
col
----------------------------------
Hello! from schema schm4 44444
(1 row)
模式是在数据库层面,用户是在实例层面:
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=> \du
List of roles
Role name | Attributes
| Member of
-----------+-------------------------------------------------------------------------------
-----------------------------------+-----------
gaussdb | Sysadmin
| {}
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin,
Operatoradmin, Policyadmin, UseFT | {}
user1 | Sysadmin
| {}
musicdb=> \dn
pkg_service | omm
public | omm
schm1 | user1
schm2 | user1
schm3 | user1
schm4 | user1
snapshot | omm
sqladvisor | omm
(14 rows)
List of schemas
Name | Owner
-----------------+-------
blockchain | omm
cstore | omm
db4ai | omm
dbe_perf | omm
dbe_pldebugger | omm
dbe_pldeveloper | omm
musicdb=> \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=# \du
List of roles
Role name | Attributes
| Member of
-----------+-------------------------------------------------------------------------------
-----------------------------------+-----------
gaussdb | Sysadmin
| {}
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin,
Operatoradmin, Policyadmin, UseFT | {}
user1 | Sysadmin
| {}
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
user1 | user1
(11 rows)
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




