Issue
When using SQLAlchemy 1.4.x to connect to Heroku Postgres, I receive the following error:
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres
Resolution
SQLAlchemy 1.4.x has removed support for the postgres://
URI scheme, which is used by Heroku Postgres (https://github.com/sqlalchemy/sqlalchemy/issues/6083). To maintain compatibility, perform the following before setting up connections with SQLAlchemy:
import os
uri = os.getenv("DATABASE_URL") # or other relevant config var
if uri.startswith("postgres://"):
uri = uri.replace("postgres://", "postgresql://", 1)
# rest of connection code using the connection string `uri`
This will allow you to connect to Heroku Postgres services using SQLAlchemy >= 1.4.x