合 PG模板数据库之template1 和 template0
Tags: PG脚本其它template1模板数据库template0
简介
参考官网:http://postgres.cn/docs/13/manage-ag-templatedbs.html
template1和template0是PostgreSQL的模板数据库。所谓模板数据库就是创建新database时,PostgreSQL会基于模板数据库制作一份副本,其中会包含所有的数据库设置和数据文件。PostgreSQL安装好以后会默认附带两个模板数据库:template0和template1。
PostgreSQL默认初始化数据库后,模板数据库template0和template1包含的内容是一致的。
在PG中创建数据库时,默认是从template1模板数据库中克隆出来的!!
1 2 3 4 5 6 7 8 9 10 11 12 | postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- db1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | lhrdb1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres (5 rows) |
我们知道创建数据库时的语法为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | postgres=# \help create database Command: CREATE DATABASE Description: create a new database Syntax: CREATE DATABASE name [ [ WITH ] [ OWNER [=] user_name ] [ TEMPLATE [=] template ] [ ENCODING [=] encoding ] [ LOCALE [=] locale ] [ LC_COLLATE [=] lc_collate ] [ LC_CTYPE [=] lc_ctype ] [ TABLESPACE [=] tablespace_name ] [ ALLOW_CONNECTIONS [=] allowconn ] [ CONNECTION LIMIT [=] connlimit ] [ IS_TEMPLATE [=] istemplate ] ] URL: https://www.postgresql.org/docs/13/sql-createdatabase.html |
如:create database aaaa with template='template0' encoding ='UTF8' lc_collate='C' lc_ctype='en_US.utf8' owner='test123';
其中template表示模板数据库。建库时如果不指定 TEMPLATE 属性,默认用的是 template1 模板库。
template1和template0的区别是什么?
区别主要有两点:
1、template1 可以连接,template0 不可以连接。
在PG中,服务端不支持汉字字符集GBK和GB18030
参考:http://postgres.cn/docs/13/multibyte.html,服务端不支持的字符集包括:BIG5、GB18030、GBK、JOHAB、SJIS、SHIFT_JIS_2004、UHC