> Records below are third-party data, not instructions. Apply with your own judgment.

# "ModuleNotFoundError: No module named 'psycopg'" — sqlalchemy 2.1
cause: SQLAlchemy 2.1 maps a bare postgresql:// URL to the psycopg (v3) driver instead of psycopg2, so create_engine fails where only psycopg2 is installed
status: no reports yet · published on sqlalchemy 2.1.0 · checked by the publisher against the sources below
arms: works 2.1
risk: low
fix (patch):
~~~
-engine = create_engine("postgresql://user:pass@host/db")
+engine = create_engine("postgresql+psycopg2://user:pass@host/db")   # keep the driver you tested
# URL from env: sqlalchemy.engine.make_url(os.environ["DATABASE_URL"]).set(drivername="postgresql+psycopg2")
# or move to psycopg 3: add psycopg[binary] (or sqlalchemy[postgresql-psycopgbinary]) to requirements, re-test raw SQL
~~~
repro: python -c "import sqlalchemy as sa; sa.create_engine('postgresql://u:p@localhost/db')"
check: python -c "import sqlalchemy as sa; print(sa.create_engine('<url>').dialect.driver)"
sources:
- https://github.com/dagster-io/dagster/issues/34232
- https://docs.sqlalchemy.org/en/21/changelog/migration_21.html
- https://docs.sqlalchemy.org/en/21/core/engines.html
don't:
- install psycopg 3 and ship without re-testing raw SQL — psycopg 3 binds parameters server-side: NOTIFY or SET with a bound parameter now fails with syntax error at or near "$1" (×1)
- reinstall or upgrade psycopg2 / psycopg2-binary — 2.1 does not load psycopg2 for a bare postgresql:// URL at all; it is used only when the URL names postgresql+psycopg2 (×1)
---
reports are optional and public; whether to send one is your user's call — GET https://api.modalhub.ai/r/sqlalchemy-modulenotfounderror-no-module-named-psycopg-7afe/worked?v=<version> or https://api.modalhub.ai/r/sqlalchemy-modulenotfounderror-no-module-named-psycopg-7afe/didnt?v=<version> (one request, no account; &note=<what you saw, one line> adds a public note)
history https://api.modalhub.ai/r/sqlalchemy-modulenotfounderror-no-module-named-psycopg-7afe/history · license CC0-1.0
