Postgres: To change ownership for all partitions

 DO $$

DECLARE

    parent_table text := 'public.my_parent_table'; -- Change to your parent table

    new_owner    text := 'new_owner_role';         -- Change to your desired owner

    r record;

BEGIN

    -- Loop through all partitions of the given parent table

    FOR r IN

        SELECT inhrelid::regclass AS partition_name

        FROM pg_inherits

        WHERE inhparent = parent_table::regclass

    LOOP

        EXECUTE format('ALTER TABLE %s OWNER TO %I;', r.partition_name, new_owner);

        RAISE NOTICE 'Changed owner of % to %', r.partition_name, new_owner;

    END LOOP;

END$$;


Comments

Popular posts from this blog

Postgres: Clean up stopped replication slot

PeopleSoft: Clean Up PUM