1

I want change owner for all tables in specific table_catalog with function:

CREATE OR REPLACE function change_owner_table_catalog(catalog_arg varchar, owner_arg varchar)
    RETURNS void AS $$
    DECLARE
    table_name_value varchar;
    sequence_name_value varchar;
    BEGIN
        for table_name_value in (SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_catalog = catalog_arg) loop
            ALTER TABLE table_name_value OWNER TO owner_arg;
        end loop;
    END;
    $$ language plpgsql;

I execute it:

select change_owner_table_catalog('cinema', 'cinema_user')

And have the next error:

ERROR: relation "table_name_value" does not exist CONTEXT: SQL statement "ALTER TABLE table_name_value OWNER TO owner_arg" PL/pgSQL function change_owner_table_catalog(character varying,character varying) line 7 at SQL statement SQL state: 42P01

1 Answer 1

2

You need dynamic SQL, since ALTER TABLE does not support parameters:

EXECUTE format('ALTER TABLE public.%I OWNER TO %I', table_name_value, owner_arg);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.