opengauss 数据库管理
1.创建表空间enmtbs和数据库musicdb
--创建表空间
omm=# create tablespace enmtbs relative location 'tablespace/enmtbs11';
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=# | | | | | omm=CTc/omm
template1 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
(5 rows)
--系统表中查询
omm=# select datname from pg_database;
datname
-----------
template1
omm
template0
postgres
musicdb
(5 rows)
3.查看数据库默认的表空间信息
omm=# select datname,dattablespace ,spcname from pg_database a ,pg_tablespace b where a.dattablespace=b.oid;
datname | dattablespace | spcname
-----------+---------------+------------
template1 | 1663 | pg_default
omm | 1663 | pg_default
template0 | 1663 | pg_default
postgres | 1663 | pg_default
musicdb | 16396 | enmtbs
(5 rows)
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 | |
pkg_service | omm | | pkg_service schema | f
public | omm | omm=UC/omm +| standard public schema | f
| | =U/omm | |
dbe_pldeveloper | omm | omm=UC/omm +| dbe_pldeveloper schema | f
sqladvisor | omm | omm=UC/omm +| sqladvisor schema | f
| | =U/omm | |
| | =U/omm | |
snapshot | omm | | snapshot schema | f
uesr1 | uesr1 | | | f
(11 rows)
--系统表查询
omm=# select * from information_schema.schemata;
catalog_name | schema_name | schema_owner | default_character_set_catalog | default_character_set_schema | default_character_set_name | s
ql_path
--------------+--------------------+--------------+-------------------------------+------------------------------+----------------------------+--
--------
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 | information_schema | omm | | | |
omm | db4ai | omm | | | |
omm | uesr1 | uesr1 | | | |
(14 rows)
omm | dbe_pldebugger | omm | | | |
omm | dbe_pldeveloper | omm | | | |
5.查询当前连接的数据库下有哪些表:
omm=# select table_catalog, table_schema, table_name, table_type
omm-# from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema','dbe_perf')
omm-# ;
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)
6.更改数据库默认的表空间
--修改数据库默认表空间
omm=# alter database musicdb set tablespace app_ts;
ALTER DATABASE
--查看数据库默认表空间
omm=# select datname,dattablespace ,spcname from pg_database a ,pg_tablespace b where a.dattablespace=b.oid;
datname | dattablespace | spcname
-----------+---------------+------------
template1 | 1663 | pg_default
omm | 1663 | pg_default
template0 | 1663 | pg_default
postgres | 1663 | pg_default
musicdb | 16391 | app_ts
7.重新命名数据库
omm=# alter database musicdb rename to musicdb1 ;
ALTER DATABASE
omm=# \l
omm | omm | UTF8 | C | C |
postgres | omm | UTF8 | C | C |
template0 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
musicdb1 | omm | UTF8 | C | C |
template1 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
(5 rows)
8.修改数据库的默认用户
--创建用户user2
omm=# create user user2 identified by 'kunpeng@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
--修改user2的权限
omm=# alter user user2 sysadmin;
ALTER ROLE
--修改数据库的owner
omm=# alter database musicdb1 owner to user2;
ALTER DATABASE
--查看数据库和owner
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
omm=# musicdb1 | user2 | 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
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




