> ## 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 to create a user and role with full access to a single database in ClickHouse Cloud

> Create a role that holds every privilege you can grant on one database with GRANT CURRENT GRANTS, assign it to a user, and check what the user can and cannot do.

To give a user full access to one database and nothing else, create a role and run `GRANT CURRENT GRANTS ON <database>.* TO <role>`. The role receives every privilege that you hold yourself with the grant option and that can be limited to a database. You do not have to list the privileges, and `GRANT ALL`, which is [not supported in ClickHouse Cloud](https://clickhouse.com/docs/reference/statements/grant.md#all), is not needed.

## Create the role and the user

Run the statements as the `default` user or as another user with `default_role`, and replace `<password>` with a strong password:

```sql theme={null}
CREATE DATABASE IF NOT EXISTS app_db;

CREATE ROLE app_db_admin;
GRANT CURRENT GRANTS ON app_db.* TO app_db_admin;

CREATE USER app_user IDENTIFIED WITH sha256_password BY '<password>';
GRANT app_db_admin TO app_user;
```

All roles granted to a new user are its default roles, so `app_db_admin` is active as soon as `app_user` connects:

```sql theme={null}
SHOW GRANTS FOR app_user;
```

```text theme={null}
GRANT app_db_admin TO app_user
```

To see the privileges that the role received, run:

```sql theme={null}
SHOW GRANTS FOR app_db_admin;
```

The result is a single `GRANT ... ON app_db.* TO app_db_admin` statement. Its list of privileges depends on the privileges of the user that ran `GRANT CURRENT GRANTS`. It includes `SHOW`, `SELECT`, `INSERT`, `ALTER`, `CREATE TABLE`, `DROP TABLE`, `TRUNCATE` and `OPTIMIZE` if that user holds them with the grant option.

## Check the access

Connect as `app_user`. Creating, changing, reading and dropping tables in `app_db` works:

```sql theme={null}
CREATE TABLE app_db.events (id UInt64, message String) ENGINE = MergeTree ORDER BY id;
INSERT INTO app_db.events VALUES (1, 'created'), (2, 'updated');
ALTER TABLE app_db.events ADD COLUMN level String DEFAULT 'info';
SELECT * FROM app_db.events ORDER BY id;
```

```text theme={null}
   ┌─id─┬─message─┬─level─┐
1. │  1 │ created │ info  │
2. │  2 │ updated │ info  │
   └────┴─────────┴───────┘
```

```sql theme={null}
DROP TABLE app_db.events;
```

Anything outside the database is refused:

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

The statement fails with the `ACCESS_DENIED` error: `Not enough privileges. To execute this query, it's necessary to have the grant CREATE DATABASE ON other_db.*`.

## If the user cannot create tables

On a server that requires grants for table engines, `CREATE TABLE` fails for the new user with `Not enough privileges. To execute this query, it's necessary to have the grant TABLE ENGINE ON MergeTree`. The [`TABLE ENGINE`](https://clickhouse.com/docs/reference/statements/grant#table-engine) privilege is granted per engine name, not per database, so `GRANT CURRENT GRANTS ON app_db.*` does not include it. Grant it to the role separately, for all engines or for the engines the user needs:

```sql theme={null}
GRANT TABLE ENGINE ON * TO app_db_admin;
```

## What the role can and cannot do

* The role has no privileges that cannot be limited to a database, even if you hold them: for example creating users and roles, `KILL QUERY`, `CREATE FUNCTION`, or reading from external sources such as `S3` and `URL`. Grant them separately if the user needs them.
* The role can drop `app_db` itself and create it again. To prevent this, run `REVOKE DROP DATABASE ON app_db.* FROM app_db_admin`.
* The privileges are copied when you run the statement. If you get more privileges later, run `GRANT CURRENT GRANTS ON app_db.* TO app_db_admin` again to add them to the role. Running it again only adds privileges. A privilege that you have lost stays on the role until a user that holds it with the grant option revokes it, for example with `REVOKE DROP TABLE ON app_db.* FROM app_db_admin`.
* Grants refer to the database by name. They stay in place if the database is dropped and created again.
* To let the user grant these privileges to other users and roles, add `WITH GRANT OPTION` to the `GRANT CURRENT GRANTS` statement.

## Related pages

* [`GRANT` statement](https://clickhouse.com/docs/reference/statements/grant#grant-current-grants-syntax)
* [Manage database users](https://clickhouse.com/docs/products/cloud/guides/security/cloud-access-management/manage-database-users.md)
* [Common access management queries](https://clickhouse.com/docs/products/cloud/guides/security/cloud-access-management/common-access-management-queries.md)
* [Common RBAC queries](https://clickhouse.com/docs/resources/support-center/knowledge-base/security/common-rbac-queries.md)
