fastfoundry-postgres
PostgreSQL connection building blocks for the fastfoundry ecosystem.
Part of the fastfoundry.* namespace (fastfoundry-settings, fastfoundry-redis, …).
An async SQLAlchemy engine and session lifecycle for a
single PostgreSQL database (via asyncpg), a
declarative model base, and a configuration model that plugs into fastfoundry.settings.
Install
pip install fastfoundry-postgres
# or
uv add fastfoundry-postgres
Requires Python 3.12+.
Configuration
DatabaseCfg holds the connection parameters. It is designed to nest inside a
fastfoundry.settings.BaseAppSettings subclass, so the values load from the environment
with the rest of your service configuration, then get handed to database_ctx:
from fastfoundry.settings import BaseAppSettings
from fastfoundry.postgres import DatabaseCfg
class Settings(BaseAppSettings):
database: DatabaseCfg = DatabaseCfg()
settings = Settings()
Give it either a single dsn or the discrete fields. With the FF_ prefix and
__ nested delimiter that BaseAppSettings applies, that is one of:
# a single DSN secret …
FF_DATABASE__DSN=postgresql+asyncpg://user:pass@host:5432/db
# … or discrete fields
FF_DATABASE__HOST=host
FF_DATABASE__PORT=5432
FF_DATABASE__USER=user
FF_DATABASE__PASSWORD=pass
FF_DATABASE__DATABASE=db
Or construct it directly — DatabaseCfg(dsn="postgresql+asyncpg://user:pass@host:5432/db")
or DatabaseCfg(host="host", user="user", password="pass", database="db"). Notes:
- When
dsnis given it is authoritative: its host/port/user/password/database overwrite the discrete fields. Pool sizing (min_size/max_size/command_timeout) is never part of a DSN and always comes from the fields (defaults5/10/60). dsnandpasswordareSecretStr, so they are masked inreprand never leak when the settings object is logged.- The scheme/driver in the DSN is parsed but not retained —
database_ctxalways connects viapostgresql+asyncpg. The port defaults to5432when the DSN omits it. - The discrete fields carry sensible PostgreSQL defaults (
localhost/5432/postgres/""/postgres), so the config is always fully populated and typed.
Engine & session lifecycle
Open database_ctx(settings.database) once for the application lifetime — it builds the
engine and session factory, publishes them for ambient access (get_engine() and
db_cfg()), and disposes the engine on exit. For FastAPI, wire it into the lifespan:
from contextlib import asynccontextmanager
from collections.abc import AsyncGenerator
from fastapi import FastAPI
from fastfoundry.postgres import database_ctx
from app.settings import settings
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncGenerator[None, None]:
async with database_ctx(settings.database):
yield
app = FastAPI(lifespan=lifespan)
Acquire a session with database_session, and open transactions explicitly with
transaction (sessions use autobegin=False, so nothing starts a transaction for you —
even read-only work should be wrapped, as SQLAlchemy recommends). A nested transaction
becomes a SAVEPOINT:
from fastfoundry.postgres import database_session, transaction
async with database_session() as session:
async with transaction(session):
session.add(user)
Models
Subclass BaseDBModel. The table name defaults to the lower-cased class name with a
trailing s; set __table_name__ to override it:
from sqlalchemy.orm import Mapped, mapped_column
from fastfoundry.postgres import BaseDBModel
class User(BaseDBModel): # -> table "users"
id: Mapped[int] = mapped_column(primary_key=True)
class Account(BaseDBModel):
__table_name__ = "accounts" # explicit table name
id: Mapped[int] = mapped_column(primary_key=True)
Development
mise run install # uv sync --dev
mise run lint # ruff check
mise run typecheck # mypy --strict
mise run test # pytest
mise run build # uv build (sdist + wheel)
License
MIT
Metadata
Release files for fastfoundry-postgres 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastfoundry_postgres-1.1.0.tar.gz | 7.4 kB | With provenance | Details for fastfoundry_postgres-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastfoundry_postgres-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for fastfoundry_postgres-1.1.0-py3-none-any.whl |
Total release size: 16.3 kB
Release files / fastfoundry_postgres-1.1.0.tar.gz
| Download URL | fastfoundry_postgres-1.1.0.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f94e60f4b73b665498b4823bf88a31d87b1a013161aef4b219fe9cdb903611c6
|
|
BLAKE2b-256 checksum How to use checksums |
95acfcd8e8155976287b1a8c629aade91fec53a284ed2e2a5d564f42c0798b95
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 24, 2026.
Transparency logRelease files / fastfoundry_postgres-1.1.0-py3-none-any.whl
| Download URL | fastfoundry_postgres-1.1.0-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6d39fd7e9925fe8187c9301915132ae217acc232df3c09c1d3e611976bb3dfa6
|
|
BLAKE2b-256 checksum How to use checksums |
17033cdedefd7c4d0cb385e29f5a2e1f4c84f10069a67cf08aae2dca2167be65
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 24, 2026.
Transparency logSecurity for fastfoundry-postgres 1.1.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for fastfoundry_postgres-1.1.0.tar.gz
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 Jun 24, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.