Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

How Do I Grant Table Permissions to a User?

Updated on 2023-03-17 GMT+08:00

This section describes how to grant users the SELECT, INSERT, UPDATE, or full permissions of tables to users.

Syntax

1
2
3
4
5
6
GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER | ANALYZE | ANALYSE } [, ...] 
      | ALL [ PRIVILEGES ] }
    ON { [ TABLE ] table_name [, ...]
       | ALL TABLES IN SCHEMA schema_name [, ...] }
    TO { [ GROUP ] role_name | PUBLIC } [, ...] 
    [ WITH GRANT OPTION ];

Scenario

Assume there are users u1, u2, u3, u4, and u5 and five schemas named after these users. Their permission requirements are as follows:

  • User u2 is a read-only user and requires the SELECT permission for the u1.t1 table.
  • User u3 requires the SELECT permission for the u1.t1 table.
  • User u3 requires the UPDATE permission for the u1.t1 table.
  • User u5 requires all permissions of table u1.t1.

Table 1 Permissions of the u1.t1 table

User

Type

GRANT Statement

Query

Insert

Update

Delete

u1

Owner

-

u2

Read-only user

1
GRANT SELECT ON u1.t1 TO u2;

x

x

x

u3

INSERT user

1
GRANT INSERT ON u1.t1 TO u3;

x

x

x

u4

UPDATE user

1
GRANT SELECT,UPDATE ON u1.t1 TO u4;  
NOTICE:

The UPDATE permission must be granted together with the SELECT permission, or information leakage may occur.

x

x

u5

Users with all permissions

1
GRANT ALL PRIVILEGES ON u1.t1 TO u5;

Procedure

Perform the following steps to grant and verify permissions:

  1. Connect to your database as dbadmin. Run the following statements to create users u1 to u5. Five schemas will be created and named after the users by default.

    1
    2
    3
    4
    5
    CREATE USER u1 PASSWORD '{password}';
    CREATE USER u2 PASSWORD '{password}';
    CREATE USER u3 PASSWORD '{password}';
    CREATE USER u4 PASSWORD '{password}';
    CREATE USER u5 PASSWORD '{password}';
    

  1. Create table u1.t1 in schema u1.

    1
    CREATE TABLE u1.t1 (c1 int, c2 int);
    

  2. Insert two records to the table.

    1
    2
    INSERT INTO u1.t1 VALUES (1,2);
    INSERT INTO u1.t1 VALUES (1,2);
    

  3. Grant schema permissions to users.

    1
    GRANT USAGE ON SCHEMA u1 TO u2,u3,u4,u5;
    

  4. Grant user u2 the permission to query the u1.t1 table.

    1
    GRANT SELECT ON u1.t1 TO u2;
    

  5. Start a new session and connect to the database as user u2. Verify that user u2 can query the u1.t1 table but cannot write to or modify the table.

    1
    2
    3
    SELECT * FROM u1.t1;
    INSERT INTO u1.t1 VALUES (1,20);
    UPDATE u1.t1 SET c2 = 3 WHERE c1 =1;
    

  6. In the session started by user dbadmin, grant permissions to users u3, u4, and u5.

    1
    2
    3
    GRANT INSERT ON u1.t1 TO u3; -- Allow u3 to insert data.
    GRANT SELECT,UPDATE ON u1.t1 TO u4; -- Allow u4 to modify the table.
    GRANT ALL PRIVILEGES ON u1.t1 TO u5; -- Allow u5 to query, insert, modify, and delete table data.
    

  7. Start a new session and connect to the database as user u3. Verify that user u3 can query the u1.t1 table but cannot query or modify the table.

    1
    2
    3
    SELECT * FROM u1.t1;
    INSERT INTO u1.t1 VALUES (1,20);
    UPDATE u1.t1 SET c2 = 3 WHERE c1 =1;
    

  8. Start a new session and connect to the database as user u4. Verify that user u4 can modify and query the u1.t1 table, but cannot insert data to the table.

    1
    2
    3
    SELECT * FROM u1.t1;
    INSERT INTO u1.t1 VALUES (1,20);
    UPDATE u1.t1 SET c2 = 3 WHERE c1 =1;
    

  9. Start a new session and connect to the database as user u5. Verify that user u5 can query, insert, modify, and delete data in the u1.t1 table.

    1
    2
    3
    4
    SELECT * FROM u1.t1;
    INSERT INTO u1.t1 VALUES (1,20);
    UPDATE u1.t1 SET c2 = 3 WHERE c1 =1;
    DELETE FROM u1.t1;
    

  10. In the session started by user dbadmin, execute the has_table_privilege function to query user permissions.

    1
    SELECT * FROM pg_class WHERE relname = 't1';
    

    Check the relacl column in the command output. rolename=xxxx/yyyy indicates that rolename has the xxxx permission on the table and the permission is obtained from yyyy.

    The following figure shows the command output.

    • u1=arwdDxtA/u1 indicates that u1 is the owner and has full permissions.
    • u2=r/u1 indicates that u2 has the read permission.
    • u3=a/u1 indicates that u3 has the insert permission.
    • u4=rw/u1 indicates that u4 has the read and update permissions.
    • u5=arwdDxtA/u1 indicates that u5 has full permissions.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback