404human Projects Writing Build logs Learning Books Progress
Rust · Zola
zsh · 404human@dev:~ Tab complete · ↑↓ history · type help

PostgreSQL vs MongoDB: decided by failure modes, not hype

When to choose PostgreSQL vs MongoDB in production: ACID transactions, JSONB vs BSON, Row-Level Security, schema drift, and operational failure modes.

Published
Read time
4 min read
Kind
guide
Topics

Choose PostgreSQL by default for almost any web app, SaaS product, or financial ledger. Modern PostgreSQL gives you strict relational integrity, multi-table ACID transactions, Row-Level Security (RLS), and indexed JSONB for flexible fields in one engine. Choose MongoDB when your data is genuinely document-shaped with independent lifecycles, you need built-in horizontal sharding across regions, and your team prefers document-native modeling over relational joins.

Most PostgreSQL vs MongoDB comparisons stop at “SQL vs NoSQL.” In production, the real difference shows up when something fails: a partial write halfway through a request, a schema change at 2 a.m., or a multi-tenant bug that leaks another customer’s rows.

PostgreSQL vs MongoDB at a glance

DimensionPostgreSQLMongoDB
Data modelRelational tables + native binary JSONBBSON documents grouped in collections
Multi-record ACID transactionsDefault, battle-tested, low overheadSupported since 4.0, but higher overhead across shards/documents
Schema enforcementStrict by default; opt-in flexibility via JSONB + CHECKFlexible by default; opt-in via JSON Schema validation
Tenant isolationNative Row-Level Security (RLS) enforced in the databaseApplication-level query filters (easy to miss one query)
Joins & reportingFirst-class SQL joins, CTEs, window functions$lookup works, but cross-collection joins are slower and awkward
Horizontal shardingExternal (Citus, application sharding, read replicas)Built-in shard key routing and replica sets
Best fitSaaS, CMS, payments, accounting, auth, relational workflowsHigh-write telemetry, product catalogs with varying attributes, globally sharded document stores

How do partial writes and transactions fail?

The most expensive bug in backend systems is a half-finished write: you debit an account or publish a release, the process crashes before the second write, and now your database disagrees with itself.

In PostgreSQL, wrapping a balance update, an audit-log row, and an outbox event inside one BEGIN ... COMMIT transaction is the default path. If the process is killed with kill -9 mid-request, Postgres rolls back every row automatically. In Payment Sandbox, PostgreSQL triggers even refuse to commit an unbalanced double-entry journal—the database itself makes corrupted state impossible.

MongoDB supports multi-document ACID transactions, too, but its storage model is optimized for single-document atomicity. If your domain fits cleanly inside one self-contained document (for example, a user profile with embedded preferences), MongoDB is fast and clean. The moment your business rules span three collections—orders, inventory, and ledger entries—you end up fighting the document model or paying extra coordination overhead.

How does PostgreSQL JSONB compare to MongoDB?

A common reason teams reach for MongoDB is “our schema changes fast.” Today, PostgreSQL’s JSONB handles most of that without giving up relational guarantees:

CREATE TABLE documents (
    id          uuid PRIMARY KEY,
    tenant_id   uuid NOT NULL,
    kind        text NOT NULL,
    payload     jsonb NOT NULL,
    created_at  timestamptz NOT NULL DEFAULT now()
);

CREATE INDEX idx_documents_payload ON documents USING GIN (payload);

With JSONB, you keep strict foreign keys and tenant IDs in real columns while letting the payload column evolve freely—and GIN indexes let you query inside JSON fields fast.

Where MongoDB still wins on document ergonomics is in-place nested updates (updating a single deeply nested array element without rewriting the whole JSONB blob) and native document-oriented aggregation pipelines when every single record has a completely different shape.

What about multi-tenant security and migrations?

Two operational differences rarely show up in benchmarks but matter every week in production:

  1. Tenant isolation: In PostgreSQL, Row-Level Security (RLS) lets the database enforce tenant_id = current_setting('app.tenant_id')::uuid on every query. Even if an engineer forgets a WHERE tenant_id = $1 clause in a new endpoint, Postgres refuses to return another tenant’s rows. In MongoDB, tenant scoping lives in application code or middleware.
  2. Schema drift: “Schemaless” in MongoDB does not mean you have no schema—it means your schema lives implicitly in application code. Six months later, you have four slightly different shapes of the same document in one collection and defensive if (doc.v2Field ?? doc.oldField) checks everywhere. Explicit SQL migrations feel slower on day one and save weeks on day 180. (If you run managed Postgres on Neon, watch out for non-superuser migration refusals.)

When should you choose PostgreSQL vs MongoDB?

Choose PostgreSQL if:

  • You are building a SaaS app, CMS, marketplace, billing system, or anything with users, permissions, and relationships.
  • You want database-enforced invariants (foreign keys, CHECK constraints, RLS, transactional outbox).
  • You want one database that handles relational data, JSONB, full-text search, and vectors (pgvector) without adding another service.

Choose MongoDB if:

  • Your data is naturally hierarchical and self-contained per document, with minimal cross-collection joins.
  • You expect multi-terabyte write volume that needs built-in horizontal sharding from the start.
  • Your team already operates MongoDB well and enforces strict JSON Schema validation on collections.

If you are unsure—and 90% of teams starting a new backend are—start with PostgreSQL. It is far easier to store flexible JSON inside PostgreSQL than to bolt relational integrity onto a document store later.

Frequently asked questions

Is PostgreSQL faster than MongoDB? Neither is universally faster. MongoDB is often faster for single-document key lookups and high-volume unindexed document inserts; PostgreSQL is typically faster for complex joins, aggregations, and multi-table transactions. Measure on your own queries and indexes before deciding.

Can PostgreSQL replace MongoDB using JSONB? For most web applications that only need flexible metadata or semi-structured payloads alongside relational tables, yes. If your workload relies heavily on frequent partial updates to large nested documents or built-in horizontal sharding, MongoDB remains a better fit.

Related reading