All Products
Search
Document Center

PolarDB:Using the DynamoDB-compatible feature

Last Updated:Mar 30, 2026

PolarDB for PostgreSQL supports the DynamoDB API, letting you connect DynamoDB-compatible clients and SDKs directly to a PolarDB cluster—no code changes required for standard DynamoDB operations.

This guide covers the full setup in five steps:

  1. Verify version requirements

  2. Enable the DynamoDB-compatible feature

  3. Configure a DynamoDB endpoint

  4. Create a DynamoDB account

  5. Configure network access and verify the connection

Prerequisites

Before you begin, ensure that your cluster meets the following requirements:

  • Kernel version: PostgreSQL 14 with revision version 2.0.14.17.35.0 or later

  • Database proxy version: 2.3.59 or later

  • Cluster type: Standard clusters only—PolarDB for PostgreSQL Distributed Edition and serverless clusters are not supported

To check your cluster's version, go to Configuration and Management > Version Management in the PolarDB console. If the version does not meet the requirements, upgrade the minor version.

Enable the DynamoDB-compatible feature

Enable the feature on an existing cluster, or select it when creating a new cluster.

For an existing cluster

On the Basic Information page of the cluster, find the DynamoDB Compatibility configuration item and click On.

image

Create a new cluster

  1. Go to the custom purchase page for PolarDB clusters.

  2. Set Database Engine to PostgreSQL 14 and select DynamoDB Compatibility.

  3. Configure the region, zone, and node specifications as needed.

image

Configure a DynamoDB endpoint

After enabling the feature, the system automatically creates a default DynamoDB endpoint in the Database Connections section on the Basic Information page.

image

(Optional) Click Create Custom Cluster Endpoint and set Endpoint Type to DynamoDB Endpoint. A custom endpoint lets you specify which nodes to attach and preset the read consistency level.

The consistency level set on a custom endpoint applies to all read requests to that endpoint. The ConsistentRead=true parameter in individual API requests cannot override a preset consistency level.
image

Create a DynamoDB account

DynamoDB API access uses dedicated DynamoDB accounts, not standard database accounts. The account name serves as the Access Key ID, and a generated key serves as the Secret Access Key.

  1. Go to Settings and Management > Accounts and click Create Account.

  2. Set Account Type to DynamoDB Account, then enter an account name and password.

    The password is used only for managing the account in the console and does not apply to API authentication.

    image

  3. After the account is created, find it in the account list. The account name is your Access Key ID. Click View in the Key column to retrieve the Secret Access Key.

    image

Configure network access and verify the connection

Configure network access

Choose the connection method based on where your application runs.

  • Private connection (recommended): If your application runs on an Alibaba Cloud Elastic Compute Service (ECS) instance in the same Virtual Private Cloud (VPC) as the PolarDB cluster, add the ECS instance's IP address or security group to the cluster's whitelist.

  • Internet connection: For local development and testing, request an Internet endpoint for the DynamoDB endpoint in the Database Connections section on the Basic Information page.

    Accessing the cluster over the Internet poses security risks. Use an Internet endpoint only for temporary development and testing, and release it when you are done.

    image

Verify the connection with Python (Boto3)

The following example uses the Boto3 SDK on an ECS instance running Alibaba Cloud Linux 3.2104 LTS 64-bit to verify the connection.

When connecting to PolarDB's DynamoDB-compatible feature, region_name must be set to an empty string ("").
  1. Create the project directory.

    mkdir /home/testDynamoDB
    cd /home/testDynamoDB
  2. Create and activate a virtual environment to isolate project dependencies.

    python3 -m venv myenv
    source myenv/bin/activate
  3. Install Boto3.

    pip3 install boto3
  4. Create main.py with the following code. Replace endpoint_url, aws_access_key_id, and aws_secret_access_key with the endpoint and DynamoDB account details of your PolarDB cluster.

    import boto3
    from botocore.exceptions import ClientError
    
    # 1. Configure your connection details
    # The endpoint URL for your PolarDB cluster
    endpoint_url = "http://<your-polardb-ddb-endpoint>:<port>"
    # The Access Key ID for your DynamoDB account
    aws_access_key_id = "<your-access-key-id>"
    # The Secret Access Key for your DynamoDB account
    aws_secret_access_key = "<your-secret-access-key>"
    # For the DynamoDB-compatible feature of PolarDB, region_name must be an empty string.
    region_name = ""
    
    # 2. Create a DynamoDB resource client
    dynamodb = boto3.resource(
        'dynamodb',
        endpoint_url=endpoint_url,
        aws_access_key_id=aws_access_key_id,
        aws_secret_access_key=aws_secret_access_key,
        region_name=region_name,
    )
    
    table_name = 'usertable'
    
    # 3. Define functions for database operations
    def create_table():
        """Creates the table, or gets it if it already exists."""
        try:
            table = dynamodb.create_table(
                TableName=table_name,
                KeySchema=[{'AttributeName': 'userid', 'KeyType': 'HASH'}], # Partition key
                AttributeDefinitions=[{'AttributeName': 'userid', 'AttributeType': 'S'}]
                # ProvisionedThroughput is optional and ignored by PolarDB
            )
            print("Creating table... Waiting for it to become active.")
            table.meta.client.get_waiter('table_exists').wait(TableName=table_name)
            print("Table is active!")
            return table
        except ClientError as e:
            if e.response['Error']['Code'] == 'ResourceInUseException':
                print("Table already exists.")
                return dynamodb.Table(table_name)
            else:
                raise
    
    def put_items(table):
        """Puts a few items into the table."""
        users = [
            {'userid': 'user01', 'name': 'Alice', 'age': 24},
            {'userid': 'user02', 'name': 'Bob', 'age': 30},
            {'userid': 'user03', 'name': 'Charlie', 'age': 28}
        ]
        for user in users:
            table.put_item(Item=user)
        print("Inserted 3 items.")
    
    def scan_table(table):
        """Scans and prints all items in the table."""
        response = table.scan()
        items = response.get('Items', [])
        print(f"Scanned {len(items)} items:")
        for item in items:
            print(item)
    
    def delete_table(table):
        """Deletes the table."""
        table.delete()
        print("Deleting table... Waiting for it to be removed.")
        table.meta.client.get_waiter('table_not_exists').wait(TableName=table_name)
        print("Table deleted successfully.")
    
    # 4. Run the verification process
    if __name__ == '__main__':
        try:
            user_table = create_table()
            put_items(user_table)
            scan_table(user_table)
        finally:
            # Ensure the table is cleaned up even if errors occur
            if 'user_table' in locals():
                delete_table(user_table)
  5. Run the script. The script creates a table, inserts three items, scans them, and then deletes the table.

    python3 main.py

    The expected output is:

    Creating table... Waiting for it to become active.
    Table is active!
    Inserted 3 items.
    Scanned 3 items:
    {'age': Decimal('24'), 'name': 'Alice', 'userid': 'user01'}
    {'age': Decimal('30'), 'name': 'Bob', 'userid': 'user02'}
    {'age': Decimal('28'), 'name': 'Charlie', 'userid': 'user03'}
    Deleting table... Waiting for it to be removed.
    Table deleted successfully.

    This output confirms a successful connection and that all configurations are working correctly.

What's next

  • Explore the DynamoDB API operations supported by PolarDB for PostgreSQL.

  • To improve security in production, switch from an Internet connection to a private connection within a VPC.

  • To customize read consistency behavior, create a custom DynamoDB endpoint and set the consistency level to match your application's requirements.