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://stackoverflow.com/questions/5408156/how-to-drop-a-postgresql-database-if-there-are-active-connections-to-it

http://www.postgresql.org/docs/9.3/static/functions-info.html

How to drop a PostgreSQL database if there are active connections to it?的更多相关文章

  1. 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 ...

  2. How do I drop a MongoDB database, from the command line?

    mongo <dbname> --eval "db.dropDatabase()" > use mydb; > db.dropDatabase(); mon ...

  3. How to change owner of PostgreSql database?

    ALTER DATABASE name OWNER TO new_owner;

  4. 安装PostgreSQL数据库 ,Database Cluster 失败!

    在安装PG数据库的过程中,会选择安装目录以及数据存放目录和端口,并需要选择Local,如果全部使用默认,并且设置好自己的密码后开始安装,前期进展还比较顺利,到了安装Database Cluster时, ...

  5. 关于alter database datafile offline和alter database datafile offline drop 的区别

    转: https://blog.csdn.net/killvoon/article/details/46913183 -----------------------2015-07-16-------- ...

  6. Drop a database in MongoDB

    http://www.linuxask.com/questions/drop-a-database-in-mongodb Drop a database in MongoDB Answer: Assu ...

  7. 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 ...

  8. PostgreSQL与RPM

    如何查看使用PostgreSQL的RPM包安装后的文件目录及相关路径(PostgreSQLRPM的spec文件已经帮我们创建好了postgres用户及postgres组). 查看RPM文档信息:/us ...

  9. PostgreSQL中initdb做了什么

    在使用数据库前,是启动数据库,启动数据库前是initdb(初始化数据库):一起来看一下initdb做了什么吧. 初始化数据库的操作为: ./initdb -D /usr/local/pgsql/dat ...

随机推荐

  1. UIlabel设置不同的颜色

    NSString *string = @"注册过程中出现问题,致电400-650-5167联系会养车工作人员";    NSRange range = [string rangeO ...

  2. GridView导出Excel

    public void OUTEXCEL() { DataSet ds = new GW_T_DemandDAL().GetWzH(GetPersonInfoData(UserInfo), Reque ...

  3. GPRS Sniffing Tutorial

    - Download sources into ~/gprs_sniffer git clone git://git.osmocom.org/osmocom-bb.git git clone git: ...

  4. 【LeetCode OJ】Sum Root to Leaf Numbers

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

  5. 【LeetCode OJ】Insertion Sort List

    Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...

  6. JQuery源码分析(四)

    jQuery多库共存处理 多库共存换句话说可以叫无冲突处理. 总的来说会有2种情况会遇到: 1.$太火热,jQuery采用$作为命名空间,不免会与别的库框架或者插件相冲突. 2.jQuery版本更新太 ...

  7. 让所有浏览器包括IE6即支持最大宽度又支持最小宽度。

    让所有浏览器包括IE6即支持最大宽度又支持最小宽度. _height  _width:针对ie6 css hack .yangshi{max-width:620px;min-width:1px;_wi ...

  8. 在Runbook中添加Checkpoint-workflow

    本文说明的是使用Checkpoint-workflow的一种场景(当然还有其他场景需要Checkpoint-workflow). 起因:Windows Azure对Automation账户中的Runb ...

  9. 【函数】oracle translate() 详解+实例

      一.语法: TRANSLATE(string,from_str,to_str) 二.目的 返回将(所有出现的)from_str中的每个字符替换为to_str中的相应字符以后的string.TRAN ...

  10. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...