openGauss 每日一练第9天
准备
-
切换用户,连接数据库
su - omm gsql -r
1.创建表空间newtbs1、 查看表空间
-- 创建表空间 newtbs1
CREATE TABLESPACE newtbs1 RELATIVE LOCATION 'tablespace/newtbs1';
-- 查看当前有哪些表空间
omm=# \db
List of tablespaces
Name | Owner | Location
--------------+-------+-------------------------
ds_location1 | omm | tablespace/ds_location1
newtbs1 | omm | tablespace/newtbs1
pg_default | omm |
pg_global | omm |
(4 rows)
2.创建一个数据库newdb1、newdb2、newdb3,默认表空间为newtbs1
omm=# CREATE DATABASE newdb1 WITH TABLESPACE =newtbs1;
CREATE DATABASE
omm=# CREATE DATABASE newdb2 WITH TABLESPACE =newtbs1;
CREATE DATABASE
omm=# CREATE DATABASE newdb3 WITH TABLESPACE =newtbs1;
CREATE DATABASE
omm=# \l+
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
-----------+-------+----------+---------+-------+-------------------+-------+------------+--------------------------------------------
newdb1 | omm | UTF8 | C | C | | 12 MB | newtbs1 |
newdb2 | omm | UTF8 | C | C | | 12 MB | newtbs1 |
newdb3 | omm | UTF8 | C | C | | 12 MB | newtbs1 |
omm | omm | UTF8 | C | C | | 12 MB | pg_default |
postgres | omm | UTF8 | C | C | | 12 MB | pg_default | default administrative connection database
template0 | omm | UTF8 | C | C | =c/omm +| 12 MB | pg_default | default template for new databases
| | | | | omm=CTc/omm | | |
template1 | omm | UTF8 | C | C | =c/omm +| 12 MB | pg_default | unmodifiable empty database
| | | | | omm=CTc/omm | | |
(7 rows)
3.使用sql查看表空间newtbs1上有几个数据库
-- 查看数据库所在的表空间,可以看到一个表空间可以有多个数据库
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
newdb2 | 16389 | newtbs1
newdb3 | 16389 | newtbs1
postgres | 1663 | pg_default
(7 rows)
4.在文件系统中查看表空间newtbs1中的多个数据库
-- 查看数据库的oid
omm=# select oid,datname from pg_database;
oid | datname
-------+-----------
1 | template1
16384 | omm
16391 | newdb1
14555 | template0
16392 | newdb2
16393 | newdb3
14560 | postgres
(7 rows)
-- 查看表空间的 oid
omm=# select oid,* from pg_tablespace ;
oid | spcname | spcowner | spcacl | spcoptions | spcmaxsize | relative
-------+------------+----------+--------+------------+------------+----------
1663 | pg_default | 10 | | | | f
1664 | pg_global | 10 | | | | f
16389 | newtbs1 | 10 | | | | t
(3 rows)
-- 查看 数据库 和 表空间的 oid之间的关系 , newdb1、newdb2、newdb3 均位于 oid 为 16389 的表空间上。可以看到 表空间oid 为 16389 对应的表空间名称为 newtbs1
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
newdb2 | 16389 | newtbs1
newdb3 | 16389 | newtbs1
postgres | 1663 | pg_default
(7 rows)
根据表空间的oid 去文件系统中查看
#进入openGauss的数据目录
cd /var/lib/opengauss/data/pg_tblspc
#ls 查看目录结构,可以看到 16389 的目录,对应的就是表空间newtbs1 的oid
omm@modb:/var/lib/opengauss/data/pg_tblspc$ ls
16389
#继续进入目录,16389/PG_9.2_201611171_gaussdb$ 后我们可以看到 有三个ID 的目录,16391、16392、16393 对应的就是上面我们查出来的newdb1、newdb2、newdb3 数据库的oid ,目录下就是各数据存放的数据了。
omm@modb:/var/lib/opengauss/data/pg_tblspc/16389/PG_9.2_201611171_gaussdb$ ls
16391 16392 16393 pgsql_tmp
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




