{"id":2237,"library":"pytest-postgresql","title":"pytest-postgresql","description":"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.","status":"active","version":"8.0.0","language":"en","source_language":"en","source_url":"https://github.com/dbfixtures/pytest-postgresql","tags":["pytest","postgresql","testing","database","fixtures","integration-testing"],"install":[{"cmd":"pip install pytest-postgresql psycopg","lang":"bash","label":"Install pytest-postgresql and its mandatory psycopg dependency"}],"dependencies":[{"reason":"Core testing framework that this plugin extends.","package":"pytest","optional":false},{"reason":"Mandatory PostgreSQL adapter (version 3).","package":"psycopg","optional":false}],"imports":[{"note":"The primary fixture providing a psycopg.Connection object.","symbol":"postgresql","correct":"def test_something(postgresql):"},{"note":"A session-scoped fixture managing the PostgreSQL server lifecycle.","symbol":"postgresql_proc","correct":"def test_something_with_proc(postgresql_proc, postgresql):"},{"note":"Used for creating custom PostgreSQL client and process fixtures.","symbol":"factories","correct":"from pytest_postgresql import factories"}],"quickstart":{"code":"import pytest\nfrom psycopg import Connection\n\ndef test_example(postgresql: Connection):\n    \"\"\"Check main postgresql fixture.\"\"\"\n    with postgresql.cursor() as cur:\n        cur.execute(\"CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);\")\n    postgresql.commit()\n\n    # Optional: Verify data insertion\n    with postgresql.cursor() as cur:\n        cur.execute(\"INSERT INTO test (num, data) VALUES (1, 'test_data');\")\n    postgresql.commit()\n\n    with postgresql.cursor() as cur:\n        cur.execute(\"SELECT num, data FROM test WHERE id = 1;\")\n        result = cur.fetchone()\n        assert result == (1, 'test_data')\n","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your test environment uses Python >= 3.10 and PostgreSQL >= 14.","message":"Version 8.0.0 has dropped support for Python 3.9 (which reached End-of-Life) and now only supports PostgreSQL versions 14 and up.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Upgrade `pytest` to version 8.2 or newer: `pip install -U pytest`.","message":"The minimum supported `pytest` version has been bumped to 8.2. Older `pytest` versions might exhibit flaky behavior with fixture chaining on Python 3.12-3.13 when used with `xdist`.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Install `psycopg` alongside `pytest-postgresql`: `pip install pytest-postgresql psycopg`.","message":"`psycopg` (version 3) is now a mandatory requirement. Previously, `psycopg2` was optional.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"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.","message":"Using `COMMIT` statements within tests relying on the `transacted_postgresql_db` fixture can lead to non-deterministic bugs. This fixture assumes tests run within a single transaction and will reset the database, but explicit commits break this isolation.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}