PostgreSQL 의 template 0 과 template 1 라 이브 러 리 사용 실전

postgresql 에는 기본적으로 세 개의 데이터베이스 가 있 습 니 다:postgres,template 0,template 1.

postgres=# \l
         List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+----------+----------+-------------+-------------+-----------------------
 postgres | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =T/postgres   +
   |   |   |    |    | postgres=CTc/postgres
 template0 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   +
   |   |   |    |    | postgres=CTc/postgres
 template1 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   +
   |   |   |    |    | postgres=CTc/postgres
(3 rows)
 
postgres=#
클 라 이언 트 는 기본적으로 postgres 라 이브 러 리 에 연 결 됩 니 다.이 라 이브 러 리 를 삭제 할 수 있 지만 기본 클 라 이언 트 연결 에 영향 을 줄 수 있 습 니 다.
postgres 라 이브 러 리 를 삭제 한 후 템 플 릿 라 이브 러 리 template 1 을 통 해 postgres 라 이브 러 리 를 만 들 수 있 습 니 다.

$ psql template1
psql (11.9)
Type "help" for help.
 
template1=# drop database postgres;
DROP DATABASE
template1=# \l
         List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+----------+----------+-------------+-------------+-----------------------
 template0 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   +
   |   |   |    |    | postgres=CTc/postgres
 template1 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   +
   |   |   |    |    | postgres=CTc/postgres
(2 rows)
 
template1=# create database postgres;
CREATE DATABASE
template1=# \l
         List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+----------+----------+-------------+-------------+-----------------------
 postgres | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   +
   |   |   |    |    | postgres=CTc/postgres
 template1 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   +
   |   |   |    |    | postgres=CTc/postgres
(3 rows)
 
template1=#
사실 create database db 를 사용 하고 있 습 니 다.name 문 구 는 새 라 이브 러 리 를 만 들 때 템 플 릿 라 이브 러 리 template 1 의 복사 본 을 만 듭 니 다.
그럼 제 가 template 1 라 이브 러 리 를 수정 하면 어떻게 되 나 요?

$ psql template1
psql (11.9)
Type "help" for help.
 
template1=# create table my_test_tab(a int);
CREATE TABLE
template1=# create extension hstore;
CREATE EXTENSION
template1=# \dx
       List of installed extensions
 Name | Version | Schema |     Description     
---------+---------+------------+--------------------------------------------------
 hstore | 1.5  | public  | data type for storing sets of (key, value) pairs
 plpgsql | 1.0  | pg_catalog | PL/pgSQL procedural language
(2 rows)
 
template1=#
수정 후 새 라 이브 러 리 를 만 들 때 새 라 이브 러 리 는 위의 표 와 확장 을 포함 합 니 다.

template1=# create database db_test;
CREATE DATABASE
template1=# \c db_test
You are now connected to database "db_test" as user "postgres".
db_test=# \dx
       List of installed extensions
 Name | Version | Schema |     Description     
---------+---------+------------+--------------------------------------------------
 hstore | 1.5  | public  | data type for storing sets of (key, value) pairs
 plpgsql | 1.0  | pg_catalog | PL/pgSQL procedural language
(2 rows)
 
db_test=# \d
   List of relations
 Schema | Name  | Type | Owner 
--------+-------------+-------+----------
 public | my_test_tab | table | postgres
(1 row)
 
db_test=# 
template 1 에 무엇 을 넣 었 든 나중에 새로 만 든 라 이브 러 리 에 있 습 니 다.
그럼 template 0 의 용 도 는 무엇 입 니까?

db_test=# select datname,datallowconn,datistemplate from pg_database order by 3;
 datname | datallowconn | datistemplate
-----------+--------------+---------------
 postgres | t   | f
 db_test | t   | f
 template1 | t   | t
 template0 | f   | t
(4 rows)
 
db_test=#
여기에서 볼 수 있 듯 이 template 0 라 이브 러 리 에 대응 하 는 datallowconn 필드 의 값 은 F 입 니 다.이것 이 바로 위 에서 potgres 를 재건 할 때 template 0 이 아 닌 template 1 에 먼저 로그 인 하 는 이유 입 니 다.
template 0 은 기본적으로 수정 할 수 없 는 데이터베이스 입 니 다.사용자 가 template 0 에 대해 어떠한 수정 도 권장 하지 않 습 니 다.초기 화 된 빈 인 스 턴 스 에서 template 0 과 template 1 은 완전히 같 습 니 다.
왜 두 개의 템 플 릿 라 이브 러 리 가 필요 합 니까?템 플 릿 1 을 어 지 럽 혔 다 고 가정 하면 템 플 릿 0 을 통 해 템 플 릿 1 을 복원 할 수 있 습 니 다.
템 플 릿 라 이브 러 리 를 만 들 려 면 라 이브 러 리 에 대응 하 는 datistemplate(pgdatabase 의 열)을 T 로 설정 하면 됩 니 다.
물론 새 라 이브 러 리 를 만 들 때 다른 라 이브 러 리 를 원본 라 이브 러 리 로 선택 할 수 있 습 니 다.

db_test=# create database db_test_2 template db_test;
CREATE DATABASE
db_test=#
단,템 플 릿 라 이브 러 리 에 다른 연결 이 연결 되 어 서 는 안 됩 니 다.그렇지 않 으 면 오류 가 발생 할 수 있 습 니 다.

db_test=# create database db_test_2 template db_test;
ERROR: source database "db_test" is being accessed by other users
DETAIL: There is 1 other session using the database.
db_test=#
추가:postgresql 템 플 릿 데이터베이스 template 1 재 구축

$ psql -U postgres postgres
postgres=# update pg_database set datistemplate = false where datname='template1';
UPDATE 1
postgres=# drop database template1;
DROP DATABASE
postgres=# create database template1 template=template0;
CREATE DATABASE
postgres=# update pg_database set datistemplate = true where datname='template1';
UPDATE 1
postgres=#
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.만약 잘못 이 있 거나 완전히 고려 하지 않 은 부분 이 있다 면 아낌없이 가르침 을 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기