`
LJ你是唯一LT
  • 浏览: 239306 次
社区版块
存档分类
最新评论

postgresql新建库2个常见报错

阅读更多
今天使用pg建库发现两个报错:

ERROR:  new collation (zh_CN.UTF-8) is incompatible with the collation of the template database (en_US.UTF-8)
ERROR:  source database "template1" is being accessed by other users


建库语句:
CREATE DATABASE tinadb
  WITH OWNER = postgres
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       LC_COLLATE = 'zh_CN.UTF-8'
       LC_CTYPE = 'zh_CN.UTF-8'
       CONNECTION LIMIT = -1
       template template1;
报错信息:
ERROR:  new collation (zh_CN.UTF-8) is incompatible with the collation of the template database (en_US.UTF-8)
HINT:  Use the same collation as in the template database, or use template0 as template.

按照提示,我们应该使用跟模板数据库相同的collation,或者使用模板template0
postgres=# \l
                                   List of databases
     Name     |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges  
--------------+----------+----------+-------------+-------------+-----------------------
postgres     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
rename_check | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =Tc/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
(4 rows)
其实我们可以看到template0和template1的collate是一样的,但template0允许建立不同的collation的库

解决办法:
CREATE DATABASE rename_check
WITH OWNER = postgres
   ENCODING = 'UTF8'
   TABLESPACE = pg_default
   LC_COLLATE = 'zh_CN.UTF-8'
   LC_CTYPE = 'zh_CN.UTF-8'
   CONNECTION LIMIT = -1
   TEMPLATE template0;    --使用template0



另外一个常见的报错信息:
ERROR:  source database "template1" is being accessed by other users
DETAIL:  There are 1 other session(s) using the database.

解决办法:
1.使用pg的另外一个模板库template0
[postgres@kenyon  ~]$ createdb -T template0 tinadb -p 5432
Password:

2.杀掉连接到template1的进程,再执行一次建库语句
postgres=# select procpid from pg_stat_activity where DATNAME = 'template1';
procpid
---------
   8879
postgres=# \q
[postgres@kenyon  ~]$ kill 8879


附带模板库template0 与 template 1以及数据库视图pg_database的说明
1.安装好数据库初始化时,template0与template1都是一样的,是一个干净的库,内容也一样
2.初始化完以后,用户可定制template1,比如新增自定义函数,在创建新库时都会附带该自定义函数而无需在新库里创建
3.一般不允许再对template0进行各种操作,以保证其是个干净的库,对数据库的恢复比较有帮助。
数据库恢复建立新库时可以指定template0为模板,可以创建一个干净的新库
4.创建新库时是不能连接新的session的,而有新的session连在模板库上会导致创建失败
5.视图pg_database的主要字段说明
postgres=# \d pg_database;
    Table "pg_catalog.pg_database"
    Column     |   Type    | Modifiers
---------------+-----------+-----------
datname       | name      | not null
datdba        | oid       | not null
encoding      | integer   | not null
datcollate    | name      | not null
datctype      | name      | not null
datistemplate | boolean   | not null
datallowconn  | boolean   | not null
datconnlimit  | integer   | not null
datlastsysoid | oid       | not null
datfrozenxid  | xid       | not null
dattablespace | oid       | not null
datacl        | aclitem[] |
Indexes:
    "pg_database_datname_index" UNIQUE, btree (datname), tablespace "pg_global"
    "pg_database_oid_index" UNIQUE, btree (oid), tablespace "pg_global"
Tablespace: "pg_global"
datistemplate:可否允许作为模板,如果true,则任何有createdb的用户都可创建,一般用户数据库该值是false
datallowconn 表示可否允许连接,template0一般不允许连接,其他数据库可连接
datconnlimit   表示连接限制,-1表示无限制

postgres=# select * from pg_database;
   datname    | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | dattablespace
|                datacl               
--------------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+---------------
+--------------------------------------
template1    |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | t             | t            |           -1 |         12772 |         1795 |          1663
| {=c/postgres,postgres=CTc/postgres}
template0    |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | t             | f            |           -1 |         12772 |         1795 |          1663
| {=c/postgres,postgres=CTc/postgres}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics