How to drop a PostgreSQL database if there are active connections to it?
1、PostgreSQL 9.1 and below:
SELECT pg_terminate_backend(pg_stat_activity.procpid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB'
AND procpid <> pg_backend_pid();
PostgreSQL 9.2 and above:
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB'
AND pid <> pg_backend_pid();
Once you disconnect everyone you will have to disconnect and issue the DROP DATABASE command from a connection from another database aka not the one your trying to drop.
Note the renaming of the procpid column to pid. See this mailing list thread.
pg_backend_pid:Process ID of the server process attached to the current session
2、
#!/usr/bin/env bash
# kill all connections to the postgres server
if [ -n "$1" ] ; then
where="where pg_stat_activity.datname = '$1'"
echo "killing all connections to database '$1'"
else
echo "killing all connections to database"
fi cat <<-EOF | psql -U postgres -d postgres
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
${where}
EOF
参考:
http://www.postgresql.org/docs/9.3/static/functions-info.html
How to drop a PostgreSQL database if there are active connections to it?的更多相关文章
- Avoiding PostgreSQL database corruption
TL;DR: Don't ever set fsync=off, don't kill -9 the postmaster then deletepostmaster.pid, don't run P ...
- How do I drop a MongoDB database, from the command line?
mongo <dbname> --eval "db.dropDatabase()" > use mydb; > db.dropDatabase(); mon ...
- How to change owner of PostgreSql database?
ALTER DATABASE name OWNER TO new_owner;
- 安装PostgreSQL数据库 ,Database Cluster 失败!
在安装PG数据库的过程中,会选择安装目录以及数据存放目录和端口,并需要选择Local,如果全部使用默认,并且设置好自己的密码后开始安装,前期进展还比较顺利,到了安装Database Cluster时, ...
- 关于alter database datafile offline和alter database datafile offline drop 的区别
转: https://blog.csdn.net/killvoon/article/details/46913183 -----------------------2015-07-16-------- ...
- Drop a database in MongoDB
http://www.linuxask.com/questions/drop-a-database-in-mongodb Drop a database in MongoDB Answer: Assu ...
- PostgreSQL pg_dump pg_dumpall and restore
pg_dump dumps a database as a text file or to other formats. Usage: pg_dump [OPTION]... [DBNAME] Gen ...
- PostgreSQL与RPM
如何查看使用PostgreSQL的RPM包安装后的文件目录及相关路径(PostgreSQLRPM的spec文件已经帮我们创建好了postgres用户及postgres组). 查看RPM文档信息:/us ...
- PostgreSQL中initdb做了什么
在使用数据库前,是启动数据库,启动数据库前是initdb(初始化数据库):一起来看一下initdb做了什么吧. 初始化数据库的操作为: ./initdb -D /usr/local/pgsql/dat ...
随机推荐
- objectARX判断当前坐标系
判断当前坐标系是WCS还是UCS 使用系统变量 WORLDUCS 请参见 用户坐标系 (UCS) 概述 (只读) 类型: 整数 保存位置: 未保存 初始值: 1 指示 UCS 是否与 WCS 相同 ...
- hdu 2029
PS: 逻辑问题... 代码: #include "stdio.h"#include "string.h"int main(){ char a[110]; i ...
- 2016-1-5第一个完整APP 私人通讯录的实现 1:登录界面及跳转的简单实现2
---恢复内容开始--- 实际效果如上 一:Segue的学习 1.什么是Segue: Storyboard上每一根用来界面跳转的线,都是一个UIStoryboardSegue对象(简称Segue) ...
- 调度 Quartz 时间格式配置
1. CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年]
- Channel Allocation_四色定理
Description When a radio station is broadcasting over a very large area, repeaters are used to retra ...
- Magento后台Grid删除Add New按钮
开发过包含后台Grid及表等Magento完整模块的朋友应该知道,默认的,在Magento后台Grid右上方都会包含一个Add New按钮,用来添加新的item.但有些情况我们也可能不需要这个Add ...
- Day02_JAVA语言基础第二天
1.常量(理解) 1.概念 在程序运行过程中,其值不会发生改变的量 2.分类(掌握) A .字面值常量 整数常量:1,2,-3 小数常量:2.3,-232.3 字符常量:'A' 字符串 ...
- 一直纠结中的"底层模板"含义(借鉴)
无意间看到这个解释,推荐给哪些和我一样迷惑的人!
- USB协议-检测设备连接与速度
在USB设备连接时,USB系统能自动检测到这个连接,并识别出其采用的数据传输速率.USB采用在D+或D-线上增加上拉电阻的方法来识别低速和全速设备. USB支持3种类型的传输速率:1.5Mb/s的低速 ...
- 使用BitTorrent-Sync实现双机文件双向同步
BitTorrent-Sync是一款基于P2P的分布式文件同步工具,简称btsync,非开源软件但免费使用.本文使用btsync实现两台服务器上的软件双向同步. 安装 直接从官网下载相应的安装包,为了 ...