You need to enable JavaScript to run this app.
RDS for PostgreSQL

RDS for PostgreSQL

Copy page
Download PDF
V2(2022-01-01)
Using Java SDK
Copy page
Download PDF
Using Java SDK
This topic describes the download, installation, and client initialization of the PostgreSQL Java SDK for RDS for PostgreSQL.​
Download and install​
Prerequisites​
Prerequisites For more details, see Java Downloads.​
tip​
You can run the java -version command to check the version of the current Java.​
SDL download URL​
Java SDK source code address for RDS for PostgreSQL, see SDK list.​
Install SDK​
Configure Maven project dependencies. Create a new Maven project and add the following settings in pom.xml in the project directory to introduce service dependencies. When a dependency is introduced, the Maven project management tool automatically downloads the relevant JAR package.​
tip​
If your Java JDK version is JDK 9 or later and you need to add additional javax.annotation-api dependencies, add the following in pom.xml.​
​
<dependency>​
<groupId>javax.annotation</groupId>​
<artifactId>javax.annotation-api</artifactId>​
<version>1.3.2</version>​
</dependency>​
​
​
<dependency>​
<groupId>com.volcengine</groupId>​
<artifactId>volcengine-java-sdk-rdspostgresql</artifactId>​
<version>0.1.157</version>​
</dependency>​
​
Initialize client side​
Prerequisites​
  • The Access Key ID (AK) and Secret Access Key (SK) information of the account have been obtained. For specific operations, refer to Access Key management.​
  • The RDS for PostgreSQL Java SDK has been downloaded and installed. For specific steps, see Download and Install.​
Initialize client​
Initialize the client with the following code.​
​
package org.example;​
​
import com.volcengine.*;​
import com.volcengine.sign.Credentials;​
import com.volcengine.rdspostgresql.RdsPostgresqlApi;​
import com.volcengine.rdspostgresql.model.*;​
​
public class Main {​
// Set AKSK authentication information and Regionpublic static String ak = "Your AK";​
public static String sk = "Your SK";​
public static String region = "Your Region";​
public static String host = "Your Host";​
//Create an RDS for PostgreSQL SDK client, which can be used to call the API to perform task requests later public static ApiClient apiClient = new ApiClient()​
.setCredentials(Credentials.getCredentials(ak, sk))​
.setRegion(region)​
.setEndpoint(host);​
public static RdsPostgresqlApi rdsPostgresql = new RdsPostgresqlApi(apiClient);​
}​
​
Sample​
Request timed out time settings​
When calling the PostgreSQL interface with the SDK, there is no timeout limit by default. You can specify a timeout (in seconds) for the request by adding a _request_timeout parameter at the end of the code when initiating an interface call request. If the request is not completed within this time, the request will terminate with a timeout error. For example, you can set the timeout to 10 seconds with the following code when calling the DescribeDBInstances interface.​
​
public static ApiClient apiClient = new ApiClient()​
.setCredentials(Credentials.getCredentials(ak,sk))​
.setRegion(region)​
.setConnectTimeout(10);​
​
Creating PostgreSQL instance​
For information about the definition, range, and other parameters used in the following SDK code, see CreateDBInstances.​
​
package org.example;​
​
import com.volcengine.*;​
import com.volcengine.sign.Credentials;​
import com.volcengine.rdspostgresql.RdsPostgresqlApi;​
import com.volcengine.rdspostgresql.model.*;​
import java.util.ArrayList;​
import java.util.List;​
​
public class Main {​
// Set AKSK authentication information and Regionpublic static String ak = "Your AK";​
public static String sk = "Your SK";​
public static String region = "Your Region";​
public static String host = "Your Host";​
//Create an RDS for PostgreSQL SDK client, which can be used to call the API to perform task requests later public static ApiClient apiClient = new ApiClient()​
.setCredentials(Credentials.getCredentials(ak, sk))​
.setRegion(region)​
.setEndpoint(host)​
.setConnectTimeout(10);​
public static RdsPostgresqlApi rdsPostgresql = new RdsPostgresqlApi(apiClient);​
​
//Create an interface imported parameter, and set the interface imported parameter according to the parameter description configuration of the interface that needs to be called public static void main(String[] args) throws Exception {​
List<NodeInfoForCreateDBInstanceInput> nodesInfo = new ArrayList<>();​
NodeInfoForCreateDBInstanceInput primaryNodeInfo = new NodeInfoForCreateDBInstanceInput();​
primaryNodeInfo.setNodeType("Primary");​
primaryNodeInfo.setZoneId("cn-beijing-c");​
primaryNodeInfo.setNodeSpec("rds.postgres.1c2g");​
NodeInfoForCreateDBInstanceInput secondaryNodeInfo = new NodeInfoForCreateDBInstanceInput();​
secondaryNodeInfo.setNodeType("Secondary");​
secondaryNodeInfo.setZoneId("cn-beijing-c");​
secondaryNodeInfo.setNodeSpec("rds.postgres.1c2g");​
nodesInfo.add(primaryNodeInfo);​
nodesInfo.add(secondaryNodeInfo);​
​
ChargeInfoForCreateDBInstanceInput chargeInfo = new ChargeInfoForCreateDBInstanceInput();​
chargeInfo.setChargeType("PostPaid");​
​
CreateDBInstanceRequest request = new CreateDBInstanceRequest();​
request.setInstanceName("pg-test-sdk-java");​
request.setDbEngineVersion("PostgreSQL_12");​
request.setStorageType("LocalSSD");​
request.setStorageSpace(30);​
request.setVpcId("vpc-2bzo8w6m87v282dx0eemg****");​
request.setSubnetId("subnet-2d6b6byrhtts058ozfenq****");​
request.setNodeInfo(nodesInfo);​
request.setChargeInfo(chargeInfo);​
​
//Call the interface to complete the operation corresponding to the interface try {​
CreateDBInstanceResponse response = rdsPostgresql.createDBInstance(request);​
System.out.println(response);​
} catch (ApiException e) {​
System.out.println(e.getResponseBody());​
}​
}​
}​
​
The parameters in this example are described in the following table.​
​
Configuration item​
Note​
Value​
DBEngineVersion​
Compatible version.​
PostgreSQL_12​
NodeInfo​
Instance specification configuration.​
[​
{​
"NodeType": "Primary",​
"ZoneId": "cn-beijing-c",​
"NodeSpec": "rds.postgres.1c2g"​
},​
{​
"NodeType": "Secondary",​
"ZoneId": "cn-beijing-c",​
"NodeSpec": "rds.postgres.1c2g"​
}​
]​
StorageType​
Storage type.​
LocalSSD​
StorageSpace​
Instance storage space.​
30​
VpcId​
A proprietary network (VPC) ID.​
vpc-2bzo8w6m87v282dx0eemg****​
SubnetId​
Subnet ID.​
subnet-2d6b6byrhtts058ozfenq****​
InstanceName​
Storage type.​
pg-test-sdk-java​
ChargeInfo​
Billing method.​
{​
"ChargeType": "PostPaid"​
}​
​
Query PostgreSQL instance information​
For information about the definition, range, and other parameters used in the following SDK requests, see DescribeDBInstanceDetail.​
​
package org.example;​
import com.volcengine.*;​
import com.volcengine.sign.Credentials;​
import com.volcengine.rdspostgresql.RdsPostgresqlApi;​
import com.volcengine.rdspostgresql.model.*;​
​
public class Main {​
//Set AKSK authentication information and Region​
public static String ak = "Your AK";​
public static String sk = "Your SK";​
public static String region = "Your Region";​
public static String host = "Your Host";​
// create an RDS for PostgreSQL SDK client, which can be used to call the API to perform task requests later​
public static ApiClient apiClient = new ApiClient()​
.setCredentials(Credentials.getCredentials(ak, sk))​
.setRegion(region)​
.setEndpoint(host)​
.setConnectTimeout(10);​
public static RdsPostgresqlApi rdsPostgresql = new RdsPostgresqlApi(apiClient);​
​
// create an interface parameter, and set the interface parameter according to the parameter description configuration of the interface that needs to be called​
public static void main(String[] args) throws Exception {​
DescribeDBInstanceDetailRequest request = new DescribeDBInstanceDetailRequest();​
// call the interface to complete the operation corresponding to the interface​
try {​
DescribeDBInstanceDetailResponse response = rdsPostgresql.describeDBInstanceDetail(request);​
System.out.println(response);​
} catch (ApiException e) {​
System.out.println(e.getResponseBody());​
}​
}​
}​
​
​
Last updated: 2025.11.17 20:31:12