Skip to content

Instantly share code, notes, and snippets.

@microchipgnu
Last active November 13, 2020 17:09
Show Gist options
  • Save microchipgnu/e445f69d0388bc988d1237c2d804005e to your computer and use it in GitHub Desktop.
Save microchipgnu/e445f69d0388bc988d1237c2d804005e to your computer and use it in GitHub Desktop.
postgres utils

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

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

-- second 
DROP DATABASE DATABASE_NAME;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment