Skip to main content

Async PostgreSQL connection pool helper for FastAPI projects

Project description

pronto-pg

Async PostgreSQL connection pool helper for FastAPI (and any async Python) projects. Wraps psycopg3 with a module-level pool and a ready-made FastAPI lifespan hook.

Installation

pip install pronto-pg

Quick start — FastAPI

from fastapi import FastAPI
from pronto_pg.v1 import make_lifespan, get_connection

app = FastAPI(lifespan=make_lifespan("DATABASE_URL"))

@app.get("/users")
async def list_users():
    async with get_connection() as conn:
        rows = await conn.execute("SELECT id, email FROM users")
        return await rows.fetchall()

Set your DSN in the environment (e.g. via .env + python-dotenv):

DATABASE_URL=postgresql://user:pass@host:5432/mydb

Pass any env var name to make_lifespanPG_DATABASE_URL, MY_DATABASE_URL, whatever fits your project.

API

make_lifespan(env_var="DATABASE_URL", min_size=2, max_size=10)

Returns a FastAPI-compatible lifespan context manager. Opens the pool on startup, closes it on shutdown.

app = FastAPI(lifespan=make_lifespan("PG_DATABASE_URL"))

init_pool(dsn, min_size=2, max_size=10)

Open the pool with an explicit DSN string.

init_pool_from_env(env_var="DATABASE_URL", min_size=2, max_size=10)

Open the pool by reading the DSN from an environment variable. Raises RuntimeError if the variable is missing or empty.

close_pool()

Drain and close the pool. Called automatically by make_lifespan.

get_connection()

Async context manager that yields a psycopg.AsyncConnection in autocommit mode. Use async with conn.transaction(): for explicit transactions.

async with get_connection() as conn:
    await conn.execute("INSERT INTO events (type) VALUES (%s)", ["login"])

Manual lifespan (without make_lifespan)

from contextlib import asynccontextmanager
from fastapi import FastAPI
from pronto_pg.v1 import init_pool_from_env, close_pool

@asynccontextmanager
async def lifespan(app):
    await init_pool_from_env("DATABASE_URL")
    yield
    await close_pool()

app = FastAPI(lifespan=lifespan)

Development

pip install -e ".[dev]"
pytest

Versioning

Versions are derived from git tags via hatch-vcs.

git tag v0.1.0
hatch build

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

pronto_pg-0.0.1.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pronto_pg-0.0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file pronto_pg-0.0.1.tar.gz.

File metadata

  • Download URL: pronto_pg-0.0.1.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pronto_pg-0.0.1.tar.gz
Algorithm Hash digest
SHA256 34bdcc00f3a6735b71d3b13bc74d105135185b2b8b0bfe65f3c9dca73619df89
MD5 4aadace095434221a8f4f087ac30cb2a
BLAKE2b-256 9b7a1aea35f01a1184bd5ca9539238de2f343fb23ae1d891bf03fd547d0d2ecc

See more details on using hashes here.

Provenance

The following attestation bundles were made for pronto_pg-0.0.1.tar.gz:

Publisher: publish.yml on justTil/pronto-pg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pronto_pg-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pronto_pg-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pronto_pg-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4a42315d850d06b1ffcfe768857392baf43d5f0c6c2c99efd9bad392d2e50ab5
MD5 0b9ae7744b814fe51450ac1ee5eb52cd
BLAKE2b-256 92af9ce9fa85df6f388f0428c8f4ffc89739e1ab07f1af06b5bcb40f08937461

See more details on using hashes here.

Provenance

The following attestation bundles were made for pronto_pg-0.0.1-py3-none-any.whl:

Publisher: publish.yml on justTil/pronto-pg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page