← Back to SQL, page 24

sql-0470 · Medium mid

A startup asks whether to use PostgreSQL or a document database such as MongoDB for its new product. How would you advise?

Start from the data and queries: if entities are related (users, orders, payments) and need joins, constraints and transactions, a relational database fits, and PostgreSQL's jsonb covers flexible attributes too. Document stores suit data read and written as self-contained documents with varying shape, and scale out writes more easily, but cross-document consistency and ad-hoc joins are weaker.

Full answer

Questions to ask:

  • Relationships: will reports join customers, orders and products? Relational wins.
  • Integrity: must the database reject orphans, duplicates and invalid states? Constraints are a relational strength.
  • Transactions: multi-entity atomic changes (money, inventory) are natural in SQL; document stores support them now but with more caveats.
  • Shape: if each record is a self-contained aggregate (a product catalogue entry with nested variants) read whole, documents fit well.
  • Scale: horizontal write scaling is built into many document stores; PostgreSQL scales far vertically and with read replicas, and further with extensions or sharding layers.
  • Team skills and tooling: SQL skills and BI tools are widespread.

For most early-stage products, PostgreSQL with some jsonb columns is a safe default that keeps options open; move parts to specialised stores when a concrete need appears.

Key points

  • Choose by relationships and queries
  • Constraints and transactions favour relational
  • Self-contained aggregates suit documents
  • jsonb covers flexible attributes
  • Defer specialised stores until needed

Common mistakes

  • Choosing by popularity
  • Assuming schema-less means no schema work

Follow-ups

  • How would you model a product catalogue in each?
  • When would you add a second database?

Tips

  • Ask about the queries first