Skip to content

Instantly share code, notes, and snippets.

@rjdp
Created April 18, 2018 13:42
Show Gist options
  • Save rjdp/dac2e8e0bc7bb7f7d6b63d23651955fb to your computer and use it in GitHub Desktop.
Save rjdp/dac2e8e0bc7bb7f7d6b63d23651955fb to your computer and use it in GitHub Desktop.
Loop over schemas in a Postgresql database and execute arbitrary DDL example
DO
$$
DECLARE
schemaname name;
BEGIN
FOR schemaname IN SELECT nspname FROM pg_namespace WHERE nspname LIKE 'schema_%' AND nspname <> 'information_schema' LOOP
RAISE NOTICE 'Running Alter on Schema : %', schemaname;
EXECUTE format('ALTER TABLE %I.common_commonconfigure ADD COLUMN google_secret_key varchar(150);', schemaname);
END LOOP;
END;
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment