Skip to content

Instantly share code, notes, and snippets.

@garciparedes
Last active June 2, 2021 10:41
Show Gist options
  • Select an option

  • Save garciparedes/6c134536756415755644522fb09b2f47 to your computer and use it in GitHub Desktop.

Select an option

Save garciparedes/6c134536756415755644522fb09b2f47 to your computer and use it in GitHub Desktop.
Remove roles with name pattern
# If out of memory is raised: https://stackoverflow.com/a/43530569/3921457
do $$
declare tablename text;
declare rolename text;
begin
for tablename in select datname FROM pg_database where datname like 'test_%'
loop
execute 'DROP DATABASE ' || tablename;
end loop;
for rolename in select rolname from pg_roles where rolname like 'test_%'
loop
execute 'DROP ROLE ' || rolename;
end loop;
end $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment