希望复用 template1 中已有的配置、扩展或对象。
创建常规数据库,无需特殊编码或区域设置要求。
5. 关键限制
如果需要自定义字符编码(ENCODING)或区域设置(LC_COLLATE/LC_CTYPE),只能使用 template0。
使用 template1 时,其编码和区域设置是固定的,无法更改。
总结一句话:
template0:干净、灵活,适合创建特殊需求的数据库。
template1:默认、可定制,适合复用现有配置的场景。
示例
template1=# create database xx WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C' LC_CTYPE = 'C';
CREATE DATABASE
template1=# create database xx2 WITH TEMPLATE = template1 ENCODING = 'UTF8' LC_COLLATE = 'C' LC_CTYPE = 'C';
CREATE DATABASE
template1=# \l xx*
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
------+----------+----------+---------+-------+------------+-----------------+-------------------
xx | postgres | UTF8 | C | C | | libc |
xx2 | postgres | UTF8 | C | C | | libc |
(2 rows)
template1=# create database xx3 WITH TEMPLATE = template1 ENCODING = 'SQL_ASCII' LC_COLLATE = 'C' LC_CTYPE = 'C';
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.
template1=#
template1=# create database xx3 WITH TEMPLATE = template1 ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF8' LC_CTYPE = 'C';
ERROR: new collation (en_US.UTF8) is incompatible with the collation of the template database (C)
HINT: Use the same collation as in the template database, or use template0 as template.
template1=#
template1=#