PostgreSQL中的template0和template1库
2020-11-11 09:48 abce 阅读(1917) 评论(4) 编辑 收藏 举报postgresql中默认会有三个数据库:postgres、template0、template1。
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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库之后,可以借助模板库template1再创建postgres库:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | $ 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库会怎样呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $ 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=# |
修改以后,再创建新库的时候,新库也会包含上面的表和扩展:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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的用途是什么呢?
1 2 3 4 5 6 7 8 9 10 | 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=# |
如果你想创建自己的模板库,只需将你选中库对应的datistemplate(pg_database中的列)设置为T即可。
当然,在创建新库的时候,还可以选择其他的库做为源库:
1 2 3 | db_test=# create database db_test_2 template db_test; CREATE DATABASE db_test=# |
但是,要求不能有其他连接连接到模板库,否则会报错:
1 2 3 4 | 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=# |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)