Sticky Code : Kill Process Session Database in PostgreSQL

Have you ever seen this error message when you are trying to delete particular DB ?

drop_db_in_used

This error message tells us that DB in used and cannot be dropped. And sometimes I feel little bit annoying because I don’t have much time to ask anyone who access / remote DB to close their connection. So what I am going to say is I will force drop db by killing active session. Here is the SQL Script to kill their active session

SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'DB_NAME'
  AND pid <> pg_backend_pid();

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>