Skip to content

Instantly share code, notes, and snippets.

@josephbuchma
Last active December 9, 2015 10:38
Show Gist options
  • Save josephbuchma/37ad9545a3aad4544cf1 to your computer and use it in GitHub Desktop.
Save josephbuchma/37ad9545a3aad4544cf1 to your computer and use it in GitHub Desktop.
do $$
declare r record;
declare t record;
begin
for t in select table_name
from information_schema.tables
where table_schema='public'
loop
for r in select a.attname
from pg_catalog.pg_attribute a
where attrelid = quote_ident(t.table_name)::regclass
and a.attnum > 0
and not a.attisdropped
and a.attnotnull
loop
if quote_ident(r.attname) = 'id' then
continue;
end if;
execute 'alter table ' || quote_ident(t.table_name) || ' alter ' || quote_ident(r.attname) || ' drop not null;';
end loop;
end loop;
end $$;
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment