GRANT(7)

πŸ“¦ version 14.24, in opensuse-leap 16.0

✨ GRANT(7) ✨

okay bestie, buckle up β€” we're doing the PostgreSQL 14.24 Documentation page for GRANT, and it's literally the "who gets to touch my stuff" command πŸ’…. This is part 1 of 2, so don't you dare click away, there's more coming 🫢


πŸŽ€ NAME πŸŽ€

GRANT β€” define access privileges

like, officially: who's allowed to do what around here πŸ‘‘


🌸 SYNOPSIS 🌸

ok so the grammar looks like a lot, and it IS a lot, but it's the same shape over and over: GRANT some privilege ON some thing TO somebody. Here's all of it, exactly as written, because I refuse to paraphrase syntax 🧚

GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
    [, ...] | ALL [ PRIVILEGES ] }
    ON { [ TABLE ] table_name [, ...]
         | ALL TABLES IN SCHEMA schema_name [, ...] }
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT { { SELECT | INSERT | UPDATE | REFERENCES } ( column_name [, ...] )
    [, ...] | ALL [ PRIVILEGES ] ( column_name [, ...] ) }
    ON [ TABLE ] table_name [, ...]
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT { { USAGE | SELECT | UPDATE }
    [, ...] | ALL [ PRIVILEGES ] }
    ON { SEQUENCE sequence_name [, ...]
         | ALL SEQUENCES IN SCHEMA schema_name [, ...] }
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT { { CREATE | CONNECT | TEMPORARY | TEMP } [, ...] | ALL [ PRIVILEGES ] }
    ON DATABASE database_name [, ...]
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT { USAGE | ALL [ PRIVILEGES ] }
    ON DOMAIN domain_name [, ...]
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT { USAGE | ALL [ PRIVILEGES ] }
    ON FOREIGN DATA WRAPPER fdw_name [, ...]
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT { USAGE | ALL [ PRIVILEGES ] }
    ON FOREIGN SERVER server_name [, ...]
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT { EXECUTE | ALL [ PRIVILEGES ] }
    ON { { FUNCTION | PROCEDURE | ROUTINE } routine_name [ ( [ [ argmode ] [ arg_name ] arg_type [, ...] ] ) ] [, ...]
         | ALL { FUNCTIONS | PROCEDURES | ROUTINES } IN SCHEMA schema_name [, ...] }
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT { USAGE | ALL [ PRIVILEGES ] }
    ON LANGUAGE lang_name [, ...]
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT { { SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] }
    ON LARGE OBJECT loid [, ...]
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT { { CREATE | USAGE } [, ...] | ALL [ PRIVILEGES ] }
    ON SCHEMA schema_name [, ...]
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT { CREATE | ALL [ PRIVILEGES ] }
    ON TABLESPACE tablespace_name [, ...]
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT { USAGE | ALL [ PRIVILEGES ] }
    ON TYPE type_name [, ...]
    TO role_specification [, ...] [ WITH GRANT OPTION ]
    [ GRANTED BY role_specification ]

GRANT role_name [, ...] TO role_specification [, ...]
    [ WITH ADMIN OPTION ]
    [ GRANTED BY role_specification ]

and where role_specification can be:

    [ GROUP ] role_name
  | PUBLIC
  | CURRENT_ROLE
  | CURRENT_USER
  | SESSION_USER

πŸ’– DESCRIPTION πŸ’–

so GRANT has two basic variants, and like… they're cousins, not twins πŸ‘―

  1. the one that grants privileges on a database object β€” and by "object" we mean table, column, view, foreign table, sequence, database, foreign-data wrapper, foreign server, function, procedure, procedural language, schema, or tablespace πŸ’«
  2. the one that grants membership in a role πŸ«‚

They're similar in many ways, but they're different enough that the docs describe them separately. So we're doing that too. periodt.

🌈 GRANT on Database Objects 🌈

This variant gives specific privileges on a database object to one or more roles. And here's the slay part: these privileges are added to the ones already granted, if any β€” it's additive, bestie, not a reset button ✨

The key word PUBLIC means the privileges go to all roles, including the ones that might be created later. Like, future roles too, they get it πŸ‘€. PUBLIC is basically an implicitly defined group that always includes every role, always. And any particular role will have the sum of:

  • privileges granted directly to it
  • privileges granted to any role it is presently a member of
  • privileges granted to PUBLIC

if WITH GRANT OPTION is specified, the recipient can turn around and grant that privilege to others β€” like, they get to pass it on πŸ’Œ. Without a grant option, the recipient cannot do that. And heads up: grant options cannot be granted to PUBLIC 🚫

if GRANTED BY is specified, the specified grantor must be the current user. This clause is currently present in this form only for SQL compatibility. (iykyk β€” it's there for the vibes/SQL, not for extra power πŸ’€)

there is no need to grant privileges to the owner of an object (usually the user that created it), because the owner has all privileges by default πŸ‘‘. The owner could, however, choose to revoke some of their own privileges for safety. cautious queen behavior.

and okay, this next bit is important: the right to drop an object, or to alter its definition in any way, is not treated as a grantable privilege. It's inherent in the owner, and cannot be granted or revoked. Period. (However, a similar effect can be obtained by granting or revoking membership in the role that owns the object β€” see below πŸ‘‡.) The owner implicitly has all grant options for the object too. main character energy fr.

πŸ¦‹ The possible privileges πŸ¦‹

  • SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER, CREATE, CONNECT, TEMPORARY, EXECUTE, USAGE β€” these are the specific types of privileges, as defined in Section 5.7 ⭐
  • TEMP β€” alternative spelling for TEMPORARY 🩷
  • ALL PRIVILEGES β€” grants all of the privileges available for the object's type. The PRIVILEGES key word is optional in PostgreSQL, though it is required by strict SQL πŸŽ€

the FUNCTION syntax works for plain functions, aggregate functions, and window functions β€” but not for procedures; use PROCEDURE for those. Alternatively, use ROUTINE to refer to a function, aggregate function, window function, or procedure regardless of its precise type. one word to rule them all, basically 🧚

there's also an option to grant privileges on all objects of the same type within one or more schemas. This functionality is currently supported only for tables, sequences, functions, and procedures πŸ“‹

  • ALL TABLES also affects views and foreign tables, just like the specific-object GRANT command πŸ‘‘
  • ALL FUNCTIONS also affects aggregate and window functions, but not procedures β€” again, just like the specific-object GRANT command
  • use ALL ROUTINES to include procedures

πŸ’œ GRANT on Roles πŸ’œ

This variant grants membership in a role to one or more other roles 🫢. Membership matters because it conveys the privileges granted to a role to each of its members β€” like, the perks get shared with the whole friend group πŸ’…

if WITH ADMIN OPTION is specified, the member can in turn grant membership in the role to others, and revoke membership in the role as well πŸ”. Without the admin option, ordinary users cannot do that. And listen: a role is not considered to hold WITH ADMIN OPTION on itself, but it may grant or revoke membership in itself from a database session where the session user matches the role πŸ€”

  • Database superusers can grant or revoke membership in any role to anyone πŸ‘‘
  • Roles having CREATEROLE privilege can grant or revoke membership in any role that is not a superuser

if GRANTED BY is specified, the grant is recorded as having been done by the specified role. Only database superusers may use this option β€” except when it names the same role executing the command 😌

⚠️ and unlike the case with privileges, membership in a role cannot be granted to PUBLIC. Also note that this form of the command does not allow the noise word GROUP in role_specification. no cap, it's just not allowed there 🚫

🌟 NOTES 🌟

the REVOKE command is used to revoke access privileges πŸ”™ (yes, it's the opposite twin, we love a duo)

since PostgreSQL 8.1, the concepts of users and groups have been unified into a single kind of entity called a role. So it's no longer necessary to use the keyword GROUP to identify whether a grantee is a user or a group. GROUP is still allowed in the command, but it is a noise word πŸ—£οΈ (it's giving "we keep it around for the vibes")

a user may perform SELECT, INSERT, etc. on a column if they hold that privilege for either the specific column or its whole table. But β€” and this is the trap πŸ™ˆ β€” granting the privilege at the table level and then revoking it for one column will not do what one might wish: the table-level grant is unaffected by a column-level operation. like, that's just not how it works bestie πŸ’€

when a non-owner of an object attempts to GRANT privileges on the object, the command will fail outright if the user has no privileges whatsoever on the object. As long as some privilege is available, the command will proceed, but it will grant only those privileges for which the user has grant options βœ…

  • the GRANT ALL PRIVILEGES forms will issue a warning message if no grant options are held πŸ“’
  • the other forms will issue a warning if grant options for any of the privileges specifically named in the command are not held

(in principle these statements apply to the object owner as well, but since the owner is always treated as holding all grant options, the cases can never occur 🫠)

πŸ”₯ it should be noted that database superusers can access all objects regardless of object privilege settings. This is comparable to the rights of root in a Unix system. As with root, it's unwise to operate as a superuser except when absolutely necessary. like, do NOT just live in superuser mode, that's not the flex you think it is 😭

if a superuser chooses to issue a GRANT or REVOKE command, the command is performed as though it were issued by the owner of the affected object. In particular, privileges granted via such a command will appear to have been granted by the object owner. (For role membership, the membership appears to have been granted by the containing role itself.) 🎭

GRANT and REVOKE can also be done by a role that is not the owner of the affected object, but is a member of the role that owns the object, or is a member of a role that holds privileges WITH GRANT OPTION on the object. In this case the privileges will be recorded as having been granted by the role that actually owns the object or holds the privileges WITH GRANT OPTION 🫢

Example time, because this one's a whole vibe check: if table t1 is owned by role g1, of which role u1 is a member, then u1 can grant privileges on t1 to u2, but those privileges will appear to have been granted directly by g1. Any other member of role g1 could revoke them later πŸ‘€

if the role executing GRANT holds the required privileges indirectly via more than one role membership path, it is unspecified which containing role will be recorded as having done the grant πŸ˜΅β€πŸ’« In such cases it is best practice to use SET ROLE to become the specific role you want to do the GRANT as. do that, bestie, save yourself the mystery πŸ’…

granting permission on a table does not automatically extend permissions to any sequences used by the table β€” including sequences tied to SERIAL columns 🚨 Permissions on sequences must be set separately. (yes, separately. don't forget. I'm begging πŸ₯Ί)

see Section 5.7 for more information about specific privilege types, as well as how to inspect objects' privileges πŸ“–


✨ okay that's part 1, there's more coming so don't go anywhere ✨

πŸŽ€ EXAMPLES πŸŽ€

okay so here’s how you actually use this thing, bestie β€” the examples are giving β€œcopy, paste, slay” ✨

First one: grant insert privilege to all users on table films:

GRANT INSERT ON films TO PUBLIC;

Grant all available privileges to user manuel on view kinds:

GRANT ALL PRIVILEGES ON kinds TO manuel;
🚨 bestie, read this note before you go wild: the above will indeed grant all privileges if it’s executed by a superuser or the owner of kinds. But when it’s executed by someone else, it will only grant those permissions for which the someone else has grant options. no cap.

Grant membership in role admins to user joe:

GRANT admins TO joe;

that’s it, those are the examples, iconic behavior πŸ’…


πŸ¦‹ COMPATIBILITY πŸ¦‹

okay, compatibility tea β˜• β€” according to the SQL standard, the PRIVILEGES key word in ALL PRIVILEGES is required. Also, the SQL standard does not support setting the privileges on more than one object per command. periodt.

PostgreSQL, though? It allows an object owner to revoke their own ordinary privileges: for example, a table owner can make the table read-only to themselves by revoking their own INSERT, UPDATE, DELETE, and TRUNCATE privileges. This is not possible according to the SQL standard. Why? Because PostgreSQL treats the owner’s privileges as having been granted by the owner to themselves β€” therefore they can revoke them too. In the SQL standard, the owner’s privileges are granted by an assumed entity β€œ_SYSTEM”. Not being β€œ_SYSTEM”, the owner cannot revoke these rights. it’s giving loophole, but make it SQL 😭.

According to the SQL standard, grant options can be granted to PUBLIC; PostgreSQL only supports granting grant options to roles. sorry, PUBLIC, no grant-option era for you πŸ’€.

The SQL standard allows the GRANTED BY option to specify only CURRENT_USER or CURRENT_ROLE. The other variants are PostgreSQL extensions. iykyk ✨.

The SQL standard provides for a USAGE privilege on other kinds of objects: character sets, collations, translations.

In the SQL standard, sequences only have a USAGE privilege, which controls the use of the NEXT VALUE FOR expression, which is equivalent to the function nextval in PostgreSQL. The sequence privileges SELECT and UPDATE are PostgreSQL extensions. The application of the sequence USAGE privilege to the currval function is also a PostgreSQL extension (as is the function itself).

Privileges on databases, tablespaces, schemas, and languages are PostgreSQL extensions.


🌈 SEE ALSO 🌈

go check out these other iconique pages, bestie: REVOKE(7), and ALTER DEFAULT PRIVILEGES (ALTER_DEFAULT_PRIVILEGES(7)). 🫢


mwah πŸ’‹ this page is from PostgreSQL 14.24, dated 2026, and it’s literally GRANT(7). bye bestie πŸŽ€

πŸ“¦ where this page comes from