第 11 天学习打卡。周日的午后学习下 openGauss 的数据库管理。本课学习目标是“创建数据库、删除数据库、重新命名数据库、查看数据库的信息”。
作业
- 创建表空间 enmtbs 和数据库 musicdb
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 TABLESPACE
omm=# CREATE DATABASE musicdb WITH TABLESPACE = enmtbs;
CREATE DATABASE
omm=#
- 查看数据库集簇中有哪些数据库
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=#
- 查看数据库默认的表空间信息
omm=# select datname,dattablespace from pg_database where datname='musicdb';
datname | dattablespace
---------+---------------
musicdb | 16389
(1 row)
- 查看数据库下有哪些模式
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=#
- 查询当前连接的数据库下有哪些表:
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=#
- 更改数据库默认的表空间
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=# CREATE TABLESPACE app_ts RELATIVE LOCATION 'tablespace/app_ts1';
CREATE TABLESPACE
omm=# ALTER DATABASE musicdb 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='musicdb' );
omm=# spcname
---------
app_ts
(1 row)
omm=#
- 重新命名数据库
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)
omm=#
- 修改数据库的默认用户
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 musicdb1 OWNER to user1;
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=#
- 删除数据库
omm=# DROP DATABASE musicdb1;
DROP DATABASE
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=#
命令总结
- 创建数据库
CREATE TABLESPACE enmtbs RELATIVE LOCATION 'tablespace/enmtbs1';
CREATE DATABASE musicdb WITH TABLESPACE = enmtbs;
- 查看数据库
SELECT datname FROM pg_database;
\l
- 重新命名数据库
ALTER DATABASE musicdb RENAME TO musicdb1;
- 修改数据库默认用户
ALTER DATABASE musicdb OWNER to user1;
- 查看默认表空间
select spcname
from pg_tablespace
where oid=( select dattablespace
from pg_database
where datname='musicdb' );
- 查询表空间信息
-- 执行下面的语句,查看musicdb数据库默认表空间的对象OID
select datname,dattablespace from pg_database where datname='musicdb';
-- 根据表空间对象的OID,来查看表空间的名字
select oid,spcname from pg_tablespace where oid=16389;
- 删除数据库
DROP DATABASE musicdb1;
总结
数据库相关的操作比较容易上手,关于数据库的相关操作已梳理到“命令总结”的部分。
历史打卡记录:
openGauss 每日一练第 1 天|openGauss 数据库状态查看
openGauss 每日一练第 2 天|学习 openGauss 客户端工具 gsql 的使用
openGauss 每日一练第 3 天|openGauss 中一个数据库集簇对应多个数据库
openGauss 每日一练第 4 天|openGauss 中一个数据库可以被多个用户访问
openGauss 每日一练第 5 天|openGauss 中一个用户可以访问多个数据库
openGauss 每日一练第 6 天|openGauss 中用户一次只能连接到一个数据库
openGauss 每日一练第 7 天|openGauss 中一个数据库中可以创建多个模式
openGauss 每日一练第 8 天|openGauss 中一个数据库可以存储在多个表空间中
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




