zoukankan      html  css  js  c++  java
  • 【PostgreSQL-9.6.3】表空间

    在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

  • 相关阅读:
    2016-02-24 工作日记
    金字塔培训
    你找到自己的路了么?
    你是个成熟的职场人么?
    码农十年总结
    码农十年连载六
    码农十年连载五
    码农十年连载四
    码农十年连载三
    码农十年连载二
  • 原文地址:https://www.cnblogs.com/NextAction/p/7400420.html
Copyright © 2011-2022 走看看