Skip to content

Instantly share code, notes, and snippets.

@garrow
Created October 8, 2012 06:15
Show Gist options
  • Save garrow/3850987 to your computer and use it in GitHub Desktop.
Save garrow/3850987 to your computer and use it in GitHub Desktop.
Restore tables from a snapshot into public.
DO
$$
DECLARE
hidden_table record;
BEGIN
FOR hidden_table IN
SELECT schemaname, tablename FROM pg_tables
WHERE schemaname = (SELECT MAX(schemaname) FROM pg_tables WHERE schemaname LIKE 'snapshot_%')
LOOP
EXECUTE 'DROP TABLE IF EXISTS public.' || quote_ident(hidden_table.tablename) || ';';
EXECUTE 'ALTER TABLE ' || quote_ident(hidden_table.schemaname) || '.' || quote_ident(hidden_table.tablename) || ' SET SCHEMA public;';
END LOOP;
END;
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment