> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How do I create a read-only user or role?

> Create a role that has only SELECT privileges and grant it to a user. Optionally add the readonly setting so that writes are refused whatever is granted later.

A user can do only what it has been granted, so a read-only user is a user whose only privilege is `SELECT`. Create a role with `SELECT` on the data that may be read and grant the role to the user. If writes must be refused no matter which privileges the user receives later, also add the `readonly` setting to the user.

## Create a read-only role and user

The example uses this table:

```sql theme={null}
CREATE DATABASE db1;

CREATE TABLE db1.table_a
(
    id UInt32,
    name String
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO db1.table_a VALUES (1, 'one'), (2, 'two');
```

Create the role, grant it `SELECT`, and grant the role to a new user:

```sql theme={null}
CREATE ROLE read_only;
GRANT SELECT ON db1.* TO read_only;

CREATE USER reader IDENTIFIED WITH sha256_password BY '<password>';
GRANT read_only TO reader;
```

Connected as `reader`, you can query the tables of `db1`:

```sql theme={null}
SELECT * FROM db1.table_a ORDER BY id;
```

```text theme={null}
   ┌─id─┬─name─┐
1. │  1 │ one  │
2. │  2 │ two  │
   └────┴──────┘
```

Statements that need any other privilege are refused. This applies to `INSERT`, `ALTER`, `CREATE`, `DROP`, `TRUNCATE` and to tables outside `db1`:

```sql theme={null}
INSERT INTO db1.table_a VALUES (3, 'three');
```

```text theme={null}
Code: 497. DB::Exception: reader: Not enough privileges. To execute this query, it's necessary to have the grant INSERT(id, name) ON db1.table_a. (ACCESS_DENIED)
```

## Choose the scope of the grant

| `ON` clause | What the role can read |
| - | - |
| `ON db1.table_a` | One table |
| `ON db1.*` | All tables of the database `db1`, including tables created later |
| `ON *.*` | All tables of all databases |
| `ON *` | All tables of the current database of the session that runs the `GRANT` statement |

`ON *` does not mean all databases, and `ON default.*` covers only the database named `default`. To let the role read several databases, repeat the grant for each database or use `*.*`. Keep in mind that `*.*` includes the `system` database, with tables such as `system.query_log` that contain the queries of all users.

The user does not need a separate `SHOW` grant: a user with `SELECT` on a table also sees the table and its database in `SHOW TABLES` and `SHOW DATABASES`.

## Add the `readonly` setting

The [`readonly`](https://clickhouse.com/docs/concepts/features/configuration/settings/permissions-for-queries#readonly) setting restricts a session to read queries, whatever privileges the user has:

```sql theme={null}
ALTER USER reader SETTINGS readonly = 1;
```

| Value | Effect |
| - | - |
| `0` | No restriction (the default) |
| `1` | Only read queries are allowed, and settings cannot be changed |
| `2` | Only read queries are allowed, and settings can be changed, except `readonly` itself |

With `readonly` set to `1` or `2`, a write is refused even if the user holds the privilege for it:

```text theme={null}
Code: 164. DB::Exception: reader: Cannot execute query in readonly mode. (READONLY)
```

With `readonly = 1`, a statement such as `SET max_threads = 2`, or a query with a `SETTINGS` clause, fails with `Cannot modify 'max_threads' setting in readonly mode`. Clients and tools that send settings with their queries therefore need `readonly = 2`, or `readonly = 1` together with a [constraint](https://clickhouse.com/docs/concepts/features/configuration/settings/constraints-on-settings.md) that allows the settings they change:

```sql theme={null}
ALTER USER reader SETTINGS readonly = 1, max_execution_time CHANGEABLE_IN_READONLY;
```

Use the setting together with the role, not instead of it. `readonly` grants nothing by itself, and the [reference](https://clickhouse.com/docs/concepts/features/configuration/settings/permissions-for-queries.md#readonly) lists statements that it does not restrict.

You can also attach the setting to the role (`ALTER ROLE read_only SETTINGS readonly = 1`). The settings of a role are applied when a user logs in with this role as one of its default roles.

> [!NOTE]
>
> Changing session settings with `SET` needs no privilege. The `ALTER SETTINGS` privilege is unrelated: it allows `ALTER TABLE ... MODIFY SETTING` and does not belong in a read-only role.

## ClickHouse Cloud

In ClickHouse Cloud you create database users and roles with the same statements; see [Manage database users](https://clickhouse.com/docs/products/cloud/guides/security/cloud-access-management/manage-database-users.md) for the password requirements. For people who work in the SQL console, assign the [SQL console read only](https://clickhouse.com/docs/products/cloud/reference/security/console-roles.md#sql-console-roles) role instead of creating a database user.

## Related documentation

* [`GRANT`](https://clickhouse.com/docs/reference/statements/grant)
* [`CREATE USER`](https://clickhouse.com/docs/reference/statements/create/user) and [`CREATE ROLE`](https://clickhouse.com/docs/reference/statements/create/role)
* [Permissions for queries](https://clickhouse.com/docs/concepts/features/configuration/settings/permissions-for-queries.md)
* [Access control and account management](https://clickhouse.com/docs/concepts/features/security/access-rights.md)
