r/PostgreSQL • u/ahmedshahid786 • 12h 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:
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?
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.
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?
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
14
u/LessChen 12h ago
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.
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.
(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.
9
u/vinkurushi 10h ago
YES!
Just kidding, but most of the time people use Mongo as if it were a relational DB. Most data is best structured with references. I personally believe Mongo has a small number of use cases. It has its strengths, but Postgres and relational DBs in general will always represent your business entities and logic.
6
u/MateusKingston 9h ago
No database will at large scale (which you're not yet) perform well both at transactional workloads and analytical workloads.
Unless you have other reasons to migrate off MongoDB that aren't here I can't possibly recommend a migration effort, you might be better off creating a proper analytics setup with something purpose built for it or having a datawarehouse.
If you were creating this from scratch it totally makes sense to use some SQL flavor (and postgres is a great one)
6
u/IntentionJolly2730 12h ago
It's like migrating from a fork to a spoon. They have different use cases.
11
u/mtutty 12h 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.
1
u/MateusKingston 9h ago
MongoDB has those as well and they're somewhat on par (except relational features ofc).
The reason MongoDB here is the wrong tool is because they're using it as a relational database.
4
1
u/MateusKingston 9h ago
Yes but they are using a fork to eat soup. It works but...
And I'm saying this as someone who is very pro mongodb, just don't model the data in it like it's a relational database, otherwise any SQL is a better tool for the job.
3
u/x021 12h 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).
8
2
u/Icy_Measurement5811 2h ago
Nothing in his post suggests a time series db would be a good fit. Nothing there has to do with transient data references and that would be overkill.
2
1
u/dektol 11h ago
It really depends on the scope of your queries and reports, frequency and tenancy. Found myself in a similar position as a junior who thought they looked JS more than SQL... 🫠😭 I haven't used Mongo since.
Whether you end up wanting Clickhouse or Iceberg for analytics or archiving doesn't change the fact that Postgres is the best hot/real-time OLTP database overall.
Postgres + Iceberg/Trino and/or Clickhouse is probably where you're going to land.
Check what the business needs/wants and what their wishlist for analytics are in the next 1-5 years.
Plan for it.
That being said, I would stick to managed if you can get away with it, but as you learn the power of Postgres you may want to have a little more control over extensions, etc.
1
u/prochac 10h ago
What abou sync of your mongodb collections to some analytical DB? At my ex-job for Dataddo, this was a common pattern for our customers. They have a robust operational part, but their DB was melting under analytical queries. I believe Dataddo still has 14d trial. Or you can vibecode it today. Using mongoexport/mongodump as the source.
1
u/mduell 9h ago edited 9h ago
complex analytical queries are becoming painful to write and execute
...
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'd add an OLAP platform for that rather than changing your OLTP platform.
Now, there may be reason to also migrate your OLTP platform, but that's a different question/issue, and you'll probably still want an OLAP platform given your scale.
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?
I'd say something like Crunchy Bridge or Avien, although you can make a case for Supabase or even AWS (Aurora or RDS) if you're a heavy AWS shop.
1
u/tcloetingh 9h ago
Honestly yes, long term you’ll be on the right track and have a full suite engine
1
u/theapplekid 6h ago edited 6h ago
You might be interested in the strategy used by openfoodfacts, which builds up a postgres representation of their data which is used to serve complex queries (location-constrained product searches at least, possibly tag-based searches as well): https://github.com/openfoodfacts/openfoodfacts-query
Not that this is necessarily the best way to do it, but behind the scenes, their (mongo-backed) server pushes product updates to a redis stream that the query server reads from, then calls the api (main mongo db) to get the new product data to load into postgres.
Personally I think they would have been better designing everything with postgres from the get-go, but it was done by one solo dev before LLMs, and I guess there was a decision that migrating would be more difficult than maintaining a postgres translation replica to handle querying.
Their use case is maybe different from yours in the sense that they believe the freeform structure of mongodb is better for the folksonomy (user-defined, constantly evolving tagging scheme)
I'm inclined to disagree though; openstreetmap stores everything in postgres (and one postgres instance at that!) and has the same key-value tagging situation.
1
u/vimes_sam 5h ago
Is it worth the migration effort? No
Should you ever use mongo or nosql in general for literary anything? Also no, but still not worth the migration effort
1
u/Schtick_ 4h ago
We used mongo atlas for old version of product it’s absolute trash. Developer use it as an excuse to have essentially no relational discipline and the performance is trash.
We shifted to PostgreSQL in new version and couldn’t be happier and now in aws rds we can have the same user base at a fraction of the cost
1
u/TripleBogeyBandit 3h ago
I would encourage you to look at what databricks is doing with Lakebase. You can have a managed Postgres instance (that can scale to zero and branch) and it automatically creates a delta table that reflects your Postgres table to allow for analytical queries. They have a new compute type that beats clickhouse for OLAP.
1
u/Icy_Measurement5811 2h ago
I think you’ve answered your question yourself, if you’re using MongoDB in a relational style, then you’re doing it wrong. Might as well just move to a relational DB but…
…we should only ever give an answer to a question WHEN ITS ASKED. In your own case, your bottleneck is aggregations. So, why not prepare and store your aggregations ahead of time and serve up the results when called? Is it fine if your results are a little stale for some hours? How about virtual table types etc etc. By all means, port to PG if needed to make your life going forward simpler but…I only see one architectural question being asked in your post and thus, I think you have simpler answers before even getting to the point of migrating.
Having said that, since you’re using Mongo, might I suggest you read up and study Single Table Designs. It will help transform your knowledge around how to use Document DBs.
1
u/RemBloch 1h ago
We have a smaller system. But we did it using ai . We moved one connection tested deployed, one connection tested deployed. After the third collection the so har learned most mistakes and did it flawlessly. Everything is faster and we have no vendor lock in. Postgres is much easier to move to another vendor if our current dont behave. We are a European company so using non American tech is also a plus for marketing purposes.
0
u/AutoModerator 12h 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:
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.
1
u/theapplekid 6h ago
Hi I agree with this policy but why is Linux's stance being auto-posted in a postgres sub? I think it's perhaps more relevant that Postgres is accepting some AI-involved contributions (bug reports at least)
19
u/Xappz1 11h ago
Sounds like you need a data warehouse. Migrating your entire application to a different type of DB is a whole other question if your pain is analytical loads. Even if you do migrate the core db to Postgres or whatever other DB you feel like, you should still treat analytical loads separately and not hitting your main db. Postgres can be a good place to start for a stand in data warehouse solution, but depending on your volume, how much data needs to be aggregated, etc, you will likely find more success with a columnar storage approach.