pytest-pg
A pytest plugin that provides session-scoped fixtures for running PostgreSQL inside Docker containers.
It automatically spins up a container, waits for PostgreSQL to become ready, exposes connection details
(host, port, user, password, database) via a PG dataclass, and tears the container down after
the test session. Pre-built fixtures are available for PostgreSQL versions 14 through 18 as well as latest.
Readiness checks work with any of the common drivers — asyncpg, psycopg2, or psycopg3.
To speed up tests, pytest-pg does the following tweaks:
- fsync=off
- full_page_writes=off
- synchronous_commit=off
- jit=off
- bgwriter_lru_maxpages=0
- data directory is mounted to a tmpfs
How to use?
You can use the following fixtures:
pg– the latest PostgreSQL image availablepg_14– PostgreSQL 14pg_15– PostgreSQL 15pg_16– PostgreSQL 16pg_17– PostgreSQL 17pg_18– PostgreSQL 18
import asyncpg
async def test_asyncpg_query(pg):
conn = await asyncpg.connect(
user=pg.user,
password=pg.password,
database=pg.database,
host=pg.host,
port=pg.port,
)
await conn.execute("CREATE TABLE test_table (id serial PRIMARY KEY, value text);")
await conn.execute("INSERT INTO test_table (value) VALUES ($1)", "hello")
row = await conn.fetchrow("SELECT value FROM test_table WHERE id = $1", 1)
assert row["value"] == "hello"
await conn.close()
Also run_pg context manager is available, you can use it to create your own fixture, using docker image you need:
import os
import pytest
import pytest_pg
@pytest.fixture(scope='session', autouse=True)
def postgres_env_vars() -> Generator[None]:
docker_image = 'postgres:18'
with pytest_pg.run_pg(docker_image) as pg:
os.environ['POSTGRES_USER'] = pg.user
os.environ['POSTGRES_PASSWORD'] = pg.password
os.environ['POSTGRES_HOST'] = pg.host
os.environ['POSTGRES_PORT'] = str(pg.port)
os.environ['POSTGRES_DBNAME'] = pg.database
yield
# or like so:
@pytest.fixture(scope='session', autouse=True)
def postgres_env_vars(pg_18: pytest_pg.PG) -> Generator[None]:
os.environ['POSTGRES_USER'] = pg_18.user
os.environ['POSTGRES_PASSWORD'] = pg_18.password
os.environ['POSTGRES_HOST'] = pg_18.host
os.environ['POSTGRES_PORT'] = str(pg_18.port)
os.environ['POSTGRES_DBNAME'] = pg_18.database
yield
Using a custom registry
By default the built-in fixtures pull postgres:<version> from Docker Hub. To pull from a mirror
instead (for example, to avoid Docker Hub rate limits), set the base image in pyproject.toml. The
fixtures append the version tag to it, so the mirror must carry the matching tags: <base>:14 …
<base>:18 for the versioned fixtures, and <base>:latest for the pg fixture. Mirrors of pinned
versions often omit latest; if yours does, use a versioned pg_NN fixture instead of pg.
[tool.pytest.ini_options]
pg_docker_image_name = "eu.gcr.io/anna-money/postgres"
Release files for pytest_pg 0.0.30
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytest_pg-0.0.30.tar.gz | 32.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_pg-0.0.30-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 39.5 kB
Release files / pytest_pg-0.0.30.tar.gz
| Download URL | pytest_pg-0.0.30.tar.gz |
|---|---|
| Size | 32.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0aaa5d6cca1f0fb1e85f3ab75b15312ac96a53e83afda55ce5235457d9abcb26
|
|
BLAKE2b-256 checksum How to use checksums |
b8df4ab710f0f1c181db7b5fd3efb05b3907006c485df13544183b2d72001476
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pytest_pg-0.0.30-py3-none-any.whl
| Download URL | pytest_pg-0.0.30-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bd4a56b05988cabf86237c548d872cdf4eaf1ddd3fbf4a851bc035dc8ad28dd7
|
|
BLAKE2b-256 checksum How to use checksums |
de77672ef6448659a6c7c420c95c83ba87f70767fa62b6172d6b3078311db0ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|