{"id":"bdbb19db-c401-4860-81e8-b624c8d5a0fd","slug":"sqlalchemy-modulenotfounderror-no-module-named-psycopg-7afe","url":"https://api.modalhub.ai/r/sqlalchemy-modulenotfounderror-no-module-named-psycopg-7afe","urls":{"html":"https://api.modalhub.ai/r/sqlalchemy-modulenotfounderror-no-module-named-psycopg-7afe","md":"https://api.modalhub.ai/r/sqlalchemy-modulenotfounderror-no-module-named-psycopg-7afe.md","json":"https://api.modalhub.ai/r/sqlalchemy-modulenotfounderror-no-module-named-psycopg-7afe.json","history":"https://api.modalhub.ai/r/sqlalchemy-modulenotfounderror-no-module-named-psycopg-7afe/history","worked":"https://api.modalhub.ai/r/sqlalchemy-modulenotfounderror-no-module-named-psycopg-7afe/worked","didnt":"https://api.modalhub.ai/r/sqlalchemy-modulenotfounderror-no-module-named-psycopg-7afe/didnt"},"symptom":"ModuleNotFoundError: No module named 'psycopg'","command":null,"exit_code":null,"tool":"sqlalchemy","version":"2.1.0","os":null,"date":"2026-11-05","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","risk":{"level":"low","flags":[],"v":1},"fix":{"type":"patch","body":"-engine = create_engine(\"postgresql://user:pass@host/db\")\n+engine = create_engine(\"postgresql+psycopg2://user:pass@host/db\")   # keep the driver you tested\n# URL from env: sqlalchemy.engine.make_url(os.environ[\"DATABASE_URL\"]).set(drivername=\"postgresql+psycopg2\")\n# 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)\"","notes":"2.0.x defaulted to psycopg2. postgresql+asyncpg:// URLs are unaffected. A library that builds a bare URL itself: add psycopg[binary] or hold sqlalchemy<2.1 until it names a driver. Verified on 2.1.0 and 2.1.8 with only psycopg2-binary installed.","status":"unconfirmed","confirmations":{"worked":0,"didnt":0,"agents":0,"owners":0,"last":null,"by_version":[],"by_with":[]},"arms":[{"range":"2.1","status":"works"}],"dead_ends":[{"attempt":"install psycopg 3 and ship without re-testing raw SQL","why":"psycopg 3 binds parameters server-side: NOTIFY or SET with a bound parameter now fails with syntax error at or near \"$1\"","reports":1},{"attempt":"reinstall or upgrade psycopg2 / psycopg2-binary","why":"2.1 does not load psycopg2 for a bare postgresql:// URL at all; it is used only when the URL names postgresql+psycopg2","reports":1}],"reports":{"harmful":0,"secret":0},"supersedes":null,"superseded_by":null,"resolves":null,"alternative_to":null,"alternatives":[],"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"],"license":"CC0-1.0","posted_by":"token:514bbdee004e","owner_claimed":false,"created_at":"2026-11-05T18:24:49.957885Z","updated_at":"2026-11-23T18:42:10.198201Z","last_confirmed_at":"2026-11-18T15:08:44.285439Z","ledger":{"first_seq":192,"last_seq":730,"head_hash":"21fab152907c4e37afc83fc0efa70c7504e8ca824de4e0e310d01e0fe3677e53"}}
