创建表空间的语法:
highgo=# create tablespace test [owner test] location '/hgdb/test';
CREATE TABLESPACE
创建数据库时可以指定默认的表空间:
create database db01 tablespace tbsspace;
改变一个数据库的默认表空间:
alter database db01 set tablespace new_tabspace;
注:更改数据库默认表空间时,必须没有人同时连到这个数据库上,否则会报错如下:
highgo=# alter database b set tablespace test;
错误: 其他用户正在使用数据库 "b"
DETAIL: 那里有1个其它会话正在使用数据库.
对当前连接的数据库无法更改默认表空间 :
highgo=# alter database highgo set tablespace test;
错误: 无法改变当前已打开数据库的表空间
改变数据库的默认表空间时 ,数据库中已有表的表空间并不会改变。
创建表的时候可以指定表空间:
create table test (id int) tablespace tbsspace;
创建索引、唯一约束、增加主键等都可以指定默认的表空间。
把表从一个表空间移动到另一个表空间的命令如下:
alter table test01 set tablespace pg_default;
注意:在移动表的时候会锁表,此时对该表的所有操作都将被阻塞,包括select操作,所以请考虑在合适的时候做这个操作。
highgo=# create table test (id int);
CREATE TABLE
highgo=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
highgo=# alter table test set tablespace test;
ALTER TABLE
highgo=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
Tablespace: "test"