has_sequence_privilege()

A system function determining whether a user has a privilege for a sequence

has_sequence_privilege() is a is a system function determining whether a user has the specified privilege for the specified sequence.

has_sequence_privilege() was added in PostgreSQL 9.0.

Usage

has_sequence_privilege() has two forms:

  • has_sequence_privilege(sequence TEXT or OID, privilege TEXT)
  • has_tablespace_privilege(user TEXT or OID, sequence TEXT or OID, privilege TEXT)

The following privileges can be queried:

  • USAGE
  • SELECT
  • UPDATE

Change history

Examples

Determine if the current user has the USAGE privilege for the specified sequence:

postgres=# SELECT has_sequence_privilege('foo_id_seq', 'USAGE');
 has_sequence_privilege 
------------------------
 t
(1 row)

Determine if the specified user has the SELECT privilege for the specified sequence:

postgres=# SELECT has_sequence_privilege('someuser', 'foo_id_seq', 'SELECT');
 has_sequence_privilege 
------------------------
 f
(1 row)

Query multiple privileges:

postgres=# SELECT has_sequence_privilege('someuser', 'foo_id_seq', 'SELECT, UPDATE');
 has_sequence_privilege 
------------------------
 f
(1 row)

Attempting to determine an unrecognized privilege for this object type:

postgres=# SELECT has_sequence_privilege('foo_id_seq', 'CREATE');
ERROR:  unrecognized privilege type: "CREATE"

Attempting to determine privileges for a non-existent sequence:

postgres=# SELECT has_sequence_privilege('bar_id_seq', 'USAGE');
ERROR:  relation "bar_id_seq" does not exist

Categories

Sequences, System function, User management