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.
  • Postgres on Lakebase fits agents that join their state to business tables, test changes on branches and read lakehouse data served in through synced tables.

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.

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, kept in sync for you

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 operations, collections and databases
Indexing JSON GIN indexes on jsonb keys and key/value pairs Multikey indexes over arrays that hold scalar values and embedded documents
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
Agent framework support A state store for agents, reached with standard Postgres drivers Integrations with AI frameworks, including LangGraph
How you run it Fully managed in Databricks on AWS, Azure and Google Cloud MongoDB Atlas on AWS, Azure and Google Cloud

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 lets analysts query the agent's tables next to lakehouse tables, with Unity Catalog permissions, lineage and audit logs applied to that analytical access.

For agent memory and sessions, Databricks' managed agent sessions and agent memory run on Lakebase and are the recommended path for new agents.

FAQs

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

Yes. Postgres stores JSON in json and jsonb columns, and the PostgreSQL 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 and documents, 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.

Can an agent on Lakebase read lakehouse data?

Yes. Synced tables serve Unity Catalog tables into Postgres through a managed sync, so an agent reads features or scores with the same driver it uses for its own tables.

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