前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PostgreSQL 理解 template1 和 template0

PostgreSQL 理解 template1 和 template0

原创
作者头像
IT工作者
发布2022-07-29 16:57:11
1K0
发布2022-07-29 16:57:11
举报
文章被收录于专栏:程序技术知识程序技术知识

模板数据库

            模板数据库就是创建新database时,PostgreSQL会基于模板数据库制作一份副本,其中会包含所有的数据库设置和数据文件。

           PostgreSQL安装好以后会默认附带两个模板数据库:template0和template1。

            了解 PostgreSQL 的人肯定听过 template1 和 template0,这两个作为模板库,

在建库的时候会用到,但这两者是有很大差别的,曾经一段时间对这两个模板库的使

用比较模糊,今天再次查看了文档,决定通过实验记录下来。

 

一 关于默认模板库

–1.1 默认模板库为 template1

 postgres=# create database db1;

CREATE DATABASE

备注:建库时如果不指定 TEMPLATE 属性,默认用的是 template1 模板库.

 

–1.2 手工指定模板库

 postgres=# create database db2 template template0;

CREATE DATABASE

备注:也可以指定模板库为 template0

 

 

二 template1 和 template0 的区别?

 

         数据库初始化之后, 就有了 template0, template1 库,开始时这两个库的内容是一样的,

但这两个库有啥异同呢?

 

         任何时候都不要对template0模板数据库进行任何修改,因为这是原始的干净模板,如果其它模板数据库被搞坏了,基于这个数据库做一个副本就可以了。如果希望定制自己的模板数据库,那么请基于template1进行修改,或者自己另外创建一个模板数据库再修改。对基于template1或你自建的模板数据库创建的数据库来说,你不能修改其字符集编码和排序规则。template0可以。

–2.1 template1 可以连接并创建对象,template0 不可以连接

 postgres=# \c template1

You are now connected to database “template1” as user “postgres”.

 

template1=# create table tmp_1( id int4);

CREATE TABLE

 

template1=# \c template0

FATAL:  database “template0” is not currently accepting connections

Previous connection kept

备注:当然可以通过其它方法连接 template0 库,有兴趣的同学自己研究下,这里不演示了。

            正因为 template1 可以创建对像,相比 template0 ,被称为非干净数据库,而 template0

            被称为干净的数据库。

 

–2.2 使用 template1 模板库建库时不可指定新的 encoding 和 locale,而 template0 可以

 template1=# create database db3 TEMPLATE template0 ENCODING ‘SQL_ASCII’ ;

CREATE DATABASE

 

template1=# create database db4 TEMPLATE template1 ENCODING ‘SQL_ASCII’ ;

ERROR:  new encoding (SQL_ASCII) is incompatible with the encoding of the template database (UTF8)

HINT:  Use the same encoding as in the template database, or use template0 as template.

   

 

–3 template0 库和 template1 都不可删除

 postgres=# drop database template0;

ERROR:  cannot drop a template database

 

postgres=# drop database template1;

ERROR:  cannot drop a template database

备注:当然有方法删除 template1 库,而且这个操作并不危险,需要修改系统表,这里不演示了。

 

 

三 关于复制数据库

        之前简单介绍了 template0 和 template1 的异同,有必要介绍通过模板库复制库的操作,例如

这里已经有个 francs 库了,现在想复制一个 francs1 库,内容和 francs 库一样。

 

–3.1 复制库

 postgres=# \c francs 

You are now connected to database “francs” as user “postgres”.

 

francs=# select count(*) from pg_stat_user_tables ;

 count 

——-

    41

(1 row)

 

postgres=# create database francs1 TEMPLATE francs ;

CREATE DATABASE

 

postgres=# \c francs1 francs

You are now connected to database “francs1” as user “francs”.

francs1=> select count(*) from pg_stat_user_tables ;

 count 

——-

    41

(1 row)

备注:这种方法在复制数据库时提供了方便, 也可以定制自己的数据库模板, 但是这么操作有个前提,

           复制时源库不可以连接,   复制过程中也不允许连接源库, 否则会报以下错误:

 

–3.2 错误代码

ERROR:  source database “francs” is being accessed by other users

DETAIL:  There is 1 other session using the database.

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档