In Postgresql I have several databases starting with hdb prefix.
What I am looking for is to to forcefully drop all such dbs, terminating existing connections if any.
I tried below way:
DO
$do$
DECLARE
_db text;
BEGIN
FOR _db IN
SELECT datname FROM pg_stat_activity where datname ilike 'hdb%'
LOOP
EXECUTE 'REVOKE CONNECT ON DATABASE ' || _db||' FROM public;';
EXECUTE 'DROP DATABASE ' || _db;
END LOOP;
END
$do$;
But that says,
ERROR: DROP DATABASE cannot be executed from a function or multi-command string