Redshift Database SQL Query for Users with Specific Table Permissions
As Amazon Redshift Data Warehouse administrators, frequently we require to query the users list who has specific privileges like read, write or delete permissions on a Redshift database table. In this Redshift tutorial for SQL developers, I want to share SQL codes where PostgreSQL access privilege inquiry functions are used like has_schema_privilege and has_table_privilege.
PostgreSQL provides more system information functions for administrators and SQL programmers building and running applications on top of it as a data platform. Since Amazon Redshift DWH analytical platform is also build on PostgreSQL like many other databases, Redshift database administrator and SQL programmers can use these PostgreSQL functions too.
Redshift Database Permissions SQL Query Functions
First of all, I want to give brief information about PostgreSQL access privilege inquiry functions. These SQL functions enable database users to query object access privileges programmatically by using SQL. Below list contains a few of the system information functions that can be used for querying specific permissions on Redshift database schema and table objects.
has_table_privilege returns a boolean value indicating that the user queried has the privilege on the table.
Here is a sample usage in SQL code
The output is as seen in below screenshot
Another build-in PostgreSQL system function which can be easily used on Amazon Redshift SQL developments is has_schema_privilege.
has_database_privilege is one step broader of the has_schema_privilege SQL function.
As in the sample SQL code I shared for has_table_privilege, I passed select, insert, update, delete and references privileges as an argument to the function.
For has_database_privilege, PostgreSQL database developers can use the "connect" privilege if the user has authorization to establish a connection to a target database.
So for the PostgreSQL developer, I would suggest them to check the official documentation for the Grant command. There they will find the possible privileges and their meanings according to the usage area.
Unfortunately, not every privilege is not available for theses permission querying functions on Amazon Redshift.
For Redshift, SQL developers can refer to official documentation.
has_database_privilege has create, temp and temporary pirivileges defined.
Create: permission to create new schema under target database
Temp, Temporary: privilege to create temporary tables on that Redshift database
has_schema_privilege has the create and usage privileges.
Create: permission to create new database objects under selected database schema.
Usage: lookup permission for database user to access objects created under that database schema
has_table_privilege can be used with select, insert, update, delete and references privileges.
Select: permission to select table column data
Insert: permission to add a new database table row
Update: privilege for updating the contents of the records of a table
Delete: privilege to delete data from a table
References: permission to create a foreign key constraint
Another permission check function is has_assumerole_privilege which is used for identifying if the user has the specified IAM role privilege.
Here is a sample SQL code for executing has_database_privilege premission check function on Amazon Redshift database
The returned output information of the above SQL query on Redshift database is:
For a specific Redshift database user's permissions on a specific schema, SQL function has_schema_privilege is used as follows
Above SQL code returns following information about the test0001 user permissions on spectrumdb schema:
Query Redshift Database Users for Specific Permissions on Selected Tables
All above SQL code sample queries are for specific users and for specific database and database objects.
If you need a list of users or list database objects list, we can combine the SQL query with pg_user database users table and pg_tables database tables table.
These system tables can be combined with above samples as shown in below common table expression, CTE query
This SQL CTE query returns the list of all Redshift database users with specific permissions (in this case Read permission or "select" privilege) on a given Redshift database table.
If SQL developers run above SELECT statement on an Amazon Redshift database by customizing the WHERE clause of the CTE expression according to their environment, they will get the list of all Redshift database users created on that server with "select" privilege on targeted schema table.