sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
2 min

banner

# sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres

The error "sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres" occurs when we use the outdated postgres dialect in a connection URL.

To solve the error, use the dialect postgresql in your connection URL instead.

sqlalchemy exc nosuchmoduleerror cant load plugin sqlalchemy dialects postgres

shell
File "/home/borislav/Desktop/bobbyhadz_python/venv2/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 343, in load raise exc.NoSuchModuleError( sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres

Here is an example of how the error occurs.

main.py
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) # ⛔️ Using postgres:// dialect app.config["SQLALCHEMY_DATABASE_URI"] = "postgres://username@localhost:5432/exampledb" db = SQLAlchemy(app)

# Use the postgresql dialect instead

To solve the error, use the postgresql dialect instead.

main.py
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) # ✅ Using postgresql:// dialect app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://username@localhost:5432/exampledb" db = SQLAlchemy(app) print(db)

postgresql connection established

Make sure you don't have 2 s characters next to one another in the string postgresql.

Database URLs use the following format.

shell
dialect://username:password@host:port/database

Username, password, host and port are optional depending on the database type and configuration.

Your connection string for PostgreSQL might look something like this.

shell
postgresql://bobbyhadz:password@localhost:5432/mydatabase

The database URL should start with postgresql://, not with postgres:// as SQLAlchemy v1.4 has removed support for the deprecated postgres dialect.

If you get the error ModuleNotFoundError: No module named 'flask', click on the link and follow the instructions.

# Using the str.replace() method to correct the string

If you have the URL stored in a string or if you get the URL using an environment variable, you can use the str.replace() method to make sure you specify the postgresql dialect instead of postgres.

main.py
# ⛔️ Incorrect URL (postgres dialect) DATABASE_URL = "postgres://username@localhost:5432/exampledb" # ✅ Correct URL (postgresql dialect) DATABASE_URL = DATABASE_URL.replace( 'postgres://', 'postgresql://', 1 ) # 👇️ postgresql://username@localhost:5432/exampledb print(DATABASE_URL)

This approach would work if you get the database URL via an environment variable (e.g. on Heroku).

You can just use the str.replace() method after os.getenv("DATABASE_URL") to replace the incorrect postgres dialect with postgresql.

If the string postgres:// is not contained in your database URL, then nothing is replaced.

Here is an example that loads the DATABASE_URL from an environment variable and replaces the substring.

main.py
import os uri = os.getenv("DATABASE_URL") if uri.startswith("postgres://"): uri = uri.replace("postgres://", "postgresql://", 1) # The rest of the connection code

The code sample is suitable for a Heroku environment and assumes that you have a DATABASE_URL environment variable.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.