Skip to content
ClickHouse Docs
ClickHouse DocsClickHouse Docs

How to Check Users Assigned to Roles and Vice Versa

Question

How to check users assigned to roles and vice versa?

Answer

Log in as the default user, which has administrator privileges, and check the current user:

Querysql
SELECT user()
Responseresponse
Query id: 9bc02d8b-ab05-4a63-b2dd-3e0093f36d31

┌─currentUser()─┐
│ default       │
└───────────────┘

1 row in set. Elapsed: 0.001 sec.

Create the user foo:

Querysql
CREATE USER foo IDENTIFIED WITH sha256_password BY 'secretPassword123!';
Responseresponse
Query id: 9711f5fc-2b5c-43f0-a760-0c67764919a2

Ok.

0 rows in set. Elapsed: 0.102 sec.

Create the user bar:

Querysql
CREATE USER bar IDENTIFIED WITH sha256_password BY 'secretPassword123!';
Responseresponse
Query id: 11a78bf5-f5e1-4f1d-bfe8-cf2aa0a1b15d

Ok.

0 rows in set. Elapsed: 0.103 sec.

Create the role role_a:

Querysql
CREATE ROLE role_a;
Responseresponse
Query id: 13ccc007-fa5a-4110-9a05-48e284cea45f

Ok.

0 rows in set. Elapsed: 0.104 sec.

Create the role role_b:

Querysql
CREATE ROLE role_b;
Responseresponse
Query id: 43f84376-76fa-4cd2-b8e2-2dcfbe41ec1b

Ok.

0 rows in set. Elapsed: 0.103 sec.

Grant role_a to the users foo and bar:

Querysql
GRANT role_a TO foo, bar;
Responseresponse
Query id: 4fe91624-efb3-4091-b680-b6905ab445b4

Ok.

0 rows in set. Elapsed: 0.107 sec.

Grant role_b to the user bar:

Querysql
GRANT role_b TO bar;
Responseresponse
Query id: 7ea38b28-2719-4dd6-8abd-0241f7b34d5c

Ok.

0 rows in set. Elapsed: 0.102 sec.

Check which users have been assigned role_a:

Querysql
SELECT * FROM system.role_grants WHERE granted_role_name = 'role_a';
Responseresponse
Query id: bf088776-f450-4150-b2e8-197b400573c1

┌─user_name─┬─role_name─┬─granted_role_name─┬─granted_role_is_default─┬─with_admin_option─┐
│ bar       │ ᴺᵁᴸᴸ      │ role_a            │                       1 │                 0 │
│ foo       │ ᴺᵁᴸᴸ      │ role_a            │                       1 │                 0 │
└───────────┴───────────┴───────────────────┴─────────────────────────┴───────────────────┘

2 rows in set. Elapsed: 0.001 sec.

Check which roles have been assigned to the users foo and bar:

Querysql
SELECT * FROM system.role_grants WHERE user_name IN ('foo', 'bar');
Responseresponse
Query id: b81dbe1c-42f0-43bd-b237-1a6b1d81ae3d

┌─user_name─┬─role_name─┬─granted_role_name─┬─granted_role_is_default─┬─with_admin_option─┐
│ bar       │ ᴺᵁᴸᴸ      │ role_b            │                       1 │                 0 │
│ bar       │ ᴺᵁᴸᴸ      │ role_a            │                       1 │                 0 │
│ foo       │ ᴺᵁᴸᴸ      │ role_a            │                       1 │                 0 │
└───────────┴───────────┴───────────────────┴─────────────────────────┴───────────────────┘

3 rows in set. Elapsed: 0.001 sec.

Log in as the user foo, then check the current user and roles:

Querysql
SELECT user();
Responseresponse
Query id: eee6eaaa-11bc-42c1-9258-fa3079ee6f80

┌─currentUser()─┐
│ foo           │
└───────────────┘

1 row in set. Elapsed: 0.001 sec.
Querysql
SHOW CURRENT ROLES;
Responseresponse
Query id: aa6a1ac1-3502-4960-bb34-f7d9f0d7986e

┌─role_name─┬─with_admin_option─┬─is_default─┐
│ role_a    │                 0 │          1 │
└───────────┴───────────────────┴────────────┘

1 row in set. Elapsed: 0.002 sec.

Log in as the user bar, then check the current user and roles:

Querysql
SELECT user();
Responseresponse
Query id: fa9ba47f-efcf-4491-9b4e-2f1130dfa84b

┌─currentUser()─┐
│ bar           │
└───────────────┘

1 row in set. Elapsed: 0.001 sec.
Querysql
SHOW CURRENT ROLES;
Responseresponse
Query id: fb3f2941-a8ce-481d-8fad-b775bfc5b532

┌─role_name─┬─with_admin_option─┬─is_default─┐
│ role_a    │                 0 │          1 │
│ role_b    │                 0 │          1 │
└───────────┴───────────────────┴────────────┘

2 rows in set. Elapsed: 0.001 sec.
Navigation