MCP for Databases: How to Let an Agent Query Your Data Without Giving It the Keys
24 November 2026 · Updated 28 November 2026 · 13 min read · by Yang Kai
Database MCP servers explained safely: read-only patterns, scoped credentials, query limits, and a safe-access config pattern for Postgres, MySQL, and SQLite.
Config inside: Safe Database Access Config Pattern for MCP
Contents
- How we researched this
- 1. Postgres MCP Pro: the Postgres specialist
- 2. DBHub: one small gateway for several engines
- 3. MCP Toolbox for Databases: predefined tools instead of free-form SQL
- First, check your config for the archived reference servers
- Rule 1: the server gets its own read-only user
- Rule 2: scope below the database level
- Rule 3: limit the queries themselves
- The safe-access config pattern
- Test the rails before the questions
- When writes are genuinely needed
- Verdict
Point an MCP server at a database and your agent stops guessing: it reads the schema, writes SQL, and answers from your real rows. Point the wrong server at it, with the wrong login, and the same loop becomes the fastest route to a damaged production table.
Most roundups of database servers skip the two facts that decide how this goes. The most-downloaded Postgres server is an archived reference project whose read-only mode had a published bypass. And the most capable maintained option ships with write access switched on by default.
Below: the three servers worth evaluating, what each one’s safety story actually is, and the access pattern — dedicated read-only user, scoped tables, hard limits — that makes any of them safe to run. Every server named here was checked against its official repository or documentation in November 2026.
| Server | Best for | Engines | Read-only story | Status |
|---|---|---|---|---|
| Postgres MCP Pro | Postgres depth: queries, plans, health checks | PostgreSQL only | Restricted mode exists, but the default is unrestricted — set the mode and a read-only role | Open source (MIT), maintained |
| DBHub | Several engines through one tiny tool surface | Postgres, MySQL, MariaDB, SQL Server, SQLite, and more | Writable until you set readonly = true per source |
Open source (MIT), maintained |
| MCP Toolbox for Databases | Teams who want predefined tools, not free-form SQL | 20+ engines, including BigQuery, Spanner, MongoDB | You define the tools; the model can only call what you wrote | Open source, by Google |
| Archived reference servers | Nothing — remove them | Postgres, SQLite, others | The Postgres one’s read-only mode was bypassed in a published write-up | Archived in 2025; no security updates |
How we researched this
This is desk research, not a benchmark. Server facts, tool lists, and defaults come from each project’s official repository and documentation, read in November 2026. The archived-server flaw is sourced to Datadog Security Labs’ published analysis, and the download figures to Glow Labs’ count of npm data. We do not claim to have run these servers against production data — the setup pattern below is the one the documentation and the security write-ups jointly point to.
1. Postgres MCP Pro: the Postgres specialist
Postgres MCP Pro (from Crystal DBA, repository crystaldba/postgres-mcp) is the community server that filled the gap when the reference Postgres server was archived. It is more than a query runner: alongside schema browsing and SQL execution it carries performance tooling — explain plans, top queries drawn from pg_stat_statements, index tuning with hypothetical indexes, and database health checks. That makes it as much a tuning assistant as a question-answerer.
The catch is the default. The server has two access modes, and unrestricted — full read/write — is both the default and what the README’s examples show. Restricted mode parses each statement and runs it inside a read-only transaction with an execution-time cap. The maintainers describe restricted mode as deliberately conservative; treat it as the second layer of defence, with the database role below as the first.
Key features:
- Tools include
list_schemas,list_objects,get_object_details,execute_sql,explain_query, andget_top_queries - Index tuning and health checks alongside plain querying
- Restricted mode: parsed SQL, read-only transactions, capped execution time
- Runs locally over stdio, or shared over HTTP-based transports
- Installs from PyPI (
postgres-mcp) or Docker (crystaldba/postgres-mcp)
Pros:
- Deepest Postgres-specific toolset of the three servers here
- Restricted mode is a real control, not a request in a prompt
- MIT-licensed and actively maintained
Cons:
- The safe configuration is opt-in; a copy-pasted example config can write
- PostgreSQL only
- The performance tools need extensions such as
pg_stat_statementsenabled to be useful
Pricing: Free and open source (MIT). Checked Nov 2026.
2. DBHub: one small gateway for several engines
DBHub, from Bytebase, takes the opposite design bet: the smallest possible surface. It exposes two tools — execute_sql and search_objects — whose definitions total 261 tokens by DBHub’s own documentation, which is why it barely dents your context budget. One process, configured in a TOML file, can front several databases at once, with settings per source: read-only on production, writable on staging, a row cap on one and not the other. It can also expose custom tools — parameterised queries you define in the same config. It is the server Anthropic’s Claude Code documentation uses as its database example.
Two defaults deserve attention. A source is writable until you set readonly = true on it. And DBHub’s HTTP transport does not authenticate clients by itself — Bytebase’s own comparison says so plainly — so run it over stdio, on localhost, or behind authentication you control.
Key features:
- Two tools by default:
execute_sqlandsearch_objects - Engines: PostgreSQL, MySQL, MariaDB, SQL Server, SQLite; current releases add Oracle and ClickHouse
- Per-source
readonly,max_rows, andquery_timeoutsettings in one TOML file - Custom tools: your own parameterised queries exposed as named tools
- SSH tunnelling and SSL connections supported
Pros:
- Smallest context footprint of the three
- One process fronts several databases, each with its own rules
- Per-source limits are written in config, where a prompt cannot change them
Cons:
- Read-only is a setting you must remember to set, per source
- No built-in identity, masking, or audit — Bytebase sells those in its main product
- Its guardrails stop at the server; the database role still decides what is possible
Pricing: Free and open source (MIT). Checked Nov 2026.
3. MCP Toolbox for Databases: predefined tools instead of free-form SQL
Google’s MCP Toolbox for Databases (repository googleapis/mcp-toolbox, renamed from Gen AI Toolbox) removes the free-form query entirely. You declare data sources and tools in a config file, and a tool is a named SQL statement you wrote, with typed parameters. The model fills in parameters; it cannot invent a join you never offered. Prebuilt tool sets cover common engines if you want generic tools, and the server handles connection pooling and authentication — including IAM-based auth on Google Cloud databases.
This is the design that scales to shared and regulated use, because the tool definitions are reviewable config. A security reviewer can read exactly which statements an agent may run, which is never true of “the model writes SQL and a parser checks it.”
Key features:
- Tools defined by you: named SQL with typed parameters, reviewed like code
- Prebuilt tool sets for teams who do want generic access
- 20+ engines: Google Cloud databases (AlloyDB, BigQuery, Cloud SQL, Spanner, Firestore) plus PostgreSQL, MySQL, SQL Server, Oracle, MongoDB, Redis, Elasticsearch, Snowflake, Neo4j, and more
- Connection pooling and IAM authentication handled by the server
- SDKs let the same tools load into agent frameworks, not just MCP clients
Pros:
- Strongest control of the three: no free-form SQL unless you define it
- Fits teams — access is config under version control, not prompt behaviour
- Broadest engine coverage here by a wide margin
Cons:
- Most setup work of the three; you write and maintain every tool definition
- Weaker for open-ended exploration — the model can only ask what you modelled
- Documentation leans Google-Cloud-flavoured, even though the server runs anywhere
Pricing: Free and open source. Checked Nov 2026.
First, check your config for the archived reference servers
Anthropic’s original reference servers were teaching examples. The Postgres and SQLite reference servers were archived in 2025 and moved to the servers-archived repository, whose notice states no security guarantees are provided. In August 2025, Datadog Security Labs published the reason that matters: the Postgres server wrapped queries in a read-only transaction, but accepted several statements at once, so a query beginning COMMIT; ended the read-only transaction and the statements after it ran as writes.
Archived does not mean gone. Glow Labs counted roughly 158,000 npm downloads of server-postgres in a single week in late October 2026 — tutorials and agents keep recommending what they remember. If your client config contains @modelcontextprotocol/server-postgres or @modelcontextprotocol/server-sqlite, replace it with one of the servers above, and run anything else you find through the registry vetting process.
Rule 1: the server gets its own read-only user
Whichever server you pick, never hand it your application’s database credentials, and never your admin login. Create a dedicated user that can only read, only the databases it needs:
-- PostgreSQL example: run as an admin, once
CREATE USER mcp_reader WITH PASSWORD 'use-a-secret-manager';
GRANT CONNECT ON DATABASE analytics TO mcp_reader;
GRANT USAGE ON SCHEMA public TO mcp_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_reader;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO mcp_reader;
Read-only is not a courtesy setting. It converts the worst case from “the agent dropped a table” to “the agent ran a silly query” — and it held even against the archived server’s bypass class of bug only when the role itself had no write grants to escape into. If a server cannot work with a read-only user at all, that is a finding about the server, and the wider MCP security checklist says what to do with findings like that: walk away, or confine it to a throwaway copy of the data. It also assumes you already understand the basic shape of a server from building the smallest one yourself — a database server is that same shape, pointed at something far less forgiving than a drafts folder.
Rule 2: scope below the database level
Read-only at the database level still exposes everything readable in it, which often includes tables the agent has no business seeing: password hashes, payment tokens, private notes. Scope further, in whichever layer your setup offers:
- A reporting replica or a scheduled copy for anything sensitive — the agent explores yesterday’s snapshot, never the live system.
- Views instead of tables. Expose limited views that omit sensitive columns entirely, and grant the read-only user access to the views only.
- One database per server. A server for analytics and a separate server for the blog’s data, each with its own user, instead of one server holding credentials for everything you own.
This layering is the database expression of a general principle from how MCP authentication and trust boundaries work: every credential should reach the minimum, and revoking one server should never mean rotating your whole infrastructure.
Rule 3: limit the queries themselves
Even read-only queries can hurt: an unindexed join across two large tables can load a small production database as effectively as an attack. Prefer servers and configurations that offer:
- Row limits applied by the server, not requested in the prompt — a cap the model cannot talk its way past.
- Statement timeouts, so a runaway query dies in seconds — set one in the database as well as the server, so the query dies even if the client disconnects.
- Single-statement execution, rejecting stacked queries smuggled into one call — the exact hole in the archived server’s story above.
- Result-size awareness — a query returning a hundred thousand rows will flood the model’s context and your bill; servers that truncate with a clear notice are behaving correctly.
Each server above expresses these differently: Postgres MCP Pro through restricted mode, DBHub through per-source settings, MCP Toolbox by only offering statements you wrote. The mechanism matters less than the property — the limit lives somewhere a prompt cannot reach.
The safe-access config pattern
Whatever server you choose, its configuration should end up shaped like this — a dedicated user, an explicit database, the restrictive mode named in the arguments, and limits written down where a prompt cannot change them:
{
"mcpServers": {
"analytics-db": {
"command": "uvx",
"args": ["postgres-mcp", "--access-mode=restricted"],
"env": {
"DATABASE_URI": "postgres://mcp_reader:SECRET@db-host:5432/analytics"
}
}
}
}
That is Postgres MCP Pro’s shape. DBHub’s equivalent lives in its TOML file as readonly = true and max_rows per source; MCP Toolbox’s equivalent is the tool list itself. Variable and flag names differ per server — check your server’s documentation for its exact switches, and treat a server with no read-only or limit switches as a server to wrap, restrict at the database level, or skip.
Test the rails before the questions
Before asking anything real, run the adversarial pass from the MCP testing routine in its database form: ask the agent to update a row and confirm the database refuses; ask for a table you did not grant and confirm it is invisible; ask a question you know needs a huge scan and watch the timeout fire. A rail you have not watched fire is a hope, not a rail.
Practise somewhere free first. Neon’s free tier gives you a real Postgres with branching — point the read-only user at a branch, not your production data, and every experiment in this article costs nothing.
When writes are genuinely needed
Sometimes the job really is writes — tagging records, queueing jobs, cleaning test data. Then do not relax the read server. Build a second, separate server whose only tools are the specific write operations you designed, each with its own narrow schema: tag_customer(id, tag) rather than run_sql. Designed operations can be validated, logged, and undone; arbitrary SQL cannot. MCP Toolbox’s predefined tools are this pattern productised, and the same design instinct — many small, purposeful tools over one powerful one — runs through the choice between tools, resources, and prompts.
Verdict
Postgres only, with performance questions in the mix: Postgres MCP Pro, in restricted mode, behind a read-only role. Several engines for personal or small-team exploration: DBHub, with readonly = true set on every source before the first real question. A team, a shared deployment, or data with rules attached: MCP Toolbox, because predefined tools are the only option a reviewer can fully read.
In all three cases the server is the second layer. The read-only role, the scoping, and the limits are the first. Once the setup is proven locally, the natural next step is deciding where the server should live — local versus hosted MCP servers — because a database server that other people or schedules depend on has a different home than a personal one. Database access is also where industry rules bite hardest; MCP for vertical industries covers how regulated sectors handle that access differently.
FAQ
Which database MCP server should I start with? The one that matches your engines and your audience. Postgres-only work usually starts with Postgres MCP Pro in restricted mode; mixed engines with DBHub; anything a team or a reviewer must approve with MCP Toolbox’s predefined tools. All three still need the read-only role from Rule 1.
Can an MCP server connect to SQLite too? Yes. SQLite is often the best starting point: the database is a single file, so you can hand the server a copy of the file and let the agent explore it with zero risk to the original. DBHub covers SQLite alongside the server databases.
Should I point a database MCP server at production? Start with a replica, a snapshot, or a local copy. Only consider production once the read-only user, table scoping, and limits have all been tested — and many people reasonably never do. For shared production setups, read MCP in production before deciding.
What if the server I like has no read-only mode? Enforce read-only at the database level with the dedicated user’s grants. The server cannot exceed what its database user is allowed to do, whatever its own settings say. Then prefer a server that adds its own limits on top, so two layers have to fail at once.
Can the model see my whole schema? Typically yes — schema discovery is how it writes sensible queries. That is another reason to scope with views: the schema it discovers is then only the schema you chose to show it.
Category: MCP Servers & Security · Tags: #mcp #security #databases #tutorial
Next step: this article is part of MCP guides, which covers the full topic from first setup to advanced use.