PostgreSQL中的template0和template1库使用实战

postgresql中默认会有三个数据库:postgres、template0、template1。

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库之后,可以借助模板库template1再创建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语句创建新库的时候,就是创建模板库template1的一个拷贝。

那如果我修改了template1库会怎样呢?

$ 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=# 

无论,在template1中加入了什么,都会在之后新建的库中。

那template0的用途是什么呢?

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=#

从这里可以看到,只有template0库对应的datallowconn字段的值是F。这就是上面重建postgres的时候先登录template1而不是template0的原因。

template0是默认的不可修改的数据库。不建议用户对template0做任何修改。在初始化后的空实例中,template0和template1是完全相同的。

为什么需要两个模板库呢?假设你搞乱了template1,还可以通过template0恢复template1。

如果你想创建自己的模板库,只需将你选中库对应的datistemplate(pg_database中的列)设置为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模板数据库template1

$ 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=#

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。如有错误或未考虑完全的地方,望不吝赐教。

(0)

相关推荐

  • 解决postgresql无法远程访问的情况

    今天刚入手这个数据库玩玩,发现无法通过IP去访问数据库,后面查询原因为,该数据库默认只能通过本地连接,也就是回环地址(127.0.0.1) 解决方案: 1.修改安装目录下的data\pg_hba.conf,在配置文件最后有IPV4和IPV6的配置,新增一行(这里我用的IPV4,开放所有IP) host all all 0.0.0.0/0 md5 说明: 该配置为允许所有IP访问,下面有对应的一些配置示例提供参考 32 -> 192.168.1.1/32 表示必须是来自这个IP地址的访问才合法:

  • postgresql 实现启动、状态查看、关闭

    利用psql启动数据库 [postgres@highgo ~]$ pg_ctl start 查看系统中运行的postgres进程 #ps -ef | grep postgres 连接postgresql数据库 #psql -h 127.0.0.1 -d postgres -U postgres 停止postgresql数据库实例 #pg_ctl stop #ps -ef | grep postgres 启动服务器最简单的方法是像下面这样: $ postgres -D /usr/local/pgs

  • 浅谈Postgresql默认端口5432你所不知道的一点

    关于Postgresql端口5432的定义: 5432端口,已经在IANA(The Internet Assigned Numbers Authority,互联网数字分配机构)注册, 并把该端口唯一分配给Postgres. 这意味着,一台安装了linux OS的服务器,哪怕没有安装过postgresql数据库,也会有这个预留端口. 查看这个预留端口的方法如下: new@newdb-> cat /etc/services |grep 5432 postgres 5432/tcp postgresq

  • postgresql修改完端口后直接psql连接数据库报错的解决

    今天修改pg的端口号port改成5435后重启完数据库的时候直接psql进库的时候进不去 [postgres@node2 data]$ psql psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432 这时,进数据库有两种方式 1.psq

  • PostgreSQL pg_archivecleanup与清理archivelog的操作

    pg_archivecleanup 和 pg_rewind 是PG 中两个重要的功能,一个是为了清理过期的 archive log 使用的命令,另一个是你可以理解为物理级别的 wal log的搬运工. 我们先说第一个 pg_archivecleanup 命令,这个命令主要是用于使用了archive log 功能的 postgresql 但在 archive log 堆积如山的情况下,你怎么来根据某些规则,清理这些日志呢? 这里面就要使用 pg_archivecleanup 这个命令了,可以定时的

  • postgresql连续归档及时间点恢复的操作

    简介 前面我们介绍了通过pgsql的流复制在生产环境中搭建高可用环境来保证服务的可持续性:我们也要对数据库进行周期备份,来防止数据的丢失,这就需要连续归档,它不仅可以用于大型数据库的增量备份和恢复,也可以用于搭建standby镜像备份.    PostgreSQL默认处于非归档模式.开启归档模式,主要涉及到三个参数:wal_level,archive_mode和archive_commandwal_level参数默认为mininal,设置此参数为archive或者之上的级别都可以打开归档.当po

  • PostgreSQL中的template0和template1库使用实战

    postgresql中默认会有三个数据库:postgres.template0.template1. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres

  • PostgreSQL中使用dblink实现跨库查询的方法

    最近一个sql语句涉及到跨库的表之间的联合查询,故研究学习下. 一开始研究知道了sql语句的写法,但是执行通过不了,需要先安装dblink扩展.这些博文里都没说清楚,感谢网友指点,在windows下只需要在相应的数据库下执行sql语句"create extension dblink"就ok了.而以上的博文基本上说的都是linux下的操作方法,也因此我走了弯路. 2.下载了PostgreSQL的源代码,却不知道语句在哪执行,貌似是在linux下的shell里执行的,我却在psql和cmd

  • 在postgreSQL中运行sql脚本和pg_restore命令方式

    今天踩坑了,把powerdesign生成的sql脚本文件,用pg_restore命令一直运行... 过程惨不忍睹,一直以为是编码问题,修改了serve和client的encoding,结果... 记录一下这个错误: postgreSQL运行sql脚本文件: psql -d dbname -U username -f (脚本所在位置).sql postgerSQL的pg_restore命令 用法: pg_restore [选项]- [文件名] 一般选项: -d, --dbname=名字 连接数据库

  • 在postgresql中通过命令行执行sql文件

    通过命令行执行初始化sql脚本是比较常见的需求,命令行下执行如下操作即可: 若是执行的命名只是创建用户,编辑用户,创建数据库的话可以不指定-d参数. psql -U username -d myDataBase -a -f init.sql 如果是远程数据库加入-h参数指定主机地址即可 psql -h host -U username -d myDataBase -a -f init.sql 补充:PostgreSQL操作-psql基本命令 一.建立数据库连接 接入PostgreSQL数据库:

  • 浅谈PostgreSQL中的孤儿文件用法(orphaned data files)

    创建一个测试表 postgres=# create table t1(a int); CREATE TABLE postgres=# select pg_relation_filepath('t1'); pg_relation_filepath ---------------------- base/75062/75297 (1 row) postgres=# 在操作系统上已经可以看到该文件. $ ls -la $PGDATA/base/75062/75297 -rw------- 1 post

  • PostgreSQL中json数据类型详解

    目录 前言 一.PG数据库中JSON的类型 1.json和jsonb的区别 2.项目开发中的选择 3.json数据类型 二.PG中json的简单操作 1.基础json数据操作 2.json和jsonb输出对比 3.jsonb包含测试 总结 前言 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.它基于 ECMAScript(European Computer Manufacturers Association, 欧洲计算机协会制定的js规范)的一个子集,

  • PostgreSQL中常用的时间日期脚本使用教程

    获取系统时间函数 select now(); --2013-11-28 16:20:25.259715+08 select current_timestamp; --2013-11-28 16:20:38.815466+08 select current_date; --2013-11-28 select current_time; --16:21:08.981171+08 时间的计算 --使用interval select now()+interval '2 day'; --2013-11-3

  • PHP 读取Postgresql中的数组

    复制代码 代码如下: function getarray_postgresql($arraystr) {     $regx1 = '/^{(.*)}$/';     $regx2 = "/\"((\\\\\\\\|\\\\\"|[^\"])+)\"|[^,]+/";     $regx3 = '/^[^"].*$|^"(.*)"$/';     $match = null;     preg_match( $reg

  • Lua中的一些常用函数库实例讲解

    前言 这篇文章将会来一些比较轻松的内容,就是简单的介绍一下Lua中几个常用的库.简单的说就是几个API的介绍.所以说,看起来比较容易,也没有多大的分量.就是纯粹的总结.使用库就是为了方便我们的开发,提高开发效率,同时也能保证代码的质量.希望大家以后也不要重复造轮子了. 数学库 数学库(math)由一组标准的数学函数构成.这里主要介绍几个常用的函数,其它的大家可以自行百度解决. 三角函数(sin,cos,tan--) 所有的三角函数都使用弧度单位,可以用函数deg(角度)和rad(弧度)来转换角度

  • 在PostgreSQL中使用数组时值得注意的一些地方

    在Heap中,我们依靠PostgreSQL支撑大多数后端繁重的任务,我们存储每个事件为一个hstore blob,我们为每个跟踪的用户维护一个已完成事件的PostgreSQL数组,并将这些事件按时间排序. Hstore能够让我们以灵活的方式附加属性到事件中,而且事件数组赋予了我们强大的性能,特别是对于漏斗查询,在这些查询中我们计算不同转化渠道步骤间的输出. 在这篇文章中,我们看看那些意外接受大量输入的PostgreSQL函数,然后以高效,惯用的方式重写它. 你的第一反应可能是将PostgreSQ

随机推荐