DO $$
DECLARE
  tbl text;
  upd int;
BEGIN
  FOR tbl IN
    SELECT table_name
    FROM information_schema.columns c
    JOIN pg_tables t ON c.table_name = t.tablename AND t.schemaname = 'public'
    WHERE c.column_name = 'organization_id'
    ORDER BY table_name
  LOOP
    BEGIN
      EXECUTE format('UPDATE %I SET organization_id = 1 WHERE organization_id IS NULL', tbl);
      GET DIAGNOSTICS upd = ROW_COUNT;
      IF upd > 0 THEN
        RAISE NOTICE 'Updated % rows in %', upd, tbl;
      END IF;
    EXCEPTION WHEN OTHERS THEN
      RAISE NOTICE 'Skipped % : %', tbl, SQLERRM;
    END;
  END LOOP;
END $$;
