Using PostgreSQL 9.0, I have a group role called “staff” and would like to grant all (or certain) privileges to this role on tables in a particular schema. None of the following work
GRANT ALL ON SCHEMA foo TO staff;
GRANT ALL ON DATABASE mydb TO staff;
Members of “staff” are still unable to SELECT or UPDATE on the individual tables in the schema “foo” or (in the case of the second command) to any table in the database unless I grant all on that specific table.
What can I do make my and my users’ lives easier?
Update: Figured it out with the help of a similar question on serverfault.com.
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA foo TO staff;
You found the shorthand to set privileges for all existing tables in the given schema. The manual clarifies:
Bold emphasis mine.
serialcolumns are implemented withnextval()on a sequence as column default and, quoting the manual:So if there are
serialcolumns, you’ll also want to grantUSAGE(orALL PRIVILEGES) on sequencesNote:
IDENTITYcolumns in Postgres 10 or later use implicit sequences that don’t require additional privileges. (Consider upgradingserialcolumns.)What about new objects?
You’ll also be interested in
DEFAULT PRIVILEGESfor users or schemas:This sets privileges for objects created in the future automatically – but not for pre-existing objects.
Default privileges are only applied to objects created by the targeted user (
FOR ROLE my_creating_role). If that clause is omitted, it defaults to the current user executingALTER DEFAULT PRIVILEGES. To be explicit:Note also that all versions of pgAdmin III have a subtle bug and display default privileges in the SQL pane, even if they do not apply to the current role. Be sure to adjust the
FOR ROLEclause manually when copying the SQL script.