types-psycopg2 provides typing stubs for the psycopg2 PostgreSQL adapter for Python. It allows static type checkers like MyPy to verify the correct usage of psycopg2 with type hints. This package is part of the Typeshed project, which aims to provide external type annotations for third-party libraries. The current version is 2.9.21.20260408, and it follows a frequent release cadence, often daily, reflecting updates in Typeshed.
pip install types-psycopg2Verified import paths — ran on the pinned version, not inferred.
Install `types-psycopg2` alongside `psycopg2` to enable static type checking for your database interactions. The example demonstrates a basic connection, cursor usage, data insertion, and retrieval, all of which will be type-checked by tools like MyPy if `types-psycopg2` is installed. Ensure your PostgreSQL server is running and accessible.
Always import from `psycopg2` (e.g., `import psycopg2` or `from psycopg2 import connect`). Type checkers will automatically discover and use the stubs from `types-psycopg2` if installed.
Pin your `types-psycopg2` version to a known good one (e.g., `types-psycopg2==X.Y.Z.build`). Regularly update and re-run your type checker to identify and fix any new type errors.
Check the `types-psycopg2` package description on PyPI for the `psycopg2` version it aims to provide accurate annotations for. Install a `types-psycopg2` version that matches your `psycopg2` installation (e.g., `types-psycopg2==2.9.x.*` for `psycopg2==2.9.x`).