Postgres MCP server: give agents safe access to production data
A Postgres MCP server lets an agent query your database. The safe version is mostly database configuration, on PostgreSQL that runs in your cloud or data center.
45 minutes with an engineer: we review your workload and sketch the design.
A Postgres MCP server exposes database tools to an AI agent over the Model Context Protocol. The protocol leaves access control to the server, so the database has to enforce it: a read-only role, row-level security, statement timeouts and an audit trail. The agent never gets more than that role can do.
- PostgreSQL
- MySQL
- MongoDB
- Redis
- Valkey
- OpenSearch
- Kafka
- RabbitMQ
- AWS
- Google Cloud
- Azure
- Kubernetes
- OpenShift
- Prometheus
- Grafana
- Datadog
- Dynatrace
- Splunk
Why agent access to production data goes wrong
An agent writes its own SQL, so it will eventually write a query nobody planned for. If the database role behind the MCP server can do more than the agent should, the first bad query does more than it should.
- A role with too many privileges.
- A server configured with an owner or superuser connection can read and change anything.
- Unbounded queries.
- A large scan or an accidental cross-join can slow production for everyone.
- Data returned into the model's context.
- Rows that reach the model are visible to whoever can see the conversation.
- Instructions hidden in data.
- Text stored in a row can try to steer the agent that reads it.
- No record of what was asked.
- Without an audit trail, an incident cannot be reconstructed.
The pattern: the database enforces the limits
The MCP specification says servers must validate tool inputs, implement access controls, rate limit calls and sanitise outputs. For a database server, the strongest place to enforce access is the database itself.
In MCP the client discovers tools with `tools/list` and invokes them with `tools/call`, and the specification recommends a human in the loop for sensitive operations (MCP tools specification, checked 30 Oct 2026). Authorisation is optional in the protocol and is defined for HTTP transports; stdio servers are expected to take credentials from the environment (MCP authorization, checked 30 Oct 2026). Either way, the credential the server holds decides what the agent can do.
| Component | Role | Documentation |
|---|---|---|
| MCP server | Exposes tools such as a query tool, validates inputs and limits calls | MCP tools |
| Dedicated database role | The only identity the server uses: no superuser, no row-security bypass, a connection limit | CREATE ROLE and GRANT |
| Row-level security | Limits which rows the role can see | Row security policies |
| Read-only default and timeouts | Blocks writes and stops long queries | Client connection defaults |
| Audit | Records which statements ran, and which role ran them | pgAudit |
Documentation checked 30 Oct 2026.
CREATE ROLE agent_ro LOGIN NOSUPERUSER NOBYPASSRLS CONNECTION LIMIT 5; ALTER ROLE agent_ro SET default_transaction_read_only = on; ALTER ROLE agent_ro SET statement_timeout = '5s'; ALTER ROLE agent_ro SET idle_in_transaction_session_timeout = '10s'; GRANT USAGE ON SCHEMA public TO agent_ro; GRANT SELECT ON public.tickets TO agent_ro; ALTER TABLE public.tickets ENABLE ROW LEVEL SECURITY; CREATE POLICY agent_scope ON public.tickets FOR SELECT TO agent_ro USING (visibility = 'internal');
The example is a starting point, not a complete design. The table, column and policy names are placeholders, and the values should be tested against your own workload.
What the public PostgreSQL MCP servers do
Two public servers show the range. The original reference server runs every query in a read-only transaction, and a later server offers a restricted mode for production and an unrestricted mode for development.
| Server | Access control it documents | Note |
|---|---|---|
| Reference Postgres server | One `query` tool. All queries run inside a READ ONLY transaction. Table schemas are exposed as resources. | The repository was archived by its owner on 29 May 2025. |
| Postgres MCP Pro | Unrestricted mode for development. Restricted mode for production, with read-only transactions, SQL parsing that rejects COMMIT and ROLLBACK, and an execution time limit. | The README notes that enabling unsafe stored procedure languages could circumvent these protections. |
Source: each project's README, checked 30 Oct 2026. Neither is endorsed or operated by OSStend.
Both rely on the database for the final guarantee. A read-only transaction in the server is a second layer, not a replacement for a read-only role.
Limits to set on the role
Each limit is a setting on the role or the database. Together they bound what a single bad query can cost.
| Setting or control | What it bounds | How to approach it |
|---|---|---|
| `default_transaction_read_only` | Writes | Set on the agent role so every transaction starts read-only. |
| `statement_timeout` | Query duration | Set a short value on the role. Raise it only for named reporting use. |
| `idle_in_transaction_session_timeout` | Sessions that hold locks while idle | Set on the role, so an abandoned session ends. |
| Connection limit on the role | Concurrent sessions | Keep it low and put a pool in front. |
| Column and table grants | What can be read at all | Grant SELECT on named tables, or on views that hide columns. Do not grant on whole schemas by default. |
| Row-level security | Which rows can be read | Key policies to the database role, or to a value set by a layer the agent's SQL cannot reach. |
| Read replica | Load on the primary | Point the server at a replica when query volume or cost matters. |
Settings are documented in the PostgreSQL manual, checked 30 Oct 2026.
Pitfalls to design for
These risks follow from how PostgreSQL and MCP behave, as documented. They are properties of the pattern rather than of one product.
- Owners and superusers bypass row security.
- Superusers and roles with BYPASSRLS always bypass it, and table owners normally do too. Connect as a role that is none of these (PostgreSQL docs).
- A policy that reads a session setting can be defeated.
- If the role can run arbitrary SQL, it can usually set the value itself. Key the policy to the role.
- Views can bypass the base table's row security.
- A view runs with its owner's privileges unless it is created with `security_invoker` (PostgreSQL 15 and later). See CREATE VIEW, and test the result.
- Tool output goes to the model.
- The MCP specification asks servers to sanitise outputs. Mask or exclude sensitive columns at the database, since the model cannot unsee a value.
- Stdio configuration holds the credential.
- Authorisation for stdio servers comes from the environment, so protect the file and the process that holds the connection string.
What OSStend runs, where it runs and who it is not for
OSStend designs, moves and runs PostgreSQL in its own cloud, in your cloud account or co-managed in your data center. The role, grant and replica design for agent access is part of the cluster design you sign off.
Agent access sits next to other parts of the AI data layer, such as agent memory, which keeps durable state for the agent in the same PostgreSQL. Context engineering explains how the rows an agent reads become part of what the model sees.
Contact us if
- ✓Agents need to read production data, and the data must stay in your own environment.
- ✓A security review wants a documented role, policy and audit design.
- ✓You want the database run by engineers who also run your other engines.
Not a fit if
- ✕The agent only needs a small, public dataset. A copy exported to a file is simpler.
- ✕You want agents to write to production tables. That needs its own design, which this page does not cover.
Questions about the PostgreSQL MCP server
A PostgreSQL MCP server gives an agent database tools, and the database role behind it sets what the agent can do. These are the questions teams ask most before connecting one to production data.
What is a PostgreSQL MCP server?
A PostgreSQL MCP server, also called a Postgres MCP server, is a program that exposes PostgreSQL to an AI agent through the Model Context Protocol. It offers tools, such as running a query, that the agent can discover and call.
What is an MCP database?
An MCP database is a database exposed to an AI agent through an MCP server. The server offers tools such as running a query, and the database role behind it decides what the agent can read or change. A PostgreSQL MCP server is the PostgreSQL case.
Is a Postgres MCP server safe for production?
It can be, when the database enforces the limits. Use a dedicated read-only role, row-level security, statement timeouts and an audit trail, and prefer a read replica. A server that connects as an owner or superuser is not safe for production.
Does MCP handle authentication?
Authorisation is optional in the protocol. For HTTP transports the specification describes an OAuth-based flow, and stdio servers are expected to take credentials from the environment. In both cases the database role decides what the agent can do.
Can row-level security stop an agent seeing other tenants' rows?
Yes, if the role is not a superuser, a table owner or a BYPASSRLS role, and the policy cannot be changed by the agent's own SQL. Test it with queries that try to cross the boundary.
Should an agent write to the database?
Start read-only. Writes need their own role, narrow grants and review, since an agent can issue a statement nobody planned for.
Related pages
- PostgreSQLManaged PostgreSQL service and Postgres hostingManaged PostgreSQL service, compared: how to choose between RDS, Aiven, Crunchy Bridge, Neon and a tailored cluster run 24/7 in your cloud or ours.
- AI dataAI database and data platform for agentsAn AI data platform built from engines you already trust: PostgreSQL with pgvector, Qdrant, OpenSearch, Redis or Valkey, Kafka and RabbitMQ, in your own environment.
- AI data · MemoryAgent memory on PostgreSQL and Redis in productionAgent memory on PostgreSQL and Redis: long-term memory and checkpoints in Postgres with pgvector, short-term state in Redis or Valkey, and retention.
- GuideContext engineering for AI agents: the data layerContext engineering for AI agents: what it is, how it differs from prompt engineering, and which data stores hold memory, retrieval and fresh context. Dated.
Talk to an engineer about agent access
Bring the data your agents need and where it lives. An engineer will help you design the role, the policies and the audit trail.
- ✓A design sketch for your workload, not a plan menu
- ✓A zero-downtime cutover path for what you run today
- ✓A price for that design, after the review