MCP server for PostgreSQL: connect Claude, Codex or any AI agent to Postgres Connect Claude, Codex or any MCP client to PostgreSQL with DeepORG: schema discovery, a searchable catalog, one parameterized SQL statement per question, role and row rules enforced by the engine, and writes held for approval. Works with Aurora, RDS, Cloud SQL, CockroachDB and YugabyteDB. guides guides guides/postgres-mcp-server guides/postgres-mcp-server.md

GuidesPostgreSQL

An MCP server for PostgreSQL that writes its own queries inside your rules.

DeepORG reads your Postgres schema, gives your AI client a small fixed set of tools, and turns each question into one parameterized SQL statement that runs inside the access rules you set. It works the same on RDS, Aurora, Cloud SQL, CockroachDB and YugabyteDB.

The short version

What you get when you point it at Postgres.

DeepORG connects to PostgreSQL, discovers the tables, columns, foreign keys and functions, and builds a catalog the agent can search. Your AI client sees the same small set of tools whether the database has three tables or three thousand.

  • The agent never sends SQL. It writes a GraphQL query against the catalog. The engine compiles it into one Postgres statement and passes every value as a parameter.
  • Joins come from your foreign keys. The relationships are discovered, not guessed, so the agent does not invent a join column.
  • Postgres does the maths. Sums, counts, averages, grouping and ranking run in the database, not in the model’s head over a page of rows.
  • Your rules apply at compile time. Roles, row filters and column lists are added to the statement before it runs. A read-only source stays read-only.

Connect

A few lines of config.

Install the CLI, then name the database in config/dev.yml:

Shell
curl -fsSL https://deeporg.io/install.sh | bash
YAML
sources:
  - name: app
    kind: database
    type: postgres
    default: true
    # postgres://user:pass@host:5432/app?sslmode=require
    connection_string: ${DATABASE_URL}
    schema: public
    read_only: true   # start read-only

read_only: true blocks every write to the source, whatever a role or the agent asks for. For a first run, also connect as a Postgres user that only has SELECT, so the database itself enforces the same limit.

Start it and check that it found your tables:

Shell
deeporg serve

Open localhost:8080/admin/runtime to see the discovered source and table count.

Your AI client

Add it to Claude Code, Codex or any MCP client.

Run it over stdio, pointed at your config folder. Nothing else needs to be running:

Shell
claude mcp add deeporg -- deeporg mcp --path /absolute/path/to/config
codex mcp add deeporg -- deeporg mcp --path /absolute/path/to/config

Or, with deeporg serve already running, register its HTTP endpoint:

Shell
deeporg mcp add claude http://localhost:8080

Your client’s own model does the reasoning, so DeepORG needs no model key in this setup. To use DeepORG’s own agent instead, with a chat console and standing questions, see the built-in agent.

What runs

A question becomes one Postgres statement.

Ask “who were our five biggest customers last quarter?” The agent searches the catalog for customers and orders, reads how they join, and writes:

GraphQL
query {
  orders(
    distinct: [customer_id]
    where: { created_at: { gte: "2026-07-01", lt: "2026-10-01" } }
    order_by: { sum_total: desc }
    limit: 5
  ) {
    customer_id
    sum_total
    count_id
  }
}

The engine compiles that into a single statement: a GROUP BY with the sum and count, the role’s row filter added to the WHERE, and the dates passed as parameters. Nested selections become LATERAL joins, and Postgres builds the JSON response itself, so there is one round trip and no stitching in application code.

On Postgres the query language reaches the most: arrays, full-text search, PostGIS geo filters, recursive relationships, JSON columns, database functions and live subscriptions. See the compiler for how the statement is built.

Choosing a server

Five questions to ask any Postgres MCP server.

Plenty of MCP servers will connect a model to Postgres. Before you give one your production database, check these:

  1. Does the model write raw SQL? If so, “read-only” is only as strong as the wrapper. The original reference Postgres MCP server ran each query in a read-only transaction, and a COMMIT inside the query string ended that transaction early, so the statements after it could write.1
  2. Where do permissions come from? A shared database user means every caller sees everything that user sees. Look for row rules tied to the signed-in person.
  3. How does it learn the schema? Dumping every table into the prompt stops working past a few dozen tables. Search on demand scales; a full dump does not.
  4. Who does the arithmetic? If the model sums a page of rows itself, a total over more rows than the page is wrong, confidently.
  5. What happens on a write? It should be refused, or held for a person to approve, never run because the model decided to.

DeepORG’s answers: GraphQL compiled with parameters, rules from the caller’s token, catalog search, aggregates in the database, a read-only switch per source, and writes from its own agent held as approvals.

Source: 1. Datadog Security Labs, SQL injection in the Postgres MCP server, 2025

Postgres-compatible

RDS, Aurora, Cloud SQL, CockroachDB, YugabyteDB.

These connect with type: postgres and their usual connection string.

  • Amazon RDS and Aurora. Use the cluster endpoint, or a reader endpoint for an agent that only reads.
  • Google Cloud SQL. Connect through the Cloud SQL Auth Proxy, or set enable_tls with server_name as project:instance.
  • CockroachDB. Queries, writes, subscriptions, arrays and full-text search. No PostGIS geo filters and no schema management.
  • YugabyteDB. Uses the Postgres-compatible YSQL API, through the same driver.

Limits

What to know before you start.

Go deeper

Related pages.