Azure Entra ID authentication extension for Python database drivers
Project description
azurepg-entra: Azure Database for PostgreSQL Entra ID Authentication
This package provides seamless Azure Entra ID authentication for Python database drivers connecting to Azure Database for PostgreSQL. It supports both legacy and modern PostgreSQL drivers with automatic token management and connection pooling.
Features
- 🔐 Azure Entra ID Authentication: Automatic token acquisition and refresh for secure database connections
- 🔄 Multi-Driver Support: Works with psycopg2, psycopg3, and SQLAlchemy
- ⚡ Connection Pooling: Built-in support for both synchronous and asynchronous connection pools
- 🏗️ Clean Architecture: Simple package structure with
azure_postgresql_auth.psycopg2,azure_postgresql_auth.psycopg3, andazure_postgresql_auth.sqlalchemy - 🔄 Automatic Token Management: Handles token acquisition, validation, and refresh automatically
- 🌐 Cross-platform: Works on Windows, Linux, and macOS
- 📦 Flexible Installation: Optional dependencies for different driver combinations
Installation
Basic Installation
Install the core package (includes Azure Identity dependencies only):
pip install azurepg-entra
Driver-Specific Installation
Choose the installation option based on which PostgreSQL drivers you need:
# For psycopg3 (modern psycopg, recommended for new projects)
pip install "azurepg-entra[psycopg3]"
# For psycopg2 (legacy support)
pip install "azurepg-entra[psycopg2]"
# For SQLAlchemy with psycopg3 backend
pip install "azurepg-entra[sqlalchemy]"
# All database drivers combined
pip install "azurepg-entra[drivers]"
# Everything including development tools
pip install "azurepg-entra[all]"
Development Installation
Install from source for development:
git clone https://github.com/v-anarendra_microsoft/entra-id-integration-for-drivers.git
cd entra-id-integration-for-drivers/python
# Install with all dependencies for development
pip install -e ".[all]"
# Or install specific driver combinations
pip install -e ".[psycopg3,dev]"
Configuration
Environment Variables
The samples use environment variables to configure database connections.
Copy .env.example into a .env file in the same directory and update the variables.
POSTGRES_SERVER=<your-server.postgres.database.azure.com>
POSTGRES_DATABASE=<your_database_name>
Quick Start
Running the Samples
The repository includes comprehensive working examples in the samples/ directory:
samples/psycopg2/getting_started/: psycopg2 (legacy driver support)samples/psycopg3/getting_started/: psycopg3 examples (modern driver, recommended)samples/sqlalchemy/getting_started/: SQLAlchemy examples with psycopg3 backend
Configure your environment variables first, then run the samples:
# Copy and configure environment
cp samples/psycopg3/getting_started/.env.example samples/psycopg3/getting_started/.env
# Edit .env with your Azure PostgreSQL server details
# Test psycopg2 (legacy driver)
python samples/psycopg2/getting_started/create_db_connection_psycopg2.py --mode both
# Test psycopg3 (modern driver, recommended)
python samples/psycopg3/getting_started/create_db_connection_psycopg.py --mode both
# Test SQLAlchemy
python samples/sqlalchemy/getting_started/create_db_connection_sqlalchemy.py --mode both
Usage
Choose the driver that best fits your project needs:
- psycopg3: Modern PostgreSQL driver (recommended for new projects)
- psycopg2: Legacy PostgreSQL driver (for existing projects)
- SQLAlchemy: High-level ORM/Core interface
psycopg2 Driver (Legacy Support)
Note: psycopg2 is in maintenance mode. For new projects, consider using psycopg3 instead.
The psycopg2 integration provides synchronous connection support with Azure Entra ID authentication through connection pooling.
Installation
pip install "azurepg-entra[psycopg2]"
Connection Pooling (Recommended)
from azure_postgresql_auth.psycopg2 import EntraConnection # import library
from psycopg2 import pool # import to use pooling
with pool.ThreadedConnectionPool(
minconn=1,
maxconn=5,
host="your-server.postgres.database.azure.com",
database="your_database",
connection_factory=EntraConnection
) as connection_pool:
Direct Connection
from azure_postgresql_auth.psycopg2 import EntraConnection # import library
with EntraConnection(
"postgresql://your-server.postgres.database.azure.com:5432/your_database"
) as conn
psycopg3 Driver (Recommended)
psycopg3 is the modern, actively developed PostgreSQL driver with native async support and better performance.
Installation
pip install "azurepg-entra[psycopg3]"
Synchronous Connection
from azure_postgresql_auth.psycopg3 import EntraConnection # import library
from psycopg_pool import ConnectionPool # import to use pooling
with ConnectionPool(
conninfo="postgresql://your-server.postgres.database.azure.com:5432/your_database",
connection_class=EntraConnection,
min_size=1, # keep at least 1 connection always open
max_size=5, # allow up to 5 concurrent connections
) as pool
Asynchronous Connection
from azure_postgresql_auth.psycopg3 import AsyncEntraConnection # import library
from psycopg_pool import AsyncConnectionPool # import to use pooling
async with AsyncConnectionPool(
conninfo="postgresql://your-server.postgres.database.azure.com:5432/your_database",
connection_class=AsyncEntraConnection,
min_size=1, # keep at least 1 connection always open
max_size=5, # allow up to 5 concurrent connections
) as pool
SQLAlchemy Integration
SQLAlchemy integration uses psycopg3 as the backend driver with automatic Entra ID authentication through event listeners.
For more information: See SQLAlchemy's documentation on controlling how parameters are passed to the DBAPI connect function.
Installation
pip install "azurepg-entra[sqlalchemy]"
Synchronous Engine
from sqlalchemy import create_engine
from azure_postgresql_auth.sqlalchemy import enable_entra_authentication # import library
with create_engine("postgresql+psycopg://your-server.postgres.database.azure.com/your_database") as engine:
# Enable Entra ID authentication
enable_entra_authentication(engine)
# Core usage
with engine.connect() as conn:
# ORM usage
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
Asynchronous Engine
from sqlalchemy.ext.asyncio import create_async_engine
from azure_postgresql_auth.sqlalchemy import enable_entra_authentication_async # import library
async with create_async_engine("postgresql+psycopg://your-server.postgres.database.azure.com/your_database") as engine:
# Enable Entra ID authentication for async
enable_entra_authentication_async(engine)
# Async Core usage
async with engine.connect() as conn:
# Async ORM usage
from sqlalchemy.ext.asyncio import async_sessionmaker
AsyncSession = async_sessionmaker(engine, expire_on_commit=False)
How It Works
Authentication Flow
- Token Acquisition: Uses Azure Identity libraries (
DefaultAzureCredentialby default) to acquire access tokens from Azure Entra ID - Automatic Refresh: Tokens are automatically refreshed before each new database connection
- Secure Transport: Tokens are passed as passwords in PostgreSQL connection strings over SSL
- Server Validation: Azure Database for PostgreSQL validates the token and establishes the authenticated connection
- User Mapping: The token's user principal name (UPN) is mapped to a PostgreSQL user for authorization
Token Scopes
The package automatically requests the correct OAuth2 scopes:
- Database scope:
https://ossrdbms-aad.database.windows.net/.default(primary) - Management scope:
https://management.azure.com/.default(fallback for managed identities)
Security Features
- 🔒 Token-based authentication: No passwords stored or transmitted
- ⏰ Automatic expiration: Tokens expire and are refreshed automatically
- 🛡️ SSL enforcement: All connections require SSL encryption
- 🔑 Principle of least privilege: Only database-specific scopes are requested
Troubleshooting
Common Issues
Authentication Errors
# Error: "password authentication failed"
# Solution: Ensure your Azure identity has been granted access to the database
# Run this SQL as a database administrator:
CREATE ROLE "your-user@your-domain.com" WITH LOGIN;
GRANT ALL PRIVILEGES ON DATABASE your_database TO "your-user@your-domain.com";
Connection Timeouts
# Increase connection timeout for slow networks
conn = SyncEntraConnection.connect(
"postgresql://server:5432/db",
connect_timeout=30 # 30 seconds instead of default 10
)
Windows Async Issues
# Fix Windows event loop compatibility
import asyncio
import sys
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
Debug Logging
Enable debug logging to troubleshoot authentication issues:
import logging
logging.basicConfig(level=logging.DEBUG)
# This will show token acquisition and connection details
conn = SyncEntraConnection.connect("postgresql://server:5432/db")
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
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 azure_postgresql_auth-1.0.0.tar.gz.
File metadata
- Download URL: azure_postgresql_auth-1.0.0.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: RestSharp/106.13.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cedad932f13a2f19a70866c9855f2b29b52950e2189b086ee5273cd3e1be4327
|
|
| MD5 |
5418a60785ab78cde2878c815a793aeb
|
|
| BLAKE2b-256 |
c5f2115b95823a1a183b6be840f4f7cd2f5da2cdfa251e8511b81ce2fdbb570e
|
File details
Details for the file azure_postgresql_auth-1.0.0-py3-none-any.whl.
File metadata
- Download URL: azure_postgresql_auth-1.0.0-py3-none-any.whl
- Upload date:
- Size: 16.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: RestSharp/106.13.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a3310907649698df4e3fa4f52aa6fe204a9e1807eb70041ad0128dcee2f2eef
|
|
| MD5 |
a7203ca90db0f326a667318bf205ab89
|
|
| BLAKE2b-256 |
3a64bc41e816f69a391a8ee8f2ccaaf48daa61ef6b2735480d90442331e589e7
|