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.

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").

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

Metadata

Release files for pgmem 0.19.1

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.1
File Actions
pgmem-0.19.1-py3-none-win_arm64.whl Python 3 none Windows ARM64 Details for pgmem-0.19.1-py3-none-win_arm64.whl
pgmem-0.19.1-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for pgmem-0.19.1-py3-none-win_amd64.whl
pgmem-0.19.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl Python 3 none Linux musl 1.1+ x86-64, Linux glibc 2.17+ x86-64 Details for pgmem-0.19.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl
pgmem-0.19.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl Python 3 none Linux musl 1.1+ ARM64, Linux glibc 2.17+ ARM64 Details for pgmem-0.19.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl
pgmem-0.19.1-py3-none-macosx_13_0_arm64.whl Python 3 none macOS 13.0+ ARM64 Details for pgmem-0.19.1-py3-none-macosx_13_0_arm64.whl

Total release size: 87.0 MB

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

Download URL pgmem-0.19.1-py3-none-win_arm64.whl
Size 16.8 MB
Tags Python 3 Windows ARM64
SHA-256 checksum
How to use checksums
5c307cec67994f146a7789b98116fe2e2fbc43f46ef023327eb17f2e9c0be2e0
BLAKE2b-256 checksum
How to use checksums
6a8f1fdf74a9549558203bc9b7298fbaec81f2f73567a1dca22998d226b72edb
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 2, 2026.

Transparency log

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

Download URL pgmem-0.19.1-py3-none-win_amd64.whl
Size 18.2 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
f147ecc755cad62e16cb83c6e1957d85628285dbd5be99df8544db2ea3a481fa
BLAKE2b-256 checksum
How to use checksums
fc5662c144d58e6898a9fd746bbba38f3bc885ea3c3d1f1979d94d8ade2bc77a
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 2, 2026.

Transparency log

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

Download URL pgmem-0.19.1-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
0ea3b4f6718cd8c55e78c8fde081c57426be6be12654b135d027d83d2c906522
BLAKE2b-256 checksum
How to use checksums
29cb1fac1146a31349ece8510168fd8439ea22b948ab71246cc2a4f1dad92cdd
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 2, 2026.

Transparency log

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

Download URL pgmem-0.19.1-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
bb783ae6549a47867e73db9840321a259aae1a3cbdacc7abfc6644baac478a41
BLAKE2b-256 checksum
How to use checksums
d33127209396cb8fd5700dd3022d2ac630fdf9e0451dc12ee620ff29a61b08bf
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 2, 2026.

Transparency log

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

Download URL pgmem-0.19.1-py3-none-macosx_13_0_arm64.whl
Size 17.1 MB
Tags Python 3 macOS 13.0+ ARM64
SHA-256 checksum
How to use checksums
12cbd6d9aea4a6754802990b2d9aa074aabe712e7da7149a3b72fe69ba307acb
BLAKE2b-256 checksum
How to use checksums
4b42156b7dd0f32959f6befc72c8a74a96a3f4f217a2e3b2c9d5f79845b85b17
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 2, 2026.

Transparency log

Security for pgmem 0.19.1

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.1-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 2, 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