> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-trino-dialect.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Check Users Assigned to Roles and Vice Versa

> Learn how to query ClickHouse's `system.role_grants` to find users assigned to roles and roles assigned to specific users.

## 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:

```sql title="Query" theme={null}
SELECT user()
```

```response title="Response" theme={null}
Query id: 9bc02d8b-ab05-4a63-b2dd-3e0093f36d31

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

1 row in set. Elapsed: 0.001 sec.
```

Create the user `foo`:

```sql title="Query" theme={null}
CREATE USER foo IDENTIFIED WITH sha256_password BY 'secretPassword123!';
```

```response title="Response" theme={null}
Query id: 9711f5fc-2b5c-43f0-a760-0c67764919a2

Ok.

0 rows in set. Elapsed: 0.102 sec.
```

Create the user `bar`:

```sql title="Query" theme={null}
CREATE USER bar IDENTIFIED WITH sha256_password BY 'secretPassword123!';
```

```response title="Response" theme={null}
Query id: 11a78bf5-f5e1-4f1d-bfe8-cf2aa0a1b15d

Ok.

0 rows in set. Elapsed: 0.103 sec.
```

Create the role `role_a`:

```sql title="Query" theme={null}
CREATE ROLE role_a;
```

```response title="Response" theme={null}
Query id: 13ccc007-fa5a-4110-9a05-48e284cea45f

Ok.

0 rows in set. Elapsed: 0.104 sec.
```

Create the role `role_b`:

```sql title="Query" theme={null}
CREATE ROLE role_b;
```

```response title="Response" theme={null}
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`:

```sql title="Query" theme={null}
GRANT role_a TO foo, bar;
```

```response title="Response" theme={null}
Query id: 4fe91624-efb3-4091-b680-b6905ab445b4

Ok.

0 rows in set. Elapsed: 0.107 sec.
```

Grant `role_b` to the user `bar`:

```sql title="Query" theme={null}
GRANT role_b TO bar;
```

```response title="Response" theme={null}
Query id: 7ea38b28-2719-4dd6-8abd-0241f7b34d5c

Ok.

0 rows in set. Elapsed: 0.102 sec.
```

Check which users have been assigned `role_a`:

```sql title="Query" theme={null}
SELECT * FROM system.role_grants WHERE granted_role_name = 'role_a';
```

```response title="Response" theme={null}
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`:

```sql title="Query" theme={null}
SELECT * FROM system.role_grants WHERE user_name IN ('foo', 'bar');
```

```response title="Response" theme={null}
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:

```sql title="Query" theme={null}
SELECT user();
```

```response title="Response" theme={null}
Query id: eee6eaaa-11bc-42c1-9258-fa3079ee6f80

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

1 row in set. Elapsed: 0.001 sec.
```

```sql title="Query" theme={null}
SHOW CURRENT ROLES;
```

```response title="Response" theme={null}
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:

```sql title="Query" theme={null}
SELECT user();
```

```response title="Response" theme={null}
Query id: fa9ba47f-efcf-4491-9b4e-2f1130dfa84b

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

1 row in set. Elapsed: 0.001 sec.
```

```sql title="Query" theme={null}
SHOW CURRENT ROLES;
```

```response title="Response" theme={null}
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.
```
