Skip to content

Commit 51fb08d

Browse files
authored
improve create example (#2640)
1 parent afa3303 commit 51fb08d

File tree

2 files changed

+65
-154
lines changed

2 files changed

+65
-154
lines changed

docs/en/developer/00-drivers/index.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ Databend provides official drivers for multiple programming languages, enabling
1515
All Databend drivers use the same DSN (Data Source Name) format:
1616

1717
```
18-
databend://user:pass@host[:port]/[database][?sslmode=disable][&arg1=value1]
18+
databend://user:pwd@host[:port]/[database][?sslmode=disable][&arg1=value1]
1919
```
2020

21+
> The `user:pwd` refers to SQL users in Databend. See [CREATE USER](/sql/sql-commands/ddl/user/user-create-user) to create users and grant privileges.
22+
2123
### Connection Examples
2224

2325
| Deployment | Connection String |
2426
|------------|-------------------|
25-
| **Self-hosted** | `databend://user:pass@host:8000/database?sslmode=disable` |
26-
| **Databend Cloud** | `databend://user:pass@host:443/database?warehouse=wh` |
27+
| **Self-hosted** | `databend://user:pwd@host:8000/database?sslmode=disable` |
28+
| **Databend Cloud** | `databend://user:pwd@host:443/database?warehouse=wh` |
2729

2830
### Parameters Reference
2931

docs/en/sql-reference/10-sql-commands/00-ddl/02-user/01-user-create-user.md

Lines changed: 60 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -6,192 +6,101 @@ import FunctionDescription from '@site/src/components/FunctionDescription';
66

77
<FunctionDescription description="Introduced or updated: v1.2.703"/>
88

9-
Creates a SQL user.
9+
Creates a SQL user for connecting to Databend. Users must be granted appropriate privileges to access databases and perform operations.
1010

1111
See also:
12-
13-
- [CREATE PASSWORD POLICY](../12-password-policy/create-password-policy.md)
14-
- [CREATE NETWORK POLICY](../12-network-policy/ddl-create-policy.md)
15-
- [GRANT](10-grant.md)
12+
- [GRANT](10-grant.md)
13+
- [ALTER USER](03-user-alter-user.md)
14+
- [DROP USER](02-user-drop-user.md)
1615

1716
## Syntax
1817

1918
```sql
2019
CREATE [ OR REPLACE ] USER <name> IDENTIFIED [ WITH <auth_type> ] BY '<password>'
2120
[ WITH MUST_CHANGE_PASSWORD = true | false ]
22-
[ WITH SET PASSWORD POLICY = '<policy_name>' ] -- Set password policy
23-
[ WITH SET NETWORK POLICY = '<policy_name>' ] -- Set network policy
24-
[ WITH SET WORKLOAD GROUP = '<workload_group_name>' ] -- Set workload group
25-
[ WITH DEFAULT_ROLE = '<role_name>' ] -- Set default role
26-
[ WITH DISABLED = true | false ] -- User created in a disabled state
21+
[ WITH SET PASSWORD POLICY = '<policy_name>' ]
22+
[ WITH SET NETWORK POLICY = '<policy_name>' ]
23+
[ WITH DEFAULT_ROLE = '<role_name>' ]
24+
[ WITH DISABLED = true | false ]
2725
```
2826

29-
- The `<name>` cannot contain the following illegal characters:
30-
- Single quote (')
31-
- Double quote (")
32-
- Backspace (\b)
33-
- Form feed (\f)
34-
- *auth_type* can be `double_sha1_password` (default), `sha256_password` or `no_password`.
35-
- When `MUST_CHANGE_PASSWORD` is set to `true`, the new user must change password at first login. Users can change their own password using the [ALTER USER](03-user-alter-user.md) command.
36-
- When you set a default role for a user using CREATE USER or [ALTER USER](03-user-alter-user.md), 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.
37-
- When `DISABLED` is set to `true`, 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](03-user-alter-user.md) command.
27+
**Parameters:**
28+
- `<name>`: Username (cannot contain single quotes, double quotes, backspace, or form feed characters)
29+
- `<auth_type>`: Authentication type - `double_sha1_password` (default), `sha256_password`, or `no_password`
30+
- `MUST_CHANGE_PASSWORD`: When `true`, user must change password at first login
31+
- `DEFAULT_ROLE`: Sets default role (role must be explicitly granted to take effect)
32+
- `DISABLED`: When `true`, user is created in disabled state and cannot log in
3833

3934
## Examples
4035

41-
### Example 1: Creating User with Default auth_type
42-
43-
```sql
44-
CREATE USER user1 IDENTIFIED BY 'abc123';
45-
46-
SHOW USERS;
47-
+-----------+----------+----------------------+---------------+
48-
| name | hostname | auth_type | is_configured |
49-
+-----------+----------+----------------------+---------------+
50-
| user1 | % | double_sha1_password | NO |
51-
+-----------+----------+----------------------+---------------+
52-
```
53-
54-
### Example 2: Creating User with sha256_password auth_type
55-
56-
```sql
57-
CREATE USER user1 IDENTIFIED WITH sha256_password BY 'abc123';
58-
59-
SHOW USERS;
60-
+-----------+----------+----------------------+---------------+
61-
| name | hostname | auth_type | is_configured |
62-
+-----------+----------+----------------------+---------------+
63-
| user1 | % | sha256_password | NO |
64-
+-----------+----------+----------------------+---------------+
65-
```
36+
### Example 1: Create User and Grant Database Privileges
6637

67-
### Example 3: Creating User with Network Policy
38+
Create a user and grant database privileges:
6839

6940
```sql
70-
CREATE USER user1 IDENTIFIED BY 'abc123' WITH SET NETWORK POLICY='test_policy';
71-
72-
SHOW USERS;
73-
+-----------+----------+----------------------+---------------+
74-
| name | hostname | auth_type | is_configured |
75-
+-----------+----------+----------------------+---------------+
76-
| user1 | % | double_sha1_password | NO |
77-
+-----------+----------+----------------------+---------------+
78-
```
79-
80-
### Example 4: Creating User with Default Role
81-
82-
1. Create a user named 'user1' with the default role set to 'manager':
83-
84-
```sql title='Connect as user "root":'
85-
SHOW ROLES;
86-
87-
┌───────────────────────────────────────────────────────────┐
88-
│ name │ inherited_roles │ is_current │ is_default │
89-
│ String │ UInt64 │ BooleanBoolean
90-
├───────────────┼─────────────────┼────────────┼────────────┤
91-
│ account_admin │ 0 │ true │ true │
92-
│ developer │ 0 │ false │ false │
93-
│ public │ 0 │ false │ false │
94-
└───────────────────────────────────────────────────────────┘
41+
-- Create a new user
42+
CREATE USER data_analyst IDENTIFIED BY 'secure_password123';
9543

96-
CREATE USER user1 IDENTIFIED BY 'abc123' WITH DEFAULT_ROLE = 'manager';
97-
98-
GRANT ROLE developer TO user1;
44+
-- Grant database privileges to the user
45+
GRANT SELECT, INSERT ON default.* TO data_analyst;
9946
```
10047

101-
2. Verify the default role of user "user1" using the [SHOW ROLES](04-user-show-roles.md) command:
102-
103-
```sql title='Connect as user "user1":'
104-
eric@Erics-iMac ~ % bendsql --user user1 --password abc123
105-
Welcome to BendSQL 0.9.3-db6b232(2023-10-26T12:36:55.578667000Z).
106-
Connecting to localhost:8000 as user user1.
107-
Connected to DatabendQuery v1.2.271-nightly-0598a77b9c(rust-1.75.0-nightly-2023-12-26T11:29:04.266265000Z)
108-
109-
user1@localhost:8000/default> SHOW ROLES;
110-
111-
SHOW ROLES
112-
113-
┌───────────────────────────────────────────────────────┐
114-
│ name │ inherited_roles │ is_current │ is_default │
115-
│ String │ UInt64 │ BooleanBoolean
116-
├───────────┼─────────────────┼────────────┼────────────┤
117-
│ developer │ 0 │ true │ true │
118-
│ public │ 0 │ false │ false │
119-
└───────────────────────────────────────────────────────┘
120-
2 rows read in 0.015 sec. Processed 0 rows, 0 B (0 rows/s, 0 B/s)
121-
```
122-
123-
### Example 5: Creating User in Disabled State
124-
125-
This example creates a user named 'u1' in a disabled state, preventing login access. After enabling the user using the [ALTER USER](03-user-alter-user.md) command, login access is restored.
126-
127-
1. Create a user named 'u1' in the disabled state:
128-
48+
Verify the user and permissions:
12949
```sql
130-
CREATE USER u1 IDENTIFIED BY '123' WITH DISABLED = TRUE;
131-
132-
SHOW USERS;
133-
134-
┌─────────────────────────────────────────────────────────────────────────────────────┐
135-
│ name │ hostname │ auth_type │ is_configured │ default_role │ disabled │
136-
├────────┼──────────┼──────────────────────┼───────────────┼───────────────┼──────────┤
137-
│ root │ % │ no_password │ YES │ account_admin │ false │
138-
│ u1 │ % │ double_sha1_password │ NO │ │ true │
139-
└─────────────────────────────────────────────────────────────────────────────────────┘
50+
SHOW GRANTS FOR data_analyst;
51+
+---------------------------------------------------+
52+
| Grants |
53+
+---------------------------------------------------+
54+
| GRANT SELECT,INSERT ON 'default'.* TO 'data_analyst'@'%' |
55+
+---------------------------------------------------+
14056
```
14157

142-
2. Attempt to connect to Databend using BendSQL as user 'u1', resulting in an authentication error:
58+
### Example 2: Create User and Grant Role
14359

144-
```shell
145-
~ bendsql --user u1 --password 123
146-
Welcome to BendSQL 0.16.0-homebrew.
147-
Connecting to localhost:8000 as user u1.
148-
Error: APIError: RequestError: Start Query failed with status 401 Unauthorized: {"error":{"code":"401","message":"AuthenticateFailure: user u1 is disabled. Not allowed to login"}}
149-
```
150-
151-
3. Enable the user 'u1' with the [ALTER USER](03-user-alter-user.md) command:
60+
Create a user and assign a role with specific privileges:
15261

15362
```sql
154-
ALTER USER u1 WITH DISABLED = FALSE;
63+
-- Create a role with specific privileges
64+
CREATE ROLE analyst_role;
65+
GRANT SELECT ON *.* TO ROLE analyst_role;
66+
GRANT INSERT ON default.* TO ROLE analyst_role;
67+
68+
-- Create user and grant the role
69+
CREATE USER john_analyst IDENTIFIED BY 'secure_pass456';
70+
GRANT ROLE analyst_role TO john_analyst;
15571
```
15672

157-
4. Re-attempt connection to Databend as user 'u1', confirming successful login access:
158-
159-
```shell
160-
~ bendsql --user u1 --password 123
161-
Welcome to BendSQL 0.16.0-homebrew.
162-
Connecting to localhost:8000 as user u1.
163-
Connected to Databend Query v1.2.424-nightly-d3a89f708d(rust-1.77.0-nightly-2024-04-17T22:11:59.304509266Z)
73+
Verify the role assignment:
74+
```sql
75+
SHOW GRANTS FOR john_analyst;
76+
+------------------------------------------+
77+
| Grants |
78+
+------------------------------------------+
79+
| GRANT SELECT ON *.* TO 'analyst_role' |
80+
| GRANT INSERT ON 'default'.* TO 'analyst_role' |
81+
+------------------------------------------+
16482
```
16583

166-
### Example 6: Creating User with MUST_CHANGE_PASSWORD
167-
168-
In this example, we will create a user with the `MUST_CHANGE_PASSWORD` option. Then, we will connect to Databend with BendSQL as the new user and change the password.
169-
170-
1. Create a new user named 'eric' with the `MUST_CHANGE_PASSWORD` option set to `TRUE`.
84+
### Example 3: Create Users with Different Authentication Types
17185

17286
```sql
173-
CREATE USER eric IDENTIFIED BY 'abc123' WITH MUST_CHANGE_PASSWORD = TRUE;
174-
```
175-
176-
2. Launch BendSQL and connect to Databend as the new user. Once connected, you'll see a message indicating that a password change is required.
87+
-- Create user with default authentication
88+
CREATE USER user1 IDENTIFIED BY 'abc123';
17789

178-
```bash
179-
MacBook-Air:~ eric$ bendsql -ueric -pabc123
90+
-- Create user with SHA256 authentication
91+
CREATE USER user2 IDENTIFIED WITH sha256_password BY 'abc123';
18092
```
18193

182-
3. Change the password with the [ALTER USER](03-user-alter-user.md) command.
183-
184-
```bash
185-
eric@localhost:8000/default> ALTER USER USER() IDENTIFIED BY 'abc456';
186-
```
94+
### Example 4: Create Users with Special Configurations
18795

188-
4. Quit BendSQL then reconnect with the new password.
96+
```sql
97+
-- Create user with password change requirement
98+
CREATE USER new_employee IDENTIFIED BY 'temp123' WITH MUST_CHANGE_PASSWORD = true;
18999

190-
```bash
191-
MacBook-Air:~ eric$ bendsql -ueric -pabc456
192-
Welcome to BendSQL 0.19.2-1e338e1(2024-07-17T09:02:28.323121000Z).
193-
Connecting to localhost:8000 as user eric.
194-
Connected to Databend Query v1.2.567-nightly-78d41aedc7(rust-1.78.0-nightly-2024-07-14T22:10:13.777450105Z)
100+
-- Create user in disabled state
101+
CREATE USER temp_user IDENTIFIED BY 'abc123' WITH DISABLED = true;
195102

196-
eric@localhost:8000/default>
103+
-- Create user with default role (role must be granted separately)
104+
CREATE USER manager IDENTIFIED BY 'abc123' WITH DEFAULT_ROLE = 'admin';
105+
GRANT ROLE admin TO manager;
197106
```

0 commit comments

Comments
 (0)