CREATE USER
Introduced or updated: v1.2.424
Creates a SQL user.
See also:
Syntax
CREATE [ OR REPLACE ] USER <name> IDENTIFIED [ WITH <auth_type> ] BY '<password>'
[ WITH SET PASSWORD POLICY = '<policy_name>' ] -- Set password policy
[ WITH SET NETWORK POLICY = '<policy_name>' ] -- Set network policy
[ WITH DEFAULT_ROLE = '<role_name>' ] -- Set default role
[ WITH DISABLED = true | false ] -- User created in a disabled state
- auth_type can be
double_sha1_password
(default),sha256_password
orno_password
. - When you set a default role for a user using CREATE USER or ALTER USER, Databend does not verify the role's existence or automatically grant the role to the user. You must explicitly grant the role to the user for the role to take effect.
- When
DISABLED
is set totrue
, the new user is created in a disabled state. Users in this state cannot log in to Databend until they are enabled. To enable or disable a created user, use the ALTER USER command.
Examples
Example 1: Creating User with Default auth_type
CREATE USER user1 IDENTIFIED BY 'abc123';
SHOW USERS;
+-----------+----------+----------------------+---------------+
| name | hostname | auth_type | is_configured |
+-----------+----------+----------------------+---------------+
| user1 | % | double_sha1_password | NO |
+-----------+----------+----------------------+---------------+
Example 2: Creating User with sha256_password auth_type
CREATE USER user1 IDENTIFIED WITH sha256_password BY 'abc123';
SHOW USERS;
+-----------+----------+----------------------+---------------+
| name | hostname | auth_type | is_configured |
+-----------+----------+----------------------+---------------+
| user1 | % | sha256_password | NO |
+-----------+----------+----------------------+---------------+
Example 3: Creating User with Network Policy
CREATE USER user1 IDENTIFIED BY 'abc123' WITH SET NETWORK POLICY='test_policy';
SHOW USERS;
+-----------+----------+----------------------+---------------+
| name | hostname | auth_type | is_configured |
+-----------+----------+----------------------+---------------+
| user1 | % | double_sha1_password | NO |
+-----------+----------+----------------------+---------------+
Example 4: Creating User with Default Role
- Create a user named 'user1' with the default role set to 'manager':
Connect as user "root":
SHOW ROLES;
┌───────────────────────────────────────────────────────────┐
│ name │ inherited_roles │ is_current │ is_default │
│ String │ UInt64 │ Boolean │ Boolean │
├───────────────┼─────────────────┼────────────┼────────────┤
│ account_admin │ 0 │ true │ true │
│ developer │ 0 │ false │ false │
│ public │ 0 │ false │ false │
└───────────────────────────────────────────────────────────┘
CREATE USER user1 IDENTIFIED BY 'abc123' WITH DEFAULT_ROLE = 'manager';
GRANT ROLE developer TO user1;
- Verify the default role of user "user1" using the SHOW ROLES command:
Connect as user "user1":
eric@Erics-iMac ~ % bendsql --user user1 --password abc123
Welcome to BendSQL 0.9.3-db6b232(2023-10-26T12:36:55.578667000Z).
Connecting to localhost:8000 as user user1.
Connected to DatabendQuery v1.2.271-nightly-0598a77b9c(rust-1.75.0-nightly-2023-12-26T11:29:04.266265000Z)
user1@localhost:8000/default> SHOW ROLES;
SHOW ROLES
┌───────────────────────────────────────────────────────┐
│ name │ inherited_roles │ is_current │ is_default │
│ String │ UInt64 │ Boolean │ Boolean │
├───────────┼─────────────────┼────────────┼────────────┤
│ developer │ 0 │ true │ true │
│ public │ 0 │ false │ false │
└───────────────────────────────────────────────────────┘
2 rows read in 0.015 sec. Processed 0 rows, 0 B (0 rows/s, 0 B/s)
Example 5: Creating User in Disabled State
This example creates a user named 'u1' in a disabled state, preventing login access. After enabling the user using the ALTER USER command, login access is restored.
- Create a user named 'u1' in the disabled state:
CREATE USER u1 IDENTIFIED BY '123' WITH DISABLED = TRUE;
SHOW USERS;
┌─────────────────────────────────────────────────────────────────────────────────────┐
│ name │ hostname │ auth_type │ is_configured │ default_role │ disabled │
├────────┼──────────┼──────────────────────┼───────────────┼───────────────┼──────────┤
│ root │ % │ no_password │ YES │ account_admin │ false │
│ u1 │ % │ double_sha1_password │ NO │ │ true │
└─────────────────────────────────────────────────────────────────────────────────────┘
- Attempt to connect to Databend using BendSQL as user 'u1', resulting in an authentication error:
➜ ~ bendsql --user u1 --password 123
Welcome to BendSQL 0.16.0-homebrew.
Connecting to localhost:8000 as user u1.
Error: APIError: RequestError: Start Query failed with status 401 Unauthorized: {"error":{"code":"401","message":"AuthenticateFailure: user u1 is disabled. Not allowed to login"}}
- Enable the user 'u1' with the ALTER USER command:
ALTER USER u1 WITH DISABLED = FALSE;
- Re-attempt connection to Databend as user 'u1', confirming successful login access:
➜ ~ bendsql --user u1 --password 123
Welcome to BendSQL 0.16.0-homebrew.
Connecting to localhost:8000 as user u1.
Connected to Databend Query v1.2.424-nightly-d3a89f708d(rust-1.77.0-nightly-2024-04-17T22:11:59.304509266Z)