Skip to content

Instantly share code, notes, and snippets.

@rhysallister
Created December 13, 2016 16:06
Show Gist options
  • Save rhysallister/3bd09b0df055b950d780b0aec430deea to your computer and use it in GitHub Desktop.
Save rhysallister/3bd09b0df055b950d780b0aec430deea to your computer and use it in GitHub Desktop.
union all tables in a schema
DO
$$
declare
vv text;
hh record;
qry text;
qar text[];
begin
qry = 'create table plus1.plus1 as ';
for hh in select * from pg_tables where schemaname = 'plus1' loop
qar = array_append(qar, ' SELECT "Name", description, geom from plus1.'|| quote_ident(hh.tablename));
end loop;
raise notice '%', qry ||array_to_string(qar, e'\n UNION \n'::text);
end;
$$ language plpgsql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment