
drop database 报错
查询当前活跃会话:使用以下命令查看正在使用数据库
发布日期:2021-05-08 12:26:56
浏览次数:18
分类:精选文章
本文共 2854 字,大约阅读时间需要 9 分钟。
在进行数据库操作时,偶尔会遇到阻止删除数据库的错误。最近在操作数据库test
时,遇到以下问题:
作者:瀚高PG实验室 (Highgo PG Lab)- 天蝎座
highgo=# drop database test; 错误: 55006: 其他用户正在使用数据库 "test"DETAIL: 那里有1个其它会话正在使用数据库。[highgo@localhost ~]$ psqlpsql (4.0.0)Type "help" for help.highgo=# \lList of databases-------------------+-------------+----------+------------+------------+---------------------------Name | Owner | Encoding | Collate | Ctype | Access privileges-------------------+-------------+----------+------------+------------+---------------------highgo | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 | test_atest_a | test | UTF8 | zh_CN.utf8 | zh_CN.utf8 | test=CTc/testtest | test | UTF8 | zh_CN.utf8 | zh_CN.utf8 |template0 | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/highgotemplate1 | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/highgo(5 rows)highgo=# drop database test; 错误: 55006: 其他用户正在使用数据库 "test"DETAIL: 那里有1个其它会话正在使用数据库。highgo=# select * from pg_stat_activity where datname = 'test';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 26617 | test | 9319 | 16384 | partysimple | NAVICAT | 172.16.12.10 | | 29821 | 2018-05-03 11:47:07.757187+08 | | 2018-05-03 11:47:41.441561+08 | 2018-05-03 11:47:41.442914+08 | f | idle | | highgo=# SELECT pg_terminate_backend(9319);pg_terminate_backend--------------------------------------t (1 row)highgo=# select * from pg_stat_activity where datname = 'test';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(0 rows)highgo=# drop database test;DROP DATABASE
在尝试删除数据库test
时,遇到以下错误信息:
错误: 55006: 其他用户正在使用数据库 "test"DETAIL: 那里有1个其它会话正在使用数据库。
为了解决此问题,我执行了以下步骤:
test
的会话。select * from pg_stat_activity where datname = 'test';
结果显示,有一个会话(PID 9319)正在使用数据库test
。此时,数据库test
正在被其他用户访问。
- 终止后台会话:为了确保能够安全删除数据库
test
,我决定终止该会话。 - 重新尝试删除数据库:在终止后台会话后,我再次尝试执行删除命令。
SELECT pg_terminate_backend(9319);
执行该命令后,会话PID 9319被成功终止。
drop database test;
这次操作成功完成,没有再次出现阻止删除数据库的错误。
最终,数据库test
被成功删除。整个过程的关键在于识别并终止阻止数据库删除的后台会话。通过使用pg_terminate_backend
函数,可以有效地终止正在使用数据库的会话,从而确保数据库可以被删除。
本文主要介绍了如何处理数据库被其他会话占用的情况,以及通过终止后台会话来解决阻止删除数据库的错误。
发表评论
最新留言
很好
[***.229.124.182]2025年03月28日 01时55分41秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
上周热点回顾(2.19-2.25)
2019-03-06
云计算之路-阿里云上:博客web服务器轮番CPU 100%
2019-03-06
云计算之路-阿里云上:服务器CPU 100%问题是memcached连接数限制引起的
2019-03-06
上周热点回顾(3.26-4.1)
2019-03-06
故障公告:IIS应用程序池停止工作造成博客站点无法访问
2019-03-06
【故障公告】极验验证码故障造成无法登录与注册
2019-03-06
上周热点回顾(6.25-7.1)
2019-03-06
【故障公告】10:30-10:45 左右 docker swarm 集群节点问题引发故障
2019-03-06
工作半年的思考
2019-03-06
不可思议的纯 CSS 滚动进度条效果
2019-03-06
【CSS进阶】伪元素的妙用--单标签之美
2019-03-06
开始CN的生活
2019-03-06
惊闻NBC在奥运后放弃使用Silverlight
2019-03-06
IE下尚未实现错误的原因
2019-03-06
Kubernetes 学习系列文章
2019-03-06
创建自己的Docker基础镜像
2019-03-06
使用Jenkins来实现内部的持续集成流程(上)
2019-03-06
HTTP 协议图解
2019-03-06
Python 简明教程 --- 20,Python 类中的属性与方法
2019-03-06
Python 简明教程 --- 21,Python 继承与多态
2019-03-06