开发环境准备

后续教程的接口调用是基于Python3版本的SDK,请先自行安装Python3.9或以上版本,并安装pip。

安装SDK

pip install alibabacloud_gpdb20160503
pip install alibabacloud_tea_openapi

本文档的alibabacloud_gpdb20160503以3.5.0版本为例,如需指定版本安装,请执行如下命令:

pip install alibabacloud_gpdb20160503==3.5.0
pip install alibabacloud_tea_openapi==0.3.8

配置环境变量

配置环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET

  • Linux和macOS系统:将以下命令中的<access_key_id><access_key_secret><instance_id><instance_region>分别替换为您自己的AccessKey ID、AccessKey Secret、AnalyticDB PostgreSQL版实例ID和实例的地域ID,然后在终端设备中逐条运行。如何获取AccessKey ID,请参见创建AccessKey

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
    export ADBPG_INSTANCE_ID=<instance_id>
    export ADBPG_INSTANCE_REGION=<instance_region>
  • Windows系统:新建环境变量文件,添加环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRETADBPG_INSTANCE_IDADBPG_INSTANCE_REGION,并写入已准备好的AccessKey ID、AccessKey Secret、AnalyticDB PostgreSQL版实例ID和实例的地域ID。然后重启Windows系统。

访问认证Client

from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_gpdb20160503.client import Client
import os

ALIBABA_CLOUD_ACCESS_KEY_ID = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
ALIBABA_CLOUD_ACCESS_KEY_SECRET = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
ADBPG_INSTANCE_ID = os.environ['ADBPG_INSTANCE_ID']
ADBPG_INSTANCE_REGION = os.environ['ADBPG_INSTANCE_REGION']


def get_client():
    config = open_api_models.Config(
        access_key_id=ALI_CLOUD_ACCESS_KEY_ID,
        access_key_secret=ALI_CLOUD_ACCESS_KEY_SECRET
    )
    config.region_id = ADBPG_INSTANCE_REGION
    # https://api.aliyun.com/product/gpdb
    if ADBPG_INSTANCE_REGION in ("cn-beijing", "cn-hangzhou", "cn-shanghai", "cn-shenzhen", "cn-hongkong", 
                                 "ap-southeast-1", "cn-hangzhou-finance", "cn-shanghai-finance-1",
                                 "cn-shenzhen-finance-1", "cn-beijing-finance-1"):
        config.endpoint = "gpdb.aliyuncs.com"
    else:
        config.endpoint = f'gpdb.{ADBPG_INSTANCE_REGION}.aliyuncs.com'
    return Client(config)