文档首页/ 数据仓库服务 DWS/ API参考/ API说明/ 数据库权限管理/ 创建数据库用户/角色 - CreateDatabaseUser
更新时间:2026-09-23 GMT+08:00
分享

创建数据库用户/角色 - CreateDatabaseUser

功能介绍

创建数据库用户/角色。

调用方法

请参见如何调用API。

授权信息

账号具备所有API的调用权限,如果使用账号下的IAM用户调用当前API,该IAM用户需具备调用API所需的权限。

  • 如果使用角色与策略授权,具体权限要求请参见权限和授权项。
  • 如果使用身份策略授权,需具备如下身份策略权限。

    授权项

    访问级别

    资源类型(*为必须)

    条件键

    别名

    依赖的授权项

    dws:cluster:createDatabaseUser

    Write

    cluster *

    • g:ResourceTag/<tag-key>

    • g:EnterpriseProjectId

    -

    -

URI

POST /v1/{project_id}/clusters/{cluster_id}/db-manager/users

表1 路径参数

参数

是否必选

参数类型

描述

project_id

是

String

参数解释:

项目ID。获取方法请参见获取项目ID。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

cluster_id

是

String

参数解释:

集群ID。获取方法请参见获取集群ID。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

请求参数

表2 请求Body参数

参数

是否必选

参数类型

描述

name

是

String

参数解释:

用户名/角色名称。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

type

是

String

参数解释:

类型。

约束限制:

不涉及。

取值范围:

user:数据库用户。

role:数据库角色。

默认取值:

不涉及。

login

否

Boolean

参数解释:

是否允许登录。

约束限制:

不涉及。

取值范围:

  • true:允许登录。

  • false:禁止登录。

默认取值:

true

password

否

String

参数解释:

密码。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

system_admin

否

Boolean

参数解释:

是否是系统管理员。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

logical_cluster

否

String

参数解释:

关联的逻辑集群名称。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

password_disable

否

Boolean

参数解释:

是否禁用密码登录。

约束限制:

不涉及。

取值范围:

  • true:禁用密码登录。

  • false:允许密码登录。

默认取值:

false

create_role

否

Boolean

参数解释:

是否允许创建角色。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

false

create_db

否

Boolean

参数解释:

是否允许创建数据库。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

false

inherit

否

Boolean

参数解释:

是否允许继承权限。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

false

conn_limit

否

Integer

参数解释:

连接数限制。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

grant_members

否

Array of strings

参数解释:

授权对象信息。

约束限制:

不涉及。

取值范围:

列表项最大值为100。

默认取值:

不涉及。

grant_list

否

Array of GrantAuthority objects

参数解释:

授权列表。

约束限制:

不涉及。

取值范围:

列表项最大值为2000。

默认取值:

不涉及。

desc

否

String

参数解释:

描述信息。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

表3 GrantAuthority

参数

是否必选

参数类型

描述

type

否

String

参数解释:

权限类型。

取值范围:

  • DATABASE:数据库。

  • SCHEMA:模式。

  • TABLE:表。

  • VIEW:视图。

  • COLUMN:列。

  • FUNCTION:函数。

  • SEQUENCE:序列。

  • NODEGROUP:节点组。

database

否

String

参数解释:

数据库名称。

取值范围:

不涉及。

schema

否

String

参数解释:

模式名称。

取值范围:

不涉及。

obj_name

否

String

参数解释:

对象名称。

取值范围:

不涉及。

all_object

否

Boolean

参数解释:

是否所有对象生效。

取值范围:

不涉及。

future

否

Boolean

参数解释:

是否对未来对象生效。

取值范围:

不涉及。

future_object_owners

否

String

参数解释:

未来对象-所属用户。

取值范围:

不涉及。

column_name

否

Array of strings

参数解释:

列名。

取值范围:

不涉及。

privileges

否

Array of Grant objects

参数解释:

权限。

取值范围:

不涉及。

表4 Grant

参数

是否必选

参数类型

描述

permission

是

String

参数解释:

权限名称,根据不同数据库对象类型,拥有权限不同。

取值范围:

  • database:CREATE | CONNECT | TEMPORARY | TEMP ALL PRIVILEGES

  • schema:CREATE | USAGE | ALTER | DROP ALL PRIVILEGES

  • table:SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER | ANALYZE | ANALYSE | VACUUM | ALTER | DROP ALL PRIVILEGES

  • view:SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER | ANALYZE | ANALYSE | VACUUM | ALTER | DROP ALL PRIVILEGES

  • column:SELECT | INSERT | UPDATE | REFERENCES ALL PRIVILEGES

  • function:EXECUTE ALL PRIVILEGES

  • sequence:SELECT | UPDATE | USAGE ALL PRIVILEGES

  • nodegroup:CREATE | USAGE | COMPUTE ALL PRIVILEGES

  • role:role_name(角色名称)

默认取值:

不涉及。

grant_with

是

Boolean

参数解释:

是否包含授权选项。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

false

响应参数

状态码:200

请求下发成功。

无

请求示例

创建数据库用户,用户名user1,密码需要自己替换成实际值。

POST https://{Endpoint}/v1/05f2cff45100d5112f4bc00b794ea08e/clusters/cc6588d6-8301-4c9a-b0c0-186bb824e8c0/db-manager/users

{
  "name" : "user1",
  "type" : "user",
  "login" : true,
  "password" : "*****",
  "system_admin" : false,
  "logical_cluster" : "v3_logical",
  "password_disable" : false,
  "create_role" : false,
  "create_db" : false,
  "inherit" : true,
  "conn_limit" : -1,
  "grant_members" : [ ],
  "grant_list" : [ ],
  "desc" : ""
}

响应示例

状态码:200

{ }

SDK代码示例

SDK代码示例如下。

创建数据库用户,用户名user1,密码需要自己替换成实际值。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.dws.v2.region.DwsRegion;
import com.huaweicloud.sdk.dws.v2.*;
import com.huaweicloud.sdk.dws.v2.model.*;

import java.util.List;
import java.util.ArrayList;

public class CreateDatabaseUserSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");
        String projectId = "{project_id}";

        ICredential auth = new BasicCredentials()
                .withProjectId(projectId)
                .withAk(ak)
                .withSk(sk);

        DwsClient client = DwsClient.newBuilder()
                .withCredential(auth)
                .withRegion(DwsRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateDatabaseUserRequest request = new CreateDatabaseUserRequest();
        request.withClusterId("{cluster_id}");
        UserAuthorityReq body = new UserAuthorityReq();
        body.withDesc("");
        body.withConnLimit(-1);
        body.withInherit(true);
        body.withCreateDb(false);
        body.withCreateRole(false);
        body.withPasswordDisable(false);
        body.withLogicalCluster("v3_logical");
        body.withSystemAdmin(false);
        body.withPassword("*****");
        body.withLogin(true);
        body.withType("user");
        body.withName("user1");
        request.withBody(body);
        try {
            CreateDatabaseUserResponse response = client.createDatabaseUser(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

创建数据库用户,用户名user1,密码需要自己替换成实际值。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkdws.v2.region.dws_region import DwsRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkdws.v2 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]
    projectId = "{project_id}"

    credentials = BasicCredentials(ak, sk, projectId)

    client = DwsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(DwsRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateDatabaseUserRequest()
        request.cluster_id = "{cluster_id}"
        request.body = UserAuthorityReq(
            desc="",
            conn_limit=-1,
            inherit=True,
            create_db=False,
            create_role=False,
            password_disable=False,
            logical_cluster="v3_logical",
            system_admin=False,
            password="*****",
            login=True,
            type="user",
            name="user1"
        )
        response = client.create_database_user(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

创建数据库用户,用户名user1,密码需要自己替换成实际值。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    dws "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dws/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dws/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dws/v2/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")
    projectId := "{project_id}"

    auth, err := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithProjectId(projectId).
        SafeBuild()

    if err != nil {
        fmt.Println(err)
        return
    }

    hcClient, err := dws.DwsClientBuilder().
         WithRegion(region.ValueOf("<YOUR REGION>")).
         WithCredential(auth).
         SafeBuild()


    if err != nil {
        fmt.Println(err)
        return
    }

    client := dws.NewDwsClient(hcClient)

    request := &model.CreateDatabaseUserRequest{}
	request.ClusterId = "{cluster_id}"
	descUserAuthorityReq:= ""
	connLimitUserAuthorityReq:= int32(-1)
	inheritUserAuthorityReq:= true
	createDbUserAuthorityReq:= false
	createRoleUserAuthorityReq:= false
	passwordDisableUserAuthorityReq:= false
	logicalClusterUserAuthorityReq:= "v3_logical"
	systemAdminUserAuthorityReq:= false
	passwordUserAuthorityReq:= "*****"
	loginUserAuthorityReq:= true
	request.Body = &model.UserAuthorityReq{
		Desc: &descUserAuthorityReq,
		ConnLimit: &connLimitUserAuthorityReq,
		Inherit: &inheritUserAuthorityReq,
		CreateDb: &createDbUserAuthorityReq,
		CreateRole: &createRoleUserAuthorityReq,
		PasswordDisable: &passwordDisableUserAuthorityReq,
		LogicalCluster: &logicalClusterUserAuthorityReq,
		SystemAdmin: &systemAdminUserAuthorityReq,
		Password: &passwordUserAuthorityReq,
		Login: &loginUserAuthorityReq,
		Type: "user",
		Name: "user1",
	}
	response, err := client.CreateDatabaseUser(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

请求下发成功。

400

请求错误。

401

鉴权失败。

403

没有操作权限。

417

服务内部错误。

500

服务内部错误。

相关文档