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正在被其他用户访问。

    1. 终止后台会话:为了确保能够安全删除数据库test,我决定终止该会话。
    2. SELECT pg_terminate_backend(9319);

      执行该命令后,会话PID 9319被成功终止。

      1. 重新尝试删除数据库:在终止后台会话后,我再次尝试执行删除命令。
      2. drop database test;

        这次操作成功完成,没有再次出现阻止删除数据库的错误。

        最终,数据库test被成功删除。整个过程的关键在于识别并终止阻止数据库删除的后台会话。通过使用pg_terminate_backend函数,可以有效地终止正在使用数据库的会话,从而确保数据库可以被删除。

        本文主要介绍了如何处理数据库被其他会话占用的情况,以及通过终止后台会话来解决阻止删除数据库的错误。

    上一篇:查询某表格上次进行vacuum的时间
    下一篇:流复制查询备库与主库差异时间

    发表评论

    最新留言

    很好
    [***.229.124.182]2025年03月28日 01时55分41秒