반응형
/********************************************************************************************
-- Title : [PGS9.2] TABLESPACE management - ver.dBRang
-- Reference : dbrang.tistory.com
-- Key word : tablespace
********************************************************************************************/
/****************************
-- 테이블스페이스 구조
**************************** /
/****************************
-- 디렉토리 생성
**************************** /
-- 디렉토리 생성
**************************** /
-- 테이블스페이스 디렉토리 생성
$ mkdir /home/postgres/pgsql/data1
$ mkdir /home/postgres/pgsql/data2
$ mkdir /home/postgres/pgsql/data3
$ mkdir /home/postgres/pgsql/data4
$ mkdir /home/postgres/pgsql/data5
$ mkdir /home/postgres/pgsql/data_default
/****************************
-- 테이블스페이스 생성/활용
****************************/
-- 테이블스페이스 생성
=# create tablespace ts_data1
owner sys
location '/home/postgres/pgsql/data1';
=# create tablespace ts_data2
owner sys
location '/home/postgres/pgsql/data2';
=# create tablespace ts_data3
owner sys
location '/home/postgres/pgsql/data3';
=# create tablespace ts_data4
owner sys
location '/home/postgres/pgsql/data4';
=# create tablespace ts_data5
owner sys
location '/home/postgres/pgsql/data5';
=# create tablespace ts_default
owner sys
location '/home/postgres/pgsql/data_default';
=# create database tttdb
with owner = sys
tablespace = ts_default;
-- db 확인
=# select d.datdba "ownr_id", pg_get_userbyid(d.datdba) "ownr_nm"
, d.oid "db_id", d.datname "db_nm"
, pg_size_pretty(pg_database_size(d.datname)) "db_sz"
, dattablespace "bas_tbs_id"
, t.spcname "bas_tbs_nm"
from pg_database d
inner join pg_tablespace t
on d.dattablespace = t.oid;
-- 테이블 생성 및 TBS 확인
=# \c tttdb; /* 반드시 tttdb에서 수행 */
=# create table ttt_default(a int);
=# alter database tttdb
set default_tablespace = ts_data1;
=# create table ttt_data1(a int); -- 기존 세션에서
=# create table ttt_data1_1(a int); -- 새로운 세션에서
=# create table ttt_data1_2(a int); -- 다시 기존 세션에서
=# alter database tttdb
set default_tablespace = ts_data2;
=# set default_tablespace = ts_data2; -- 세션에 강제로 설정
=# create table ttt_data2(a int); -- 기존 세션에서(잘 반영)
=# create table ttt_data2_1(a int); -- 새로운 세션에서(잘 반영)
=# create table ttt_data2_2(a int); -- 다시 기존 세션에서(잘 반영)
=# create table ttt_data3(a int) tablespace ts_data3;
=# create table ttt_data4(a int) tablespace ts_data2;
=# create table ttt_data5(a int) tablespace ts_data1;
※ 기존 세션에서는 뭔지 몰라도 기존 default tablespace값을 가지고 있는 듯.
※ 기존 세션에서는 뭔지 몰라도 기존 default tablespace값을 가지고 있는 듯.
=# select * from sp_table0;
/******************************
-- 테이블스페이스 변경
******************************/
-- 소유자 변경
=# alter database tttdb owner to mapbak;
-- 디폴트 테이블스페이스 변경
=# alter database tttdb set default_tablespace = ts_default;
=# select * from pg_settings where name = 'default_tablespace';
-- 다른 테이블스페이스로 이동
-- ERROR: cannot change the tablespace of the currently open database 발생
-- 관리자 모드란게 있어서 나중에 되는지 잘 몰렁..
-- ERROR: cannot change the tablespace of the currently open database 발생
-- 관리자 모드란게 있어서 나중에 되는지 잘 몰렁..
=# alter database tttdb set tablespace ts_data4;
-- 테이블스페이스명 변경
alter tablespace ts_data1 rename to jp_trade;
-- 소유자 변경
alter tablespace jp_trade owner to pgsys; 반응형