r/PostgreSQL • u/ahmedshahid786 • 4d 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
21
u/LessChen 4d ago
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.
18
u/vinkurushi 4d 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.
31
u/Xappz1 4d 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.
11
u/MateusKingston 4d 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)
7
u/IntentionJolly2730 4d ago
It's like migrating from a fork to a spoon. They have different use cases.
2
u/dektol 4d 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.
2
u/vimes_sam 4d 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
2
u/humanshield85 4d ago
I wouldn’t migrate, check if a columnar db can’t solve your issue here ?
2
u/x021 4d 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).
1
u/prochac 4d 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/Discordbothost 4d ago
Take the slowest dashboard and run its Mongo and SQL versions against the same frozen export. Compare totals by course and week as well as runtime. A mismatch can surface missing references or duplicate joins before you schedule a data sync or touch the API.
1
u/mduell 4d ago edited 4d ago
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.
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 4d ago
Honestly yes, long term you’ll be on the right track and have a full suite engine
1
u/my-ka 4d ago
mongo is a non-relational docyment db
postgres is a relational db which can do some full text
not 1 to 1 conversion
1
u/Hufe 4d ago
Maybe start with DocumentDB.io / azure docdb
1
u/theapplekid 4d ago edited 4d 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/Schtick_ 4d 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 4d 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 4d 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 4d 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.
1
u/gabor_legrady 3d ago
Pain in the ass.
Use Debezium + Official MondgoDB CDC plugin to push records to kafka, than Flink to store them in Postgres. Use "upsert" (on confilct do update) to store efficiently.
In time both databases should contain same data, if something never changed in mongoDB just do an update on it to be picked up by CDC.
You might like to keep Mongo for normal behavior and use only Postgres for analytical.
1
u/Desperate-Country440 3d ago
For relational problem: yes!
For the rest: you should find out.
It is a very complicated question because you cannot really rewrite an application overnight by yourself, you need a team and the team to believe in the same dream not only in you because when the shit hits the fan the whole team needs to help and the head of the Team Leader is in balance ...
Others had very good points, at the end of the day is up to the team.
1
u/merlinm 3d ago
Hello, over 25 years postgres experience here. The short answer is that yes, you should switch, or at least supplement, your database backend with postgres assuming you are willing to build up the skills and expertise to make it worthwhile. In my experience, outgrowing nosql systems is a very common thing. To answer:
1
u/DreamsTheyStole 3d ago
Skip the full rewrite if it's only internal dashboards timing out. Streaming the MongoDB changes into a read-only analytics store fixes those 10-collection
$lookupjoins without touching live writes. If you actually need real transactions and foreign keys everywhere, go to managed RDS instead of running your own EC2. I picked MariaDB on RDS because there's no vacuum tuning and RDS handles most of the upgrade work, so my pager stays quiet.Postgres on RDS is fine too, and a dual-write cutover can keep downtime close to zero.
1
u/grommich 2d ago
Leave everything as it is, and for analytics, set up data streaming/copy into ClickHouse (for example).
1
u/de6u99er 2d ago
I don't know how big your code base is vut this could be done, including data migration, within a few days using a coding AI.
1
u/Apprehensive-Ice8996 16h ago
For conventional apps just use SQL. If your doing something unconventional or experimental look at NoSQL. I find NoSQL very interesting as computer scientist though. Pretty cool tech if you ask me.
0
u/AutoModerator 4d 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.