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

PostgreSQL关闭指定数据库

发布时间:2020-12-13 17:21:24 所属栏目:百科 来源:网络整理
导读:在pgsql中使用pg_terminate_backend()函数来关闭数据库. 官方文档中: pg_terminate_backend( pid int ) , Terminate a backend. You can execute this against another backend that has exactly the same role as the user calling the function. In all ot

在pgsql中使用pg_terminate_backend()函数来关闭数据库.

官方文档中:

pg_terminate_backend(pidint),Terminate a backend. You can execute this against another backend that has exactly the same role as the user calling the function. In all other cases,you must be a superuser.return type:boolean

pg_terminate_backend()函数需要超级用户才能够执行,此外它还需要接收一个pid的参数,这个参数来自pg_stat_activity表

方法:

先找到指定数据库的pid:

SELECT datname,pid FROM pg_stat_activity;

关闭数据库连接:

SELECT pg_terminate_backend(11480);

其实可以一步完成:

SELECT CAST(pg_terminate_backend(pid) AS VARCHAR(10)) FROM pg_stat_activity WHERE datname='db3';

关闭指定用户:

SELECT CAST(pg_terminate_backend(pid) AS VARCHAR(10)) FROM pg_stat_activity WHERE usename='sec03';

使用pg_terminate_backend()关闭连接但是它会等待当前所有的查询结束才会断开,如果想立即断开所有连接我们可以使用pg_cancel_backend()断开连接,并且不再允许连接:

SELECT CAST(pg_cancel_backend(pid) AS VARCHAR(10)) FROM pg_stat_activity WHERE datname='db3';

(编辑:李大同)

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

    推荐文章
      热点阅读