Skip to content

Users & privileges: CREATE USER, GRANT, REVOKE

View as Markdown

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.

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 and are stored in uppercase; db_user lists them.

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:

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:

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 changes the password or the comment of a user. Users other than DBA and the DBA group can change only their own password.

ALTER USER user_name PASSWORD 'password' [ COMMENT 'comment' ];
ALTER USER user_name COMMENT 'comment';
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 user_name;

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

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

Change the owner of those objects first with ALTER ... OWNER TO.

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

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

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:

-- connected as alice
SELECT COUNT(*) FROM orders; -- 2910
UPDATE orders SET shipping_cost = 0 WHERE id = 1;
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.

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

REVOKE privilege [, privilege ...] ON table_name [, table_name ...]
FROM user_name [, user_name ...];
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.

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

ALTER { TABLE | CLASS | VIEW | VCLASS | TRIGGER | FUNCTION | PROCEDURE } name OWNER TO user_name;
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.

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:

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.
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.