作者:马顺华
从事运维管理工作多年,目前就职于某科技有限公司,熟悉运维自动化、OceanBase部署运维、MySQL 运维以及各种云平台技术和产品。并已获得OceanBase认证OBCA、OBCP证书。
第11天 |openGauss逻辑结构:数据库管理
学习目标
数据库是数据库对象的容器,在数据库中,可以创建模式、表、索引等数据库对象。openGauss数据库管理包括:创建数据库、删除数据库、重新命名数据库、查看数据库的信息。
创建一个新的数据库。缺省情况下,新数据库将通过复制标准系统数据库template0来创建,且仅支持使用template0来创建。
课程学习
openGauss逻辑结构:数据库管理
1.创建表空间enmtbs和数据库musicdb:
su - omm
gsql -r
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/enmtbs1’;
CREATE DATABASE musicdb WITH TABLESPACE = enmtbs;
omm=# CREATE TABLESPACE enmtbs RELATIVE LOCATION 'tablespace/enmtbs1';
CREATE TABLESPACE
omm=# CREATE DATABASE musicdb WITH TABLESPACE = enmtbs;
CREATE DATABASE
omm=#

2.查看数据库集簇中有哪些数据库
SELECT datname FROM pg_database;
或
\l
omm=# SELECT datname FROM pg_database;
datname
-----------
template1
omm
musicdb
template0
postgres
(5 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)
omm=#

3.查看数据库默认的表空间信息
1)–连接数据库首先执行下面的语句,查看musicdb数据库默认表空间的对象OID:
select datname,dattablespace from pg_database where datname=‘musicdb’;
omm=# select datname,dattablespace from pg_database where datname='musicdb';
datname | dattablespace
---------+---------------
musicdb | 16389
(1 row)
omm=#

2)–连接数据库根据表空间对象的OID,来查看表空间的名字:
select oid,spcname from pg_tablespace where oid=16389;
omm=# select oid,spcname from pg_tablespace where oid=16389;
oid | spcname
-------+---------
16389 | enmtbs
(1 row)
omm=#

3)–连接数据库可以将上面的两条语句合并成一条语句,来查询数据库musicdb默认表空间的名字:
select spcname
from pg_tablespace
where oid=( select dattablespace
from pg_database
where datname=‘musicdb’ );
omm=# from pg_tablespace
omm-# select spcname
omm-# where oid=( select dattablespace
omm(# from pg_database
omm(# where datname='musicdb' );
spcname
---------
enmtbs
(1 row)
omm=#

4.查看数据库下有哪些模式
\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=#

–或
SELECT catalog_name, schema_name, schema_owner FROM information_schema.schemata;
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 | blockchain | omm
omm | pg_catalog | omm
omm | public | omm
omm | sqladvisor | omm
omm | dbe_pldebugger | omm
omm | dbe_pldeveloper | omm
omm | information_schema | omm
omm | db4ai | omm
(13 rows)
omm=#

5.查询当前连接的数据库omm下有哪些表:
with my_tables(table_catalog, table_schema, table_name, table_type) 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;
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-#
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)
omm=#

6.更改数据库默认的表空间
CREATE TABLESPACE app_ts RELATIVE LOCATION ‘tablespace/app_ts1’;
ALTER DATABASE musicdb SET TABLESPACE app_ts;
omm=# CREATE TABLESPACE app_ts RELATIVE LOCATION 'tablespace/app_ts1';
CREATE TABLESPACE
omm=# ALTER DATABASE musicdb SET TABLESPACE app_ts;
ALTER DATABASE
omm=#

–查看表空间是否更改完成
select spcname
from pg_tablespace
where oid=( select dattablespace
from pg_database
where datname=‘musicdb’ );
omm=# select spcname
omm-# from pg_tablespace
omm-# where oid=( select dattablespace
omm(# from pg_database
omm(# where datname='musicdb' );
spcname
---------
app_ts
(1 row)
omm=#

7.修改数据库的默认用户
CREATE USER user1 IDENTIFIED BY ‘user1@ustb2020’;
ALTER USER user1 SYSADMIN;
ALTER DATABASE musicdb OWNER to user1;
omm=# CREATE USER user1 IDENTIFIED BY 'user1@ustb2020';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# ALTER USER user1 SYSADMIN;
ALTER ROLE
omm=# ALTER DATABASE musicdb OWNER to user1;
ALTER DATABASE
omm=#

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

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

9.删除数据库
DROP DATABASE musicdb1;
omm=# DROP DATABASE musicdb1;
DROP DATABASE
omm=#

课程总结
1.创建表空间enmtbs和数据库musicdb
2.查看数据库集簇中有哪些数据库
3.查看数据库默认的表空间信息
4.查看数据库下有哪些模式
5.查询当前连接的数据库下有哪些表:
6.更改数据库默认的表空间
7.重新命名数据库
8.修改数据库的默认用户
9.删除数据库
1.创建表空间enmtbs2和数据库musicdb2
root@modb:~#
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 enmtbs2 relative location 'tablespace/enmtbs2';
CREATE TABLESPACE
omm=# create database musicdb2 with tablespace = enmtbs2;
CREATE DATABASE
omm=#

2.查看数据库集簇中有哪些数据库
omm=#
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
enmdb | omm | UTF8 | C | C |
musicdb2 | omm | UTF8 | C | C |
omm | omm | UTF8 | C | C |
postgres | omm | UTF8 | C | C |
template0 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
(6 rows)
omm=# | | | | | omm=CTc/omm
template1 | omm | UTF8 | C | C | =c/omm +
omm=#
3.查看数据库默认的表空间信息
omm=# select spcname
omm-# from pg_tablespaceomm-#
where oid=( select dattablespace
omm(# from pg_database
omm(# where datname='musicdb2' );
spcname
---------
enmtbs2
(1 row)
omm=#

4.查看数据库下有哪些模式
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 | |
public | omm | omm=UC/omm +| standard public schema | f
| | =U/omm | |
schm3 | omm | | | f
schm1 | user1 | | | f
schm2 | omm | | | f
| | =U/omm | |
testsm1 | omm | | | f
john | john | | | f
pkg_service | omm | | pkg_service schema | f
--More-- snapshot | omm | | snapshot schema | f
sqladvisor | omm | omm=UC/omm +| sqladvisor schema | f
testsm2 | john | | | f
user1 | user1 | | | f
(17 rows)
omm=#

5.查询当前连接的数据库下有哪些表:
omm=# with my_tables 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
omm | testsm1 | t1 | BASE TABLE
omm | public | test | BASE TABLE
(5 rows)
omm=#

6.更改数据库默认的表空间
omm=#
omm=# create tablespace app_ts relative location 'tablespace/app_ts1';
CREATE TABLESPACE
omm=# alter database musicdb2 set tablespace app_ts ;
ALTER DATABASE
omm=# select spcname
omm-# from pg_tablespace
omm-# where oid=( select dattablespace
omm(# from pg_database
omm(# where datname='musicdb2' );
spcname
---------
app_ts
(1 row)
omm=#

7.重新命名数据库
omm=#
omm=# create user user01 identified by 'user1@test';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# alter user user01 sysadmin ;
ALTER ROLE
omm=# alter database musicdb2 owner to user01;
ALTER DATABASE
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
omm=# -----------+--------+----------+---------+-------+-------------------
enmdb | omm | UTF8 | C | C |
musicdb2 | user01 | 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
(6 rows)
omm=#

8.修改数据库的默认用户
omm=# alter database musicdb2 rename to open01;
ALTER DATABASE
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+--------+----------+---------+-------+-------------------
enmdb | omm | UTF8 | C | C |
omm | omm | UTF8 | C | C |
open01 | user01 | 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
(6 rows)
omm=#

9.删除数据库
omm=# drop database open01;
DROP DATABASE
omm=#





