Last active
June 15, 2023 15:26
-
-
Save mrsarm/8572f796b95fa6d326433569d9748eb1 to your computer and use it in GitHub Desktop.
drop-db-connections-opened.sql: Delete Postgres Database with connections opened
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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