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

RDS for PostgreSQL

Copy page
Download PDF
V2(2022-01-01)
Using Python SDK
Copy page
Download PDF
Using Python SDK
This topic describes the download, installation, and client initialization information of the Python SDK for RDS for PostgreSQL.​
Download and install​
Prerequisites​
Python 2.7 or above is installed.​
tip​
You can run the python --version command to check the current Python version.​
SDK download URL​
For the source code address of the Python SDK for RDS for PostgreSQL, see the SDK list.​
Install SDK​
You can install the SDK using any of the following methods:​
  • Run the following command to install the SDK for the specified user.​
  • ​
    python setup.py install --<username>​
    ​
  • Run the following command to install the SDK for all users.​
  • ​
    sudo python setup.py install​
    ​
tip​
If the SDK has already been installed, you can upgrade it using the following command.​
​
pip install --upgrade volcengine​
​
Initialize client​
The PostgreSQL Python client provides a series of interfaces to interact with the PostgreSQL service for managing PostgreSQL database resources. You need to initialize the PostgreSQL Python client before sending HTTP/HTTPS requests to the PostgreSQL service.​
Prerequisites​
  • A Volcano Engine account has been created and the real-name authentication has been completed. For account registration and real name authentication, refer to Create account and Verify account.​
  • The Access Key ID (AK) and Secret Access Key (SK) information of the account have been obtained. For specific operations, refer to Managing an access key.​
  • The supported Regions and corresponding Hosts for RDS for PostgreSQL have been retrieved. See service address.​
  • The RDS for PostgreSQL Python SDK has been downloaded and installed. For specific steps, see Download and Install.​
Initialize client​
Initialize the client using the following code.​
​
import volcenginesdkcore​
import volcenginesdkrdspostgresql as pg​
from volcenginesdkcore.rest import ApiException​
if __name__ == "__main__":​
configuration = volcenginesdkcore.Configuration()​
# Set AKSK authentication information and Region​
configuration.ak = "Your AK"​
configuration.sk = "Your SK"​
configuration.region = "Your Region"​
configuration.host = "Your Host"​
try:​
# Create PostgreSQL SDK client; all interface calls need to be executed through this client​
api_instance = pg.RDSPOSTGRESQLApi(volcenginesdkcore.ApiClient(configuration))​
except ApiException as e:​
print("Exception: %s\n" % e)​
​
Sample​
Request timed out time settings​
When calling the PostgreSQL interface with the SDK, there is no timeout limit by default. You can specify the request timeout (unit: seconds) by adding the _request_timeout parameter at the end of the code when initiating the interface call request. If the request is not completed within this time, it will be immediately terminated and report a timeout error. For example, you can set the timeout to 10 seconds with the following code when calling the DescribeDBInstances interface.​
​
resp = api_instance.describe_db_instances(pg.DescribeDBInstancesRequest(​
page_number=1,​
page_size=10,​
), _request_timeout=10)​
​
Creating PostgreSQL instance​
For information about the definition, range, and other parameters used in the following SDK code, see CreateDBInstance.​
​
from pprint import pprint​
import volcenginesdkcore​
import volcenginesdkrdspostgresql as pg​
from volcenginesdkcore.rest import ApiException​
if __name__ == "__main__":​
configuration = volcenginesdkcore.Configuration()​
configuration.ak = "Your AK"​
configuration.sk = "Your SK"​
configuration.region = "Your Region"​
configuration.host = "Your Host"​
try:​
api_instance = pg.RDSPOSTGRESQLApi(volcenginesdkcore.ApiClient(configuration))​
resp = api_instance.create_db_instance(pg.CreateDBInstanceRequest(​
db_engine_version="PostgreSQL_12",​
node_info=[​
pg.NodeInfoForCreateDBInstanceInput(​
zone_id="cn-beijing-a",​
node_type="Primary",​
node_spec="rds.postgres.1c2g",​
node_operate_type="Create"​
),​
pg.NodeInfoForCreateDBInstanceInput(​
zone_id="cn-beijing-a",​
node_type="Secondary",​
node_spec="rds.postgres.1c2g",​
node_operate_type="Create"​
),​
],​
storage_type="LocalSSD",​
storage_space=20,​
vpc_id="vpc-2bzo8w6m87v282dx0eemg****",​
subnet_id="subnet-2d6b6byrhtts058ozfenq****",​
charge_info=pg.ChargeInfoForCreateDBInstanceInput(charge_type="PostPaid"),​
instance_name="testname",​
))​
pprint(resp)​
except ApiException as e:​
print("Exception: %s\n" % e)​
​
The parameter configuration in this example is shown in the table below.​
​
Configuration item​
Note​
Value​
DBEngineVersion​
Instance version.​
PostgreSQL_12​
NodeInfo​
Instance specification configuration.​
[{​
"ZoneId": "cn-beijing-a",​
"NodeSpec": "rds.postgres.1c2g",​
"NodeType": "Primary",​
"NodeOperateType": "Create"​
}, {​
"ZoneId": "cn-beijing-a",​
"NodeSpec": "rds.postgres.1c2g",​
"NodeType": "Secondary",​
"NodeOperateType": "Create"​
}]​
StorageType​
Storage type.​
LocalSSD​
StorageSpace​
Instance storage space.​
20​
VpcId​
A proprietary network (VPC) ID.​
vpc-2bzo8w6m87v282dx0eemg****​
SubnetId​
Subnet ID.​
subnet-2d6b6byrhtts058ozfenq****​
InstanceName​
Storage type.​
testname​
ChargeInfo​
Billing method.​
{​
"ChargeType": "PostPaid"​
}​
​
Query PostgreSQL instance information​
​
from pprint import pprint​
import volcenginesdkcore​
import volcenginesdkrdspostgresql as pg​
from volcenginesdkcore.rest import ApiException​
if __name__ == "__main__":​
configuration = volcenginesdkcore.Configuration()​
configuration.ak = "Your AK"​
configuration.sk = "Your SK"​
configuration.region = "Your Region"​
configuration.host = "Your Host"​
try:​
# Create PostgreSQL SDK client; all interface calls need to be executed through this client​
api_instance = pg.RDSPOSTGRESQLApi(volcenginesdkcore.ApiClient(configuration))​
resp = api_instance.describe_db_instance_detail(pg.DescribeDBInstanceDetailRequest(​
instance_id="Your InstanceId",​
))​
print(resp)​
except ApiException as e:​
print("Exception: %s\n" % e)​
​
​
​
Last updated: 2026.01.05 17:39:36