# Migrate a Sequelize App From SQLite to Postgres

Yes. An agent swaps the SQLite file for a Postgres URL in your Sequelize setup, adds pg, checks the models and seed script against a real Postgres, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/migrate-sqlite-to-postgres/sequelize/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads where the database is wired before it changes anything. In an app laid out like sequelize/express-example (the `express-main-example` directory), `package.json` depends on `express` 4.17, `sequelize` 6.3, `sqlite3` 5 and `body-parser`, declares `engines.node >= 10`, and has two scripts: `start` (`node index.js`) and `setup-example-db` (`node sqlite-example-database/setup.js`). `sequelize/index.js` builds the connection with `new Sequelize({ dialect: 'sqlite', storage: 'sqlite-example-database/example-db.sqlite', logQueryParameters: true, benchmark: true })`, with a commented-out line showing the intended production form, `new Sequelize(process.env.DB_CONNECTION_URL)`. The three model files define `user` (a unique `username` validated against `/^\w{3,}$/`), `orchestra` and `instrument` (a `purchaseDate` of `DataTypes.DATE`), and `extra-setup.js` adds the `hasMany` and `belongsTo` between orchestra and instrument. `index.js` calls `sequelize.authenticate()` and exits with the error message if it fails, then listens on 8080; `express/app.js` mounts the CRUD routes under `/api/`. `.gitignore` lists `*.sqlite`, and there are no tests and no workflow.

The code change is small and the verification is the work. The agent replaces `sqlite3` with `pg` and `pg-hstore`, the two packages Sequelize 6 needs for its `postgres` dialect, and rewrites the constructor as `new Sequelize(process.env.DATABASE_URL, { dialect: 'postgres', logQueryParameters: true, benchmark: true })`, failing fast with a clear message when the variable is unset rather than letting `authenticate()` report an unreadable connection string. The setup script moves out of a directory named for SQLite to `scripts/setup-db.js`, the `setup-example-db` script follows it, and `.gitignore` trades `*.sqlite` for `.env`, with a committed `.env.example` holding a local `postgres://` URL.

Then it runs the thing. The sandbox has no database server, so the agent installs PostgreSQL with `apt-get`, starts it, creates a database and a role, exports `DATABASE_URL`, and runs `npm run setup-example-db`: `sequelize.sync({ force: true })` drops and recreates `users`, `orchestras` and `instruments` on Postgres, where the `INTEGER` auto-increment ids become `SERIAL` columns and `DataTypes.DATE` becomes `timestamp with time zone`, and the `bulkCreate` and `orchestra.createInstrument` calls insert the sample rows. It starts the server and requests `/api/users`, `/api/users/1`, `/api/orchestras?includeInstruments` and a `POST` and `PUT` on `/api/users`, reading the SQL that `logQueryParameters` prints to confirm the parameters bind as expected. Two differences it watches for in a larger app and reports on: SQLite compares `LIKE` without case and Postgres does not, so a search route needs `Op.iLike`; and a `DATE` written as a string without a zone is interpreted in UTC by Sequelize on both, so the stored values match but a client that relied on SQLite's text dates may read them differently.

Because the example has no test runner, the agent adds a small smoke test with Node's built-in `node:test` that runs the setup, starts the app on a free port and asserts the `/api/orchestras?includeInstruments` shape, wires it to an `npm test` script, and raises `engines.node` to 18, which `node:test` needs; the pull request says so, since that is a support-window decision for the team. The README's SQLite sentences become Postgres ones, and the pull request opens when the smoke test passes against the sandbox's server.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Migrate this Sequelize app from SQLite to Postgres. Read package.json, index.js, sequelize/index.js, sequelize/extra-setup.js, sequelize/models/*.js, sqlite-example-database/setup.js, express/app.js, .gitignore and readme.md first. Replace sqlite3 with pg and pg-hstore, build the connection from DATABASE_URL with dialect postgres keeping logQueryParameters and benchmark, and fail with a clear message when the variable is unset. Move the setup script to scripts/setup-db.js and update the npm script, replace the *.sqlite gitignore entry with .env, and add .env.example. Install PostgreSQL in the sandbox, start it, run the setup script and the server against it, and exercise the users and orchestras routes including includeInstruments. Add a node:test smoke test for the setup and the orchestras route, an npm test script, and raise engines.node to 18 for it. Update the README. Run npm test until it passes, then open a pull request that lists each schema difference Postgres introduced and any query that would behave differently.",
  "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

The pull request changes `package.json`, `sequelize/index.js`, `.gitignore` and `readme.md`, moves the setup script to `scripts/setup-db.js`, and adds `.env.example` and a `test/` directory with the smoke test. Its description lists the DDL `sync({ force: true })` emitted on Postgres for the three tables, the routes it exercised with the status codes they returned, and the `node:test` output from the sandbox.

A reviewer checks the connection options (SSL and pool settings for the team's hosting, which the example never had), decides on the `engines.node` bump, and reads the two behaviour notes, case-insensitive `LIKE` and date handling, against the app's real queries. The pull request names them as the places to look rather than claiming the app has none.

The data in the SQLite file is not migrated, because the example seeds its own; for an app with real data the pull request lists the tables and points at `pg_dump` and a one-off copy script as the next step.

## Run it on every repository still on sqlite3

A team moving several small services off SQLite sends this body once per repository from its own server. `GET /o/{org}/github` lists every repository the Rush GitHub App installation can reach, so a script fetches each `package.json`, keeps the ones that depend on both `sequelize` and `sqlite3`, and sends one `POST /o/{org}/sessions` per match with that repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started. Each run reads its own models: a service with `DataTypes.JSON` columns gets a note that Postgres offers `JSONB`, and one with migrations under `sequelize-cli` gets a migration instead of `sync`.

An automation on the Project with a `linear` trigger on `linear.issue.labeled`, `database` in `config.labels`, and the prompt above starts one run per labelled issue, with the trigger adding the issue after the prompt and filling in no placeholder, so the issue body says which environment's `DATABASE_URL` the team has provisioned.

## Why run it on Rush?

Changing a dialect is a one-line edit that is only true once the schema has been created on the new database and the queries have run against it: an auto-increment id that becomes `SERIAL`, a date type that changes, a search that was case-insensitive by accident. The agent installs a real PostgreSQL in its own sandbox, runs the sync, seeds the rows and drives the routes, and keeps the SQL log and the test output with the pull request.

A reviewer reads the emitted DDL and the list of queries flagged for behaviour differences rather than trusting that the constructor change was enough. A team with several services starts the same request across all of them at once.

## Frequently asked questions

### Why pg-hstore as well as pg?

Sequelize 6's Postgres dialect requires both: `pg` is the driver and `pg-hstore` serialises the `HSTORE` type the dialect supports. Leaving it out fails at connection time with a message asking for it, so the agent adds both in one step.

### Does sync({ force: true }) work on Postgres?

Yes. It drops and recreates the tables, cascading through the foreign key between instruments and orchestras. It is right for a seeded example and wrong for a database with data, which is why the pull request points at migrations for anything beyond the example.

### What about the existing SQLite data?

The agent does not copy it, because the example regenerates its rows. For an app with real rows it lists the tables and suggests a one-off script that reads from the SQLite file and writes through the same models, or a `pg_dump`-style export, as a separate change.

### Will queries behave differently?

Two are worth checking in any app: `LIKE` is case-insensitive on SQLite and case-sensitive on Postgres, where `Op.iLike` restores the old behaviour, and dates stored as text on SQLite become `timestamp with time zone`. The agent greps the routes for both patterns and lists what it found.

## More Sequelize jobs

- [Write Integration Tests for a Sequelize and Express API](https://getrush.ai/run/write-integration-tests/sequelize/)
- [Add API Error Handling to an Express and Sequelize App](https://getrush.ai/run/add-api-error-handling/sequelize/)
- [Add an Audit Log to a Sequelize and Express API](https://getrush.ai/run/add-audit-log/sequelize/)

## The same job on other stacks

- [Move a Shopify App's Session Storage From SQLite to Postgres](https://getrush.ai/run/migrate-sqlite-to-postgres/shopify-app/)
- [Move a Socket.IO Chat From SQLite to PostgreSQL](https://getrush.ai/run/migrate-sqlite-to-postgres/socket-io/)
- [Migrate a Strapi App From SQLite to PostgreSQL](https://getrush.ai/run/migrate-sqlite-to-postgres/strapi/)

## 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/)
