标签
PostgreSQL , create database , collate , ctype , pg_encoding , pg_encoding_to_char()
背景
PostgreSQL实例支持创建多个数据库,创建数据库时,可以指定模板库,并为每个数据库设置不同的字符集、本地化collate, 货币等属性。
接下来举例讲述CREATE DATABASE的具体使用方法。
一、CREATE DATABASE语法
Command: CREATE DATABASE
Description: create a new database
Syntax:
CREATE DATABASE name
[ [ WITH ] [ OWNER [=] user_name ]
[ TEMPLATE [=] template ]
[ ENCODING [=] encoding ]
[ LC_COLLATE [=] lc_collate ]
[ LC_CTYPE [=] lc_ctype ]
[ TABLESPACE [=] tablespace_name ]
[ CONNECTION LIMIT [=] connlimit ] ]
二、克隆模板库(从指定模板创建数据库)
例子
以test数据库为模板,克隆一个名为test01的数据库。
test=> create database test01 with template test;
CREATE DATABASE
如果不指定模板,则默认的模板为template1。
注意
从指定模板库创建数据库,除了当前连接,不能有其他用户连在对应的模板库上面。
如果有其他用户连在test库,可能会报类似这样的错误
test=> create database test01 with template test;
ERROR: source database "test" is being accessed by other users
DETAIL: There is 1 other session using the database.
三、创建数据库时,如何指定encoding
PostgreSQL支持哪些encoding
用户可以参考PostgreSQL的官方文档,有对应的字符集支持列表
https://www.postgresql.org/docs/9.6/static/multibyte.html
Server=Yes表示该字符集支持用于create database。否则只支持作为客户端字符集。
Name | Description | Language | Server? | Bytes/Char | Aliases |
---|---|---|---|---|---|