暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

openGauss每日一练第11天 - 学习心得体会

原创 尚雷 2022-12-04
746

一、学习目标

不知不觉,这次在线课程已经过半了。感谢墨天轮给的这次学习openGauss数据库机会,也感谢负责指定这套课程的老师。

本节课的重点是要学习openGauss逻辑结构中数据库这一对象的相关知识。

首先数据库是数据库对象的容器,在数据库中,可以创建模式、表、索引等数据库对象。

openGauss数据库管理包括:创建数据库、删除数据库、重命名数据库、查看数据库信息,数据库备份、数据库审计等。

openGauss 3.0 版本在安装数据库软件后,默认会有三个库,分别是postgres和两个模板库(template0和template1)。

创建一个新的数据库,缺省情况下,新数据库将通过复制标准系统数据库template0来创建,且仅支持使用template0来创建。

此处仍然要借用openGauss的一副图来描述openGauss数据库,想要理解openGauss数据库,就要熟悉理解这幅图的所描述的数据库关系。

image.png

二、测试练习

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=# CREATE TABLESPACE enmtbs RELATIVE LOCATION 'tablespace/enmtbs';
CREATE TABLESPACE
omm=# CREATE DATABASE musicdb WITH TABLESPACE = enmtbs;
CREATE DATABASE
omm=# \db
          List of tablespaces
    Name    | Owner |     Location      
------------+-------+-------------------
 enmtbs     | omm   | tablespace/enmtbs
 pg_default | omm   | 
 pg_global  | omm   | 
(3 rows)

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)

image.png

2.2 查看数据库集簇有哪些库

omm=# SELECT datname FROM pg_database;
  datname  
-----------
 template1
 omm
 musicdb
 template0
 postgres
(5 rows)

-- 可以看到使用如上语句查询到的信息和执行 \l 相同
omm=# \l 
                         List of databases
   Name    | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+-------+----------+---------+-------+-------------------
 template1 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
(5 rows)

omm=#  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

image.png

2.3 查看数据库默认表空间信息

-- 查询新创建数据库musicdb的oid信息
omm=# select datname,dattablespace from pg_database where datname='musicdb';

(1 row)

omm=#  datname | dattablespace 
---------+---------------
 musicdb |         16389
omm=# select spcname 
omm-# from pg_tablespace 
omm-# where oid=( select dattablespace
omm(#            from pg_database
omm(#            where datname='musicdb' );
 spcname 
---------
 enmtbs
(1 row)

omm=# select datname,dattablespace from pg_database where datname='musicdb';
 datname | dattablespace 
---------+---------------
 musicdb |         16389
(1 row)

根据以上查询的数据库oid来查询其默认表空间信息
omm=# select oid,spcname from pg_tablespace where oid=16389;
omm=#   oid  | spcname 
-------+---------
 16389 | enmtbs
(1 row)

omm=# select spcname 
omm-# from pg_tablespace 
omm-# where oid=( select dattablespace
omm(#            from pg_database
omm(#            where datname='musicdb' );
 spcname 
---------
 enmtbs
(1 row)

image.png

2.4 查看数据库下相关模式

可以使用\dn 来查看数据库下有哪些模式
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)
-- 如想查看详细信息,还可以采用 \dn+来查看,信息更详细
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            |                                  | 
 snapshot        | omm   |                   | snapshot schema                  | f
 sqladvisor      | omm   | omm=UC/omm       +| sqladvisor schema                | f
                 |       | =U/omm            |                                  | 
(10 rows)omm=# 

--除了上述两种方式,还可以采用如下SQL来查看数据库及其所属的模式对象
omm=# SELECT catalog_name, schema_name, schema_owner FROM information_schema.schemata;
 catalog_name |    schema_name     | schema_owner 
--------------+--------------------+--------------
 omm          | pg_toast           | omm
 omm          | cstore             | omm
 omm          | pkg_service        | omm
 omm          | dbe_perf           | omm
 omm          | snapshot           | omm omm          | db4ai              | omm
(13 rows)


 omm          | blockchain         | omm
 omm          | pg_catalog         | omm
 omm          | public             | omm
 omm          | sqladvisor         | omm
 omm          | dbe_pldebugger     | omm
 omm          | dbe_pldeveloper    | omm
 omm          | information_schema | omm

image.png

2.5 查询当前数据库omm用户下相关表

omm=# with my_tables(table_catalog, table_schema, table_name, table_type) as
omm-#       (   select table_catalog, table_schema, table_name, table_type
omm(#           from information_schema.tables
omm(#          where table_schema not in ('pg_catalog', 'information_schema','dbe_perf')
omm(#        )
omm-#  select * from my_tables;
 table_catalog |  table_schema   | table_name | table_type 
---------------+-----------------+------------+------------
 omm           | db4ai           | snapshot   | BASE TABLE
 omm           | dbe_pldeveloper | gs_errors  | BASE TABLE
 omm           | dbe_pldeveloper | gs_source  | BASE TABLE
(3 rows)

image.png

2.6 更改数据库默认表空间

-- 创建一新表空间
omm=# CREATE TABLESPACE new_enmtbs RELATIVE LOCATION 'tablespace/new_enmtbs';
CREATE TABLESPACE
修改数据库musicdb默认表空间为新表空间new_enmtbs 
omm=# ALTER DATABASE musicdb  SET TABLESPACE new_enmtbs;
ALTER DATABASE
-- 查看数据库及对应默认表空间
omm=# select spcname 
omm-# from pg_tablespace 
omm-# where oid=( select dattablespace
omm(#            from pg_database
omm(#            where datname='musicdb' );
  spcname   
------------
 new_enmtbs
(1 row)

image.png

2.7 重命名数据库

-- 查看所有数据库
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)

-- 重命名musicdb 数据库为musicdb01
omm=# ALTER DATABASE musicdb RENAME TO  musicdb01;
ALTER DATABASE
omm=#   
omm=# \l
                         List of databases
   Name    | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+-------+----------+---------+-------+-------------------
 musicdb01 | 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)

image.png

2.8 修改数据库默认用户

omm=# \l
                         List of databases
   Name    | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+-------+----------+---------+-------+-------------------
 musicdb01 | 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)

-- 创建新用户mogdbuser 
omm=# CREATE USER mogdbuser IDENTIFIED BY 'mogdbuser_123';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
-- 授权mogdbuser 用户为SYSADMIN权限
omm=# ALTER USER mogdbuser SYSADMIN;
ALTER ROLE

omm=# ALTER DATABASE musicdb OWNER to mogdbuser;
ERROR:  database "musicdb" does not exist
-- 修改musicdb01 默认用户为 mogdbuser
omm=# ALTER DATABASE musicdb01 OWNER to mogdbuser;
ALTER DATABASE
omm=# 
omm=# \l
                           List of databases
   Name    |   Owner   | Encoding | Collate | Ctype | Access privileges 
-----------+-----------+----------+---------+-------+-------------------
 musicdb01 | mogdbuser | 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=# \q

image.png

2.9 删除数据库

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 
-----------+-----------+----------+---------+-------+-------------------
 musicdb01 | mogdbuser | 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=# drop database musicdb01;
DROP DATABASE
omm=#  
omm=# \l
 template0 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
 template1 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
(4 rows)

                         List of databases
   Name    | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+-------+----------+---------+-------+-------------------
 omm       | omm   | UTF8     | C       | C     | 
 postgres  | omm   | UTF8     | C       | C     

image.png

三、心得体会

通过本节课的学习,要重点熟悉如何创建数据库、指定数据库默认表空间及用户,如何查看数据库及所对应的表空间上的对象信息。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论