Skip to main content

MongoDB vs Postgres for AI agents: which should you use?

Summary

  • For most AI agents, use Postgres, and run it on Lakebase, the serverless Postgres database in the Databricks Data + AI Platform: an agent's structured records, JSON payloads and embeddings live in one ACID database queried with SQL, on compute that spins up, scales to zero and branches.
  • MongoDB is a document database known for its flexible data model: records are JSON-like documents, fields can differ between documents, and MongoDB Atlas adds vector search on the same data.
  • Postgres handles semi-structured agent state with jsonb columns and GIN indexes, and handles retrieval with pgvector. On Lakebase, Lakebase Search adds keyword and hybrid search.
  • MongoDB fits teams whose application data is already modeled as documents. Postgres on Lakebase fits agents that join their state to business tables, test changes on branches and read lakehouse data.

MongoDB vs Postgres for AI agents: which should you use?

Use Postgres for most AI agents, and run it on Lakebase, the serverless Postgres database in the Databricks Data + AI Platform. A single agent task touches three kinds of data: structured business records it reads and updates, semi-structured payloads such as tool calls and checkpoints, and embeddings it searches by meaning. Postgres keeps all three in one ACID database you query with SQL, and Lakebase adds what agents do to a database: create it from code, leave it idle, branch it and connect it to lakehouse data. MongoDB is a document database known for its flexible document model and integrated vector search, and it suits teams whose application data already lives in documents.

What an agent's data looks like

Agent data Example What the database needs
Business records Orders, tickets, accounts the agent reads and changes Joins, constraints and transactions across tables
Working state Chat history, checkpoints, tool-call results Flexible JSON storage that's still indexed and transactional
Memory and context Embeddings of documents and past conversations Vector search next to the other data
Analytical context Features, risk scores, history from the lakehouse Governed, low-latency access without a fragile copy

Postgres (on Lakebase) vs MongoDB: how they compare

Capability Postgres on Lakebase MongoDB
Data model Relational tables, plus json and jsonb columns for semi-structured data Documents made of field and value pairs, similar to JSON objects, stored in collections
Schema Defined per table and changed with migrations, which branches let you test on a copy Flexible: documents in a collection needn't share fields, with optional schema validation
Query language SQL The MongoDB Query API, including aggregation, text search and geospatial queries
Transactions ACID, with every SQL-standard isolation level including Serializable Single-document operations are atomic; multi-document ACID transactions span collections, databases and shards
Indexing JSON GIN indexes on jsonb keys and key/value pairs Indexes can include keys from embedded documents and arrays
Vector and text search pgvector, plus Lakebase Search for vector, BM25 keyword and hybrid search Vector search on MongoDB data, combinable with full-text search and filters, with automated embedding (preview, per MongoDB's docs)
Agent framework support Standard Postgres clients and drivers connect, so Postgres-based framework checkpointers can use it; Databricks' managed agent sessions and agent memory (Beta) run on Lakebase and work with agents built on any framework Integrations with AI frameworks, including a LangGraph long-term memory store
Scaling Each branch has one primary read-write compute that autoscales and scales to zero, plus read replicas Sharding distributes data across a cluster; replica sets provide automatic failover
How you run it Fully managed in Databricks MongoDB Atlas on AWS, Azure and Google Cloud, or self-managed Enterprise and Community editions

How Lakebase runs Postgres for agents

Lakebase is still Postgres. The product page states it plainly: "Lakebase uses open source Postgres, not a fork or a new SQL dialect," with extensions such as pgvector and PostGIS. What changes is the architecture underneath. The LTAP engineering blog explains that Lakebase moves the write-ahead log and data files into independent storage services, which makes compute "a stateless Postgres engine that can be started, stopped, and replicated freely." For agents, that means:

  • Create from code. The REST API, Databricks CLI and SDKs manage projects, branches and endpoints.
  • Go idle. Scale to zero suspends compute after inactivity and reactivates it on the next query.
  • Branch before you write. Branches are copy-on-write, so a schema change or risky update can run on a copy of production and be discarded. Point-in-time restore recovers from an accidental deletion or schema change.
  • Read the lakehouse. Synced tables serve Unity Catalog tables into Postgres at low latency, so an agent reads features or scores next to its own records. Unity Catalog registration adds permissions, lineage and audit logs.

For agent memory and sessions, Databricks' managed agent sessions and agent memory run on Lakebase, are in Beta, and are the recommended path for new agents. The page's argument doesn't depend on them: Lakebase is the Postgres database underneath. The larger LTAP architecture is still being rolled out, so the four paths above are what to use today. Read the LTAP engineering blog for the design.

FAQs

Can Postgres store flexible agent state the way a document database does?

Yes. Postgres stores JSON in json and jsonb columns, and the docs recommend jsonb for most applications. GIN indexes search keys and key/value pairs inside jsonb, and the same transaction can update relational tables.

Does MongoDB support ACID transactions?

Yes. MongoDB's docs describe multi-document ACID transactions across operations, collections, databases, documents and shards, and every single-document operation is atomic.

Can an agent keep vectors in the same database as its state?

Yes, in either. With pgvector, Postgres stores vectors with the rest of the data, and Lakebase Search adds keyword and hybrid search. MongoDB's vector search indexes vector data alongside other MongoDB data.

The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.