表空间是数据的容器。掌握表空间的管理,包括创建表空间、删除表空间、重命名表空间、查看表空间的情况。
1、创建表空间t_tbspace、用户test,并使用test,在这个表空间上创建表t1
CREATE USER test IDENTIFIED BY 'olabll@123';
CREATE TABLESPACE t_tbspace RELATIVE LOCATION 'tablespace/t_tbspace1';
GRANT CREATE ON TABLESPACE t_tbspace TO test;
\c omm test
create table t1(c1 varchar(20)) tablespace t_tbspace;

不赋权表空间的话无法建表
2、查看表空间t_tbspace的oid和大小
SELECT PG_TABLESPACE_SIZE('t_tbspace');
select oid,* from pg_tablespace where spcname ='t_tbspace';

3、查看数据库在默认表空间下有哪些对象
with objectInDefaultTS as
( select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),
reltablespace,relowner
from pg_class a
where a.relkind in ('r', 'i') and reltablespace='0'
)
select *
from objectInDefaultTS
where relname not like 'pg_%' and relname not like 'gs_%' and relname not like 'sql_%'
order by relpages desc;

4、查看数据库在非默认表空间下有哪些对象
select relname,relkind,relpages,pg_size_pretty(pg_relation_size(a.oid)),
reltablespace,relowner
from pg_class a, pg_tablespace tb
where a.relkind in ('r', 'i')
and a.reltablespace=tb.oid
and tb.spcname='t_tbspace'
order by a.relpages desc;
relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
---------+---------+----------+----------------+---------------+----------
t1 | r | 0 | 0 bytes | 16406 | 16402
(1 row)
5、重命名表空间
普通用户没有权限操作,赋管理员权限后可以
ALTER TABLESPACE t_tbspace RENAME TO t_tbspace2;
ERROR: permission denied for tablespace t_tbspace
DETAIL: N/A
omm=> \q
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=# ALTER TABLESPACE t_tbspace RENAME TO t_tbspace2;
ALTER TABLESPACE
alter user test SYSADMIN;
ALTER TABLESPACE t_tbspace2 RENAME TO t_tbspace3;
omm=# alter user test SYSADMIN;
ALTER ROLE
omm=# \c omm test
Password for user test:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "omm" as user "test".
omm=> ALTER TABLESPACE t_tbspace2 RENAME TO t_tbspace3;
ALTER TABLESPACE
6、删除表空间
omm=> drop table test.t1 ;
DROP TABLE
omm=> DROP TABLESPACE t_tbspace3;
DROP TABLESPACE
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




