If you just know the ones you want to keep, and not the ones you want to quit, you can do:
DO $$
DECLARE
crow record;
excludes varchar[] := array['C', 'E'];
yourtab varchar := 'a_table';
BEGIN
FOR crow IN
SELECT * FROM information_schema.columns WHERE table_schema = 'public' and table_name = yourtab and column_name != ALL(excludes)
LOOP
EXECUTE format ('ALTER TABLE %s DROP COLUMN %s', yourtab, crow.column_name);
END LOOP;
END;
$$ language plpgsql
source
share