【PostgreSQL-9.6.3】表空间

在PostgreSQL中,表空间其实是为表指定一个存储目录,这样方便咱们把不一样的表放在不一样的存储介质或者文件系统中。在建立数据库、表、索引时均可以指定表空间。sql

1. 建立表空间数据库

--表空间目录必须是系统中已存在的目录
test=# create tablespace tb_01 location '/opt/postgresql/data/pg_data';
CREATE TABLESPACE

2. 建立数据库,指定表空间post

test=# create database test01 tablespace tb_01;
CREATE DATABASE

3. 修改数据库的表空间spa

test=# alter database test01 set tablespace tb_02;
ALTER DATABASE
--修改数据库的默认表空间后,数据库中表的表空间不会改变。

4. 建表时,指定表空间postgresql

test=# create table t1 (id int,note text) tablespace tb_01;
CREATE TABLE

5. 建立索引时,指定表空间code

test=# create index idx_t1_id on t1(id) tablespace tb_02;
CREATE INDEX

6. 增长约束时,指定表空间blog

test=# alter table t1 add constraint unique_t1_id unique (id) using index tablespace tb_02;
ALTER TABLE 
test=# alter table t1 add constraint pk_t1_id primary key (id) using index tablespace tb_02;
ALTER TABLE

7. 把表移动到新的表空间索引

test=# alter table t1 set tablespace tb_02;
ALTER TABLE
--表移动过程当中会被锁定,全部的操做都被阻塞,包括Select,因此要选择合适的时间移动表。

 

The End!io

2017-08-20table

相关文章
相关标签/搜索