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 ...
随机推荐
- 如何修改svn的密码或重新输入用户名密码
在Eclipse 使用SVN 的过程中大多数人往往习惯把访问SVN 的用户名密码自动保存起来以便下次自动使用,不要再次手工输入,而此时(自动保存密码后),svn又不存在一个显式的登陆框了,但是有些时候 ...
- iOS 在UILabel显示不同的字体和颜色
转自:http://my.oschina.net/CarlHuang/blog/138363 在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串, ...
- React Native 组件之TextInput
React Native 组件之TextInput类似于iOS中的UITextView或者UITextField,是作为一个文字输入的组件,下面的TextInput的用法和相关属性. /** * Sa ...
- Objective-C determine data network type of the iOS device
Im on an application that receive data from server, the problem is when user connect to cellular dat ...
- 自定义NavigationBar
[self.navigationController.navigationBarsetHidden:YES]; CGRect screenRect = [[UIScreen mainScreen] b ...
- Android Priority Job Queue (Job Manager)(一)
Android Priority Job Queue (Job Manager)(一) 一.为什么要引入Android Priority Job Queue (Job Manager)?如今的A ...
- 8、C#基础整理(数组和冒泡排序)
数组 概念:定义一组同类型的指定个数的变量,索引从0开始 例: ];//定义一组有10个数据的数组 shuname[] = ; Console.WriteLine(shuname[]);//打印出1 ...
- magento在产品详细页面添加分享链接的方法
1,在产品详细页面的对用位置加入一下代码 <div class="sharethis_box"> <?php echo $this->ge ...
- nginx+c+cgi开发
http://blog.csdn.net/marising/article/details/3932938 1.Nginx 1.1.安装 Nginx 的中文维基 http://wiki.codemon ...
- stdobj to array php
The lazy one-liner method You can do this in a one liner using the JSON methods if you're willing to ...