Skip to main content
https://raw.githubusercontent.com/dbfixtures/pytest-postgresql/main/logo.png

pytest-postgresql

Latest PyPI version Wheel Status Supported Python Versions License

What is this?

This is a pytest plugin that enables you to test code relying on a running PostgreSQL database. It provides fixtures for managing both the PostgreSQL process and the client connections.

Quick Start

  1. Install the plugin:

    pip install pytest-postgresql

    You will also need to install psycopg (version 3). See its installation instructions.

    For async tests with psycopg.AsyncConnection, install the optional async extra:

    pip install pytest-postgresql[async]

    This installs:

    • pytest-asyncio (>= 1.4) — required for @pytest.mark.asyncio and postgresql_async fixtures.

    • aiofiles (>= 23.0) — required only when loading SQL files via the async loader (sql_async).

    On Windows, the plugin configures a SelectorEventLoop automatically for asyncio tests when no earlier pytest-asyncio loop factory is registered. This is required because psycopg async is incompatible with the default ProactorEventLoop on Windows (documented by psycopg). Without it, postgresql_async tests fail with Psycopg cannot use the 'ProactorEventLoop' to run in async mode. No extra configuration is needed when you install pytest-postgresql[async].

    With pytest-asyncio >= 1.4 on Windows, the plugin registers a selector loop factory via pytest-asyncio’s loop-factory hook for all asyncio tests when no prior factory is provided. On Python 3.14+, the legacy asyncio policy fallback is not used because that API is deprecated.

    When an earlier hook implementation already supplies loop factories, those are preserved unchanged. Tests that use a prior factory may show different loop names in pytest IDs (for example test_example[custom] instead of test_example[selector]).

    If you use an older pytest-asyncio (< 1.4) on Windows with Python < 3.14, the plugin falls back to setting a global WindowsSelectorEventLoopPolicy for the entire test session — not only for postgresql async tests. That can change event-loop behaviour for unrelated asyncio tests in the same run. Install pytest-postgresql[async] (which pulls pytest-asyncio >= 1.4) to avoid that legacy path.

    pytest-asyncio configuration

    pytest-asyncio 1.x defaults to asyncio_mode = strict, so each async test must be marked with @pytest.mark.asyncio. If you set asyncio_mode = auto in pytest.ini or pyproject.toml, unmarked async test functions are detected automatically — postgresql_async still requires the [async] extra.

    [pytest]
    asyncio_mode = strict
  2. Run a test:

    Simply include the postgresql fixture in your test. It provides a connected psycopg.Connection object.

    def test_example(postgresql):
        """Check main postgresql fixture."""
        with postgresql.cursor() as cur:
            cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
            postgresql.commit()

    For async code, use postgresql_async with pytest.mark.asyncio:

    import pytest
    
    @pytest.mark.asyncio
    async def test_example_async(postgresql_async):
        """Check main async postgresql fixture."""
        async with postgresql_async.cursor() as cur:
            await cur.execute(
                "CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);"
            )
            await postgresql_async.commit()

How to use

How does it work

Project Architecture Diagram (sync fixtures) Project Architecture Diagram (async fixtures)

The plugin provides two main types of fixtures:

1. Client Fixtures

These provide a connection to a database for your tests.

  • postgresql - A function-scoped fixture. It returns a connected psycopg.Connection. After each test, it terminates leftover connections and drops the test database to ensure isolation.

  • postgresql_async - The async counterpart. It returns a connected psycopg.AsyncConnection. Requires pytest-postgresql[async] (pytest-asyncio >= 1.4), and each test must be marked with @pytest.mark.asyncio.

Async fixtures

postgresql_async and custom factories created with factories.postgresql_async are async generator fixtures using pytest_asyncio.fixture.

Minimum versions when installing manually instead of via [async]:

pytest-asyncio >= 1.4
aiofiles >= 23.0        # only for async SQL file loading

If pytest-asyncio is missing, fixture setup raises ImportError.

Async SQL file loading

Process and noproc fixtures always populate their template database synchronously during session setup (via DatabaseJanitor.load()), even when you use postgresql_async as the client fixture. SQL Path entries in a process fixture load list are executed with the sync sql() loader.

Use sql_async (requires aiofiles from the [async] extra) when you call AsyncDatabaseJanitor.load() directly with a Path. Callable loaders passed to AsyncDatabaseJanitor.load() may be sync or async; return values that are awaitable are awaited automatically.

from pathlib import Path
from pytest_postgresql import factories

postgresql_my_proc = factories.postgresql_proc(load=[Path("schema.sql")])
postgresql_my_async = factories.postgresql_async("postgresql_my_proc")
2. Process Fixtures

These manage the PostgreSQL server lifecycle.

  • postgresql_proc - A session-scoped fixture that starts a PostgreSQL instance on its first use and stops it when all tests are finished.

  • postgresql_noproc - A fixture for connecting to an already running PostgreSQL instance (e.g., in Docker or CI).

Customizing Fixtures

You can create additional fixtures using factories:

from pytest_postgresql import factories

# Create a custom process fixture
postgresql_my_proc = factories.postgresql_proc(
    port=None, unixsocketdir='/var/run')

# Create a client fixture that uses the custom process
postgresql_my = factories.postgresql('postgresql_my_proc')

# Async client fixture (requires pytest-postgresql[async], pytest-asyncio >= 1.4)
postgresql_my_async = factories.postgresql_async('postgresql_my_proc')

Pre-populating the database for tests

If you want the database to be automatically pre-populated with your schema and data, there are two levels you can achieve it:

  1. Per test: In a client fixture, by using an intermediary fixture.

  2. Per session: In a process fixture.

The process fixture accepts a load parameter, which supports:

  • SQL file paths: Loads and executes the SQL files.

  • Loading functions: A callable or an import string (e.g., "path.to.module:function"). These functions receive host, port, user, dbname, and password and must perform the connection themselves (or use an ORM).

The process fixture pre-populates the database once per session into a template database. The client fixture then clones this template for each test, which significantly speeds up your tests.

from pathlib import Path
postgresql_my_proc = factories.postgresql_proc(
    load=[
        Path("schemafile.sql"),
        "import.path.to.function",
        load_this_callable
    ]
)

Defining pre-population on the command line:

pytest --postgresql-load=path/to/file.sql --postgresql-load=path.to.function

If a loaded .sql file contains statements that cannot run inside a transaction block (for example CREATE DATABASE), enable autocommit on the loader connection. This can be set without writing a custom loader, via the factory argument, the command line, or pytest.ini:

postgresql_my_proc = factories.postgresql_proc(load=[Path("with_create_db.sql")], load_autocommit=True)
pytest --postgresql-load-autocommit

Connecting to an existing PostgreSQL database

To connect to an external server (e.g., running in Docker), use the postgresql_noproc fixture.

For async tests against an external server, create a client fixture with factories.postgresql_async("postgresql_noproc") (see tests/examples/test_drop_test_database_async.py).

postgresql_external = factories.postgresql('postgresql_noproc')

By default, it connects to 127.0.0.1:5432.

Using a maintenance database other than postgres

Creating and dropping the test databases requires a connection to a database that already exists - by default postgres. If the test role merely lacks the privilege, GRANT CONNECT ON DATABASE postgres TO myuser is the simpler fix. When postgres is not reachable at all - behind a connection pooler with a fixed database list, or on a managed server that does not expose it - point the noproc fixture at another database:

pytest --postgresql-maintenance-dbname=my_existing_db
postgresql_external = factories.postgresql_noproc(maintenance_dbname="my_existing_db")

The database is only connected to, never created, modified or dropped. Avoid template1: CREATE DATABASE clones it when no template is given, and PostgreSQL does not expect the source database to be in use while it is being copied.

Chaining fixtures

You can chain multiple postgresql_noproc fixtures to layer your data pre-population. Each fixture in the chain will create its own template database based on the previous one.

from pytest_postgresql import factories

# 1. Start with a process or a no-process base
base_proc = factories.postgresql_proc(load=[load_schema])

# 2. Add a layer with some data
seeded_noproc = factories.postgresql_noproc(depends_on="base_proc", load=[load_data])

# 3. Add another layer with more data
more_seeded_noproc = factories.postgresql_noproc(depends_on="seeded_noproc", load=[load_more_data])

# 4. Use the final layer in your test
client = factories.postgresql("more_seeded_noproc")
Fixture Chaining Diagram

Configuration

You can define settings via fixture factory arguments, command line options, or pytest.ini. They are resolved in this order:

  1. Fixture factory argument

  2. Command line option

  3. pytest.ini configuration option

Configuration options

Setting

postgresql_proc argument

postgresql_noproc argument

Command line option

pytest.ini option

Default

Path to executable

executable

n/a

–postgresql-exec

postgresql_exec

discovered (see note)

host

host

host

–postgresql-host

postgresql_host

127.0.0.1

port

port

port

–postgresql-port

postgresql_port

random (proc), 5432 (noproc)

Port search count

—

n/a

–postgresql-port-search-count

postgresql_port_search_count

5

postgresql user

user

user

–postgresql-user

postgresql_user

postgres

password

password

password

–postgresql-password

postgresql_password

Starting parameters (extra pg_ctl arguments)

startparams

n/a

–postgresql-startparams

postgresql_startparams

-w

Postgres exe extra arguments (passed via pg_ctl’s -o argument)

postgres_options

n/a

–postgresql-postgres-options

postgresql_postgres_options

Location for unixsockets

unixsocket

n/a

–postgresql-unixsocketdir

postgresql_unixsocketdir

$TMPDIR

Database name

dbname

dbname (handles xdist)

–postgresql-dbname

postgresql_dbname

tests

Maintenance database name

n/a

maintenance_dbname

–postgresql-maintenance-dbname

postgresql_maintenance_dbname

postgres

Default Schema (load list)

load

load

–postgresql-load

postgresql_load

Autocommit for the SQL loader connection

load_autocommit

load_autocommit

–postgresql-load-autocommit

postgresql_load_autocommit

False

Test database cloning strategy

create_strategy

create_strategy

–postgresql-create-strategy

postgresql_create_strategy

PostgreSQL’s default

PostgreSQL connection options

options

options

–postgresql-options

postgresql_options

Drop test database on start

—

—

–postgresql-drop-test-database

false

Fixture to layer the template database on

n/a

depends_on

— means the setting applies to that fixture but has no factory argument; n/a means the setting does not apply to that fixture at all.

Examples

Using SQLAlchemy

This example shows how to create an SQLAlchemy session fixture:

from typing import Iterator
import pytest
from psycopg import Connection
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker, scoped_session
from sqlalchemy.pool import NullPool

@pytest.fixture
def db_session(postgresql: Connection) -> Iterator[Session]:
    """Session for SQLAlchemy."""
    user = postgresql.info.user
    host = postgresql.info.host
    port = postgresql.info.port
    dbname = postgresql.info.dbname

    connection_str = f'postgresql+psycopg://{user}:@{host}:{port}/{dbname}'
    engine = create_engine(connection_str, echo=False, poolclass=NullPool)

    # Assuming you use a Base model
    from my_app.models import Base
    Base.metadata.create_all(engine)

    SessionLocal = scoped_session(sessionmaker(bind=engine))
    yield SessionLocal()

    SessionLocal.close()
    Base.metadata.drop_all(engine)

Advanced Usage: Test database cloning

Use create_strategy on postgresql_proc or postgresql_noproc to choose how synchronous and asynchronous client fixtures clone the template database for each test. Initial and layered template creation keep the server default.

postgresql_my_proc = factories.postgresql_proc(create_strategy="clone")
postgresql_my = factories.postgresql("postgresql_my_proc")

For an existing server, including PostgreSQL in Docker, configure the noproc factory instead:

postgresql_external = factories.postgresql_noproc(create_strategy="clone")
postgresql_my = factories.postgresql("postgresql_external")
postgresql_my_async = factories.postgresql_async("postgresql_external")

The same setting is available on the command line and in pytest.ini:

pytest --postgresql-create-strategy=clone
[pytest]
postgresql_create_strategy = clone

The available values are:

  • Unset (the default): no STRATEGY clause or session setting is sent.

  • wal_log: CREATE DATABASE ... STRATEGY WAL_LOG (PostgreSQL 15+).

  • file_copy: SET file_copy_method = copy, then CREATE DATABASE ... STRATEGY FILE_COPY (PostgreSQL 18+).

  • clone: SET file_copy_method = clone, then CREATE DATABASE ... STRATEGY FILE_COPY (PostgreSQL 18+).

The session setting and database creation use the same connection; no server restart is needed. PostgreSQL reports unsupported settings or strategies directly. Factory arguments take precedence over command line and ini options. Chained fixtures use the client-facing noproc factory’s argument or global configuration, not the strategy of its depends_on fixture.

clone needs operating-system support, and sharing disk blocks depends on the server’s filesystem supporting reflinks. It is not guaranteed to improve performance; compare the full test suite at its usual worker count. For strategy, checkpoint and filesystem details, see PostgreSQL’s CREATE DATABASE documentation and file_copy_method documentation.

Advanced Usage: DatabaseJanitor

DatabaseJanitor is an advanced API for managing database state outside of standard fixtures. It is used by projects like Warehouse (pypi.org).

import psycopg
from pytest_postgresql.janitor import DatabaseJanitor

def test_manual_janitor(postgresql_proc):
    with DatabaseJanitor(
        user=postgresql_proc.user,
        host=postgresql_proc.host,
        port=postgresql_proc.port,
        dbname="my_custom_db",
        password="secret_password",
    ):
        with psycopg.connect(
            dbname="my_custom_db",
            user=postgresql_proc.user,
            host=postgresql_proc.host,
            port=postgresql_proc.port,
            password="secret_password",
        ) as conn:
            # use connection
            pass

Advanced Usage: AsyncDatabaseJanitor

AsyncDatabaseJanitor is the async counterpart to DatabaseJanitor. Use it when managing database state with psycopg.AsyncConnection outside of standard fixtures. It requires psycopg (a core dependency). Install pytest-postgresql[async] when you need aiofiles for SQL file loading via sql_async, or pytest-asyncio for pytest async tests.

import pytest
import psycopg
from pytest_postgresql.janitor import AsyncDatabaseJanitor

@pytest.mark.asyncio
async def test_manual_async_janitor(postgresql_proc):
    async with AsyncDatabaseJanitor(
        user=postgresql_proc.user,
        host=postgresql_proc.host,
        port=postgresql_proc.port,
        dbname="my_custom_db",
        password="secret_password",
    ):
        async with await psycopg.AsyncConnection.connect(
            dbname="my_custom_db",
            user=postgresql_proc.user,
            host=postgresql_proc.host,
            port=postgresql_proc.port,
            password="secret_password",
        ) as conn:
            # use async connection
            pass

Connecting to PostgreSQL in Docker

To connect to a Docker-run PostgreSQL, use the noproc fixture.

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecret -d postgres

In your tests:

from pytest_postgresql import factories

postgresql_in_docker = factories.postgresql_noproc()
postgresql = factories.postgresql("postgresql_in_docker", dbname="test")

def test_docker(postgresql):
    with postgresql.cursor() as cur:
        cur.execute("SELECT 1")

Run with:

pytest --postgresql-host=172.17.0.2 --postgresql-password=mysecret

Basic database state for all tests

You can define a load function and pass it to your process fixture factory:

import psycopg
from pytest_postgresql import factories

def load_database(**kwargs):
    with psycopg.connect(**kwargs) as conn:
        with conn.cursor() as cur:
            cur.execute("CREATE TABLE stories (id serial PRIMARY KEY, name varchar);")
            cur.execute("INSERT INTO stories (name) VALUES ('Silmarillion'), ('The Expanse');")

postgresql_proc = factories.postgresql_proc(load=[load_database])
postgresql = factories.postgresql("postgresql_proc")

def test_stories(postgresql):
    with postgresql.cursor() as cur:
        cur.execute("SELECT count(*) FROM stories")
        assert cur.fetchone()[0] == 2

The process fixture populates the template database once, and the client fixture clones it for every test. This is fast, clean, and ensures no dangling transactions. This approach works with both postgresql_proc and postgresql_noproc.

Metadata

Release files for pytest-postgresql 11.0.0

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

Source distribution (sdist)

Source distribution for pytest-postgresql 11.0.0
File Size Uploaded Provenance Actions
pytest_postgresql-11.0.0.tar.gz 51.4 kB With provenance Details for pytest_postgresql-11.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-postgresql 11.0.0
File Interpreter ABI Platform Actions
pytest_postgresql-11.0.0-py3-none-any.whl Python 3 none any Details for pytest_postgresql-11.0.0-py3-none-any.whl

Total release size: 95.7 kB

Release files / pytest_postgresql-11.0.0.tar.gz

Download URL pytest_postgresql-11.0.0.tar.gz
Size 51.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e41dc36787b98a0522988e85199df587c7cca8be6dcc554e4e86ad5f4099d5e9
BLAKE2b-256 checksum
How to use checksums
1554f38ff69dd999cca446952f89614b588a98ba3aeac1533bfa1c198aa84877
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Dec 4, 2026.

Transparency log

Release files / pytest_postgresql-11.0.0-py3-none-any.whl

Download URL pytest_postgresql-11.0.0-py3-none-any.whl
Size 44.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1bc008d64ee6344aa1c6ec8036b00be25c41d83fbb056d11e88b265b6c30ca6e
BLAKE2b-256 checksum
How to use checksums
ad1bcb2103ac7fadb6d786f6a2522b64ad68992dddd9157b9074bed92d0272cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Dec 4, 2026.

Transparency log

Security for pytest-postgresql 11.0.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.

Select from 2 files

Attestation for pytest_postgresql-11.0.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 Dec 4, 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.

Release history Release notifications | RSS feed

This release

11.0.0 This release

2 release files Full provenance

0.0.0

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