Guide
Postgres MCP Server: Which One to Use and How to Run It Read-Only
Compare the Postgres MCP servers you can run today, see why the old reference server is unsafe, and connect an AI agent to Postgres with a role that can only read what it should.
By the Agentic Infrastructure Review editors · Updated
A Postgres MCP server lets an AI agent work with your PostgreSQL database directly. Once it is connected, Claude Code, Cursor, VS Code or another Model Context Protocol client can list your schemas, read table definitions and run SQL on its own. There are now several servers to choose from, and the one most tutorials still point to is the one you should stop using.
This guide compares the maintained options, shows how to connect one with a role that can only read, and explains where a Postgres MCP server stops being the right tool. The commands and flags below follow each project's own documentation as of November 2026. These projects change quickly, so check the linked docs before you rely on a specific flag.
Stop using the reference server
The first Postgres MCP server most people met was Anthropic's reference implementation, @modelcontextprotocol/server-postgres (Docker image mcp/postgres). It exposes one query tool and wraps each query in a READ ONLY transaction.
Datadog Security Labs showed that the wrapper is easy to escape. The server passes the agent's SQL straight to a driver that accepts several statements at once, so a query that starts with COMMIT; ends the read-only transaction and everything after it runs with full write access:
COMMIT; DROP SCHEMA public CASCADE;
The fix was merged into the source repository on May 29, 2025, the same day the server was archived. The last version on npm, 0.6.2, was never patched. If any of your configs still contain @modelcontextprotocol/server-postgres or mcp/postgres, replace them. Zed Industries' fork, @zeddotdev/postgres-context-server 0.1.4, carries the patch if you need a drop-in replacement while you move.
The Postgres MCP servers worth using
| Server | Best for | Runs as | Read-only control |
|---|---|---|---|
| Postgres MCP Pro (Crystal DBA) | Development and performance work on any Postgres | Python package or Docker; stdio or SSE | --access-mode=restricted: read-only transactions, SQL parsed to reject COMMIT/ROLLBACK, execution time limit |
| MCP Toolbox for Databases (Google) | Teams that want curated tools, or run Cloud SQL and AlloyDB | Single binary, --prebuilt postgres |
Read-only mode that removes write tools; engine-level session lock on Cloud SQL and AlloyDB |
| pgEdge Postgres MCP Server | Self-hosted Postgres served over HTTP to several clients | Binary or Docker; stdio or HTTP with TLS and token auth | Read-only enforcement (check the docs for your release); individual tools can be disabled in config |
| AWS Labs Postgres MCP Server | Aurora PostgreSQL and RDS for PostgreSQL | Python package or Docker, on the same host as your client | Read-only unless you pass --allow_write_query; --privilege_check enforce refuses over-privileged roles |
| Your provider's own server (e.g. Supabase, Neon) | Databases hosted on that provider | Usually hosted, with OAuth | Provider-specific; Supabase uses read_only=true |
Postgres MCP Pro
Postgres MCP Pro is the most complete general-purpose option. Besides execute_sql and schema tools (list_schemas, list_objects, get_object_details), it has explain_query, get_top_queries, analyze_workload_indexes, analyze_query_indexes and analyze_db_health. Index tuning and slow-query analysis need the pg_stat_statements and hypopg extensions. It needs Docker or Python 3.12 or later, and is MIT-licensed.
It has two access modes. unrestricted allows reads and writes and is meant for development databases. restricted limits the agent to read-only transactions with a time limit on each query. The project notes that its read-only protection can be bypassed if your database has unsafe stored-procedure languages enabled.
MCP Toolbox for Databases
MCP Toolbox for Databases is Google's open-source MCP server for many databases. For Postgres you run the binary with --prebuilt postgres and pass the connection as POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DATABASE, POSTGRES_USER and POSTGRES_PASSWORD. The prebuilt set includes execute_sql, list_tables, list_schemas, get_query_plan, list_active_queries, list_locks, list_query_stats and other diagnostic tools.
Toolbox stands out in two ways. You can write your own tools with fixed SQL templates instead of exposing execute_sql at all, which is the safest way to give an agent database access. And its read-only mode hides write tools from the agent. On Cloud SQL and AlloyDB it also locks the session as read-only at the engine level, which a COMMIT; can't undo.
pgEdge Postgres MCP Server
pgEdge's server works with standard community Postgres, Amazon RDS and pgEdge's own distributions. It is the one to look at if you want a single server reached over HTTP, with TLS and user or token authentication, rather than a process started by each developer's editor. It includes schema introspection, execute_explain, count_rows, and optional embedding and semantic-search tools. Check its docs for the Postgres versions it supports and how to enable or disable writes in the release you install.
AWS Labs Postgres MCP Server
AWS Labs' server is built for Aurora PostgreSQL and RDS for PostgreSQL. It connects over the RDS Data API (Aurora only), the Postgres wire protocol, or the wire protocol with IAM authentication, and reads database credentials from AWS Secrets Manager. Writes are off unless you add --allow_write_query. Its read-only check parses every query with PostgreSQL's own parser and rejects multi-statement input, but AWS describes it as best-effort defence in depth, not a security boundary. For production, AWS recommends --privilege_check enforce, which refuses to run as a superuser, an rds_superuser member or a role with BYPASSRLS.
Your database provider's server
If your database is hosted on Supabase or Neon, start with the provider's own MCP server. These combine SQL access with project management, branching and logs, and sign you in with OAuth instead of a connection string in a config file. Our Supabase MCP server guide and Neon MCP server guide cover setup and read-only mode for each.
How to connect a Postgres MCP server read-only
Every server above has a read-only setting, and you should use it. But the setting that holds even when the server's own check fails is the Postgres role it connects as. Create one just for the agent.
1. Create a read-only role
CREATE ROLE mcp_readonly WITH LOGIN PASSWORD 'change-me'
NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION;
GRANT CONNECT ON DATABASE mydb TO mcp_readonly;
GRANT USAGE ON SCHEMA public TO mcp_readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO mcp_readonly;
-- Defaults for every session this role opens
ALTER ROLE mcp_readonly SET default_transaction_read_only = on;
ALTER ROLE mcp_readonly SET statement_timeout = '30s';
A few things to get right:
- Grant
SELECTonly on what the agent needs. If a table holds customer emails, password hashes or tokens and the agent doesn't need it, grant on a narrower set of tables or on views that leave those columns out. default_transaction_read_onlyis only a default. A session can switch it off withSET. The role's grants are what actually stop writes.- Never connect as a superuser or the database owner. Superusers and roles with
BYPASSRLSskip row-level security, and the owner can change anything. - Don't grant
pg_read_server_files,pg_write_server_filesorpg_execute_server_program. They let SQL reach the host's filesystem and run programs. - Keep the timeout. An agent exploring a large table can easily write a query that scans all of it.
2. Point the server at the role
With Postgres MCP Pro in restricted mode, add this to your client's MCP config (.mcp.json for Claude Code, .cursor/mcp.json for Cursor):
{
"mcpServers": {
"postgres": {
"command": "uvx",
"args": ["postgres-mcp", "--access-mode=restricted"],
"env": {
"DATABASE_URI": "postgresql://mcp_readonly:change-me@db.example.com:5432/mydb"
}
}
}
}
With MCP Toolbox for Databases:
{
"mcpServers": {
"postgres": {
"command": "./PATH/TO/toolbox",
"args": ["--prebuilt", "postgres", "--stdio"],
"env": {
"POSTGRES_HOST": "db.example.com",
"POSTGRES_PORT": "5432",
"POSTGRES_DATABASE": "mydb",
"POSTGRES_USER": "mcp_readonly",
"POSTGRES_PASSWORD": "change-me"
}
}
}
}
VS Code uses a top-level servers key in .vscode/mcp.json instead of mcpServers. Most clients store this file in plain text, so keep it out of version control and don't put a production password in a file the whole team shares.
3. Check what the agent can do
Ask the agent "What tables can you see? Use the Postgres MCP tools." Then ask it to insert a test row. With the role above, the insert should fail with a permission or read-only error. If it succeeds, the server is not connecting as the role you think it is.
What a read-only role does not protect against
A read-only role stops writes. It doesn't stop the agent from reading every row it is allowed to read, and it doesn't stop the agent from doing what text in those rows tells it to do. If a support ticket or user profile in your database contains instructions, an agent that reads it through MCP may follow them, for example by querying a sensitive table and pasting the result somewhere you didn't intend. We cover this in Prompt Injection via Untrusted Database Results.
What helps:
- Keep manual approval on for tool calls, and read the SQL before you approve it.
- Use a development database or a read replica where you have one. An agent's exploratory queries then don't compete with your application for the primary's connections and CPU. See Postgres Connection Pooling Limits Under Analytical Query Load.
- Prefer fixed tools over open SQL for anything an agent does unattended. A tool that runs one reviewed query with parameters is far harder to misuse than
execute_sql.
Where a Postgres MCP server stops being the right tool
A Postgres MCP server is built for development: exploring a schema, writing and testing queries, reading query plans, finding missing indexes. Using it to answer business questions such as "what was our churn last month?" runs into problems that a read-only role doesn't solve:
- Each agent writes its own SQL. Two agents can define "active customer" differently and both return a confident number. See Why AI Agents Require Stricter Data Governance Than Human Analysts.
- It queries the live database. Every question becomes a query on the instance that serves your users. See Production Database Access Risks for AI Agents.
- Access stops at the table. The agent sees whatever its role sees. It can't be limited to a list of approved metrics.
For recurring metrics and agents that run without someone watching, put a governed layer between the database and the agent. Define metrics once, compute them away from the primary, and expose that layer through MCP instead of the raw schema. MCP Servers as the Access Control Boundary for Agent Data explains the approach. Dreambase, for example, models a Supabase project's data into governed Parquet datasets that agents query through DuckDB and one MCP server, so they never touch the production database. Check each vendor's own site for current features and pricing.
A practical split: give developers a Postgres MCP server, read-only and scoped, for building the product. Give analytics agents a governed layer for answering questions about the business.
Frequently asked questions
- What is a Postgres MCP server?
- It is a Model Context Protocol server that connects an AI client such as Claude Code, Cursor or VS Code to a PostgreSQL database. It exposes tools like listing schemas, describing tables and running SQL, so the agent can read your data model and query it without you pasting results into the chat.
- Is the official Postgres MCP server still maintained?
- No. The reference server, @modelcontextprotocol/server-postgres, was archived on May 29, 2025 and now lives in the modelcontextprotocol/servers-archived repository. Datadog Security Labs showed that its read-only mode could be escaped with a stacked COMMIT, and the last version published to npm (0.6.2) was never patched. Use a maintained server instead.
- Which Postgres MCP server should I use?
- For development work against any Postgres, Postgres MCP Pro (crystaldba/postgres-mcp) adds query plans, index tuning and health checks on top of SQL access. MCP Toolbox for Databases suits teams that want curated tools or run on Google Cloud. AWS Labs' server is built for Aurora PostgreSQL and RDS for PostgreSQL. If your database is on Supabase or Neon, start with that provider's own MCP server.
- How do I make a Postgres MCP server read-only?
- Turn on the server's read-only mode (restricted mode in Postgres MCP Pro, leaving out --allow_write_query in AWS Labs' server), and connect it as a dedicated Postgres role that has only CONNECT, USAGE and SELECT on the schemas the agent needs. The role's privileges are the real boundary. Server-side read-only checks are a second layer.
- Can I connect a Postgres MCP server to my production database?
- You can, but treat it as a last resort. Use a read-only role with a statement timeout, point it at a read replica rather than the primary where you have one, and keep manual approval of tool calls on. For recurring analytics, give agents a governed copy of the data instead of the live database.
- Are Postgres MCP servers free?
- The servers in this guide are open source and free to run. You pay for the database they connect to and for any cloud services they call. Check each project's repository and your cloud provider's pricing page for current terms.
Sources
- MCP vulnerability case study: SQL injection in the Postgres MCP server (Datadog Security Labs)
- Archived reference Postgres MCP server (modelcontextprotocol/servers-archived)
- Postgres MCP Pro (crystaldba/postgres-mcp on GitHub)
- MCP Toolbox for Databases, PostgreSQL prebuilt config
- MCP Toolbox for Databases, Read-Only Tools
- pgEdge Postgres MCP Server documentation
- Amazon Aurora Postgres MCP Server (AWS Labs)
- Model Context Protocol introduction





