Skip to main content

A tiny plugin for pytest which runs PostgreSQL in Docker

Project description

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 11 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:

  1. fsync=off
  2. full_page_writes=off
  3. synchronous_commit=off
  4. jit=off
  5. bgwriter_lru_maxpages=0
  6. data directory is mounted to a tmpfs

How to use?

You can use the following fixtures:

  • pg – the latest PostgreSQL image available
  • pg_11 – PostgreSQL 11
  • pg_12 – PostgreSQL 12
  • pg_13 – PostgreSQL 13
  • pg_14 – PostgreSQL 14
  • pg_15 – PostgreSQL 15
  • pg_16 – PostgreSQL 16
  • pg_17 – PostgreSQL 17
  • pg_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

v0.0.27 (2026-05-01)

v0.0.26 (2026-02-23)

v0.0.25 (2025-05-18)

  • Add pg_17 fixture

v0.0.24 (2025-05-11)

v0.0.23 (2025-04-11)

v0.0.22 (2025-04-03)

  • Override docker host with env variable

v0.0.17 (2024-04-03)

  • Override docker host with env variable

v0.0.15 (2023-05-04)

v0.0.14 (2022-09-19)

  • Disable jit by default
  • Set bgwriter_lru_maxpages

v0.0.12 (2022-01-18)

  • Ditch logging because it seems to be useless

v0.0.11 (2022-01-09)

  • Move volume to tmpfs

v0.0.10 (2022-01-08)

  • Volumes used to remain after running test consuming disk space

v0.0.9 (2021-12-28)

  • Disable fsync, full_page_writes, synchronous_commit to speedup the tests

v0.0.8 (2021-12-26)

  • Add typing marker, mypy. Run tests in CI

v0.0.7 (2021-12-25)

  • Fix is_ready based on psycopg2

v0.0.6 (2021-12-25)

  • Add all recent major versions of PG

v0.0.4 (2021-12-10)

  • Fix is_postgres_ready usage

v0.0.3 (2021-12-10)

  • Fix uuid generation

v0.0.2 (2021-12-10)

  • Follow pytest guide for plugins

v0.0.1 (2021-12-10)

  • A first version

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_pg-0.0.27.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_pg-0.0.27-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest_pg-0.0.27.tar.gz.

File metadata

  • Download URL: pytest_pg-0.0.27.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pytest_pg-0.0.27.tar.gz
Algorithm Hash digest
SHA256 6c13d0f285ab5cdf7b9866525ed74f20b9e79ea45c4a128646f0b6099cf65e3a
MD5 17558bd739cca5f834590da9b52cc50b
BLAKE2b-256 e5fc57680dff6b1d7d4326a19b3f3c16d5a1676d57f61cd10a7ae39b0040a3e4

See more details on using hashes here.

File details

Details for the file pytest_pg-0.0.27-py3-none-any.whl.

File metadata

  • Download URL: pytest_pg-0.0.27-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pytest_pg-0.0.27-py3-none-any.whl
Algorithm Hash digest
SHA256 fb592ed09733bbbe4bfacb8ddae9aa24c3bdd9f014c9f4f9b9db96bb782dee71
MD5 93afdf589b134af1d7be83d9bcb6d704
BLAKE2b-256 dcb71b75d8036069f128c253ae2a423ed41e8e6df777a85bda5b4d05517daa13

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page