模板数据库

模板数据库就是创建新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.

PostgreSQL 理解 template1 和 template0相关推荐

  1. pgsql之template1 和 template0

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

  2. postgresql 15源码浅析(1)—— postgres中的1号数据库

    摘要 在创建数据库集簇后,该集簇中默认会包含三个系统数据库template1.template0和postgres,其中template0和postgres都是在初始化过程中从template1复制出 ...

  3. PostgreSQL 数据目录结构

    根目录介绍 data ├── base # use to store database file(SELECT oid, datname FROM pg_database;) ├── global # ...

  4. PostgreSQL 编译安装

    PostgreSQL 作为一款优秀的开源关系型数据库产品,一直受到各大企业和IT人士的关注.虽然在中国的流行度远远落后于MySQL,但是相信随着开源社区的宣传,还有其优秀的特性,PostgreSQL在 ...

  5. 安装配置postgreSQL+pgcli+pgadmin3

    记录了postgreSQL数据库的完整的安装配置过程,以及postgreSQL的pgcli命令行智能提醒扩展,pgadmin3图形化管理客户端的配置安装.此postgresql是bigsql版安装详情 ...

  6. Postgresql在Windows下的解压安装

    1.将下载的压缩包解压,我是解压在D:\postgreSQL\pgsql中. 2.设置环境变量如下: set PGHOME=D:\postgreSQL\pgsql     set PGDATA=%PG ...

  7. PostgreSQL 压缩包 在win7上安装

    1:下载postgresql-9.4.5-3-windows-x64-binaries.zip http://get.enterprisedb.com/postgresql/postgresql-9. ...

  8. PostgreSQL数据库、表空间、角色及用户

    一.创建数据库 1.通过pgAdmin创建数据库TestDb1: 打开数据库TestDb1看到建库脚本: 在目录--PostgreSQL(pg_catalog)--数据表--pg_database中可 ...

  9. ubuntu14.04 LTS 源码编译安装postgreSQL

    一.环境 ubuntu14.04 postgresql-9.4.3 二.安装 1.依赖包安装 sudo apt-get install libperl-dev python-dev libreadli ...

最新文章

  1. day3 python 学习随笔
  2. 刀片服务器提升三要点
  3. SecureCRT 端口转发连接服务器
  4. Python爬取Json格式数据并读写(改中文编码)
  5. 前端中全部盒子靠左对齐_前端面试一百问之弹性盒子中 flex: 0 1 auto 表示什么意思...
  6. MySQL子查询嵌套查询
  7. Javascript正则表达式完全学习手册
  8. nrf52832芯片手册_nRF52832低功耗问题不完全总结
  9. 图像处理中的通信原理——冈萨雷斯读书笔记(四)
  10. 邮箱服务器怎么清理缓存,如何清除Outlook的附件缓存?
  11. 前魅族副总裁李楠上手魅族17:颜值提升了
  12. html鼠标互动旋转立方体,css3 transform及原生js实现鼠标拖动3D立方体旋转的示例介绍...
  13. H3C nqa 配置
  14. Tera Term和TTL(Tera Term Language)
  15. rabbit 消息丢失
  16. 人工智能守护青山绿水 内蒙古环保厅引入阿里云ET环境大脑
  17. Mybatis源码学习笔记之Mybatis二级缓存
  18. python socket清空接收缓冲区_Python网络编程——修改套接字发送和接收的缓冲区大小...
  19. 实战:从零开始制作一个跑步微信小程序
  20. 为什么每次用计算机算数都不对,Excel算出来的数,和计算器结果对不上?

热门文章

  1. device or resource busy_北海or涠洲岛旅游攻略(收藏篇)
  2. Keystone controller.py routers.py代码解析
  3. django 基础知识 ~ forms详解
  4. Github工程师为MySQL高可用性采用了新架构
  5. js算法入门(2)--哈希表
  6. 注解 java.lang.annotation.Inherited 介绍
  7. Atitit.软件GUI按钮与仪表盘--db数据库区--导入mysql sql错误的解决之道
  8. 差异分析定位Ring 3保护模块
  9. Prismatic 机器学习不一定得用大框架
  10. First Chapter--Getting Started With Testing