Cloud

has_database_privilege

The has_database_privilege() function is an access privilege inquiry function that checks whether a user has specific privileges on a database. It checks the named user (or user_id) when you provide one, and the current user when you omit it.

Syntax

The has_database_privilege() function has three available syntax versions:

SELECT has_database_privilege('user', 'database', 'privilege');
SELECT has_database_privilege(user_id, 'database', 'privilege');
SELECT has_database_privilege('database', 'privilege');

The function returns a boolean value: TRUE if the user has the specified privilege, FALSE otherwise.

Parameters

  • database: Name of the database to check privileges for (can be any string value or string columns from other tables).

  • user: Name of the user who has the privileges (can be any string value).

  • user_id: OID of the user who has the privileges (can be any integer value), as an alternative to user.

  • privilege: Specifies the privilege to check for on the database.

The comparison for the privilege is case-insensitive, so you can use lowercase or uppercase notation for the privilege name.

Examples

Check the current user’s privilege on a database

This example uses the has_database_privilege() function to determine whether the current user has a privilege on the redpanda database, without naming a user explicitly:

SELECT has_database_privilege('redpanda', 'connect');

The query returns TRUE, which means that the current user has the connect privilege on the redpanda database.

 has_database_privilege
------------------------
 t

Check another user’s privilege on a database

Use the has_database_privilege() function to check a named user’s privilege on a database:

SELECT has_database_privilege('cahyo', 'redpanda', 'connect');

The query returns TRUE, which means the named user has the connect privilege on the redpanda database.

 has_database_privilege
------------------------
 t