Skip to main content
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:
Create the role, grant it SELECT, and grant the role to a new user:
Connected as reader, you can query the tables of db1:
Statements that need any other privilege are refused. This applies to INSERT, ALTER, CREATE, DROP, TRUNCATE and to tables outside db1:

Choose the scope of the grant

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 setting restricts a session to read queries, whatever privileges the user has:
With readonly set to 1 or 2, a write is refused even if the user holds the privilege for it:
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 that allows the settings they change:
Use the setting together with the role, not instead of it. readonly grants nothing by itself, and the reference 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.
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 for the password requirements. For people who work in the SQL console, assign the SQL console read only role instead of creating a database user.