title | summary | toc | docs_area |
---|---|---|---|
CREATE USER |
The CREATE USER statement creates SQL users, which let you control privileges on your databases and tables. |
true |
reference.sql |
The CREATE USER
statement creates SQL users, which let you control privileges on your databases and tables.
You can use the keywords ROLE
and USER
interchangeably. CREATE USER
is equivalent to CREATE ROLE
, with one exception: CREATE ROLE
sets the NOLOGIN
role option, which prevents the new role from being used to log in to the database. You can use CREATE ROLE
and specify the LOGIN
role option to achieve the same result as CREATE USER
.
{% include {{ page.version.version }}/misc/schema-change-stmt-note.md %}
- After creating users, you must grant them privileges to databases and tables.
- All users belong to the
public
role, to which you can grant and revoke privileges. - On secure clusters, you must create client certificates for users and users must authenticate their access to the cluster.
To create other users, the user must be a member of the admin
role or have the CREATEROLE
parameter set.
Parameter | Description |
---|---|
name |
The name of the user to create. |
WITH role_option |
Apply a role option to the role. |
- Are case-insensitive.
- Must start with a letter or underscore.
- Must contain only letters, numbers, periods, or underscores.
- Must be between 1 and 63 characters.
- Cannot be
none
. - Cannot start with
pg_
orcrdb_internal
. Object names with these prefixes are reserved for system catalogs. - User and role names share the same namespace and must be unique.
{% include {{page.version.version}}/sql/role-options.md %}
Secure clusters require users to authenticate their access to databases and tables. CockroachDB offers three methods for this:
-
Client certificate and key authentication, which is available to all users. To ensure the highest level of security, we recommend only using client certificate and key authentication.
-
Password authentication, which is available to users and roles who you've created passwords for. To create a user with a password, use the
WITH PASSWORD
clause ofCREATE USER
. To add a password to an existing user, use theALTER USER
statement.Users can use passwords to authenticate without supplying client certificates and keys; however, we recommend using certificate-based authentication whenever possible.
Password creation is supported only in secure clusters.
-
GSSAPI authentication, which is available to Enterprise users.
To run the following examples, start a secure single-node cluster and use the built-in SQL shell:
$ cockroach sql --certs-dir=certs
> SHOW USERS;
username | options | member_of
---------+---------+------------
admin | | {}
root | | {admin}
(2 rows)
{{site.data.alerts.callout_info}}
The following statements are run by the root
user that is a member of the admin
role and has ALL
privileges.
{{site.data.alerts.end}}
Usernames are case-insensitive; must start with a letter or underscore; must contain only letters, numbers, periods, or underscores; and must be between 1 and 63 characters.
root@:26257/defaultdb> CREATE USER no_options;
root@:26257/defaultdb> SHOW USERS;
username | options | member_of
-------------+---------+------------
admin | | {}
no_options | | {}
root | | {admin}
(3 rows)
After creating users, you must:
- Grant them privileges to databases.
- For secure clusters, you must also create their client certificates.
root@:26257/defaultdb> CREATE USER with_password WITH LOGIN PASSWORD '$tr0nGpassW0rD' VALID UNTIL '2021-10-10';
root@:26257/defaultdb> SHOW USERS;
username | options | member_of
--------------+---------------------------------------+------------
admin | | {}
no_options | | {}
root | | {admin}
with_password | VALID UNTIL=2021-10-10 00:00:00+00:00 | {}
(4 rows)
The following statement prevents the user from using password authentication and mandates certificate-based client authentication:
{% include_cached copy-clipboard.html %}
root@:26257/defaultdb> CREATE USER no_password WITH PASSWORD NULL;
root@:26257/defaultdb> SHOW USERS;
username | options | member_of
--------------+---------------------------------------+------------
admin | | {}
no_options | | {}
no_password | | {}
root | | {admin}
with_password | VALID UNTIL=2021-10-10 00:00:00+00:00 | {}
(5 rows)
The following example allows the user to create other users and manage authentication methods for them:
root@:26257/defaultdb> CREATE USER can_create_users WITH CREATEROLE CREATELOGIN;
root@:26257/defaultdb> SHOW USERS;
username | options | member_of
-----------------+---------------------------------------+------------
admin | | {}
can_create_users | CREATELOGIN, CREATEROLE | {}
no_options | | {}
no_password | | {}
root | | {admin}
with_password | VALID UNTIL=2021-10-10 00:00:00+00:00 | {}
(6 rows)
The following example allows the user to create or rename databases:
root@:26257/defaultdb> CREATE USER can_create_db WITH CREATEDB;
root@:26257/defaultdb> SHOW USERS;
username | options | member_of
----------------------+---------------------------------------+------------
admin | | {}
can_create_db | CREATEDB | {}
can_create_users | CREATELOGIN, CREATEROLE | {}
no_options | | {}
no_password | | {}
root | | {admin}
with_password | VALID UNTIL=2021-10-10 00:00:00+00:00 | {}
(7 rows)
The following example allows the user to cancel queries and sessions for other non-admin
roles:
The following example allows the user to pause, resume, and cancel jobs:
root@:26257/defaultdb> CREATE USER can_control_job WITH CONTROLJOB;
root@:26257/defaultdb> SHOW USERS;
username | options | member_of
----------------------+---------------------------------------+------------
admin | | {}
can_control_job | CONTROLJOB | {}
can_create_db | CREATEDB | {}
can_create_users | CREATELOGIN, CREATEROLE | {}
no_options | | {}
no_password | | {}
root | | {admin}
with_password | VALID UNTIL=2021-10-10 00:00:00+00:00 | {}
(8 rows)
The following example allows the user to cancel queries and sessions for other non-admin
roles:
root@:26257/defaultdb> CREATE USER can_manage_queries WITH CANCELQUERY VIEWACTIVITY;
root@:26257/defaultdb> SHOW USERS;
username | options | member_of
----------------------+---------------------------------------+------------
admin | | {}
can_control_job | CONTROLJOB | {}
can_create_db | CREATEDB | {}
can_create_users | CREATELOGIN, CREATEROLE | {}
can_manage_queries | CANCELQUERY, VIEWACTIVITY | {}
no_options | | {}
no_password | | {}
root | | {admin}
with_password | VALID UNTIL=2021-10-10 00:00:00+00:00 | {}
(9 rows)
The following example allows the user to run CREATE CHANGEFEED
:
root@:26257/defaultdb> CREATE USER can_control_changefeed WITH CONTROLCHANGEFEED;
root@:26257/defaultdb> SHOW USERS;
username | options | member_of
-----------------------+---------------------------------------+------------
admin | | {}
can_control_changefeed | CONTROLCHANGEFEED | {}
can_control_job | CONTROLJOB | {}
can_create_db | CREATEDB | {}
can_create_users | CREATELOGIN, CREATEROLE | {}
can_manage_queries | CANCELQUERY, VIEWACTIVITY | {}
no_options | | {}
no_password | | {}
root | | {admin}
with_password | VALID UNTIL=2021-10-10 00:00:00+00:00 | {}
(10 rows)
The following example allows the user to modify cluster settings:
root@:26257/defaultdb> CREATE USER can_modify_cluster_setting WITH MODIFYCLUSTERSETTING;
root@:26257/defaultdb> SHOW USERS;
username | options | member_of
---------------------------+---------------------------------------+------------
admin | | {}
can_control_changefeed | CONTROLCHANGEFEED | {}
can_control_job | CONTROLJOB | {}
can_create_db | CREATEDB | {}
can_create_users | CREATELOGIN, CREATEROLE | {}
can_manage_queries | CANCELQUERY, VIEWACTIVITY | {}
can_modify_cluster_setting | MODIFYCLUSTERSETTING | {}
no_options | | {}
no_password | | {}
root | | {admin}
with_password | VALID UNTIL=2021-10-10 00:00:00+00:00 | {}
(11 rows)