数据库初始化后默认创建pg_default和pg_global表空间;

如果在创建表时没有指定表空间,默认存放在pg_default表空间;

在数据库集群层管理的表存放在pg_global表空间;

pg_default物理位置$PGDATA\base;
pg_global物理位置$PGDATA\global;

一个表空间可以被多个数据库使用,这时会在表空间文件夹下创建该数据库对应的子文件夹;

创建用户表空间会在$PGDATA\pg_tblspc文件夹下创建指向该用户表空间的链接。


创建/查询表空间

postgres=# --
postgres=# create tablespace mytbs01 location '/acdata/pgsql/11/tbs/mytbs01';
CREATE TABLESPACE
postgres=# 
postgres=# select oid , * from pg_tablespace ;
oid    | spcname    | spcowner | spcacl | spcoptions 
-------+------------+----------+--------+------------
1663   | pg_default | 10       |        | 
1664   | pg_global  | 10       |        | 
16391  | mytbs01    | 10       |        | 


$ ls -l /acdata/pgsql/11/data/pg_tblspc
total 0
lrwxrwxrwx. 1 pgsql dba 28 May 17 16:36 16391 -> /acdata/pgsql/11/tbs/mytbs01



更改表所属的表空间

postgres=# alter table t1 set tablespace mytbs01 ;
ALTER TABLE
postgres=# 
postgres=# select relname,reltablespace from pg_class where relname = 't1';

更改表空间位置

$ /usr/pgsql-11/bin/pg_ctl -D /acdata/pgsql/11/data/ stop

$ ls -l
lrwxrwxrwx. 1 pgsql dba 28 May 17 16:36 16391 -> /acdata/pgsql/11/tbs/mytbs01

$ mv /acdata/pgsql/11/tbs/mytbs01 /acdata/pgsql/11/tbs/myts01 

$ unlink 16391

$ ln -s /acdata/pgsql/11/tbs/myts01 16391
$ ls -l
lrwxrwxrwx. 1 pgsql dba 27 May 17 17:19 16391 -> /acdata/pgsql/11/tbs/myts01

$ /usr/pgsql-11/bin/pg_ctl -D /acdata/pgsql/11/data/ -l /acdata/pgsql/11/logs/alert_pg.log start

临时表空间

--默认安装后,临时文件存放于$PGDATA/base/pgsql_temp
--可以新建临时表空间

postgres=# create tablespace for_temp_files location '/tmp';
CREATE TABLESPACE
postgres=# 
postgres=# alter system set temp_tablespace to '/tmp';
ALTER SYSTEM
postgres=# 
postgres=# select pg_reload_conf();
postgres=# 
postgres=# show temp_tablespaces;
  • No labels