Amazon Aurora DSQL dialect for SQLAlchemy
Project description
Amazon Aurora DSQL dialect for SQLAlchemy
Introduction
The Aurora DSQL dialect for SQLAlchemy provides integration between SQLAlchemy ORM and Aurora DSQL. This dialect enables Python applications to leverage SQLAlchemy's powerful object-relational mapping capabilities while taking advantage of Aurora DSQL's distributed architecture and high availability.
Sample Application
There is an included sample application in examples/pet-clinic-app that shows how to use Aurora DSQL with SQLAlchemy. To run the included example please refer to the sample README.
Prerequisites
- Python 3.10 or higher
- SQLAlchemy 2.0.0 or higher
- One of the following drivers:
- psycopg 3.2.0 or higher
- psycopg2 2.9.0 or higher
Installation
Install the packages using the commands below:
pip install aurora-dsql-sqlalchemy
# driver installation (in case you opt for psycopg)
# DO NOT use pip install psycopg-binary
pip install "psycopg[binary]"
# driver installation (in case you opt for psycopg2)
pip install psycopg2-binary
Dialect Configuration
After installation, you can connect to an Aurora DSQL cluster using the create_dsql_engine helper function:
from aurora_dsql_sqlalchemy import create_dsql_engine
engine = create_dsql_engine(
host="<CLUSTER_ENDPOINT>",
user="<CLUSTER_USER>",
driver="psycopg", # or "psycopg2"
)
The helper function handles:
- IAM authentication via the Aurora DSQL Python Connector
- SSL configuration with certificate verification
- Direct SSL negotiation optimization (when supported by libpq >= 17)
- Connection pooling with sensible defaults
For more control, you can customize additional parameters:
engine = create_dsql_engine(
host="<CLUSTER_ENDPOINT>",
user="<CLUSTER_USER>",
driver="psycopg",
pool_size=10,
max_overflow=20,
)
Note: Each connection has a maximum duration limit. See the Maximum connection duration time limit in the Cluster quotas and database limits in Amazon Aurora DSQL page.
SSL/TLS Configuration
Aurora DSQL requires TLS for all connections. Plaintext connections are not supported. Enabling certificate verification protects against on-path and impersonation attacks.
create_dsql_engine defaults to:
sslmode="verify-full"- verifies the server certificate and hostnamesslrootcert="system"- uses the default certificate authority (CA) trust defined by libpq’s TLS backend
See SSL Configuration for detailed setup instructions.
Best Practices
Primary Key Generation
SQLAlchemy applications connecting to Aurora DSQL should use UUID for the primary key column since auto-incrementing integer keys (sequences or serial) are not supported in DSQL. The following column definition can be used to define an UUID primary key column.
Column(
"id",
UUID(as_uuid=True),
primary_key=True,
default=text('gen_random_uuid()')
)
gen_random_uuid() returns an UUID version 4 as the default value.
Dialect Features and Limitations
-
Column Metadata: The dialect fixes an issue related to
"datatype json not supported"when calling SQLAlchemy's metadata() API. -
Foreign Keys: Aurora DSQL does not support foreign key constraints. The dialect disables these constraints, but be aware that referential integrity must be maintained at the application level.
-
Index Creation: Aurora DSQL does not support
CREATE INDEXorCREATE UNIQUE INDEXcommands. The dialect instead usesCREATE INDEX ASYNCandCREATE UNIQUE INDEX ASYNCcommands. See the Asynchronous indexes in Aurora DSQL page for more information.The following parameters are used for customizing index creation
-
auroradsql_include- specifies which columns to includes in an index by using theINCLUDEclause:Index( "include_index", table.c.id, auroradsql_include=['name', 'email'] )
Generated SQL output:
CREATE INDEX ASYNC include_index ON table (id) INCLUDE (name, email)
-
auroradsql_nulls_not_distinct- controls howNULLvalues are treated in unique indexes:Index( "idx_name", table.c.column, unique=True, auroradsql_nulls_not_distinct=True )
Generated SQL output:
CREATE UNIQUE INDEX idx_name ON table (column) NULLS NOT DISTINCT
-
-
Index Interface Limitation:
NULLS FIRST | LAST- SQLalchemy's Index() interface does not have a way to pass in the sort order of null and non-null columns. (Default:NULLS LAST). IfNULLS FIRSTis required, please refer to the syntax as specified in Asynchronous indexes in Aurora DSQL and execute the corresponding SQL query directly in SQLAlchemy. -
Psycopg (psycopg3) support: When connecting to DSQL using the default postgresql dialect with psycopg, an unsupported
SAVEPOINTerror occurs. The DSQL dialect addresses this issue by disabling theSAVEPOINTduring connection.
Developer instructions
Instructions on how to build and test the dialect are available in the Developer Instructions.
Security
See CONTRIBUTING for more information.
License
This project is licensed under the Apache-2.0 License.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aurora_dsql_sqlalchemy-1.1.3.tar.gz.
File metadata
- Download URL: aurora_dsql_sqlalchemy-1.1.3.tar.gz
- Upload date:
- Size: 82.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77f32f36681b866c35c07aa6ced729854a76e19374bae6a63bcae26811d764f4
|
|
| MD5 |
cdc48799e38d004b3b1dbaed2bcff268
|
|
| BLAKE2b-256 |
de51ab7cb50793b55c12c352068acc3f43bb341334ab97ae37541cf498930c89
|
Provenance
The following attestation bundles were made for aurora_dsql_sqlalchemy-1.1.3.tar.gz:
Publisher:
python-sqlalchemy-release.yml on awslabs/aurora-dsql-orms
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aurora_dsql_sqlalchemy-1.1.3.tar.gz -
Subject digest:
77f32f36681b866c35c07aa6ced729854a76e19374bae6a63bcae26811d764f4 - Sigstore transparency entry: 874285247
- Sigstore integration time:
-
Permalink:
awslabs/aurora-dsql-orms@93c13b3437738f5645e42de43e9c156096e23030 -
Branch / Tag:
refs/tags/python/sqlalchemy/v1.1.3 - Owner: https://github.com/awslabs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-sqlalchemy-release.yml@93c13b3437738f5645e42de43e9c156096e23030 -
Trigger Event:
push
-
Statement type:
File details
Details for the file aurora_dsql_sqlalchemy-1.1.3-py3-none-any.whl.
File metadata
- Download URL: aurora_dsql_sqlalchemy-1.1.3-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d33ef4699c2c3367327e25862db8c51aceca4a84c22c7e087095b8f74bcb6f13
|
|
| MD5 |
4aaca7e09d8b98cd66a5c3f4907b524b
|
|
| BLAKE2b-256 |
6891a5eeb39ceb0c31c0143e578de586b63e25108ef511a3031313f145f9f278
|
Provenance
The following attestation bundles were made for aurora_dsql_sqlalchemy-1.1.3-py3-none-any.whl:
Publisher:
python-sqlalchemy-release.yml on awslabs/aurora-dsql-orms
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aurora_dsql_sqlalchemy-1.1.3-py3-none-any.whl -
Subject digest:
d33ef4699c2c3367327e25862db8c51aceca4a84c22c7e087095b8f74bcb6f13 - Sigstore transparency entry: 874285272
- Sigstore integration time:
-
Permalink:
awslabs/aurora-dsql-orms@93c13b3437738f5645e42de43e9c156096e23030 -
Branch / Tag:
refs/tags/python/sqlalchemy/v1.1.3 - Owner: https://github.com/awslabs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-sqlalchemy-release.yml@93c13b3437738f5645e42de43e9c156096e23030 -
Trigger Event:
push
-
Statement type: