Help Center > > Developer Guide> SQL Reference> SQL Syntax> GRANT

GRANT

Updated at: Mar 13, 2020 GMT+08:00

Function

GRANT grants rights to roles and users.

GRANT is used in the following scenarios:

  • Granting system permissions to roles or users

    System permissions are also called user attributes, including SYSADMIN, CREATEDB, CREATEROLE, AUDITADMIN, and LOGIN.

    They can be specified only by the CREATE ROLE or ALTER ROLE syntax. The SYSADMIN permission can be granted and revoked using GRANT ALL PRIVILEGE and REVOKE ALL PRIVILEGE, respectively. System permissions cannot be inherited by a user from a role, and cannot be granted using PUBLIC.

  • Granting database object permissions to roles or users

    Grant permissions related to database objects (tables, views, specified columns, databases, functions, and schemas) to specified roles or users.

    GRANT grants specified database object permissions to one or more roles. These permissions are appended to those already granted, if any.

    DWS grants the permissions for objects of certain types to PUBLIC. By default, permissions for tables, table columns, sequences, external data sources, external servers, schemas, and tablespace are not granted to PUBLIC. However, permissions for the following objects are granted to PUBLIC: CONNECT and CREATE TEMP TABLE permissions for databases, EXECUTE permission for functions, and USAGE permission for languages and data types (including domains). An object owner can revoke the default permissions granted to PUBLIC and grant permissions to other users as needed. For security purposes, you are advised to create an object and set permissions for it in the same transaction so that other users do not have time windows to use the object. In addition, you can run the ALTER DEFAULT PRIVILEGES statement to modify the initial default permissions.

  • Granting a role's or user's permissions to other roles or users

    Grant a role's or user's permissions to one or more roles or users. In this case, every role or user can be regarded as a set of one or more database permissions.

    If WITH ADMIN OPTION is specified, the member can in turn grant permissions in the role to others, and revoke permissions in the role as well. If a role or user granted with certain permissions is changed or revoked, the permissions inherited from the role or user also change.

    A database administrator can grant permissions to and revoke them from any role or user. Roles having CREATEROLE permission can grant or revoke membership in any role that is not an administrator.

Precautions

None

To isolate permissions, DWS disables the WITH GRANT OPTION and TO PUBLIC syntax.

Syntax

  • Grant the table or view access permission to a specified role or user. GRANT to a table partition will cause alarms, and therefore is forbidden.
    1
    2
    3
    4
    5
    6
    GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES } [, ...] 
          | ALL [ PRIVILEGES ] }
        ON { [ TABLE ] table_name [, ...]
           | ALL TABLES IN SCHEMA schema_name [, ...] }
        TO { [ GROUP ] role_name | PUBLIC } [, ...] 
        [ WITH GRANT OPTION ];
    
  • Grant the column access permission to a specified role or user.
    1
    2
    3
    4
    5
    GRANT { {{ SELECT | INSERT | UPDATE | REFERENCES } ( column_name [, ...] )} [, ...] 
          | ALL [ PRIVILEGES ] ( column_name [, ...] ) }
        ON [ TABLE ] table_name [, ...]
        TO { [ GROUP ] role_name | PUBLIC } [, ...]
        [ WITH GRANT OPTION ];
    
  • Grant the database access permission to a specified role or user.
    1
    2
    3
    4
    5
    GRANT { { CREATE | CONNECT | TEMPORARY | TEMP } [, ...]
          | ALL [ PRIVILEGES ] }
        ON DATABASE database_name [, ...]
        TO { [ GROUP ] role_name | PUBLIC } [, ...]
        [ WITH GRANT OPTION ];
    
  • Grant the domain access permission to a specified role or user.
    1
    2
    3
    4
    GRANT { USAGE | ALL [ PRIVILEGES ] }
        ON DOMAIN domain_name [, ...]
        TO { [ GROUP ] role_name | PUBLIC } [, ...]
        [ WITH GRANT OPTION ];
    

    The current version does not support granting the domain access permission.

  • Grant the external data source access permission to a specified role or user.
    1
    2
    3
    4
    GRANT { USAGE | ALL [ PRIVILEGES ] }
        ON FOREIGN DATA WRAPPER fdw_name [, ...]
        TO { [ GROUP ] role_name | PUBLIC } [, ...]
        [ WITH GRANT OPTION ];
    
  • Grant the external server access permission to a specified role or user.
    1
    2
    3
    4
    GRANT { USAGE | ALL [ PRIVILEGES ] }
        ON FOREIGN SERVER server_name [, ...]
        TO { [ GROUP ] role_name | PUBLIC } [, ...]
        [ WITH GRANT OPTION ];
    
  • Grant the function access permission to a specified role or user.
    1
    2
    3
    4
    5
    GRANT { EXECUTE | ALL [ PRIVILEGES ] }
        ON { FUNCTION {function_name ( [ {[ argmode ] [ arg_name ] arg_type} [, ...] ] )} [, ...]
           | ALL FUNCTIONS IN SCHEMA schema_name [, ...] }
        TO { [ GROUP ] role_name | PUBLIC } [, ...]
        [ WITH GRANT OPTION ];
    
  • Grant the procedural language access permission to a specified role or user.
    1
    2
    3
    4
    GRANT { USAGE | ALL [ PRIVILEGES ] }
        ON LANGUAGE lang_name [, ...]
        TO { [ GROUP ] role_name | PUBLIC } [, ...]
        [ WITH GRANT OPTION ];
    

    The current version does not support granting the procedural language access permission.

  • Grant the large object access permission to a specified role or user.
    1
    2
    3
    4
    GRANT { { SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] }
        ON LARGE OBJECT loid [, ...]
        TO { [ GROUP ] role_name | PUBLIC } [, ...]
        [ WITH GRANT OPTION ];
    

    The current version does not support granting the large object access permission.

  • Grant the sub-cluster access permission to a specified role or user. Common users cannot grant or revoke the Node Group access permission.
    1
    2
    3
    4
    GRANT { CREATE | USAGE | COMPUTE | ALL [ PRIVILEGES ] }
        ON NODE GROUP group_name [, ...]
        TO { [ GROUP ] role_name | PUBLIC } [, ...]
        [ WITH GRANT OPTION ];
    
  • Grant the schema access permission to a specified role or user.
    1
    2
    3
    4
    GRANT { { CREATE | USAGE } [, ...] | ALL [ PRIVILEGES ] }
        ON SCHEMA schema_name [, ...]
        TO { [ GROUP ] role_name | PUBLIC } [, ...]
        [ WITH GRANT OPTION ];
    

    When you grant table or view rights to other users, you also need to grant the USAGE permission for the schema that the tables and views belong to. Without this permission, the users granted with the table or view rights can only see the object names, but cannot access them.

  • Grant the type access permission to a specified role or user.
    1
    2
    3
    4
    GRANT { USAGE | ALL [ PRIVILEGES ] }
        ON TYPE type_name [, ...]
        TO { [ GROUP ] role_name | PUBLIC } [, ...]
        [ WITH GRANT OPTION ];
    

    The current version does not support granting the type access permission.

  • Grant a role's rights to other users or roles.
    1
    2
    3
    GRANT role_name [, ...]
       TO role_name [, ...]
       [ WITH ADMIN OPTION ];
    
  • Grant the SYSADMIN permission to a specified role.
    1
    2
    GRANT ALL { PRIVILEGES | PRIVILEGE }
       TO role_name;
    

Parameter Description

GRANT grants the following permissions:

  • SELECT

    Allows SELECT from any column, or the specific columns listed, of the specified table, view, or sequence.

  • INSERT

    Allows INSERT of a new row into the specified table.

  • UPDATE

    Allows UPDATE of any column, or the specific columns listed, of the specified table. SELECT ... FOR UPDATE and SELECT ... FOR SHARE also require this permission on at least one column, in addition to the SELECT permission.

  • DELETE

    Allows DELETE of a row from the specified table.

  • TRUNCATE

    Allows TRUNCATE on the specified table.

  • REFERENCES

    To create a foreign key constraint, it is necessary to have this permission on both the referencing and referenced columns.

  • CREATE
    • For databases, allows new schemas to be created within the database.
    • For schemas, allows new objects to be created within the schema. To rename an existing object, you must own the object and have this permission for the schema where the object is located.
    • For sub-clusters, allows tables to be created.
  • CONNECT

    Allows the user to connect to the specified database.

  • EXECUTE

    Allows the use of the specified function and the use of any operators that are implemented on top of the function.

  • USAGE
    • For procedural languages, allows the use of the specified language for the creation of functions in that language.
    • For schemas, allows access to objects contained in the specified schema. Without this permission, it is still possible to see the object names.
    • For sequences, allows the use of the nextval function.
    • For sub-clusters, allows users who can access objects contained in the specified schema to access tables in a specified sub-cluster.
  • COMPUTE

    Allows users to perform elastic computing in a computing sub-cluster that they have the compute permission on.

  • ALL PRIVILEGES

    Grants all of the available permissions at once. Only system administrators have permission to run GRANT ALL PRIVILEGES.

GRANT parameters are as follows:

  • role_name

    Specifies an existing user name.

  • table_name

    Specifies an existing table name.

  • column_name

    Specifies an existing column name.

  • schema_name

    Specifies an existing schema name.

  • database_name

    Specifies an existing database name.

  • funcation_name

    Specifies an existing function name.

  • sequence_name

    Specifies an existing sequence name.

  • domain_name

    Specifies an existing domain type.

  • fdw_name

    Specifies an existing foreign data wrapper name.

  • lang_name

    Specifies an existing language name.

  • type_name

    Specifies an existing type name.

  • group_name

    Specifies an existing sub-cluster name.

  • argmode

    Specifies the parameter mode.

    Value range: a string. It must comply with the naming convention.

  • arg_name

    Indicates the parameter name.

    Value range: A string. It must comply with the naming convention rule.

  • arg_type

    Specifies the parameter type.

    Value range: A string. It must comply with the naming convention rule.

  • loid

    Identifier of the large object that includes this page

    Value range: A string. It must comply with the naming convention rule.

  • directory_name

    Specifies a directory name.

    Value range: a string. It must comply with the naming convention.

Examples

Example: Grant system rights to a user or role.

Create user joe and grant the sysadmin permissions to it.

1
2
CREATE USER joe PASSWORD 'Bigdata123@';
GRANT ALL PRIVILEGES TO joe;

Afterward, user joe has the sysadmin permissions.

Example: Grant object rights to a user or role.

  1. Revoke user joe's sysadmin permissions, and grant it with the usage permission of the tpcds schema and all the rights of the tpcds.reason table.
    1
    2
    3
    REVOKE ALL PRIVILEGES FROM joe;
    GRANT USAGE ON SCHEMA tpcds TO joe;
    GRANT ALL PRIVILEGES ON tpcds.reason TO joe;
    

    After the granting succeeds, user joe has all the rights of the tpcds.reason table, including addition, deletion, modification, and query rights.

  2. Grant the query permission for r_reason_sk, r_reason_id, and r_reason_desc columns and the update permission for the r_reason_desc column in the tpcds.reason table to user joe.
    1
    GRANT select (r_reason_sk,r_reason_id,r_reason_desc),update (r_reason_desc) ON tpcds.reason TO joe;
    

    After the granting succeeds, user joe immediately has the query permission of r_reason_sk and r_reason_id columns in the tpcds.reason table.

    1
    GRANT select (r_reason_sk, r_reason_id) ON tpcds.reason TO joe ;
    

    Grant the postgres database connection permission and schema creation permission to user joe.

    1
    GRANT create,connect on database postgres TO joe ;
    

    Create role tpcds_manager, grant the tpcds schema access permission and object creation permission to this role, but do not enable it to grant these permissions to others.

    1
    2
    CREATE ROLE tpcds_manager PASSWORD 'Bigdata123@';
    GRANT USAGE,CREATE ON SCHEMA tpcds TO tpcds_manager;
    

Example: Grant a user's or role's rights to other users or roles.

  1. Create role manager, grant user joe's rights to this role, and enable this role to grant its rights to others.
    1
    2
    CREATE ROLE manager PASSWORD 'Bigdata123@';
    GRANT joe TO manager WITH ADMIN OPTION;
    
  2. Create user senior_manager and grant manager's rights to it.
    1
    2
    CREATE ROLE senior_manager PASSWORD 'Bigdata123@';
    GRANT manager TO senior_manager;
    
  3. Revoke the rights and delete the user.
    1
    2
    3
    REVOKE manager FROM joe;
    REVOKE senior_manager FROM manager;
    DROP USER manager;
    

Example: Revoke the granted rights and delete the roles and users.

1
2
3
4
REVOKE USAGE,CREATE ON SCHEMA tpcds FROM tpcds_manager;
DROP ROLE tpcds_manager;
DROP ROLE senior_manager;
DROP USER joe CASCADE;

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel