注意
只有超级用户或具有特殊的CREATEDB特权才可以创建数据库。
默认情况下,可通过克隆标准系统数据库template1创建新数据库。
可通过写TEMPLATE name指定不同的模板。
可通过写TEMPLATE template0您可以创建一个干净的数据库,只包含Teledb所预定义的标准对象。
以下为创建数据库相关操作。
- 默认参数创建数据库
teledb=# create database teledb_db; CREATE DATABASE
- 指定克隆库
teledb=# create database teledb_db_template TEMPLATE template0; CREATE DATABASE
- 指定所有者
teledb=# create role teledb_user with login; CREATE ROLE teledb=# create database teledb_db_owner owner teledb_user; CREATE DATABASE teledb=# \l+ teledb_db_owner List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description ------------------+--------------+----------+-------------+-------------+-------------------+-------+------------+------------- teledb_db_owner | teledb_user | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | 19 MB | pg_default | (1 row)
- 指定编码
teledb=# create database teledb_db_encoding ENCODING UTF8; CREATE DATABASE teledb=# \l+ teledb_db_encoding List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description ---------------------+---------+----------+-------------+-------------+-------------------+-------+------------+------------- teledb_db_encoding | teledb | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | 19 MB | pg_default | (1 row)
- 创建gbk编码
teledb=# CREATE DATABASE db_gbk template template0 encoding = gbk LC_COLLATE = 'zh_CN.gbk' LC_CTYPE = 'zh_CN.gbk'; CREATE DATABASE teledb=# \d db_gbk Did not find any relation named "db_gbk". teledb=# \l+ db_gbk List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description --------+---------+----------+-----------+-----------+-------------------+-------+------------+------------- db_gbk | teledb | GBK | zh_CN.gbk | zh_CN.gbk | | 19 MB | pg_default | (1 row)
- 指定排序规则
teledb=# create database teledb_db_lc_collate lc_collate 'C' template template0; CREATE DATABASE
- 指定分组规则
teledb=# create database teledb_lc_ctype LC_CTYPE 'C' template template0; CREATE DATABASE
- 配置数据可连接
teledb=# create database teledb__allow_connections ALLOW_CONNECTIONS true; CREATE DATABASE teledb=# select datallowconn from pg_database where datname = 'teledb__allow_connections'; datallowconn -------------- t (1 row) teledb=# \c teledb__allow_connections You are now connected to database "teledb__allow_connections" as user "teledb".
- 配置连接数
teledb=# create database teledb_connlimit CONNECTION LIMIT 100; CREATE DATABASE teledb=# select datconnlimit from pg_database where datname='teledb_connlimit'; datconnlimit -------------- 100 (1 row)
- 配置数据库可以被复制(是否模板数据库)
teledb=# create database teledb_istemplate is_template true; CREATE DATABASE teledb=# select datconnlimit from pg_database where datname='teledb_connlimit'; datconnlimit -------------- 100 (1 row)
- 同时配置多个参数
teledb=# create database teledb_mul owner teledb_user CONNECTION LIMIT 50 template template0 encoding 'utf8' lc_collate 'C'; CREATE DATABASE