---
title: "Users & privileges: CREATE USER, GRANT, REVOKE"
description: "Manage CoraDB users and groups with CREATE, ALTER and DROP USER, grant and revoke privileges, and change object owners with OWNER TO."
url: "https://www.coradb.ai/manual/sql/authorization.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/authorization.md"
product: "CoraDB"
version: "1.0"
keywords: "coradb create user, alter user, drop user, grant, revoke, with grant option, privileges, dba, public, groups, members, owner to, change owner"
---

# Users & privileges: CREATE USER, GRANT, REVOKE

CoraDB controls access with database users, groups and table privileges. A user who creates a table owns it and has all privileges on it; other users need privileges granted with **GRANT**.

## Database users

Every database has two built-in users, **DBA** and **PUBLIC**, both without a password when the database is created.

- **PUBLIC** is a group that every user belongs to. A privilege granted to **PUBLIC** is granted to all users.
- **DBA** is the database administrator. **DBA** and the members of the **DBA** group have every privilege on every object and do not need to be granted anything.

A user can also act as a group: a privilege granted to a user is also held by all its members, directly or through intermediate groups. User names follow the rules for [identifiers](https://www.coradb.ai/manual/sql/identifier.html) and are stored in uppercase; [db_user](https://www.coradb.ai/manual/sql/catalog_classes.html#db_user) lists them.


## CREATE USER

```sql
CREATE USER user_name
    [ PASSWORD 'password' ]
    [ GROUPS group_name [, group_name ...] ]
    [ MEMBERS user_name [, user_name ...] ]
    [ COMMENT 'comment' ];
```

- **PASSWORD**: the password; without it, the user has no password.
- **GROUPS**: existing users (groups) the new user becomes a member of.
- **MEMBERS**: existing users who become members of the new user.
- **COMMENT**: a description of the user.

Only **DBA** and members of the **DBA** group can create, change and drop other users.

The following statements create two groups, `sales` and `support`, and two users who belong to them; `ops` is created as a group that has both users as members:

```sql
CREATE USER sales;
CREATE USER support;
CREATE USER alice PASSWORD 'Secret#1' GROUPS sales COMMENT 'sales analyst';
CREATE USER bob GROUPS sales, support;
CREATE USER ops MEMBERS alice, bob;
```

The groups of each user can be listed from `db_user`. Every user is also a member of **PUBLIC**:

```sql
SELECT u.name, g.name AS group_name
FROM db_user u, TABLE(u.direct_groups) AS t(g)
WHERE u.name IN ('ALICE', 'BOB')
ORDER BY 1, 2;
```

| name | group_name |
|---|---|
| ALICE | OPS |
| ALICE | PUBLIC |
| ALICE | SALES |
| BOB | OPS |
| BOB | PUBLIC |
| BOB | SALES |
| BOB | SUPPORT |


## ALTER USER

**ALTER USER** changes the password or the comment of a user. Users other than **DBA** and the **DBA** group can change only their own password.

```sql
ALTER USER user_name PASSWORD 'password' [ COMMENT 'comment' ];
ALTER USER user_name COMMENT 'comment';
```

```sql
ALTER USER alice PASSWORD 'N3w#pass';
ALTER USER alice COMMENT 'senior sales analyst';
ALTER USER bob PASSWORD '';      -- removes the password
```

The comments are shown by `SELECT name, comment FROM db_user`.


## DROP USER

```sql
DROP USER user_name;
```

A user who owns tables, views, triggers, serials or stored procedures cannot be dropped:

```text
ERROR: Cannot drop the user who owns database objects(class/trigger/serial etc).
```

Change the owner of those objects first with [ALTER ... OWNER TO](https://www.coradb.ai/manual/sql/authorization.html#alter--owner).


## GRANT

The unit of privilege in CoraDB is a table (or view). **GRANT** gives users and groups privileges on one or more tables:

```sql
GRANT privilege [, privilege ...] ON table_name [, table_name ...]
TO user_name [, user_name ...] [ WITH GRANT OPTION ];
```

| Privilege | Allows |
|---|---|
| **SELECT** | Reading rows and the table definition |
| **INSERT** | Inserting rows |
| **UPDATE** | Updating rows |
| **DELETE** | Deleting rows |
| **ALTER** | Changing, renaming or dropping the table |
| **INDEX** | Creating and dropping indexes on the table |
| **EXECUTE** | Calling methods of the table |
| **ALL PRIVILEGES** or **ALL** | All of the above |

- The grantee can be a user, a group or **PUBLIC**. Members of a group receive the group's privileges.
- **WITH GRANT OPTION** lets the grantee grant the same privileges to others.
- To grant a privilege, a user must own the table or hold the privilege **WITH GRANT OPTION**; otherwise **GRANT** fails with `No GRANT option.`
- Privileges on columns are not supported: `GRANT UPDATE (shipped_date) ON orders …` is rejected. The keywords **ADD**, **DROP** and **REFERENCES** are accepted for compatibility but grant nothing.
- Vertex and edge tables are granted like other tables.

```sql
GRANT SELECT ON orders, order_items TO sales;
GRANT SELECT, UPDATE ON orders TO support WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON order_items TO ops;
```

The privileges are listed in [db_auth](https://www.coradb.ai/manual/sql/catalog_views.html#db_auth):

```sql
SELECT grantee_name, class_name, auth_type, is_grantable
FROM db_auth
WHERE grantee_name IN ('SALES', 'SUPPORT')
ORDER BY 1, 2, 3;
```

| grantee_name | class_name | auth_type | is_grantable |
|---|---|---|---|
| SALES | order_items | SELECT | NO |
| SALES | orders | SELECT | NO |
| SUPPORT | orders | SELECT | YES |
| SUPPORT | orders | UPDATE | YES |

`alice`, a member of `sales`, can now read `orders` but not change it:

```sql
-- connected as alice
SELECT COUNT(*) FROM orders;                         -- 2910
UPDATE orders SET shipping_cost = 0 WHERE id = 1;
```

```text
ERROR: UPDATE is not authorized on orders.
```

The catalog views show `alice` only the tables she can read; `SELECT class_name FROM db_class WHERE is_system_class = 'NO'` returns `orders` alone.

> **Note:**
> - **TRUNCATE** requires the **DELETE**, **ALTER** and **INDEX** privileges on the table.
> - A view reads its tables with the privileges of the view's owner. A user granted **SELECT** on a view can read it without any privilege on the underlying tables.



## REVOKE

**REVOKE** takes privileges back. Any subset of the privileges granted earlier can be revoked, for any subset of the users and tables.

```sql
REVOKE privilege [, privilege ...] ON table_name [, table_name ...]
FROM user_name [, user_name ...];
```

```sql
REVOKE UPDATE ON orders FROM support;            -- support keeps SELECT
REVOKE ALL PRIVILEGES ON order_items FROM ops;   -- ops keeps nothing on order_items
```

Revoking a privilege from a user does not revoke the privileges that user granted to others with **WITH GRANT OPTION**. If `support` granted **SELECT** on `orders` to `bob`, `bob` keeps it after `REVOKE SELECT ON orders FROM support`; revoke it from `bob` separately.


## ALTER ... OWNER TO

**DBA** and members of the **DBA** group can change the owner of tables, views, triggers and stored functions and procedures:

```sql
ALTER { TABLE | CLASS | VIEW | VCLASS | TRIGGER | FUNCTION | PROCEDURE } name OWNER TO user_name;
```

```sql
ALTER TABLE coupon OWNER TO ops;
ALTER VIEW open_orders OWNER TO ops;
ALTER TRIGGER trg_coupon OWNER TO ops;
```

The new owner has all privileges on the object; the owner is shown in `db_class.owner_name`, `db_trigger.owner` and `db_stored_procedure.owner`.


## Authorization methods

The system classes `db_user` and `db_authorizations` have methods that perform the same tasks as the statements above. They are called with **CALL**, and a result can be stored in a session variable with **TO**:

```sql
CALL method(arguments) ON CLASS class_name [ TO variable ];
CALL method(arguments) ON variable;
```

| Method | Called on | Effect |
|---|---|---|
| `login('user', 'password')` | `CLASS db_user` | Switches the session to another user. **DBA** and the **DBA** group can switch without a password. |
| `add_user('user', 'password')` | `CLASS db_user` | Creates a user (**DBA** only) and returns it. |
| `drop_user('user')` | `CLASS db_user` | Drops a user (**DBA** only). |
| `find_user('user')` | `CLASS db_user` | Returns the user, or **NULL** if it does not exist. |
| `set_password('password')` | A user object | Changes that user's password. |
| `change_owner('table', 'user')` | `CLASS db_authorizations` | Changes the owner of a table (**DBA** only), like **ALTER TABLE … OWNER TO**. |

```sql
CALL add_user ('user_4', '') ON CLASS db_user TO u;
CALL set_password ('abcdef') ON u;

CALL find_user ('user_4') ON CLASS db_user TO x;
SELECT x.name FROM db_root;            -- USER_4

CALL find_user ('nobody') ON CLASS db_user TO y;
SELECT y FROM db_root;                 -- NULL

CALL drop_user ('user_4') ON CLASS db_user;
```

`db_root` is a system table with exactly one row, convenient for reading session variables.
