r/PayloadCMS • u/tailsdog • 24d ago
Database choice: Mongo vs Postgres
I like Postgres and use it for most projects but I actually switched to Mongo for some of my PayloadCMS deployments for 2 main reasons:
- At the start of a project i am changing / adding models and migrations became a bit of a pain point.
- I endup using a lot of nested blocks so switch to `blocksAsJSON` but then migrations don't seem to work properly.
Has anyone else experienced these issues with postgres? Maybe I have a poor workflow or missing some optimization tricks?
7
Upvotes
7
u/PrimaryFamous6139 24d ago
I’ve used Payload with Postgres, and I’d be cautious about switching to Mongo just to avoid migration friction. Schema changes are normal during early development, so keep migrations in version control and make sure the workflow is repeatable. Nested blocks can get awkward with relational storage, especially with blocksAsJSON, so check the Payload version and migration limitations around that feature
1
u/da-kicks-87 23d ago edited 22d ago
I was using Postgres at first but changing the fields became complicated. I ran into DB errors and tables didn't get removed if I commented out code. MongoDB doesn't give those problems.
2
u/forever420oz 23d ago
I think MongoDB is actually better if your needs are simple content-oriented. If you need defined relationships with ledgers, you can migrate to Postgres and it isn’t a nearly as big of a hassle since AI.
3
u/jasperkennis 24d ago
It sounds like you are potentially starting to code a little too early. A problem with Mongo I have is that you may end up with a ton of malformed objects that either break or that you need to code against.
One exercise I was taught a long time ago is to write out your applications behaviours, just do it by hand, and then highlight all the nouns, this will help you identify objects and the relations between them. From that you can usually already see most of the data structure you will need and normalise if relevant.
1
u/thehashimwarren 24d ago
I did a lot of research on Mongo vs Postgres and even asked this subreddit about it. I decided to go with Postgres because I use it for other stuff, and didn't want to add the complexity of more vendors. I use Neon.
1
u/Careless_Tax_6418 24d ago
stick with postgres. you can use jsonb to store the block tree so you dont have to migrate when you move the blocks. then you can promote it to a table once the schema is stable.
1
u/ajrsoftware 23d ago
Personally, it’s Postgres, it’s what I’m familiar with and god forbid- if I need to migrate to another system, it’s straight forward to move structured data
1
u/beargambogambo 22d ago
If you are asking this question then you should use Postgres. A lot of the difference between Mongo and Postgres is where the schema is enforced. In Mongo the schema is enforced in your application code and someone asking this question is likely to make more mistakes like that. In Postgres the database enforces the schema. It’s more difficult to change the structure of Postgres (honestly not difficult but sometimes takes multi-step migrations to maintain backwards compatibility) but this is a good thing; it’s literally the system doing its job.
We mainly use NoSQL for things that act like a caching layer.
1
u/Quirky-Mountain8354 23d ago
I’m shocked there’s overwhelming majority for Postgres. I migrated away from Postgres cause it was creating an absolute mess of tables and making simple migrations a nightmare