Sqlalchemy.exc.nosuchmoduleerror: Can't load plugin: sqlalchemy.dialects:postgres
The NoSuchModuleError
arises due to SQLAlchemy expecting the URI scheme to be postgresql://
rather than postgres://
. Simply adjust your connection string as shown here:
Additionally, do make sure psycopg2
is installed, as this PostgreSQL adapter is integral to SQLAlchemy:
Updating to 'postgresql' schema
SQLAlchemy 1.4+ strictly requires using postgresql://
prefix in your database URI. It's not asking for much:
Deploying an application using Flask? Substitute SQLALCHEMY_DATABASE_URI
to the postgresql://
protocol:
If you're a Heroku fan, remember to update your configuration variables to display postgresql://
.
AppConfig madness
For configuration files like alembic.ini
, ensure the sqlalchemy.url
key conforms to the postgresql://
protocol:
Prefer to specify the driver explicitly? Use "postgresql+psycopg2://"
. SQLAlchemy doesn’t mind:
Regardless of your workflow, ensure your database URL's prefix matches SQLAlchemy's expectations.
Automate your way out
If you need to programmatically rectify DATABASE_URL
within your application, Python's built-in libraries like os
and re
are more than happy to help:
This code snippet arms you with an automated method of keeping your URIs SQLAlchemy-compliant.
Solving common quirks
Even with the correct URI, issues might crop up. But don't fear, solutions are here:
Check for typos
Ensure no spaces or unwanted characters have snuck into your connection string.
Confirm psycopg2 installation
Make sure psycopg2-binary
or psycopg2
resides in your virtual environment and both are updated.
URL's Format
Ensure your URL always sticks to postgresql://user:pass@localhost/dbname
. A slight deviation might create big headaches.
Was this article helpful?