pytest-postgresql 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 client connections, automatically cleaning up temporary databases after tests. The current version is 8.0.0, and it is actively maintained.
pip install pytest-postgresql psycopgVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic test using the `postgresql` fixture to interact with a temporary PostgreSQL database. The fixture provides a `psycopg.Connection` object, automatically handling database setup and teardown for isolation.
Ensure your test environment uses Python >= 3.10 and PostgreSQL >= 14.
Upgrade `pytest` to version 8.2 or newer: `pip install -U pytest`.
Install `psycopg` alongside `pytest-postgresql`: `pip install pytest-postgresql psycopg`.
Avoid `COMMIT` in `transacted_postgresql_db` tests. If `COMMIT` is strictly necessary, use the `postgresql_db` fixture instead, which provides full isolation by dropping and recreating the database after each test, though this is slower.