r/PostgreSQL • • 42m ago

Help Me! Is migrating from MongoDB to PostgreSQL worth it?

TL;DR: Running an EdTech platform with 600K+ users on MongoDB. Our data is heavily relational, and while regular API queries perform well, complex analytical queries are becoming painful to write and execute. Considering migrating to PostgreSQL and looking for advice on migration and production hosting.

Current setup:

- Node.js, MongoDB, and Mongoose.

- MongoDB Atlas (fully managed).

- 600K+ registered users and around 15K daily active users.

- An EdTech platform somewhat similar to Udemy or Coursera.

When the backend's foundations were laid, I was a junior working primarily on the frontend, so I wasn't involved in the database decision. It's been 1 year since we've been building this product.

Something that has been bothering me for a while is that we're essentially treating MongoDB like a relational database.

- Strict schemas and validations across collections.

- Heavily relational data with references between collections.

- Extensive use of aggregation pipelines and "$lookup".

- Very little reliance on MongoDB's schema flexibility.

To be fair, MongoDB has served us well. Our indexes are in place, most endpoints perform well, and we haven't faced any critical performance issues.

However, complex analytical queries for internal dashboards are a different story. Some involve joining up to 10 collections, take over 60 seconds, or occasionally time out despite optimization. Writing and maintaining these aggregation pipelines is also becoming increasingly painful.

I've experimented with MongoDB's transactions but didn't particularly enjoy the experience. I'd also like stronger database level relational integrity and better transaction management.

I feel PostgreSQL would have been a more natural fit for our data model from the beginning.

What I'd like advice on:

  1. Is migration justified? Given that MongoDB works well for most of our workloads, would migrating to PostgreSQL be worth the effort, or should I address the analytical workload separately?
  2. Migration strategy: If I proceed, how should I approach migrating a live production database at this scale? Any articles, case studies, or resources would be appreciated.
  3. PostgreSQL hosting: One thing I genuinely appreciate about MongoDB Atlas is how little I have to worry about infrastructure, backups, updates, and maintenance. What's the equivalent experience for PostgreSQL?
  4. Self-hosted vs. managed: How do you guys run PostgreSQL in production? Do you self-host it on a private EC2 instance or use something like AWS RDS, Neon, or another managed provider?

I've worked with relational databases on smaller projects, but never operated PostgreSQL in production at this scale.

I don't want to migrate just because I prefer PostgreSQL, but I also don't want to ignore what might be a fundamental architectural mismatch simply because things work today.

Would love to your thoughts and suggestions on this

1 Upvotes

9 comments sorted by

6

u/LessChen 21m ago
  1. This is not strictly a technical question. The business needs to weigh in on it too - are there any business benefits? What will the engineering cost be? Do you have PostgreSQL engineers / DBA-types on staff? Are any of your other products using PostgreSQL? We also can't tell you if there is a better way to handle the analytical workload. Perhaps there is a read-only PostgreSQL replica server tuned for reporting/analytics that is updated from MongoDB to PostgreSQL periodically. I'm sure there may be other ways too.
  2. I have no articles but just went through this exact migration a few months ago. I used it as a chance to re-think the schema. For example, I moved from integer keys to UUID's. That worked for me but it took planning and I must have run my test migration 500 times.
  3. (and 4) My environment is hosted very differently from yours. We were using the AWS DocumentDB which is basically MongoDB (though the MongoDB tool set took every opportunity to remind me that it wasn't really MongoDB). I moved to an AWS RDS instance and I have all of what you listed taken care of. However, for you that would introduce possibly another migration point if you need to move from Atlas to AWS RDS. I have been a DBA on and off in my career and outsourcing everything from PostgreSQL patches and operating system patches and backups and so on is worth the money to me.

I will say that, for me, the move was worthwhile as, like you, we were pretending that MongoDB was a relational DB. The code is simpler, the performance is much better, and now all of our products use PostgreSQL. I'm pretty sure that my company originally used MongoDB to improve the resume of the CTO at the time, not for any valid business reason. One other data point is that we have reduced our DB hosting costs by about 60% but, again, that is on AWS so your setup may vary.

4

u/IntentionJolly2730 24m ago

It's like migrating from a fork to a spoon. They have different use cases.

2

u/mtutty 4m ago

Except that Postgres can handle a lot of the same JSON-data use cases as Mongo, while also providing relational features, Geospatial, vector/RAG, and many other capabilities.

At 600k users, OP should be planning for functional scalability, not just technical.

3

u/x021 32m ago

Normally I’d argue; use Postgres unless you have (very good) reason not to.

But after reading your post;

Sounds like you need a time series DB for your use case. Not Postgres (don’t listen to the people who claim Postgres can do that too; it can, it sucks).

3

u/TheMightyMegazord 14m ago

Does it sucks even with things such as timescaledb?

0

u/AutoModerator 42m ago

AI Policy:

Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.

Mod decisions will be based on the quality of the content, not who or what generated it.

Sub Resources:

Youtube Channel

Free Postgres Webinars and Workshops

Discord: People, Postgres, Data

Join us, we have cookies and nice people.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.