Skip to main content

pypg-iam

Python library for pg-iam.

Installation

Basic installation (sync only)

pip install pypg-iam
# or
poetry add pypg-iam

This installs dependencies required for the synchronous API only, which uses SQLAlchemy and psycopg2.

Installation with async support

Note: Async support requires SQLAlchemy 2.0 or higher. The synchronous API works with SQLAlchemy 1.4+.

# Install with all async database drivers: psycopg 3 will be used by default
pip install pypg-iam[async]
# or
poetry add pypg-iam[async]
# or
uv add pypg-iam[async]

# Install with only psycopg 3 (the most widely used PostgreSQL driver for Python, recommended)
pip install pypg-iam[async-psycopg]

# Alternative: only asyncpg (optimized for performance)
pip install pypg-iam[async-asyncpg]

This will install SQLAlchemy 2.x with async support and async database libraries for it to use. It's recommended to pick a specific database driver for your project instead of adding everything, but the "async" dependency group is provided for convenience.

Features

  • Synchronous API: Traditional blocking operations using SQLAlchemy
  • Async API (optional): Native async/await support using SQLAlchemy's asyncio extension with psycopg 3 or asyncpg for modern async frameworks (FastAPI, aiohttp, etc.)

Usage

Synchronous

from iam import Db, iam_engine

dsn = "postgresql://user:pw@host:5432/dbname"
engine = iam_engine(dsn)
db = Db(engine)

# Query data
result = db.exec_sql("select * from persons where name=:name", {'name': 'Alice'})
groups = db.person_groups(person_id)

Asynchronous

import asyncio
from iam import AsyncDb, async_iam_engine

async def main():
    dsn = "postgresql://user:pw@host:5432/dbname"

    # Auto-detect: tries psycopg 3 first, then asyncpg
    engine = async_iam_engine(dsn)

    # Or explicitly request a specific driver:
    # engine = async_iam_engine(dsn, driver='psycopg')  # psycopg 3
    # engine = async_iam_engine(dsn, driver='asyncpg')  # asyncpg

    db = AsyncDb(engine)

    # Query data
    result = await db.exec_sql("select * from persons where name=:name", {'name': 'Alice'})
    groups = await db.person_groups(person_id)

    # Clean up
    await engine.dispose()

asyncio.run(main())

Choosing an async driver

Both psycopg 3 and asyncpg are supported. Choose based on your needs:

  • pypg-iam[async-psycopg] - psycopg 3.x (default)
    • The most widely used PostgreSQL driver, full feature coverage
  • pypg-iam[async-asyncpg] - asyncpg
    • Faster, optimized for high-throughput applications

When calling async_iam_engine(dsn, driver='auto') (auto keyword being the default), pypg-iam will use psycopg 3 if available, otherwise asyncpg. If you explicitly request a driver that isn't installed, you'll get an ImportError with installation instructions.

Tests

Tests use pytest-postgresql to automatically set up a temporary PostgreSQL database with pg-iam schemas. No manual database setup required!

Prerequisites

The test suite automatically:

  • Clones the pg-iam repository to get schema files
  • Starts a temporary PostgreSQL server
  • Installs pg-iam schemas
  • Runs tests
  • Cleans up everything

You just need PostgreSQL binaries installed on your system (psql, initdb, etc.).

Running tests

# Install all dependencies including test and async extras
uv sync --all-extras

# Run all tests (sync + async with both psycopg and asyncpg drivers)
uv run pytest -v

# Run only sync tests
uv run pytest tests/test_sync.py -v

# Run only async tests
uv run pytest tests/test_async.py -v

# Run async tests with a specific driver
uv run pytest tests/test_async.py::TestAsyncPgIam::test_async_pgiam[psycopg] -v
uv run pytest tests/test_async.py::TestAsyncPgIam::test_async_pgiam[asyncpg] -v

Testing against an existing database (optional)

The test fixtures automatically detect and use an existing pg-iam database if you set these environment variables:

# Set postgres environment variables
export PYPGIAM_USER="your_user"
export PYPGIAM_PW="your_password"  # Optional, defaults to empty string
export PYPGIAM_HOST="localhost"
export PYPGIAM_DB="your_db"

# Run tests - they'll use your database instead of creating a temporary one
uv run pytest -v

Note: When using a manual database, the tests expect pg-iam schemas to already be installed. The automated schema installation only happens with pytest-postgresql's temporary databases.

LICENSE

BSD.

Release files for pypg-iam 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pypg-iam 0.8.0
File Size Uploaded
pypg_iam-0.8.0.tar.gz 18.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pypg-iam 0.8.0
File Interpreter ABI Platform
pypg_iam-0.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 39.1 kB

Release files / pypg_iam-0.8.0.tar.gz

Download URL pypg_iam-0.8.0.tar.gz
Size 18.6 kB
Tags Source
SHA-256 checksum
How to use checksums
23e85eef65073cc6181144fcddee3c364c8590dd23728776303133e362298df5
BLAKE2b-256 checksum
How to use checksums
ff01f5e934dfc4331f99d92f000ef02066cfdbaf49dcf30c4e5034577d40f8a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / pypg_iam-0.8.0-py3-none-any.whl

Download URL pypg_iam-0.8.0-py3-none-any.whl
Size 20.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
604484bd01a5f2e133fee3ff40248f5e2dfe25861e7a71891362de197dfebec7
BLAKE2b-256 checksum
How to use checksums
fa50b33394a7d62088f8c833bea9fe027bd827b7550689bd41be376785ab8fe4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files

0.7.8

2 release files

0.7.7

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page