ERROR: source database "template1" is being accessed by other users解决方法

postgresql创建新库时报错,显示其他用户正在访问模板库“template1”,详细信息如下:

    postgres=# create database cctest;
    ERROR:  source database "template1" is being accessed by other users 
    DETAIL:  There are 1 other session(s) using the database. 

解决方法:

select * from pg_stat_activity where DATNAME = 'template1'; 
postgres=# select * from pg_stat_activity where DATNAME = 'template1'; 
 datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | state_change | waiting | state | backend_xid | backend_xmin | query 
-------+---------+-----+----------+---------+------------------+-------------+-----------------+-------------+---------------+------------+-------------+--------------+---------+-------+-------------+--------------+-------
(0 rows)

将查出的pid kill掉
SELECT pg_terminate_backend(PID);
之后创建新库即可
也可以写成一条语句

select pg_terminate_backend(pid) from pg_stat_activity where DATNAME = 'template1';

或者使用其他模板库创建新库

你可能感兴趣的:(postgres,postgres)