Migrating from MongoDB to PostgreSQL
diy-analytics stores all data in PostgreSQL via Drizzle ORM. If you're upgrading from a legacy release (v0.1.0 or earlier) that used MongoDB, this guide walks you through moving your workspaces, projects, goals, alerts, funnels, pageviews, and custom events over using the automated migration script.
Back up your MongoDB database (mongodump) before starting. The migration is read-only against MongoDB, but a snapshot costs you nothing and saves you everything if something goes wrong downstream.
Before you start
- MongoDB stays untouched. The migration script only reads from MongoDB and writes new records to PostgreSQL. Your source database is never mutated.
- Sessions get invalidated. Users log in again once after cutover — session tokens are short-lived and don't transfer across database engines.
- Safe for production datasets. The script streams large
pageviewsandeventscollections in batches rather than loading them into memory.
Migration steps
1. Provision a PostgreSQL database
Set up PostgreSQL 14+ with connection pooling enabled — Neon, Supabase, or Vercel Postgres all work.
2. Configure DATABASE_URL
Add your pooled connection string to .env.local:
3. Keep your MongoDB variables for now
The migration runner needs them to read your legacy data:
4. Apply the PostgreSQL schema
5. Run the migration script
Under the hood, it:
- Translates IDs. Converts MongoDB
ObjectIdstrings into PostgreSQLUUIDprimary keys. - Preserves relationships. Updates every foreign key across Workspace → Project → Goals, Funnels, Pageviews, Events.
- Logs failures separately. Invalid records that fail migration are written to
migration-errors.login your project root instead of halting the run. - Summarizes the result. Prints a side-by-side row-count comparison when it finishes.
6. Verify row counts
Check the console summary to confirm table counts match between MongoDB and PostgreSQL.
7. Remove the MongoDB variables
Once verified:
- Remove
MONGODB_URIandMONGODB_DATABASEfrom your environment. - Trigger a production redeploy on Vercel.
8. Log in
Sign into your dashboard with your existing credentials and confirm your analytics are intact.
Renamed environment variables
| Old (MongoDB) | New (PostgreSQL) |
|---|---|
MONGODB_URI | DATABASE_URL |
MONGODB_DATABASE | Embedded in DATABASE_URL connection path |
MONGODB_STORAGE_CAP_MB | DATABASE_STORAGE_CAP_MB |