본문 바로가기

프로그래밍/Database

[PGSQL|번역] Template Databases

반응형

http://www.postgresql.org/docs/8.4/static/manage-ag-templatedbs.html


21.3. Template Databases


CREATE DATABASE 는 실제론 이미 존재하는 데이터베이스를 복사하는 방식으로 동작한다. 기본(default)으론 template1 이라는 이름의 표준 시스템 데이터베이스를 복사한다. 그래서 이 데이터베이스의 이름이 "template"인 것이다. template1 에 어떤 객체를 추가하면, 그 객체들은 사용자가 만든 데이터베이스에 복사되어 따라들어가게 된다. 이런 방식으로 데이터베이스의 표준 객체 집합 이외의 사이트에 특성화된 수정을 할 수 있도록 해 준다. 예를 들면, 프로시져 언어 PL/pgSQL을 template1에 설치하면, 아무 추가 작용없이 자동적으로 사용자 데이터베이스에도 PL/pgSQL을 사용할 수 있는 것이다.


CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1. Thus that database is the "template" from which new databases are made. If you add objects to template1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural language PL/pgSQL in template1, it will automatically be available in user databases without any extra action being taken when those databases are made.


template0 이라는 두번째 표준 시스템 데이터베이스도 존재한다. 이 데이터베이스는 template1의 초기값과 동일한 데이터를 갖고 있다. 즉, 당신이 사용하는 PostgreSQL 버전에서 정의된 표준 객체들만 들어 있다. CREATE DATABASE를 함에 있어 template0 을 복제하도록 함으로서 (template1이 아닌), template1에 있는 사이트에 특성화된 추가사항이 없는 깨끗한 사용자 데이터베이스를 만들 수 있는 것이다. 이 방법은 특히 pg_dump 덤프를 복구할 때 유용하다. 덤프 스크립트는 덤프된 데이터베이스의 정확한 내용을 복구하기 위해 깨끗한 데이터베이스에 복구해야 하는 것이다. 그렇게 하지 않으면, template1에 이후에 추가된 내용과 충돌이 있을 수 있다.


There is a second standard system database named template0. This database contains the same data as the initial contents of template1, that is, only the standard objects predefined by your version of PostgreSQL. template0 should never be changed after initdb. By instructing CREATE DATABASE to copy template0 instead of template1, you can create a "virgin" user database that contains none of the site-local additions in template1. This is particularly handy when restoring a pg_dump dump: the dump script should be restored in a virgin database to ensure that one recreates the correct contents of the dumped database, without any conflicts with objects that might have been added to template1 later on.


template0 을 template1 대신에 사용하는 또 다른 이유는 template0 을 복사하면서 새로운 인코딩과 로케일 설정을 지정할 수 있기 때문이다. template1 을 복사하여 생성하면 인코딩과 로케일을 바꿀 수 없다. 이것은 template1이 인코딩이나 로케일에 특화된 데이터를 가지고 있을 수 있기 때문이다. template0 은 무관하다.


Another common reason for copying template0 instead of template1 is that new encoding and locale settings can be specified when copying template0, whereas a copy of template1 must use the same settings it does. This is because template1 might contain encoding-specific or locale-specific data, while template0 is known not to.


template0 을 복사하여 데이터베이스를 만들려면 다음과 같이 한다. :


To create a database by copying template0, use:


CREATE DATABASE dbname TEMPLATE template0;


위 명령은 SQL 환경에서 사용한다. 아니면, :

from the SQL environment, or:


createdb -T template0 dbname


위 명령은 셸에서 사용한다.


from the shell.


다른 추가적인 템플릿 데이터베이스를 만들고, CREATE DATABASE 시에 템플릿으로 그 데이터베이스 이름을 지정하여 새로운 데이터베이스를 만들 수도 있다. 하지만, 이것은 일반목적의 "COPY DATABASE" 기능을 위해 만들어진 것이 아님에 주의해야 한다. 가장 큰 제한은 데이터베이스가 복사될 동안 소스 데이터베이스에 세션이 연결될 수 없다는 점이다. 시작할 때 연결이 존재한다면 CREATE DATABASE 는 실패할 것이고, 존재하지 않는다면, CREATE DATABASE 가 끝날 때까지 연결이 락된다.


It is possible to create additional template databases, and indeed one can copy any database in a cluster by specifying its name as the template for CREATE DATABASE. It is important to understand, however, that this is not (yet) intended as a general-purpose "COPY DATABASE" facility. The principal limitation is that no other sessions can be connected to the source database while it is being copied. CREATE DATABASE will fail if any other connection exists when it starts; otherwise, new connections to the source database are locked out until CREATE DATABASE completes.


모든 데이터베이스에 pg_database에는 두 개의 유용한 플래그가 존재한다. : datistemplate 과 datallowconn 컬럼. datistemplate 은 이 데이터베이스가 CREATE DATABASE의 템플릿 용인지를 알려준다. 이 플래그가 세팅되어 있으면, 그 데이터베이스는 CREATEB 권한을 가진 모든 사용자가 복제할 수 있다. 세팅되어 있지 않으면 superuser와 데이터베이스의 소유자만 복제할 수 있다. datallowconn 이 false 라면, 해당 데이터베이스로의 연결을 새롭게 만들 수 없다. (하지만 이미 존재하는 세션을 이 플래그를 false로 두어 끊을 수는 없다.) template0 데이터베이스는 일반적으로 datallowconn = false 로 두어 수정할 수 없도록 한다. template0과 template1 모두 언제나 datistemplate = true 로 세팅되어 있어야 한다.


Two useful flags exist in pg_database for each database: the columns datistemplate and datallowconn. datistemplate can be set to indicate that a database is intended as a template for CREATE DATABASE. If this flag is set, the database can be cloned by any user with CREATEDB privileges; if it is not set, only superusers and the owner of the database can clone it. If datallowconn is false, then no new connections to that database will be allowed (but existing sessions are not killed simply by setting the flag false). The template0 database is normally marked datallowconn = false to prevent modification of it. Both template0 and template1 should always be marked with datistemplate = true.


노트 : template1 과 template0 모두 template1 이 CREATE DATABASE 의 기본(default) 템플릿이라는 점 이외의 특별한 지위를 갖지 않는다. 예를 들면, template1을 삭제하고 template0 에서 다시 만들어 낼 수도 있고, 이 방법은 template1 에 잡다한 것들이 많이 추가되었을 때 추천되기도 한다. (template1 을 지우기 위해서는 pg_database.datistemplate 을 false 로 먼저 세팅해야 한다.)


postgres 데이터베이스도 데이터베이스 클러스터가 초기화 될 때 생성된다. 이 데이터베이스는 사용자와 애플리케이션이 연결하는 기본 database이기 때문에 생성된다. postgres 는 단순한 template1의 복제이며 필요하다면 삭제(drop)하고 다시 생성할 수 있다.


Note: template1 and template0 do not have any special status beyond the fact that the name template1 is the default source database name for CREATE DATABASE. For example, one could drop template1 and recreate it from template0 without any ill effects. This course of action might be advisable if one has carelessly added a bunch of junk in template1. (To delete template1, it must have pg_database.datistemplate = false.)

The postgres database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy of template1 and can be dropped and recreated if required.

728x90