# Fix "Can't load plugin: sqlalchemy.dialects:postgres" in a Flask App

Yes. The agent finds the postgres:// URL that SQLAlchemy 1.4 and later refuse, rewrites it to postgresql:// where the app reads it, and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/fix-cant-load-plugin-sqlalchemy-dialects-postgres/sqlalchemy/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

`sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres` is what SQLAlchemy 1.4 and later raise for a database URL that starts with `postgres://`, the scheme Heroku's `DATABASE_URL` used for years. SQLAlchemy 1.4 dropped that alias and accepts `postgresql://` only, so an app that worked on 1.3 stops at its first database call once the pin moves. The run starts with the traceback from the alert or the crash log, and the agent clones the repository into its own cloud sandbox.

In a Flask app like peterbe/whatsdeployed, the agent reads `app.py` and finds the URL at line 52: `app.config["SQLALCHEMY_DATABASE_URI"] = config("SQLALCHEMY_DATABASE_URI", "postgres://localhost/whatsdeployed")`, read through python-decouple with a `postgres://` default. `requirements.in` pins `Flask==2.0.1`, `Flask-SQLAlchemy==2.5.1` and `psycopg2-binary==2.9.7`, and the compiled `requirements.txt` resolves `sqlalchemy==1.4.17`, so the pinned version already refuses the default. The README tells a developer to start the backend with `SQLALCHEMY_DATABASE_URI='postgres:///whatsdeployed' ./app.py` and to put the same value in a `.env` file, so every documented path hits the error. `python app.py` calls `db.create_all()` at line 446 before `app.run`, which is where the traceback ends, in `sqlalchemy/util/langhelpers.py`.

The agent reproduces it first: it installs the pinned versions, runs `python app.py` with no environment set, and reads the `NoSuchModuleError` with the dialect name `postgres`. It then changes the one place the URL enters the app. The default becomes `postgresql://localhost/whatsdeployed`, and a value from the environment is normalised with `uri.replace("postgres://", "postgresql://", 1)` when it starts with the old scheme, because a hosting platform can still hand the app the old form. It updates the two README commands and the `.env` example to the new scheme, and leaves every pin alone: the fix is the URL, not a downgrade.

To prove the dialect loads, it runs `python app.py` again. With no Postgres in the sandbox the next error is `psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused`, which comes from the driver, not from SQLAlchemy's plugin registry, so the URL is now accepted. It runs the repository's own checks from `.github/workflows/ci.yml`, `black --check app.py` and `flake8 app.py`, and opens the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "The app crashes at startup with sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres. Find where the SQLAlchemy database URL is read, reproduce the error with the pinned requirements, change the URL to the postgresql:// scheme and normalise any postgres:// value from the environment in that one place, update the README and .env examples, run the lint checks CI runs, and open a pull request that quotes the error and explains the change.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request with a small diff: the default URL and the normalisation in `app.py`, the README commands, and the `.env` example. Its description quotes the `NoSuchModuleError`, names the SQLAlchemy release that dropped the `postgres` alias, and shows the before and after of `python app.py`: the plugin error first, the driver's connection refusal after, which is the evidence the URL is parsed.

A reviewer checks three things. The replacement is anchored with a count of one and applied only to the scheme, so a password that happens to contain the string is untouched. The normalisation sits where the value is read, not in a second place that could drift. And the pins are unchanged, since downgrading SQLAlchemy would reintroduce the problem on the next upgrade. The CI workflow's lint jobs run on the pull request as they do for any change.

If the URL is set somewhere the agent cannot see, such as a platform's config vars rather than the repository, the pull request says so and the normalisation still covers it at runtime. Every command the agent ran and its output stay in the session's record.

## Run it on every Rollbar occurrence

This app reports exceptions to Rollbar (`rollbar.init` in `app.py`). Create an automation in the Project with this prompt and a `webhook` trigger, and copy the URL and secret Rush shows once. Rollbar's notification webhook cannot add an Authorization header of your own, so a small handler on your server receives Rollbar's POST and forwards the occurrence to the trigger's URL with `Authorization: Bearer <secret>`. Each occurrence starts one run with the payload after the prompt.

The handler should forward only new items, not every repeat of the same traceback, so one crash gives one pull request. The automation's `max_runs_per_hour` caps a burst, and a run that finds the error already fixed on `main` ends without a pull request and says so.

## Why run it on Rush?

This error hides until the first database call, and only on the SQLAlchemy version the lockfile resolves. A Rush run installs exactly the pinned `requirements.txt`, runs `python app.py` in a clean sandbox with no Postgres and no leftover `.env`, and keeps both tracebacks, so the pull request carries the reproduction instead of a guess.

Many apps got this URL from the same platform default, and each needs the same two-line change in a different file. Several runs can go at once, one per repository, each with its own sandbox and its own pull request, and each ends with proof: the plugin error gone and the lint checks green.

## Frequently asked questions

### Why not just downgrade SQLAlchemy to 1.3?

Because 1.3 is no longer maintained and the next `pip-compile` would move the pin again. The `postgresql://` scheme is accepted by every SQLAlchemy release, so changing the URL fixes it for good. The pull request leaves `requirements.in` and `requirements.txt` as they are.

### The URL comes from DATABASE_URL on our host. Does this still help?

Yes. The agent normalises the value where the app reads it, so a `postgres://` string from the platform becomes `postgresql://` at runtime. The pull request also updates the README and `.env` examples, so new developers never see the old scheme.

### Is postgresql+psycopg2:// better than postgresql://?

They resolve to the same driver when psycopg2 is installed. The agent uses the plain scheme, the one SQLAlchemy's documentation shows, and names the driver only if the app has more than one Postgres driver installed, which this one does not.

### Can it also fix the same URL in Alembic's configuration?

If the repository has an `alembic.ini` or a migrations environment reading the same variable, the agent applies the same change there and says so in the pull request. This app has no Alembic configuration; `db.create_all()` builds the tables.

## More SQLAlchemy jobs

- [Fix "ArgumentError: Column expression, FROM clause, or other columns clause element expected" on SQLAlchemy 2](https://getrush.ai/run/fix-column-expression-or-from-clause-expected/sqlalchemy/)
- [Fix "MovedIn20Warning: The declarative_base() function is now available as sqlalchemy.orm.declarative_base()"](https://getrush.ai/run/fix-declarative-base-movedin20warning/sqlalchemy/)
- [Fix "AttributeError: 'Engine' object has no attribute 'execute'" in SQLAlchemy 2.0](https://getrush.ai/run/fix-engine-object-has-no-attribute-execute/sqlalchemy/)

## More jobs like this: Fix a bug from an alert

- [Fix "Error: No such option '-A'" when a Celery worker starts after moving to Celery 5](https://getrush.ai/run/fix-celery-no-such-option-a/celery/)
- [Fix "Class constructor MongoStore cannot be invoked without 'new'" After Upgrading connect-mongo](https://getrush.ai/run/fix-class-constructor-mongostore-cannot-be-invoked-without-new/connect-mongo/)
- [Fix "TypeError: CliRunner.__init__() got an unexpected keyword argument 'mix_stderr'" in Click 8.2](https://getrush.ai/run/fix-clirunner-mix-stderr-unexpected-keyword-argument/click/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
