PostgreSQL template0 / template1 삭제 시 재생성 방법

PostgreSQL에서 template1은 다른 데이터베이스를 생성할 때 복사 템플릿 역할을 하기 때문에 해당 데이터베이스를 삭제하면 새로운 데이터베이스를 생성하는데 문제가 생긴다.

postgres=# create database test;
ERROR:  template database "template1" does not exist
template error

이런 식으로 template1이 존재하지 않는다는 에러가 발생하게 된다.

그나저나 template1과 template0은 애초에 지우지 못하게 설정되어 있는데 이걸 지웠다는 건 설정 값을 굳이 수정하고 기어코 지웠다는거라 그런 집념의 동기가 궁금하긴 하다. 안 지워지면 다 이유가 있지 않겠나.

아무튼 template 데이터베이스를 지웠다면 postgres 데이터베이스를 템플릿 삼아 만들어주자.

postgres=# create database template0 TEMPLATE postgres;
CREATE DATABASE

그리고 이어 template1은 template0을 템플릿 삼아 만들어준다.

postgres=# CREATE DATABASE template1 TEMPLATE template0;
CREATE DATABASE

사실 template이라는 게 그냥 말그대로 템플릿인거라 initdb 시점에서 postgres, template0, template1의 차이는 크게 없다.

단지 운영 과정에서 template0은 수정되지 않고 완전히 초기 상태 그대로의 템플릿 역할을 계속하고 template1은 데이터베이스 생성시 사용되는 디폴트 템플릿 역할을 하면서 상속 기능을 활용하는 목적인 것이다.

마지막으로 template0과 template1을 삭제할 수 없도록 업데이트를 해준다.

postgres=# update pg_database set datistemplate=true  where datname='template0';
postgres=# update pg_database set datistemplate=true  where datname='template1';

그리고 같은 실수를 반복하지 않는다.

전체 과정

관련 글

댓글 남기기