PostgreSQL中的template0和template1库使用实战

 更新时间:2021年01月12日 10:04:02   作者:abce  
这篇文章主要介绍了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=#

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

相关文章

  • postgresql~*符号的含义及用法说明

    postgresql~*符号的含义及用法说明

    这篇文章主要介绍了postgresql~*符号的含义及用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • Postgresql 赋予用户权限和撤销权限的实例

    Postgresql 赋予用户权限和撤销权限的实例

    这篇文章主要介绍了Postgresql 赋予用户权限和撤销权限的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • PostgreSQL中json数据类型详解

    PostgreSQL中json数据类型详解

    json数据也可以被存储为text,但是 与text数据类型相比,JSON 数据类型的优势在于能强制要求每个被存储的值符合 JSON 规则,这篇文章主要介绍了PostgreSQL中json数据类型,需要的朋友可以参考下
    2023-04-04
  • PostgreSQL数据库中修改表字段的常用命令小结

    PostgreSQL数据库中修改表字段的常用命令小结

    这篇文章主要给大家介绍了PostgreSQL数据库中修改表字段的常用命令操作,文中有详细的代码示例供大家参考,具有一定的参考价值,需要的朋友可以参考下
    2023-12-12
  • postgresql查询锁表以及解除锁表操作

    postgresql查询锁表以及解除锁表操作

    这篇文章主要介绍了postgresql查询锁表以及解除锁表操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • 自定义函数实现单词排序并运用于PostgreSQL(实现代码)

    自定义函数实现单词排序并运用于PostgreSQL(实现代码)

    这篇文章主要介绍了自定义函数实现单词排序并运用于PostgreSQL,本文给大家分享实现代码,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • 在Ubuntu中安装Postgresql数据库的步骤详解

    在Ubuntu中安装Postgresql数据库的步骤详解

    PostgreSQL 是一款强大的,开源的,对象关系型数据库系统。它支持所有的主流操作系统,包括 Linux、Unix(AIX、BSD、HP-UX,SGI IRIX、Mac OS、Solaris、Tru64) 以及 Windows 操作系统。本文给大家介绍了在Ubuntu中安装Postgresql数据库的步骤,需要的朋友可以参考下。
    2017-09-09
  • 浅谈PostgreSQL中大小写不敏感问题

    浅谈PostgreSQL中大小写不敏感问题

    这篇文章主要介绍了浅谈PostgreSQL中大小写不敏感问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • Postgresql 查询表引用或被引用的外键操作

    Postgresql 查询表引用或被引用的外键操作

    这篇文章主要介绍了Postgresql 查询表引用或被引用的外键操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • postgresql varchar字段regexp_replace正则替换操作

    postgresql varchar字段regexp_replace正则替换操作

    这篇文章主要介绍了postgresql varchar字段regexp_replace正则替换操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01

最新评论