码迷,mamicode.com
首页 > 数据库 > 详细

【PostgreSQL】表空间

时间:2017-08-20 18:20:37      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:post   text   创建数据库   add   创建索引   指定   tables   alt   uniq   

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

1. 创建表空间

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

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

test=# create database test01 tablespace tb_01;
CREATE DATABASE

3. 修改数据库的表空间

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

4. 建表时,指定表空间

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

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

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

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

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!

2017-08-20

 

【PostgreSQL】表空间

标签:post   text   创建数据库   add   创建索引   指定   tables   alt   uniq   

原文地址:http://www.cnblogs.com/NextAction/p/7400420.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!