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 ...
随机推荐
- jvm之xms、xmx等参数分析
注:本文摘自http://www.cnblogs.com/mingforyou/archive/2012/03/03/2378143.html ,感谢原作者 1.参数的含义-vmargs -Xms12 ...
- Bayeux
Bayeux是一种用来在客户端和服务器端传输低延迟的异步消息(主要通过http)的一种协议.它定义的消息通过命名通道进行路由并且能够进行交互传 送:server -> client, clien ...
- 2016 - 1 - 3 国旗选择demo
// // ViewController.m // 国旗 // // Created by Mac on 16/1/3. // Copyright © 2016年 Mac. All rights re ...
- Ubuntu 启动黑屏解决
要sudo apt-get install xserver...................balabala... then.... sudo gedit /boot/grub/grub.cf ...
- Rest文件下载
public void DownloadFile(string fileId) { //Stream fileStream = null; try { int fileID = Convert.ToI ...
- [转】HTTP请求流程(二)----Telnet模拟HTTP请求
转自: http://www.cnblogs.com/stg609/archive/2008/07/06/1237000.html 上一部分"流程简介", 我们大致了解了下HTTP ...
- VirtualBox 设置共享文件夹自动挂载
1.在VirtualBox中设置文件夹位置和名称,这里使用的名称是share 1.在ubuntu12.04的/etc/rc.local中加入自动挂载命令 sudo mount -t vboxsf sh ...
- postgresql 分区表创建及测试
1 建立分区 1.1. 创建主表 CREATE TABLE measurement ( city_id int not null, logdate date ...
- PAT (Basic Level) Practise:1004. 成绩排名
[题目链接] 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3 ...
- Intellij IDEA 创建Web项目并在Tomcat中部署运行
一.创建Web项目 1.File -> New Module,进入创建项目窗口 2.选择Java类型,在 Module name 处输入项目名,点击Next 3.勾选 Web Applica ...