暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第 7 天 | 表空间

原创 手机用户2761 2021-12-11
560

表空间用于管理数据对象,与磁盘上的一个目录对应

0. 进入环境

#第一次进入等待15秒
#数据库启动中...
su - omm
gsql -r

1.创建表空间,表空间tspc1使用相对路径指定所在目录,表空间tspc2指定owner为Lucy

创建前先看看原空间有哪些tablespace.

\db+

– 回显

                       List of tablespaces
    Name    | Owner | Location | Access privileges | Description
------------+-------+----------+-------------------+-------------
 pg_default | omm   |          |                   |
 pg_global  | omm   |          |                   |
(2 rows)

创建后来对比一下

CREATE TABLESPACE tspc1 RELATIVE LOCATION 'tablespace/tspc1';
CREATE TABLESPACE tspc2 RELATIVE LOCATION 'tablespace/tspc2';
CREATE USER Lucy PASSWORD 'abcd@123';
ALTER TABLESPACE tspc2 OWNER TO Lucy;
\db+

– 回显

                           List of tablespaces
    Name    | Owner |     Location     | Access privileges | Description
------------+-------+------------------+-------------------+-------------
 pg_default | omm   |                  |                   |
 pg_global  | omm   |                  |                   |
 tspc1      | omm   | tablespace/tspc1 |                   |
 tspc2      | lucy  | tablespace/tspc2 |                   |
(4 rows)

2.在表空间tspc1中建表,并使用视图pg_tables查看信息

create table tspc1_t1(id int, name char(30)) tablespace tspc1;
\d+
select * from pg_tables where tablename = 'tspc1_t1';

– 回显

Schema |   Name   | Type  | Owner |  Size   |             Storage              | Description
public | tspc1_t1 | table | omm   | 0 bytes | {orientation=row,compression=no} |


 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | tablecreator | created | last_ddl_time
------------+-----------+------------+------------+------------+----------+-------------+--------------+---------+-------------------------------
 public     | tspc1_t1  | omm        | tspc1      | f          | f        | f           | omm          | 2021-12-11 19:20:33.336437+08 | 2021-12-11 19:20:33.336437+08

我来创tspc2 作为一个对比, 熟能生巧!

create table tspc2_t2(id int, name char(30)) tablespace tspc2;
select * from pg_tables where tablename = 'tspc2_t2';
create table tspc2_t0(id int, name char(30)) tablespace tspc2;
select * from pg_tables where tablename = 'tspc2_t0';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | tablecreator |           created            |        last_ddl_time
------------+-----------+------------+------------+------------+----------+-------------+--------------+------------------------------+------------------------------
 public     | tspc2_t2  | omm        | tspc2      | f          | f        | f           | omm          | 2021-12-11 19:25:45.40856+08 | 2021-12-11 19:25:45.40856+08
(1 row)

3.重命名tspc1,修改tspc2的用户为Lily,使用\db查看表空间信息

ALTER TABLESPACE tspc1 RENAME TO tpspc10;
CREATE USER Lily PASSWORD "abcd@123";
ALTER TABLESPACE tspc2 OWNER TO Lily;
\db

一个番外Tips, 如果要换用Lucy的角色来完成表的创建, 要先给予OMM换Lucy的权限.

GRANT Lucy to OMM;
SET ROLE Lucy PASSWORD 'abcd@123';

– 回显

 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | tablecreator |            created            |         last_ddl_time
------------+-----------+------------+------------+------------+----------+-------------+--------------+-------------------------------+-------------------------------
 lucy       | tspc2_t0  | lucy       | tspc2      | f          | f        | f           | lucy         | 2021-12-11 19:33:13.210434+08 | 2021-12-11 19:33:13.210434+08
(1 row)

4.删除表空间

删除表空间是否可以使用CASCADE Sorry, 不行, 老老实实把表删干净吧!!

DROP TABLE tspc2_t2;
DROP TABLE tspc2_t0;
DROP TABLE tspc1_t1;
DROP TABLESPACE tspc10;
DROP TABLESPACE tspc2;
\db+

– 回显

                       List of tablespaces
    Name    | Owner | Location | Access privileges | Description
------------+-------+----------+-------------------+-------------
 pg_default | omm   |          |                   |
 pg_global  | omm   |          |                   |
(2 rows)

一切回归原点(完)

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论