I have two tables. One has a foreign key referencing a sequential field in another table. I have granted INSERT privileges for a role other than the owner, but I still cannot insert it into the table containing the foreign key, unless I granted the owner of the UPDATE table privilege in the table containing the reference field. I donβt quite understand why the owner needs UPDATE permission so that another role (with the INSERT privilege) can insert a row in this case.
This is a bit confusing, so I gave my problem as an example.
createuser -U postgres testowner -DIRS --pwprompt createdb -U postgres -O testowner testdb createuser -U postgres testupdater -DIRS --pwprompt psql -d testdb -U testowner CREATE TABLE a ( id serial PRIMARY KEY ); CREATE TABLE b ( a_id integer REFERENCES a(id) ); GRANT SELECT,INSERT ON ALL TABLES IN SCHEMA public TO testupdater; GRANT USAGE,UPDATE ON SEQUENCE a_id_seq TO testupdater; REVOKE INSERT, UPDATE ON ALL TABLES IN SCHEMA public FROM testowner; INSERT INTO a VALUES (DEFAULT); -- as expected: ERROR: permission denied for relation a \q psql -d testdb -U testupdater INSERT INTO a VALUES (DEFAULT); SELECT id FROM a LIMIT 1; -- selects the first id (1) INSERT INTO b VALUES (1); -- unexpected error: see below \q
ERROR: permission denied for relation a
CONTEXT: SQL statement "SELECT 1 FROM ONLY "public"."a" x WHERE "id" OPERATOR(pg_catalog.=) $1 FOR SHARE OF x"
However, the above insertion works if I give testowner the UPDATE privilege back ( GRANT UPDATE ON a TO testowner; ). Why in this case does the tester need to UPDATE?
NOTE: GRANT UPDATE ON a TO testupdater; Does not help; it seems that I should PAGE UPDATE to the testowner role.
sql postgresql permissions
David underhill
source share