GRANT(7)
π GRANT(7) π
okay bestie, so this is the GRANT page from the PostgreSQL 17.11 Documentation β it lives in section 7, the SQL commands part. and it is a lot, but we're gonna get through it together, fr fr. this is part 1, so don't go anywhere.
β¨ NAME β¨
GRANT β define access privileges π
π SYNOPSIS π
okay so like. there are a bunch of forms. don't panic. just vibe with it:
GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER | MAINTAIN }
[, ...] | 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 { { SET | ALTER SYSTEM } [, ... ] | ALL [ PRIVILEGES ] }
ON PARAMETER configuration_parameter [, ...]
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 | INHERIT | SET } { OPTION | TRUE | FALSE } ]
[ GRANTED BY role_specification ]
and where role_specification can be:
[ GROUP ] role_name
| PUBLIC
| CURRENT_ROLE
| CURRENT_USER
| SESSION_USER
π¦ DESCRIPTION π¦
okay so GRANT has two basic variants, and they're lowkey cousins β similar vibes, but different enough that the docs describe them separately:
- the one that grants privileges on a database object β and the list is long, bestie: table, column, view, foreign table, sequence, database, foreign-data wrapper, foreign server, function, procedure, procedural language, large object, configuration parameter, schema, tablespace, or type π
- the one that grants membership in a role π
they're related, sure, but they get their own sections. so let's go.
πΈ GRANT on Database Objects πΈ
this variant gives specific privileges on a database object to one or more roles. and the way it works is additive β these privileges get added to whatever was already granted, if anything. no replacing, just stacking π
the keyword PUBLIC means the privileges go to all roles, including ones that might get created later. think of PUBLIC as an implicitly defined group that always includes every role, periodt. so 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 the privilege to other people. without the grant option? nope, they can't. and π¨ grant options cannot be granted to PUBLIC π¨.
if GRANTED BY is specified, the grantor you name must be the current user. this clause is currently present in this form only for SQL compatibility β iykyk.
π«Ά bestie PSA: you do not need to grant privileges to the owner of an object (usually whoever created it), because the owner already has all privileges by default. (tho the owner could choose to revoke some of their own privileges for safety, iconic behaviour honestly.)
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. (but 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, honestly π.
the possible privileges are:
SELECTINSERTUPDATEDELETETRUNCATEREFERENCESTRIGGERCREATECONNECTTEMPORARYEXECUTEUSAGESETALTER SYSTEMMAINTAIN
all of those are specific types of privileges, as defined in Section 5.8. π
TEMPβ alternative spelling forTEMPORARY. same girl, shorter name πALL PRIVILEGESβ grants all of the privileges available for the object's type. thePRIVILEGESkey word is optional in PostgreSQL, though it is required by strict SQL. so yes, you can be lazy, but strict SQL will judge you π
okay so about the function forms β 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, covers everyone, we love a flexible icon β¨
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. quick notes:
ALL TABLESalso affects views and foreign tables, just like the specific-objectGRANTcommand.ALL FUNCTIONSalso affects aggregate and window functions, but not procedures, again just like the specific-objectGRANTcommand.- use
ALL ROUTINESto include procedures.
π GRANT on Roles π
this variant grants membership in a role to one or more other roles, and also handles modification of the membership options SET, INHERIT, and ADMIN. see Section 21.3 for the details, bestie.
membership in a role is a big deal because it potentially allows access to the privileges granted to a role to each of its members β and potentially also the ability to make changes to the role itself. however, the actual permissions conferred depend on the options associated with the grant. to modify the options of an existing membership, simply specify the membership with updated option values. easy π
each of the options described below can be set to either TRUE or FALSE. the keyword OPTION is accepted as a synonym for TRUE, so that WITH ADMIN OPTION is a synonym for WITH ADMIN TRUE. when altering an existing membership, omitting an option means the current value is retained. no take-backs, no resets π
ADMINβ the admin option allows the member to 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. a role is not considered to holdWITH ADMIN OPTIONon itself. database superusers can grant or revoke membership in any role to anyone. this option defaults toFALSE. π¨INHERITβ controls the inheritance status of the new membership; see Section 21.3 for the details on inheritance. if it's set toTRUE, it causes the new member to inherit from the granted role. if set toFALSE, the new member does not inherit. if unspecified when creating a new role membership, this defaults to the inheritance attribute of the new member.SETβ if it's set toTRUE, it allows the member to change to the granted role using theSET ROLEcommand. if a role is an indirect member of another role, it can useSET ROLEto change to that role only if there is a chain of grants each of which hasSET TRUE. this option defaults toTRUE.
okay so this next bit matters π β to create an object owned by another role, or give ownership of an existing object to another role, you must have the ability to SET ROLE to that role. otherwise, commands such as ALTER ... OWNER TO or CREATE DATABASE ... OWNER will fail. however, a user who inherits the privileges of a role but does not have the ability to SET ROLE to that role may be able to obtain full access to the role by manipulating existing objects owned by that role (e.g. they could redefine an existing function to act as a Trojan horse π΄). therefore, if a role's privileges are to be inherited but should not be accessible via SET ROLE, it should not own any SQL objects. that's not a vibe. write it down.
if GRANTED BY is specified, the grant is recorded as having been done by the specified role. a user can only attribute a grant to another role if they possess the privileges of that role. the role recorded as the grantor must have ADMIN OPTION on the target role, unless it is the bootstrap superuser. when a grant is recorded as having a grantor other than the bootstrap superuser, it depends on the grantor continuing to possess ADMIN OPTION on the role β so if ADMIN OPTION is revoked, dependent grants must be revoked as well. messy, but that's the rules π
and finally, two important differences from the privilege variant:
- unlike the case with privileges, membership in a role cannot be granted to
PUBLIC. - this form of the command does not allow the noise word
GROUPinrole_specification.
okay that's where part 1 lands β more coming, don't go anywhere π¦
π NOTES π
Okay so real talk: the REVOKE command is the one that takes access privileges away. π
Ever since PostgreSQL 8.1, the whole concept of users and groups got 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βs a noise word β decorative, lowkey. β¨
A user may perform SELECT, INSERT, etc. on a column if they hold that privilege for either the specific column or its whole table. And hereβs the trap, bestie: 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. No cap. π
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, while 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. π
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 bootstrap superuser.)
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. For example, 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.
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. π¦
See Section 5.8 for more information about specific privilege types, as well as how to inspect objectsβ privileges.
π EXAMPLES π
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;
Note that while the above will indeed grant all privileges if executed by a superuser or the owner of kinds, when executed by someone else it will only grant those permissions for which the someone else has grant options. π
Grant membership in role admins to user joe:
GRANT admins TO joe;
π COMPATIBILITY π
- According to the SQL standard, the
PRIVILEGESkey word inALL PRIVILEGESis required. The SQL standard does not support setting the privileges on more than one object per command. - PostgreSQL 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, andTRUNCATEprivileges. This is not possible according to the SQL standard. The reason is that 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. π - According to the SQL standard, grant options can be granted to
PUBLIC; PostgreSQL only supports granting grant options to roles. - The SQL standard allows the
GRANTED BYoption to specify onlyCURRENT_USERorCURRENT_ROLE. The other variants are PostgreSQL extensions. - The SQL standard provides for a
USAGEprivilege on other kinds of objects: character sets, collations, translations. - In the SQL standard, sequences only have a
USAGEprivilege, which controls the use of theNEXT VALUE FORexpression, which is equivalent to the functionnextvalin PostgreSQL. The sequence privilegesSELECTandUPDATEare PostgreSQL extensions. The application of the sequenceUSAGEprivilege to thecurrvalfunction is also a PostgreSQL extension (as is the function itself). π₯ - Privileges on databases, tablespaces, schemas, languages, and configuration parameters are PostgreSQL extensions.
π SEE ALSO π
REVOKE(7)ALTER DEFAULT PRIVILEGES(ALTER_DEFAULT_PRIVILEGES(7))
Thatβs the whole page, bestie! π PostgreSQL 17.11 Β· 2026 Β· GRANT(7) π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms