当前位置:数据库 > Access >>

PG:template1 is being accessed by other users

PG:template1 is being accessed by other users
 
开发在给一台开发机上新建一个Postgres数据库的时候报错。 建库语句:
CREATE DATABASE temp_kenyon
  WITH OWNER = postgres
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       LC_COLLATE = 'C'
       LC_CTYPE = 'C'
       CONNECTION LIMIT = -1;
报错信息:
ERROR:  source database "template1" is being accessed by other users
DETAIL:  There are 1 other session(s) using the database.
 
********** 错误 **********
 
ERROR: source database "template1" is being accessed by other users
SQL 状态: 55006
详细:There are 1 other session(s) using the database.
分析: 错误提示比较明显,有一个其他的session正连在template1上,而这个是模板库,PG不允许在新建库时有其他连接连在上面 
 
解决办法: 
1.使用PG的另外一个模板库template0
[postgres@kenyon  ~]$ createdb -T template0 tets_kenyon -p 5432
Password: 
[postgres@kenyon  ~]$ psql -p 5432
Password: 
psql (9.1.2)
Type "help" for help.
 
postgres=# \l
                              List of databases
    Name     |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-------------+----------+----------+---------+-------+-----------------------
 postgres    | postgres | UTF8     | C       | C     | 
 template0   | postgres | UTF8     | C       | C     | =c/postgres          +
             |          |          |         |       | postgres=CTc/postgres
 template1   | postgres | UTF8     | C       | C     | =c/postgres          +
             |          |          |         |       | postgres=CTc/postgres
 testof      | postgres | UTF8     | C       | C     | 
 tets_kenyon | postgres | UTF8     | C       | C     | 
(6 rows)
 
postgres=# drop database test_kenyon;
ERROR:  database "test_kenyon" does not exist
postgres=# drop database tets_kenyon;
DROP DATABASE
2.杀掉连接到template1的进程,再执行一次建库语句
postgres=# select procpid from pg_stat_activity where DATNAME = 'template1';
 procpid 
---------
   8879
postgres=# \q
[postgres@kenyon  ~]$ kill 8879
 
CREATE DATABASE blacktea
  WITH OWNER = postgres
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       LC_COLLATE = 'C'
       LC_CTYPE = 'C'
       CONNECTION LIMIT = -1;
It works. 
 
附带模板库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表示无限制
 
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,