Skip to main content

pgmem for Python

A real PostgreSQL 18 that runs entirely in memory, packaged as a single binary and driven from Python. Prepare the schema once, then give every test its own fork of that state in about 20 ms.

import pgmem, psycopg

with pgmem.start(database="app") as pg:
    with psycopg.connect(pg.template.dsn) as conn:
        conn.execute(open("schema.sql").read())
    snap = pg.template.snapshot()
    with snap.fork() as fork:            # private copy of the prepared database
        psycopg.connect(fork.dsn) ...

pytest

The wheel registers a plugin. Define a session-scoped pgmem_prepare fixture to run migrations and seed data once. @shadow_pg(prepare=True) routes its normal PostgreSQL SDK calls to the template; the plugin snapshots it after the fixture returns:

# conftest.py
from pgmem import shadow_pg

@pytest.fixture(scope="session")
@shadow_pg(prepare=True)
def pgmem_prepare(pgmem_server):
    run_migrations()  # may use psycopg or SQLAlchemy; use asyncio.run for asyncpg
    load_seed()

# test_orders.py
def test_orders(pgmem_dsn):
    with psycopg.connect(pgmem_dsn) as conn: ...

Keep @pytest.fixture outside @shadow_pg(prepare=True) as shown. The pgmem_server parameter supplies the template to the decorator; preparation code can ignore it. Return from the fixture after committing or closing its connections. Override pgmem_snapshot directly when you need custom snapshot options such as max_forks.

pgmem_class_dsn shares one fork across a test class for read-only tests. pgmem_options returns the keyword arguments passed to pgmem.start.

Any driver works (psycopg, asyncpg, pg8000, SQLAlchemy): the wrapper only hands out DSNs. Each connection has its own PostgreSQL backend; pools, transactions and locks behave as on a server.

Select isolation without changing your application fixture

Make a function-scoped application fixture depend on pgmem_test_dsn. It uses a fresh fork by default. @pytest.mark.pgmem(fork=False) selects the shared prepared template for a read-only test.

Override session-scoped pgmem_prepare with a callback accepting the template server to migrate and seed once; the plugin then creates the snapshot. pgmem_snapshot remains available for custom snapshot management.

For a session-scoped client, use pgmem_shared_dsn and mark its serial tests with @pytest.mark.pgmem(isolation="reset"). The plugin restores its shared fork after each marked test, including failed tests. Commit or roll back open transactions before reset. Server.reset(snapshot=None, timeout=5.0) is also available for manual lifecycles.

pgmem_fixture_options configures isolation, max_forks, fork_timeout (default 30 seconds), snapshot_timeout (30 seconds) and reset_timeout (5 seconds). See the complete pytest quickstart.

Route a test with @shadow_pg

Prepare the schema and seeds once in pgmem_prepare as above. The marker intercepts new psycopg 3, psycopg2 and asyncpg connections, including SQLAlchemy engines using those drivers. Application code can keep its usual connection arguments:

import psycopg
from pgmem import shadow_pg

@shadow_pg
def test_create_order():
    with psycopg.connect("postgresql://app@db.example/app") as db:
        ...  # connects to a fresh pgmem fork

@shadow_pg(fork=False)
def test_lookup():
    with psycopg.connect("postgresql://app@db.example/app") as db:
        ...  # reads from one prepared fork shared in this pytest session

It also sets DATABASE_URL and libpq's PGHOST, PGPORT, PGUSER, PGDATABASE, and PGSSLMODE for code that reads those variables; pytest restores them afterward. fork=False is for read-only tests: writes to the shared fork affect later tests. The database does not enforce read-only access. SQLAlchemy can reuse an Engine created earlier; pooled connections from a previous fork are invalidated on checkout. Connections already checked out, asyncpg pools created before the test, and functions copied with from psycopg import connect before the marker runs cannot be rerouted. Use an explicit pgmem_dsn fixture for those patterns.

Other drivers still work through the pgmem_dsn fixture. The wrapper does not replace PostgreSQL's wire protocol or the drivers themselves.

Long-lived HTTP application

For FastAPI, Starlette, Django, or another application that keeps a connection pool alive across tests, use one stable endpoint per pytest worker. Configure the application once with pgmem_live_fork.dsn and request pgmem_live_db in every sequential HTTP test. The latter calls reset() before the test, restoring the prepared snapshot without changing the endpoint or client connections. Override pgmem_live_snapshots with a mapping of names to snapshots and select one with @pytest.mark.pgmem_dataset("name").

If the application creates its schema while it starts, with Alembic in a lifespan handler, Django migrate, or SQLAlchemy create_all, request pgmem_live_baseline where the application is started and call capture() once it is up. Later resets restore that state instead of the prepared snapshot, and named datasets keep their own snapshots. The captured snapshot holds one more copy of the data directory for the session.

An open transaction blocks reset and raises ProtocolError with code busy after the timeout. The live fork holds one snapshot fork slot for the session; raise max_forks if other forks coexist. For Django, use plain pytest tests with its request Client or a separately launched app; avoid TestCase and pytest-django database fixtures that manage rollback or flushing themselves.

Binary

Platform wheels bundle the pgmem binary. PGMEM_BINARY overrides the lookup for platforms without a wheel or for local builds (go build ./cmd/pgmem).

Shared read guard and cleanup / 共有接続のガードと後片付け

pgmem_fixture_options accepts shared_read_only: True to guard selected shared and class DSNs. Isolated writing forks and the stable pgmem_shared_dsn used by reset mode remain writable. readonly_dsn(dsn) creates a guarded URL explicitly. This sets a session default, which callers can deliberately disable. Context managers preserve a primary error and cleanup failures through CleanupError.primary_error and .cleanup_errors on Python 3.9+.

pgmem_fixture_options に shared_read_only: True を指定すると、選択した共有 DSN とクラス用 DSN を保護します。独立した書き込み用フォークと reset 用の pgmem_shared_dsn は書き込み可能です。URL を自分で作る場合は readonly_dsn(dsn) を使えます。意図的に無効化できるセッションの既定値です。context manager は Python 3.9 以降で、元のエラーと解放時の失敗を CleanupError.primary_error と .cleanup_errors に残します。

Runnable SQLAlchemy application / 実行可能な SQLAlchemy アプリ: examples.

Metadata

Release files for pgmem 0.19.2

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

Built distributions (wheels)

Table of built distributions (wheels) for pgmem 0.19.2
File Actions
pgmem-0.19.2-py3-none-win_arm64.whl Python 3 none Windows ARM64 Details for pgmem-0.19.2-py3-none-win_arm64.whl
pgmem-0.19.2-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for pgmem-0.19.2-py3-none-win_amd64.whl
pgmem-0.19.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64, Linux musl 1.1+ x86-64 Details for pgmem-0.19.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl
pgmem-0.19.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64, Linux musl 1.1+ ARM64 Details for pgmem-0.19.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl
pgmem-0.19.2-py3-none-macosx_13_0_arm64.whl Python 3 none macOS 13.0+ ARM64 Details for pgmem-0.19.2-py3-none-macosx_13_0_arm64.whl

Total release size: 87.1 MB

Release files / pgmem-0.19.2-py3-none-win_arm64.whl

Download URL pgmem-0.19.2-py3-none-win_arm64.whl
Size 16.8 MB
Tags Python 3 Windows ARM64
SHA-256 checksum
How to use checksums
ffc67adddbf8b097bfdcac39d9f674ad7ba53dbef9ae5cea135f1056b3a9ac7a
BLAKE2b-256 checksum
How to use checksums
8b015002f1849560c445b41dc5c5467499e69b761561e6d2902b21803aa526f4
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 Oct 5, 2026.

Transparency log

Release files / pgmem-0.19.2-py3-none-win_amd64.whl

Download URL pgmem-0.19.2-py3-none-win_amd64.whl
Size 18.2 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
724d488bb0e4864da903ca2ddded32dc2d19bffaad6ac40bbf5d7dfb8c1b618b
BLAKE2b-256 checksum
How to use checksums
62a8a3bb93f91e6b78950cc23e2e1a14f329e91337355ccebab02fd77394acd8
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 Oct 5, 2026.

Transparency log

Release files / pgmem-0.19.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl

Download URL pgmem-0.19.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl
Size 18.1 MB
Tags Linux glibc 2.17+ x86-64 Linux musl 1.1+ x86-64 Python 3
SHA-256 checksum
How to use checksums
a628442e2195d05414533b0f0d228e041ecafb2ee18b87b158041aaf204e281e
BLAKE2b-256 checksum
How to use checksums
b0b1ea8085122a23c0d401246adca4d55132e0407ad59768ba47e50e55e1a4e4
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 Oct 5, 2026.

Transparency log

Release files / pgmem-0.19.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl

Download URL pgmem-0.19.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl
Size 16.8 MB
Tags Linux glibc 2.17+ ARM64 Linux musl 1.1+ ARM64 Python 3
SHA-256 checksum
How to use checksums
e9c594e6a7fb478f19ed4ab805f44236700ac2e1c2db3b748af971be9cd27d4b
BLAKE2b-256 checksum
How to use checksums
011461db6bb11f828ce248f3023ed76d428b4741c9c8b998834501f25d00ccae
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 Oct 5, 2026.

Transparency log

Release files / pgmem-0.19.2-py3-none-macosx_13_0_arm64.whl

Download URL pgmem-0.19.2-py3-none-macosx_13_0_arm64.whl
Size 17.2 MB
Tags Python 3 macOS 13.0+ ARM64
SHA-256 checksum
How to use checksums
e055a2a9bc816b0cfdae06dfc3ed8972998962f101a306cf023ab082b3ec14f3
BLAKE2b-256 checksum
How to use checksums
13aeee1613d8d1f2a1ab22031d43315e2dbfc277c1dbc44c0cafb1dc40c6a66a
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 Oct 5, 2026.

Transparency log

Security for pgmem 0.19.2

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

All 5 files in this release have been uploaded with provenance metadata.

Select from 5 files

Attestation for pgmem-0.19.2-py3-none-win_arm64.whl

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 Oct 5, 2026.

Transparency log

A 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.

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