ERROR: source database "template1" is being accessed by other users


一開始,開發童鞋說他在測試環境沒有創建數據庫的權限。心想,不對呀,開發環境沒有怎么做權限管控,明明給予授權了。
上來一看:

postgres=# CREATE DATABASE "abce" WITH OWNER = "a_admin"
postgres-# ;
ERROR:  source database "template1" is being accessed by other users
DETAIL:  There is 1 other session using the database.

  

原來不是權限的問題!

查看一下,誰在使用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          | wait_event_type | wait_event | state | backend_xid | backend_xmin |                                                             
                                    query                                                                                                 |  backend_type  
-------+-----------+-------+----------+-----------+------------------+--------------+-----------------+-------------+------------------------------+------------+----------------
---------------+-------------------------------+-----------------+------------+-------+-------------+--------------+-------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------+----------------
     1 | template1 | 23498 |    16384 | a_admin | NAVICAT          | xx.xx.xx.xxx |                 |       61664 | 2019-10-08 16:15:06.46307+08 |            | 2019-10-08 16:1
5:16.542588+08 | 2019-10-08 16:15:16.545124+08 | Client          | ClientRead | idle  |             |              | SELECT rolname, rolsuper, rolinherit, rolcreaterole, rolcrea
tedb, rolcanlogin, rolconnlimit, rolvaliduntil, rolconfig, oid , pg_catalog.shobj_description(oid, 'pg_authid') AS comment FROM pg_roles  | client backend
(1 row)

將查出的pid kill掉

postgres=# SELECT pg_terminate_backend( 23498);
 pg_terminate_backend 
----------------------
 t
(1 row)

  

也可以使用一條語句,直接將使用template1的會話kill掉:

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

  

然后再執行數據庫創建語句即可!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM