Skip to content

Instantly share code, notes, and snippets.

@Amitesh
Created February 25, 2013 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Amitesh/5027713 to your computer and use it in GitHub Desktop.
Save Amitesh/5027713 to your computer and use it in GitHub Desktop.
kill all connection to db and drop postgresql db
Query pg_stat_activity and get the pid values you want to kill and issue select pg_terminate_backend(pid int) to them.
PostgreSQL 9.1 and below:
SELECT pg_terminate_backend(pg_stat_activity.procpid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'mydb_prod_jan';
PostgreSQL 9.2 and above:
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'mydb_prod_jan';
============================================
DROP DATABASE mydb_prod_jan;
CREATE DATABASE mydb_prod_jan
WITH OWNER = postgres
ENCODING = 'UTF8'
TABLESPACE = pg_default
LC_COLLATE = 'POSIX'
LC_CTYPE = 'POSIX'
CONNECTION LIMIT = -1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment