# huaweicloud-rdsforPostgresql-java **Repository Path**: HuaweiCloudDeveloper/huaweicloud-rdsfor-postgresql-java ## Basic Information - **Project Name**: huaweicloud-rdsforPostgresql-java - **Description**: 新增 SDK RdsForPostgresql产品代码示例上架申请 数据库相关代码场景 1、查看数据库用户列表 2、创建数据库用户 3、查看数据库用户列表 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master-dev - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-10-25 - **Last Updated**: 2023-12-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 1. 介绍 华为云关系型数据库(Relational Database Service,简称RDS)是一种基于云计算平台的即开即用、稳定可靠、弹性伸缩、便捷管理的在线关系型数据库服务。 本示例展示如何通过接口调用方式,创建RDS。 ## 2. 前置条件 1.已 [注册](https://reg.huaweicloud.com/registerui/cn/register.html?locale=zh-cn#/register) 华为云,并完成 [实名认证](https://account.huaweicloud.com/usercenter/?region=cn-north-4#/accountindex/realNameAuth) 。 2.获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。 3.已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 [访问密钥](https://support.huaweicloud.com/usermanual-ca/zh-cn_topic_0046606340.html) 。 4.已具备开发环境 ,支持Java JDK 1.8及其以上版本。 ## 3. SDK获取和安装 您可以通过Maven方式获取和安装SDK,首先需要在您的操作系统中下载并安装Maven ,安装完成后您只需要在Java项目的pom.xml文件中加入相应的依赖项即可。 具体的SDK版本号请参见 [SDK开发中心](https://sdkcenter.developer.huaweicloud.com?language=java) 。 ```xml com.huaweicloud.sdk huaweicloud-sdk-rds 3.1.5 ``` ## 4.关键代码片段 以下代码展示如何使用SDK查询和创建数据库用户: ```java public class RdsDbUserDemo { private static final Logger logger = LoggerFactory.getLogger(RdsDbUserDemo.class.getName()); public static void main(String[] args) { // 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 String ak = System.getenv("HUAWEICLOUD_SDK_AK"); String sk = System.getenv("HUAWEICLOUD_SDK_SK"); String regionId = ""; String instanceId = ""; String name = ""; String password = ""; int page = 1; // int limit = 10; // RdsClient client = createClient(ak, sk); // 查看数据库用户列表 listDatabaseUser(client, instanceId, page, limit); // 创建数据库用户 createDatabaseUser(client, instanceId, name, password); // 查看数据库用户列表 listDatabaseUser(client, instanceId, page, limit); } private static RdsClient createClient(String ak, String sk) { ICredential auth = new BasicCredentials().withAk(ak).withSk(sk); return RdsClient.newBuilder().withCredential(auth).withRegion(RdsRegion.CN_EAST_3).build(); } private static void listDatabaseUser(RdsClient client, String instanceId, int page, int limit) { ListPostgresqlDbUserPaginatedRequest request = new ListPostgresqlDbUserPaginatedRequest(); request.withInstanceId(instanceId); request.withPage(page); request.withLimit(limit); try { ListPostgresqlDbUserPaginatedResponse response = client.listPostgresqlDbUserPaginated(request); logger.info(response.toString()); } catch (ConnectionException e) { logger.error("ConnectionException", e); } catch (RequestTimeoutException e) { logger.error("RequestTimeoutException", e); } catch (ServiceResponseException e) { logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg()); } } private static void createDatabaseUser(RdsClient client, String instanceId, String name, String password) { CreatePostgresqlDbUserRequest request = new CreatePostgresqlDbUserRequest(); request.withInstanceId(instanceId); PostgresqlUserForCreation body = new PostgresqlUserForCreation(); body.withName(name); body.withPassword(password); request.withBody(body); try { CreatePostgresqlDbUserResponse response = client.createPostgresqlDbUser(request); logger.info(response.toString()); } catch (ConnectionException e) { logger.error("ConnectionException", e); } catch (RequestTimeoutException e) { logger.error("RequestTimeoutException", e); } catch (ServiceResponseException e) { logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg()); } } } ``` ## 5.返回结果示例 - 查询数据库用户(ListPostgresqlDbUserPaginated)接口的返回值: ``` { users: [ { "name": "user_test_1", "attributes": { "rolsuper": false, "rolinherit": true, "rolcreaterole": true, "rolcreatedb": true, "rolcanlogin": true, "rolconnlimit": -1, "rolreplication": true, "rolbypassrls": false }, "memberof": ["pg_monitor", "pg_read_all_stats", "pg_stat_scan_tables", "pg_signal_backend"] }] "totalCount": 1 } ``` - 创建数据库用户(createPostgresqlDbUser)接口的返回值: ``` class CreatePostgresqlDbUserResponse { "resp": "successful" } ``` - 查看数据库用户(ListPostgresqlDbUserPaginated)接口的返回值: ``` { "users": [ { "name": "user_test_1", "attributes": { "rolsuper": false, "rolinherit": true, "rolcreaterole": true, "rolcreatedb": true, "rolcanlogin": true, "rolconnlimit": -1, "rolreplication": true, "rolbypassrls": false }, "memberof": ["pg_monitor", "pg_read_all_stats", "pg_stat_scan_tables", "pg_signal_backend"] }, { "name": "user_test_2", "attributes": { "rolsuper": false, "rolinherit": true, "rolcreaterole": true, "rolcreatedb": true, "rolcanlogin": true, "rolconnlimit": -1, "rolreplication": true, "rolbypassrls": false }, "memberof": [] }], "totalCount": 2 } ``` ## 6.参考链接 请见 [查询数据库用户API](https://support.huaweicloud.com/api-rds/rds_11_0007.html) 您可以在 [API Explorer](https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=RDS&api=ListPostgresqlDbUserPaginated) 中直接运行调试该接口。 请见 [创建数据库用户API](https://support.huaweicloud.com/api-rds/rds_11_0003.html) 您可以在 [API Explorer](https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=RDS&api=CreatePostgresqlDbUser) 中直接运行调试该接口。 ## 修订记录 | 发布日期 | 文档版本 | 修订说明 | |:----------:| :------: | :----------: | | 2022-10-30 | 1.0 | 文档首次发布 |