GRANT <privileges>

The GRANT <privileges> statement lets you control each role or user's SQL privileges for interacting with specific databases and tables.

For privileges required by specific statements, see the documentation for the respective SQL statement.

Synopsis

GRANTALLCREATEGRANTSELECTDROPINSERTDELETEUPDATE,ONTABLEtable_name,DATABASEdatabase_name,TOuser_name,

Required privileges

The user granting privileges must have the GRANT privilege on the target databases or tables.

Supported privileges

Roles and users can be granted the following privileges. Some privileges are applicable both for databases and tables, while other are applicable only for tables (see Levels in the table below).

  • When a role or user is granted privileges for a database, new tables created in the database will inherit the privileges, but the privileges can then be changed.
  • When a role or user is granted privileges for a table, the privileges are limited to the table.
  • The root user automatically belongs to the admin role and has the ALL privilege for new databases.
  • For privileges required by specific statements, see the documentation for the respective SQL statement.
    PrivilegeLevels
    ALLDatabase, Table
    CREATEDatabase, Table
    DROPDatabase, Table
    GRANTDatabase, Table
    SELECTTable
    INSERTTable
    DELETETable
    UPDATETable

Parameters

ParameterDescription
table_nameA comma-separated list of table names. Alternately, to grant privileges to all tables, use . ON TABLE table. grants apply to all existing tables in a database but will not affect tables created after the grant.
database_nameA comma-separated list of database names.Privileges granted on databases will be inherited by any new tables created in the databases, but do not affect existing tables in the database.
user_nameA comma-separated list of users and/or roles to whom you want to grant privileges.

Examples

Grant privileges on databases

  1. > GRANT CREATE ON DATABASE db1, db2 TO maxroach, betsyroach;
  1. > SHOW GRANTS ON DATABASE db1, db2;
  1. +----------+------------+------------+
  2. | Database | User | Privileges |
  3. +----------+------------+------------+
  4. | db1 | betsyroach | CREATE |
  5. | db1 | maxroach | CREATE |
  6. | db1 | root | ALL |
  7. | db2 | betsyroach | CREATE |
  8. | db2 | maxroach | CREATE |
  9. | db2 | root | ALL |
  10. +----------+------------+------------+
  11. (6 rows)

Grant privileges on specific tables in a database

  1. > GRANT DELETE ON TABLE db1.t1, db1.t2 TO betsyroach;
  1. > SHOW GRANTS ON TABLE db1.t1, db1.t2;
  1. +-------+------------+------------+
  2. | Table | User | Privileges |
  3. +-------+------------+------------+
  4. | t1 | betsyroach | DELETE |
  5. | t1 | root | ALL |
  6. | t2 | betsyroach | DELETE |
  7. | t2 | root | ALL |
  8. +-------+------------+------------+
  9. (4 rows)

Grant privileges on all tables in a database

  1. > GRANT SELECT ON TABLE db2.* TO henryroach;
  1. > SHOW GRANTS ON TABLE db2.*;
  1. +-------+------------+------------+
  2. | Table | User | Privileges |
  3. +-------+------------+------------+
  4. | t1 | henryroach | SELECT |
  5. | t1 | root | ALL |
  6. | t2 | henryroach | SELECT |
  7. | t2 | root | ALL |
  8. +-------+------------+------------+
  9. (4 rows)

Make a table readable to every user in the system

  1. > GRANT SELECT ON TABLE myTable TO public;
  1. > SHOW GRANTS ON TABLE myTable;
  1. database_name | schema_name | table_name | grantee | privilege_type
  2. +---------------+-------------+------------+---------+----------------+
  3. defaultdb | public | mytable | admin | ALL
  4. defaultdb | public | mytable | public | SELECT
  5. defaultdb | public | mytable | root | ALL
  6. (3 rows)

See also

Was this page helpful?
YesNo