Skip to content

Add non-blocking asyncpg support to SQLAlchemy Entra authentication #48365

Description

@pabloacan

Is your feature request related to a problem? Please describe.
enable_entra_authentication_async configures an asynchronous SQLAlchemy engine,
but its do_connect event listener is synchronous and requires a synchronous
TokenCredential. As a result, applications using
postgresql+asyncpg and azure.identity.aio.DefaultAzureCredential cannot obtain
Microsoft Entra tokens without introducing custom token caching and connection-event
workarounds. Calling the synchronous credential path from an async application can
also block the event loop.

Describe the solution you'd like
Add a SQLAlchemy integration path for postgresql+asyncpg that accepts an
AsyncTokenCredential and acquires Entra connection information through the
existing get_entra_conninfo_async API.

Because SQLAlchemy's do_connect listener cannot await a coroutine, the integration
should use SQLAlchemy's asynchronous connection-creation hook (async_creator) to:

  • acquire the token and PostgreSQL principal asynchronously;
  • open an asyncpg connection with the Entra token as its password;
  • return the driver connection to SQLAlchemy's async pool.

The existing enable_entra_authentication_async API must remain compatible with
synchronous TokenCredential implementations and async engines backed by drivers
that use the synchronous event hook.

The package should provide:

  • a documented public helper or factory for the asyncpg integration;
  • an optional dependency extra for asyncpg, if required;
  • unit tests covering async credential validation, token injection, failures, and
    concurrent connection creation;
  • a live test and a README example using azure.identity.aio.DefaultAzureCredential.

Describe alternatives you've considered
Applications can manually cache tokens, inject them into asyncpg connection
parameters, and refresh them before creating connections. This duplicates
azure-postgresql-auth behavior, risks stale tokens and secret exposure in URLs,
and does not provide a reusable solution for SQLAlchemy users.

Using enable_entra_authentication_async with a synchronous
DefaultAzureCredential works for supported drivers, but token acquisition still
runs synchronously in the connection event and is unsuitable when the application
requires an end-to-end asynchronous credential flow.

Additional context
The package already exposes get_entra_conninfo_async and supports
AsyncTokenCredential in psycopg3.AsyncEntraConnection; this feature would extend
that capability to SQLAlchemy applications using asyncpg.

The change must not attempt to make SQLAlchemy's do_connect listener awaitable:
SQLAlchemy invokes that listener synchronously.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ClientThis issue points to a problem in the data-plane of the library.PostgreSQL Authcustomer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions