代码改变世界

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

  abce  阅读(2812)  评论(0编辑  收藏  举报

一开始,开发童鞋说他在测试环境没有创建数据库的权限。心想,不对呀,开发环境没有怎么做权限管控,明明给予授权了。
上来一看:

1
2
3
4
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:

1
2
3
4
5
6
7
8
9
10
11
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掉

1
2
3
4
5
postgres=# SELECT pg_terminate_backend( 23498);
 pg_terminate_backend
----------------------
 t
(1 row)

  

也可以使用一条语句,直接将使用template1的会话kill掉:

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

  

然后再执行数据库创建语句即可!

 

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2016-10-08 NULL对反连接的影响
2015-10-08 RHCE7 管理II-5管理进程的优先级
点击右上角即可分享
微信分享提示