第11天-openGauss逻辑结构:数据库管理
学习目标
数据库是数据库对象的容器,在数据库中,可以创建模式、表、索引等数据库对象。openGauss数据库管理包括:创建数据库、删除数据库、重新命名数据库、查看数据库的信息。
创建一个新的数据库。缺省情况下,新数据库将通过复制标准系统数据库template0来创建,且仅支持使用template0来创建。
课程学习
- 创建表空间enmtbs和数据库musicdb:
su - omm
gsql -r
CREATE TABLESPACE enmtbs RELATIVE LOCATION 'tablespace/enmtbs1';
CREATE DATABASE musicdb WITH TABLESPACE = enmtbs;
- 查看数据库集簇中有哪些数据库
SELECT datname FROM pg_database;
或
\l
- 查看数据库默认的表空间信息
- 首先执行下面的语句,查看musicdb数据库默认表空间的对象OID
select datname,dattablespace from pg_database where datname='musicdb';
- 根据表空间对象的OID,来查看表空间的名字
select oid,spcname from pg_tablespace where oid=16389;
- 可以将上面的两条语句合并成一条语句,来查询数据库musicdb默认表空间的名字:
select spcname
from pg_tablespace
where oid=( select dattablespace
from pg_database
where datname='musicdb' );
- 查看数据库下有哪些模式
\dn+
--或
SELECT catalog_name, schema_name, schema_owner FROM information_schema.schemata;
- 查询当前连接的数据库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;
- 更改数据库默认的表空间
CREATE TABLESPACE app_ts RELATIVE LOCATION 'tablespace/app_ts1';
ALTER DATABASE musicdb SET TABLESPACE app_ts;
--查看表空间是否更改完成
select spcname
from pg_tablespace
where oid=( select dattablespace
from pg_database
where datname='musicdb' );
- 修改数据库的默认用户
CREATE USER user1 IDENTIFIED BY 'user1@ustb2020';
ALTER USER user1 SYSADMIN;
ALTER DATABASE musicdb OWNER to user1;
\l
- 重新命名数据库
ALTER DATABASE musicdb RENAME TO musicdb1;
\l
- 删除数据库
DROP DATABASE musicdb1;
课程作业
- 创建表空间enmtbs和数据库musicdb
omm=# create tablespace enmtbs relative location 'tablespace_tbs1';
omm=# CREATE TABLESPACE
omm=# create database musicdb with tablespace = enmtbs;
CREATE DATABASE
omm=#
- 查看数据库集簇中有哪些数据库
#查看有哪些数据库,元命令方式
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)
#查看有哪些数据库,SQL语句方式
omm=# select datname from pg_database;
datname
-----------
template1
omm
template0
postgres
musicdb
(5 rows)
- 查看数据库默认的表空间信息
omm=# select spcname from pg_tablespace where oid=(select dattablespace from pg_database where datname='musicdb');
spcname
---------
enmtbs
(1 row)
- 查看数据库下有哪些模式
#查询schema,元命令方式
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 | |
jack | jack | | | f
pkg_service | omm | | pkg_service schema | f
dbe_pldeveloper | omm | omm=UC/omm +| dbe_pldeveloper schema | f
| | =U/omm | |
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 | |
test | test | | | f
user1 | user1 | | | f
(13 rows)
#查询schema,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 | 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
omm | user1 | user1
omm | jack | jack
omm | test | test
(16 rows)
可以看到,两种查询方式,数据的条数有差异(pg_toast,pg_catalog , information_schema)
- 查询当前连接的数据库下有哪些表:
#SQL语句,查询omm库下有哪些表
with 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')
)
select * from my_tables;
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
(3 rows)
- 更改数据库默认的表空间
#新建表空间 new_tbs
omm=# create tablespace new_tbs relative location 'tablespace/new_tbs1';
CREATE TABLESPACE
#变更默认表空间
omm=# alter database musicdb set tablespace new_tbs;
ALTER DATABASE
#查询变更结果
omm=# select spcname from pg_tablespace where oid=(select dattablespace from pg_database where datname='musicdb');
spcname
---------
new_tbs
(1 row)
- 重新命名数据库
omm=# alter database musicdb rename to musicdb1;
ALTER DATABASE
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
musicdb1 | 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)
- 修改数据库的默认用户
#创建新用户 user1
omm=# create user user1 identified by 'user1@ustb20';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# alter user user1 sysadmin;
ALTER ROLE
#修改数据库的默认用户为 user1
omm=# alter database musicdb1 owner to user1;
ALTER DATABASE
omm=#
#查询更改结果
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=# drop database musicdb1;
DROP DATABASE
omm=#
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
2025年3月国产数据库大事记
墨天轮编辑部
875次阅读
2025-04-03 15:21:16
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
287次阅读
2025-04-17 10:41:41
openGauss 7.0.0-RC1 版本正式发布!
Gauss松鼠会
204次阅读
2025-04-01 12:27:03
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
184次阅读
2025-04-16 09:52:02
openGauss 7.0.0-RC1 版本体验:一主一备快速安装指南
孙莹
180次阅读
2025-04-01 10:30:07
鲲鹏RAG一体机解决方案正式发布 openGauss DataVec向量数据库助力DeepSeek行业应用
Gauss松鼠会
124次阅读
2025-03-31 10:00:29
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
108次阅读
2025-04-23 17:39:13
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
75次阅读
2025-04-18 10:49:53
opengauss使用gs_probackup进行增量备份恢复
进击的CJR
70次阅读
2025-04-09 16:11:58
Postgresql数据库单个Page最多存储多少行数据
maozicb
58次阅读
2025-04-23 16:02:19
目录