How to drop a PostgreSQL database if there is active connections to it?

Source: Internet
Author: User

1. PostgreSQL 9.1 and below:

SELECT pg_terminate_backend (pg_stat_activity.procpid)  from pg_stat_activity WHERE = ' target_db '   and <> pg_backend_pid ();

PostgreSQL 9.2 and above:

SELECT pg_terminate_backend (pg_stat_activity.pid)  from pg_stat_activity WHERE = ' target_db '   and <> pg_backend_pid ();

Once Disconnect everyone you'll have to disconnect and issue the DROP DATABASE command from a connection from anothe R Database aka Not the one your trying to drop.

Note the renaming of the procpid column to pid . See this mailing list thread.

 of  to  Current session

2.

#!/usr/bin/Envbash#KillAll connections to the postgres serverif[-N" $"] ; Thenwhere="where Pg_stat_activity.datname = ' $ '"  Echo "killing all connections to database ' $ '"Else  Echo "killing all connections to database"fiCat<<-eof | Psql-u Postgres-d postgres SELECT pg_terminate_backend (pg_stat_activity.pid) from pg_stat_activity${where}eof

Reference:

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 is active connections to it?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.