0

I'm having trouble when using the variable arrow in my loop. Postgres ignores the declared variable and reads it as text. How can I solve that?

I have the following error: ERROR: relation "arrow" does not exist Where: SQL statement "GRANT SELECT ON arrow TO test" PL/pgSQL function inline_code_block line 8 at SQL statement

do $$
declare
    arrow record;
BEGIN
FOR arrow IN
    SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'
LOOP
   GRANT SELECT ON arrow TO test;
END LOOP;
END;
$$ LANGUAGE plpgsql;

1 Answer 1

2

You need dynamic SQL for this.

do $$
declare
    arrow record;
BEGIN
FOR arrow IN
    SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'
LOOP
   execute format('GRANT SELECT ON %I TO test', arrow.table_name);
END LOOP;
END;
$$ LANGUAGE plpgsql;

But you can do that much easier without any looping or PL/pgSQL:

grant select on all tables in schema public to test;
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.