{"id":1761,"library":"types-psycopg2","title":"Psycopg2 Type Stubs","description":"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.","status":"active","version":"2.9.21.20260408","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","database","postgresql","psycopg2","mypy","type-checking"],"install":[{"cmd":"pip install types-psycopg2","lang":"bash","label":"Install package"}],"dependencies":[{"reason":"Provides type stubs for this runtime package.","package":"psycopg2","optional":false}],"imports":[{"note":"This package provides type stubs for `psycopg2` and is used by static type checkers (e.g., MyPy, Pyright, PyCharm) implicitly. You should always import `psycopg2` or its submodules directly; do not attempt to import from `types-psycopg2`.","wrong":"from types_psycopg2 import connect","symbol":"connect","correct":"from psycopg2 import connect"},{"note":"This package provides type stubs for `psycopg2` and is used by static type checkers (e.g., MyPy, Pyright, PyCharm) implicitly. You should always import `psycopg2` or its submodules directly; do not attempt to import from `types-psycopg2`.","wrong":"import types_psycopg2","symbol":"psycopg2","correct":"import psycopg2"}],"quickstart":{"code":"import psycopg2\nimport os\n\n# Configure connection details from environment variables or provide defaults\ndbname = os.environ.get('PG_DBNAME', 'testdb')\nuser = os.environ.get('PG_USER', 'postgres')\npassword = os.environ.get('PG_PASSWORD', 'password')\nhost = os.environ.get('PG_HOST', 'localhost')\nport = os.environ.get('PG_PORT', '5432')\n\ntry:\n    # Establish a database connection\n    conn = psycopg2.connect(f\"dbname={dbname} user={user} password={password} host={host} port={port}\")\n    conn.autocommit = True # For simplicity in this quickstart\n\n    # Open a cursor to perform database operations\n    with conn.cursor() as cur:\n        # Create a table\n        cur.execute(\"CREATE TABLE IF NOT EXISTS test_table (id SERIAL PRIMARY KEY, name VARCHAR(255), age INTEGER)\")\n        print(\"Table 'test_table' ensured to exist.\")\n\n        # Insert data\n        cur.execute(\"INSERT INTO test_table (name, age) VALUES (%s, %s)\", ('Alice', 30))\n        cur.execute(\"INSERT INTO test_table (name, age) VALUES (%s, %s)\", ('Bob', 24))\n        print(\"Data inserted.\")\n\n        # Query data\n        cur.execute(\"SELECT id, name, age FROM test_table ORDER BY id\")\n        rows = cur.fetchall()\n        print(\"Fetched data:\")\n        for row in rows:\n            print(f\"  ID: {row[0]}, Name: {row[1]}, Age: {row[2]}\")\n\n    print(\"Quickstart example completed successfully.\")\n\nexcept psycopg2.Error as e:\n    print(f\"Database error: {e}\")\n    if 'conn' in locals() and not conn.closed:\n        conn.rollback()\nfinally:\n    if 'conn' in locals() and not conn.closed:\n        conn.close()\n        print(\"Database connection closed.\")\n","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"Do not attempt to import any symbols directly from `types-psycopg2`. This package only provides type stubs for static analysis and is not meant for runtime imports.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Updates to type stubs, even in minor versions, can introduce changes that might cause your code to fail type checking, even if the runtime behavior remains unchanged.","severity":"breaking","affected_versions":"All versions"},{"fix":"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`).","message":"Ensure the installed version of `types-psycopg2` is compatible with your `psycopg2` runtime version. The stub package versions are structured to indicate the `psycopg2` version they target.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}