加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgreSQL dropdb 时 还有会话没有关闭

发布时间:2020-12-13 17:21:27 所属栏目:百科 来源:网络整理
导读:如果数据库尚有活动连接,则drop数据库时会失败并有错误提示。 1 2 3 4 postgres=# DROP DATABASE testdb; ERROR: database "testdb" is being accessed by other users DETAIL: There are 3 other sessions using the database . 可以先用下面的语句把testd

如果数据库尚有活动连接,则drop数据库时会失败并有错误提示。

1
2
3
4
postgres=# DROP DATABASE testdb;
ERROR: database "testdb" is being accessed by other users
DETAIL: There are 3 other sessions using the database .

可以先用下面的语句把testdb的活动连接中止,然后再DROP数据库就可以了。

1
2
3
4
5
6
7
8
9
10
postgres=# SELECT pg_terminate_backend(pid)
postgres-# FROM pg_stat_activity
postgres-# WHERE datname= 'testdb' AND pid<>pg_backend_pid();
pg_terminate_backend
----------------------
t
t
t
(3 rows )

pg_stat_activity是一个系统视图,表中的每一行代表一个服务进程的属性和状态。

boolean pg_terminate_backend(pid int)是一个系统函数,用于终止一个后端服务进程。

int pg_backend_pid()系统函数用于获取附加到当前会话的服务器进程的ID

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读