All Products
Search
Document Center

AnalyticDB for PostgreSQL:API usage example

Last Updated:Sep 14, 2023

This topic describes how to use Alibaba Cloud SDK for Python to call the DescribeDBInstances operation of AnalyticDB for PostgreSQL.

View the API documentation

Before you call an API operation, we recommend that you read the documentation about the API operation. For more information, see List of operations by function.

Create a RAM user and grant permissions to the RAM user

If you have created a Resource Access Management (RAM) user and granted permissions to the RAM user, skip this step.

  1. Perform the following steps to create a RAM user:

    1. Log on to the RAM console, go to the Users page, and then click Create User.

    2. Set the Logon Name parameter to adbpg-openapi-operator and select OpenAPI Access for Access Mode.

    3. Click OK. After the RAM user is created, save the AccessKey ID and AccessKey secret of the user.

  2. Perform the following steps to grant permissions to the RAM user:

    1. Go to the Users page, find the RAM user that you created, and then click Add Permissions in the Actions column.

    2. In the Select Policy section, enter AliyunGPDB in the text box and select the AliyunGPDBReadOnlyAccess policy.

      Note

      The AliyunGPDBReadOnlyAccess policy has permissions to query AnalyticDB for PostgreSQL instances. You can also select the AliyunGPDBFullAccess policy that has permissions to query and modify AnalyticDB for PostgreSQL instances or customize a policy. For more information, see Create a custom policy.

    3. Click OK.

Call the API operation

In this example, Alibaba Cloud SDK for Python is used to call the API operation. You can use SDKs for other languages in a similar manner. For more information, see AnalyticDB for PostgreSQL SDK.

Prepare a Python environment

Download and install Python 3. If you have installed Python, you can run the python --version command to query the Python version.

Configure environment variables

This section describes how to configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your operating system.

  • If you use the Linux or macOS operating system, replace <access_key_id> and <access_key_secret> in the following commands with your AccessKey ID and AccessKey secret. Then, run the commands on your device.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • If you use the Windows operating system, create a file to add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, specify your AccessKey ID and AccessKey secret by using the environment variables, and then restart the system.

Install dependencies

Run the following commands on your device to install environment dependencies:

pip install alibabacloud_credentials
pip install alibabacloud_gpdb20160503
pip install alibabacloud_tea_console

Download the sample code

  1. Go to the DescribeDBInstances page.

  2. On the left-side Parameters tab, specify the required parameters. In this example, specify cn-hangzhou for the RegionId parameter.

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

  4. Decompress the sample code package on your device and go to the alibabacloud_sample directory.

Run the code

Run the following command:

python sample.py

Sample result:

[
  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
  }
}