pytest-pgsql
Pytest plugins and helpers for tests using a Postgres database.
What it is and what it does
pytest-pgsql is a pytest plugin that automates database fixture management for tests that need PostgreSQL. It provides two main fixtures—postgresql_db and transacted_postgresql_db—each exposing a SQLAlchemy session and either an engine or connection object. The plugin automatically creates a fresh temporary database for each test and cleans it up afterward, ensuring test isolation without manual teardown.
The package integrates freezegun for time manipulation within tests, allowing you to freeze and advance time in both Python and the database. It also provides utility methods for common test setup tasks: loading CSV files into tables, executing SQL scripts with variable binding, managing PostgreSQL extensions, and creating schemas and tables from SQLAlchemy definitions. Both fixtures rely on testing.postgresql to handle temporary instance provisioning.
Use it for:
- Unit testing ORM code that requires a real database without manual setup or teardown between tests.
- Testing time-dependent database logic by freezing and advancing time within test blocks.
- Loading test data from CSV files or SQL scripts into a clean database for each test run.
- Testing PostgreSQL extension availability and installation within test fixtures.
- Verifying schema and table creation logic in database migration or initialization code.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
A pytest plugin that provides fixtures for running tests against temporary, auto-cleaned PostgreSQL databases, with support for time freezing and common database operations.
Yes, with conditions. The package is stable and production-ready for projects that need isolated PostgreSQL test databases. However, adopt it only if you can accept that maintenance is minimal—the last release was 2020-05-13, and there is no active development roadmap. Verify compatibility with your PostgreSQL version and Python version before committing, and monitor the repository for any breaking changes in dependencies like testing.postgresql.
Install
pytest-pgsql on PyPI
pip
pip install pytest-pgsqluv
uv add pytest-pgsqlpoetry
poetry add pytest-pgsqlInstalling pytest-pgsql
Before you install
Low install friction with a pure-Python wheel. However, the package is aging—last release was 2020-05-13 and the last commit 2025-08-20, suggesting minimal active maintenance despite the repository not being archived.
License in practice
Licensed under a permissive BSD license (Copyright Clover Health, Inc.), which imposes no significant restrictions on use or redistribution in most contexts.
Quickstart
pip install pytest-pgsql
import pytest
from pytest_pgsql import postgresql_db
def test_example(postgresql_db):
postgresql_db.session.execute('CREATE TABLE test (id INT)')
postgresql_db.session.commit()
Requires a PostgreSQL server accessible to the test environment; testing.postgresql (a runtime dependency) handles temporary instance setup but still needs PostgreSQL binaries available on the system.
Verify before relying
- Whether the package works reliably with modern PostgreSQL versions beyond those released around 2020.
- Compatibility with Python versions beyond 3.6 (classifiers only list up to 3.6, but requires_python is unspecified).
- Current state of the testing.postgresql dependency and whether it remains actively maintained.
Package facts
| License | Copyright Clover Health, Inc. (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 4 — freezegun, pytest, sqlalchemy, testing.postgresql |
| Maintenance | aging — 2,284 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 85,357/month — #13,928 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_pgsql-1.1.2-py3-none-any.whl
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
testing.postgresqlAutomatically creates and tears down temporary…
permissive · top 5,000 on PyPI
pytest-databasesProvides pytest fixtures that spin up isolated…
permissive · top 15,000 on PyPI
pgsanityPgSanity validates PostgreSQL SQL syntax by…
permissive · top 15,000 on PyPI
py-pglitepy-pglite provides a zero-config PostgreSQL…
permissive · top 15,000 on PyPI
testgrestestgres orchestrates temporary PostgreSQL…
unclear · top 15,000 on PyPI
pytest-postgresqlA pytest plugin that provides fixtures to…
copyleft · top 5,000 on PyPI
pytest-freezerpytest-freezer provides a pytest fixture that…
permissive · top 5,000 on PyPI
pytest-mock-resourcesProvides pytest fixtures that spin up real…
permissive · top 15,000 on PyPI
pytest-datadirA pytest plugin that provides fixtures for…
permissive · top 5,000 on PyPI
pytest-freezegunpytest-freezegun provides a pytest plugin that…
permissive · top 5,000 on PyPI