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_lifespan — PG_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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34bdcc00f3a6735b71d3b13bc74d105135185b2b8b0bfe65f3c9dca73619df89
|
|
| MD5 |
4aadace095434221a8f4f087ac30cb2a
|
|
| BLAKE2b-256 |
9b7a1aea35f01a1184bd5ca9539238de2f343fb23ae1d891bf03fd547d0d2ecc
|
Provenance
The following attestation bundles were made for pronto_pg-0.0.1.tar.gz:
Publisher:
publish.yml on justTil/pronto-pg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pronto_pg-0.0.1.tar.gz -
Subject digest:
34bdcc00f3a6735b71d3b13bc74d105135185b2b8b0bfe65f3c9dca73619df89 - Sigstore transparency entry: 1620866349
- Sigstore integration time:
-
Permalink:
justTil/pronto-pg@150e3c317d80f19592483f469a610d6ed9f18ece -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/justTil
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@150e3c317d80f19592483f469a610d6ed9f18ece -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a42315d850d06b1ffcfe768857392baf43d5f0c6c2c99efd9bad392d2e50ab5
|
|
| MD5 |
0b9ae7744b814fe51450ac1ee5eb52cd
|
|
| BLAKE2b-256 |
92af9ce9fa85df6f388f0428c8f4ffc89739e1ab07f1af06b5bcb40f08937461
|
Provenance
The following attestation bundles were made for pronto_pg-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on justTil/pronto-pg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pronto_pg-0.0.1-py3-none-any.whl -
Subject digest:
4a42315d850d06b1ffcfe768857392baf43d5f0c6c2c99efd9bad392d2e50ab5 - Sigstore transparency entry: 1620866467
- Sigstore integration time:
-
Permalink:
justTil/pronto-pg@150e3c317d80f19592483f469a610d6ed9f18ece -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/justTil
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@150e3c317d80f19592483f469a610d6ed9f18ece -
Trigger Event:
push
-
Statement type: