数据库是数据库对象的容器,在数据库中,可以创建模式、表、索引等数据库对象。openGauss数据库管理包括:创建数据库、删除数据库、重新命名数据库、查看数据库的信息。
创建一个新的数据库。缺省情况下,新数据库将通过复制标准系统数据库template0来创建,且仅支持使用template0来创建。
1.创建表空间enmtbs和数据库musicdb
-- 连接openGuass数据库
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=# drop database if exists musicdb;
NOTICE: database "musicdb" does not exist, skipping
DROP DATABASE
omm=# drop database if exists musicdb1;
NOTICE: database "musicdb1" does not exist, skipping
DROP DATABASE
omm=# drop tablespace if exists enmtbs;
DROP TABLESPACE
omm=# NOTICE: Tablespace "enmtbs" does not exist, skipping.
drop tablespace if exists app_ts;
DROP TABLESPACE
NOTICE: Tablespace "app_ts" does not exist, skipping.
omm=# drop user if exists user1;
NOTICE: role "user1" does not exist, skipping
DROP ROLE
-- 创建表空间enmtbs和数据库musicdb
omm=# CREATE TABLESPACE enmtbs RELATIVE LOCATION 'tablespace/enmtbs1';
CREATE TABLESPACE
omm=# CREATE DATABASE musicdb WITH TABLESPACE = enmtbs;
CREATE DATABASE
2.查看数据库集簇中有哪些数据库
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)
3.查看数据库默认的表空间信息
omm=# select spcname
omm-# from pg_tablespace
omm-# where oid=( select dattablespace
omm(# from pg_database
omm(# where datname='musicdb' );
spcname
---------
enmtbs
(1 row)
4.查看数据库下有哪些模式
omm=# \c musicdb omm
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb" as user "omm".
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)
5.查询当前连接的数据库下有哪些表:
musicdb=# with my_tables(table_catalog, table_schema, table_name, table_type) 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')
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
(3 rows)
6.更改数据库默认的表空间
musicdb=# CREATE TABLESPACE app_ts RELATIVE LOCATION 'tablespace/app_ts1';
CREATE TABLESPACE
musicdb=# \c omm omm
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "omm" as user "omm".
omm=# ALTER DATABASE musicdb SET TABLESPACE app_ts;
ALTER DATABASE
--查看表空间是否更改完成
omm=# select spcname
omm-# from pg_tablespace
omm-# where oid=( select dattablespace
omm(# omm(# from pg_database
where datname='musicdb' );
spcname
---------
app_ts
(1 row)
7.重新命名数据库
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)
8.修改数据库的默认用户
omm=# CREATE USER user1 IDENTIFIED BY 'test@123';
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)
9.删除数据库
omm=# DROP DATABASE musicdb1;
DROP DATABASE
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
omm=# -----------+-------+----------+---------+-------+-------------------
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)
最后修改时间:2022-12-04 12:01:40
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




