Skip to main content

postgres-component

Async Postgres connection-lifecycle component for python-components.

Install

uv add postgres-component

Requirements

  • Python >= 3.11
  • A running Postgres server

Usage

import asyncio

from python_components import System
from postgres_component import PostgresComponent

database = PostgresComponent(url="postgresql+asyncpg://user:pass@localhost:5432/mydb")

system = System({"database": database})

async def main():
    async with system:
        async with database.session() as session:
            await session.execute(...)  # commits on success, rolls back on exception

asyncio.run(main())

PostgresComponent can also be built from individual kwargs instead of a URL:

PostgresComponent(host="localhost", port=5432, user="user", password="pass", database="mydb")

url takes priority over the kwargs when both are given.

Health route

from fastapi_component import create_app

app = create_app(system)  # RouteProvider discovery adds GET /health automatically

Migrations

PostgresComponent never runs migrations itself. migration_wiring() hands your own Alembic env.py the same connection config so it doesn't have to be re-derived:

# alembic/env.py
from postgres_component import create_migration_engine

engine = create_migration_engine(database.migration_wiring())
# use `engine` with Alembic's async run_sync() pattern

Semantics and caveats

  • Fail-fast startup. start() runs an explicit SELECT 1 before considering itself connected — a dead database makes start() raise rather than "starting" successfully and failing later on first query.
  • Managed Session is a unit of work. database.session() begins a transaction, commits on clean exit, and rolls back on exception — one call is one unit of work. For manual control (long-lived reads, streaming), use the raw database.session_factory directly.
  • Errors propagate raw. SQLAlchemy exceptions (OperationalError, IntegrityError, etc.) are never wrapped; get_status()["last_error"] is updated as a string for introspection, but the original exception type always reaches the caller.
  • No ORM, no migrations. The component owns connection lifecycle only. Models and Alembic setup belong to the consuming app; migration_wiring() only removes the boilerplate of re-deriving connection config.

Development

uv sync --all-groups
uv run pytest
uv run ruff format --check .
uv run ruff check .

Integration tests spin up a real Postgres instance via testcontainers. For local development against a persistent instance instead, run docker compose up -d.

License

MIT

Metadata

Release files for postgres-component 0.1.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 postgres-component 0.1.0
File Size Uploaded
postgres_component-0.1.0.tar.gz 15.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for postgres-component 0.1.0
File Interpreter ABI Platform
postgres_component-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.8 kB

Release files / postgres_component-0.1.0.tar.gz

Download URL postgres_component-0.1.0.tar.gz
Size 15.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cd1b73d41ccad07aff5a7b92c6a4be1d818cfb92d1d10aa9dc9653e80aa16e0a
BLAKE2b-256 checksum
How to use checksums
f15625603fdf07c22b35e9a24ae5d5bcb238f8f4ccf1284d9e29b958aa673856
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Release files / postgres_component-0.1.0-py3-none-any.whl

Download URL postgres_component-0.1.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d708cbac99c6e1bccecdf380ba8b41ef0eb58f5340a6cfdaf5f6c090525c981
BLAKE2b-256 checksum
How to use checksums
20f334ebd6be804a23fc28f87e6556fdc86de7ddf06f8e4e953ad6aff5b5612a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

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