Why I Chose PostgreSQL Over MongoDB for My Startup
Why I Chose PostgreSQL Over MongoDB for My Startup
When building our SaaS, everyone told me to use MongoDB. "It's web scale!" "Schema-less is the future!" "All the cool startups use it!"
I chose PostgreSQL instead. Two years later, I'm convinced it was the right call.
The MongoDB Pitch
MongoDB's appeal is obvious. No schema migrations. Just throw JSON at it. Perfect for rapid iteration, right?
We prototyped with MongoDB. It was fast to get started. But cracks appeared quickly.
The Problems We Hit
Data Integrity
Without a schema, our data got messy fast. Some users had email, others had emailAddress. Some orders had total, others had totalAmount.
We spent weeks cleaning up inconsistent data. A schema would've prevented this.
Complex Queries
MongoDB's aggregation pipeline is powerful but painful. A simple join in SQL becomes 20 lines of aggregation stages.
Example: Get users with their order count.
PostgreSQL:
SELECT u.*, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
GROUP BY u.id;
MongoDB: 30+ lines of aggregation pipeline that's hard to read and debug.
Transactions
We needed transactions for payment processing. MongoDB added transactions in 4.0, but they're limited and slower than PostgreSQL.
PostgreSQL has had rock-solid ACID transactions for decades.
Why PostgreSQL Won
1. JSONB Support
PostgreSQL has JSONB columns. You get schema-less data when you need it, with the power of SQL.
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
metadata JSONB
);
SELECT * FROM products
WHERE metadata->>'category' = 'electronics';
Best of both worlds.
2. Data Integrity
Foreign keys, constraints, and triggers keep data clean. Our database enforces business rules automatically.
ALTER TABLE orders
ADD CONSTRAINT valid_total
CHECK (total >= 0);
MongoDB can't do this.
3. Mature Ecosystem
PostgreSQL has been around since 1996. The tooling is mature:
- pgAdmin for management
- pg_stat_statements for query analysis
- Extensive extensions (PostGIS, full-text search, etc.)
4. Performance
For our workload (lots of joins, complex queries), PostgreSQL is faster. We benchmarked both. PostgreSQL won consistently.
5. Cost
Managed PostgreSQL (RDS, Cloud SQL) is cheaper than managed MongoDB (Atlas) at our scale.
When MongoDB Makes Sense
To be fair, MongoDB shines for:
- Truly schema-less data (logs, events)
- Horizontal scaling (sharding)
- Geographically distributed data
But most startups don't have these problems.
Our Stack
We use PostgreSQL for:
- User data
- Orders and payments
- Product catalog
- Analytics
We use Redis for:
- Caching
- Session storage
- Real-time features
We use S3 for:
- File storage
No MongoDB needed.
The Migration That Never Happened
We planned to "start with MongoDB and migrate to PostgreSQL later." That migration would've been a nightmare.
Starting with PostgreSQL saved us months of migration pain.
Lessons Learned
- Don't follow hype: Choose the right tool for your problem
- Schema is good: It prevents bugs and keeps data clean
- SQL is powerful: Don't underestimate it
- JSONB exists: You can have schema-less data in PostgreSQL
- Boring technology wins: PostgreSQL is boring. That's a feature.
The Bottom Line
MongoDB isn't bad. It's just not the right choice for most web applications.
PostgreSQL gives you:
- Data integrity
- Powerful queries
- Transactions
- Mature tooling
- Lower costs
For our startup, it was the obvious choice. Two years in, I have zero regrets.