Python SDK example

Updated at:
Copy as MD

Use the Alibaba Cloud SDK for Python to call the DescribeDBInstances operation and query your AnalyticDB for PostgreSQL instances.

Before you begin

Before you call an API operation, review its request parameters and authorization requirements. For more information, see API overview.

Create a RAM user and grant permissions

If you have already created a RAM user and granted the required permissions, you can skip this step.

  1. Create a RAM user:

    1. Go to the Users page in the RAM console and click Create User.

    2. Set User Login Name to adbpg-openapi-operator, and select both Console Access and Permanent AccessKey.

    3. Click OK and save the AccessKey ID and AccessKey Secret.

  2. Grant permissions to the RAM user:

    1. On the Users page, find the RAM user and click Add Permissions in the Actions column.

    2. In the search box, enter AliyunGPDB and select the AliyunGPDBReadOnlyAccess policy.

      Note

      The AliyunGPDBReadOnlyAccess policy lets you query AnalyticDB for PostgreSQL instances. You can also select the AliyunGPDBFullAccess policy, which grants full control over querying and modifying AnalyticDB for PostgreSQL instances, or create a custom policy. For more information, see Create a custom policy.

    3. Click OK to grant the permission.

Call an API

The following example uses the SDK for Python. SDKs for other languages work similarly. For more information, see SDKs for AnalyticDB for PostgreSQL.

Set up a Python environment

Download and install Python 3. If Python is already installed, check the version by running the following command: python --version.

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

  • For Linux and macOS: Replace <access_key_id> and <access_key_secret> with your AccessKey ID and AccessKey Secret. Then, run the following commands in your terminal.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<ACCESS_KEY_ID>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET>
  • For Windows: Create an environment variable file. Add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET variables and set their values to your AccessKey ID and AccessKey Secret. Restart Windows to apply the changes.

Install dependencies

Run the following commands to install the dependencies.

pip install alibabacloud_credentials
pip install alibabacloud_gpdb20160503
pip install alibabacloud_tea_console

Download sample code

  1. Go to the OpenAPI Portal page for the DescribeDBInstances operation.

  2. On the Parameters tab, specify the required parameters. For this example, enter cn-hangzhou for the RegionId parameter.

  3. On the SDK Sample Code tab, select Python, and click Download Project to download the sample code package.

  4. Decompress the package locally and go to the alibabacloud_sample directory.

Run code

Run the following command.

python sample.py

The command returns the following response:

[
  LOG
] {
  "headers":{
    "date":"Fri, 04 Aug 2023 07:53:15 GMT",
    "content-type":"application/json;charset=utf-8",
    "content-length":"1530","connection":"keep-alive",
    "keep-alive":"timeout=25","vary":"Accept-Encoding",
    "access-control-allow-origin":"*",
    "access-control-expose-headers":"*",
    "x-acs-request-id":"22996ADD-A917-5F47-AA7E-2D9A08****",
    "x-acs-trace-id":"a1f567557dc98fb5f368346302e6****",
    "etag":"1rgk9Fbs7Z+HYg9IhB1****"
  },
  "statusCode":200,
  "body":{
    "Items":{
      "DBCluster":[
        {
         "CreateTime":"2023-07-31T07:09:32Z",
         "DBInstanceCategory":"Basic",
         "DBInstanceDescription":"gp-bp1j20nrgf91****",
         "DBInstanceId":"gp-bp1j20nrgf91****",
         "DBInstanceMode":"StorageElastic",
         "DBInstanceNetType":"2",
         "DBInstanceStatus":"Running",
         "Engine":"gpdb",
         "EngineVersion":"6.0",
         "ExpireTime":"2023-08-31T16:00:00Z",
         "InstanceDeployType":"cluster",
         "InstanceNetworkType":"VPC",
         "LockMode":"Unlock",
         "LockReason":"0",
         "MasterNodeNum":1,
         "PayType":"Prepaid",
         "RegionId":"cn-hangzhou",
         "ResourceGroupId":"rg-acfmz7u4zzr****",
         "SegNodeNum":"2",
         "StorageSize":"50",
         "StorageType":"cloud_essd",
         "Tags":{"Tag":[]},
         "VSwitchId":"vsw-bp1sxxsodv28ey5****",
         "VpcId":"vpc-bp1ov7as4yvz4kxei****",
         "ZoneId":"cn-hangzhou-j"
        }
      ]
    },
    "PageNumber":1,
    "PageRecordCount":1,
    "RequestId":"22996ADD-A917-5F47-AA7E-2D9A08AF1AC2",
    "TotalRecordCount":1
  }
}