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://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

时间: 2024-10-29 19:06:04

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 PostgreSQL on network file systems. Reports of database corruption on the PostgreSQL mailing list are uncommon, but hardly rare. While a few data corru

PostgreSQL Database Download

9.3.25 (Not Supported)Windows x86-32: https://get.enterprisedb.com/postgresql/postgresql-9.3.25-1-windows.exeWindows x86-64https://get.enterprisedb.com/postgresql/postgresql-9.3.25-1-windows-x64.exe 9.4.25Windows x86-32:https://get.enterprisedb.com/p

Drop a database in MongoDB

http://www.linuxask.com/questions/drop-a-database-in-mongodb Answer: Assuming you are going to drop the `test` database in MongoDB, follow the steps below to drop (delete) it. > use test switched to db test > db.dropDatabase() { "dropped"

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

在安装PG数据库的过程中,会选择安装目录以及数据存放目录和端口,并需要选择Local,如果全部使用默认,并且设置好自己的密码后开始安装,前期进展还比较顺利,到了安装Database Cluster时,提示需要花费几分钟,没想到之后出现一个警告框,如下图. 点击OK,倒是可以安装到最后,一开始也没有管那么多.但是打开PG数据库登录的时候,出现错误"PostgreSql服务器未监听!",导致无法连接到PG数据库服务器.完了,服务器登不上,这下啥也干不成. 于是网上找办法,有的说是需要关闭防

PostgreSQL与RPM

如何查看使用PostgreSQL的RPM包安装后的文件目录及相关路径(PostgreSQLRPM的spec文件已经帮我们创建好了postgres用户及postgres组). 查看RPM文档信息:/usr/share/doc/rpm-4.11.1 RPM数据库文件:file /var/lib/rpm/* |grep Berkeley [[email protected] soft_bak]# rpm -qpl postgresql94-9.4.5-1PGDG.rhel7.x86_64.rpm /u

PostgreSQL中initdb做了什么

在使用数据库前,是启动数据库,启动数据库前是initdb(初始化数据库):一起来看一下initdb做了什么吧. 初始化数据库的操作为: ./initdb -D /usr/local/pgsql/data initdb把用户指定的选项转换成对应的参数,通过外部程序调用的方式执行postgres程序.postgres程序在这种方式下将进入bootstrap模式创建数据集簇,并读取后端接口postgres.bki文件来创建模板数据库. /*-------------------------------

【转帖】Windows下PostgreSQL安装图解

Windows下PostgreSQL安装图解 这篇文章主要为大家介绍了如果在Windows下安装PostgreSQL数据库的方法,需要的朋友可以参考下 现在谈起免费数据库,大多数人首先想到的可能是MySQL,的确MySQL目前已经应用在国内很多领域,尤其是网站架设方面.但是,实际上功能最强大.特性最丰富和最复杂的免费数据库应该是PostgreSQL.它的很多特性正是当今许多商业数据库例如Oracle.DB2等的前身. 其实笔者最近也是因为项目需要,接触了一点PostgreSQL的皮毛,最近Pos

PostgreSQL对象重组工具【pg_reorg】

 Description: pg_reorg is an utility program toreorganize tables in PostgreSQL databases. Unlike clusterdb, it doesn't blockany selections and updates during reorganization. You can choose one of thefollowing methods to reorganize. Online CLUSTER (

Metasploit自动连接postgresql

1. 启动postgresql [email protected]:~# service postgresql start 2. 设置用户与数据库 [email protected]:~# su postgres [email protected]:/root$ createuser msf4 -P Enter password for new role: Enter it again: [email protected]:/root$ createdb --owner=msf4 msf4[em