Skip to content

Instantly share code, notes, and snippets.

@mrsarm
Last active June 15, 2023 15:26
Show Gist options
  • Save mrsarm/8572f796b95fa6d326433569d9748eb1 to your computer and use it in GitHub Desktop.
Save mrsarm/8572f796b95fa6d326433569d9748eb1 to your computer and use it in GitHub Desktop.
drop-db-connections-opened.sql: Delete Postgres Database with connections opened
-- Delete Database with connections opened
-- Postgres older than 13 don't support FORCE option, in this case pg_terminate_backend() is used:
SELECT pid, pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'dbname';
DROP DATABASE dbname;
-- Postgres +13:
DROP DATABASE IF EXISTS dbname WITH (FORCE);
CREATE DATABASE dbname;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment