1.创建表空间newtbs1、 ds_location1,查看表空间
omm=# create tablespace newtbs2 relative location 'tablespace/newtbs1_c2';
CREATE TABLESPACE
omm=# create tablespace ds_location1 relative location 'tablespace/ds_location1';
CREATE TABLESPACE
omm=# create user user1 identified by 'kunpeng@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# alter user user1 sysadmin;
ALTER ROLE
omm=# \db
List of tablespaces
Name | Owner | Location
--------------+-------+-------------------------
ds_location1 | omm | tablespace/ds_location1
newtbs1 | omm | tablespace/newtbs1_c1
pg_default | omm |
pg_global | omm |
(4 rows)
2.创建一个数据库newdb1,默认表空间为newtbs1
omm=# create database newdb1 with tablespace = newtbs1;
CREATE DATABASE
3.创建用户user5,并授予SYSADMIN权限,访问数据库newdb1,在表空间ds_location1上,创建一个表newt1(表结构自定义)
omm=# create user user5 identified by 'kunpeng@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# alter user user5 sysadmin;
ALTER ROLE
newdb1=# create table newt1 (col char(10)) tablespace ds_location1;
CREATE TABLE
newdb1=# \d+ newt1
Table "public.newt1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+---------------+-----------+----------+--------------+-------------
col | character(10) | | extended | |
Has OIDs: no
Tablespace: "ds_location1"
Options: orientation=row, compression=no
4.查看表所在的表空间
---pg_tables中查询
newdb1=# select schemaname,tablename ,tablespace,created,last_ddl_time from pg_tables where tename='newt1';
schemaname | tablename | tablespace | created | last_ddl_time
------------+-----------+--------------+-------------------------------+------------------------
-------
public | newt1 | ds_location1 | 2022-12-01 11:37:41.892657+08 | 2022-12-01 11:37:41.892
657+08
(1 row)
--使用元命令查询
newdb1=# \d+ newt1
Table "public.newt1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+---------------+-----------+----------+--------------+-------------
col | character(10) | | extended | |
Has OIDs: no
Tablespace: "ds_location1"
Options: orientation=row, compression=no
--
5.查看表空间newtbs1、 ds_location1上的对象
newdb1=# \c newdb1
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "newdb1" as user "omm".
newdb1=#
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 in('ds_location1' ,'newtbs1')
order by a.relpnewdb1=# ages desc;newdb1-# newdb1-# newdb1-# newdb1-# newdb1-#
relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
---------+---------+----------+----------------+---------------+----------
newt1 | r | 0 | 8192 bytes | 16390 | 10
(1 row)
6. 查看数据库默认的表空间
omm=# select datname,dattablespace,spcname from pg_database d,pg_tablespace t where d.dattablespace=t.oid;
datname | dattablespace | spcname
-----------+---------------+------------
template1 | 1663 | pg_default
omm | 1663 | pg_default
newdb1 | 16389 | newtbs1
template0 | 1663 | pg_default
postgres | 1663 | pg_default
(5 rows)
7. 查询默认表空间的表
omm=# \d pg_toast_2618
Did not find any relation named "pg_toast_2618".
omm=# 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'
order by a.relpages desc;omm-# omm-# omm-# omm-#
relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
------------------------------------------------+---------+----------+----------------+---------------+----------
pg_attribute | r | 184 | 1472 kB | 0 | 10
pg_proc | r | 140 | 1120 kB | 0 | 10
pg_depend | r | 59 | 472 kB | 0 | 10
pg_class | r | 52 | 416 kB | 0 | 10
pg_attribute_relid_attnam_index | i | 44 | 352 kB | 0 | 10
pg_proc_proname_all_args_nsp_index | i | 41 | 328 kB | 0 | 10
pg_proc_proname_args_nsp_new_index | i | 39 | 312 kB | 0 | 10
pg_proc_proname_args_nsp_index | i | 39 | 312 kB | 0 | 10
pg_rewrite | r | 37 | 296 kB | 0 | 10
pg_depend_reference_index | i | 34 | 272 kB | 0 | 10
pg_depend_depender_index | i | 34 | 272 kB | 0 | 10
pg_description | r | 31 | 248 kB | 0 | 10
pg_attribute_relid_attnum_index | i | 30 | 240 kB | 「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




