How to Build a Postgres MCP Server: Four Layers of Defense for Natural-Language SQL
TL;DR · What you'll learn
- 1 At POSETTE, Microsoft's Pamela explains the full range of Postgres MCP architectures, from exploratory servers that let agents send arbitrary SQL to fully typed operational servers.
- 2 The talk opens with a demo in which GitHub Copilot, using Opus 4.6, answers a question about which bee species are active locally in April by issuing SQL through MCP.
- 3 MCP stands for Model Context Protocol, an open protocol that defines how AI apps and agents retrieve context from external tools and data sources. It was proposed by Anthropic and is now part of the Linux Foundation.
- 4 By using least-privilege roles that grant only SELECT access to a specific schema, the system can block INSERT, DROP, DELETE, and even CTE-based evasions at the database level.
- 5 To be confident that agents can safely generate their own SQL, multiple protection layers are required, since tricky queries can slip through any single layer.
- 6 Potential DDoS-like queries such as `pg_sleep()` or massive `CROSS JOIN`s are handled by enforcing tool timeouts such as 30 seconds.
- 7 The talk concludes that exploratory, read-only, and fully typed server designs each have strengths and tradeoffs, and should be combined according to the situation.
Read as slides
3 slides total
What MCP is and how it works with Postgres
The session opens with a demo in which GitHub Copilot, working through Opus 4.6, queries Postgres and answers a question using local bee observation data. The agent recognizes the MCP server, decides to use it, and executes SQL on its own.
MCP is an open protocol that defines how AI applications and agents retrieve context from external data sources. It was first proposed by Anthropic, later broadly adopted, and now sits under the Linux Foundation.
Before MCP, every data source typically required a custom integration. Pamela explains that by putting an MCP server in front of each source, developers now get a common access mechanism instead.
Four layers of defense for natural-language SQL
The core of the talk is a defensive design that makes it safer to let agents write SQL freely. The first layer is a least-privilege role that allows only SELECT access to a specific schema, blocking INSERT, DROP, and DELETE at the database level.
Even CTE-based evasions using `WITH` clauses can be stopped when the database itself enforces those restrictions. Function calls with side effects can likewise be prevented through the least-privilege role.
On top of that, DDoS-style operations such as `pg_sleep()` or huge `CROSS JOIN`s are handled by giving the tool a timeout and terminating the query. The message is that only by combining multiple layers can teams have confidence in executing generated SQL.
Choosing the right server design for the job
In the closing section, Pamela reviews several ways to build an MCP server on top of Postgres. An exploratory design is flexible because it can answer a wide range of questions, but it also carries the risk of permitting any SQL operation.
A fully typed toolset is safer, but the scope of questions it can answer is narrower. Between those extremes sits the option of read-only SQL queries.
She concludes that because so much can be enforced at the database level, this becomes the safest practical approach. The talk ends by recommending natural-language database interaction for both internal and external users, while emphasizing the need for careful server design.
Editor's Take
The appeal of natural-language database access always leaves a security question. Granting SELECT-only via a least-privilege role and physically blocking CTEs and DDoS-style queries at the database layer is a principle that applies far beyond MCP — to any case of handing an agent permissions. Prompt-level warnings can be bypassed; a database permission boundary cannot. It is a clean example of placing the trust barrier one layer below the application, not within it.
Source
Microsoft Developer
An MCP for your Postgres DB | POSETTE: An Event for Postgres 2026
This article auto-summarizes the YouTube video's transcript with Claude. Please refer to the original video for nuance and exact wording.
Watch on YouTube →Related
3 articles
Mac-TV
Apple Enters MCP: Toward an Era When Software Is Written for AI Bots, Not GUIs
Apple recently announced MCP (Model Context Protocol) support, a move the presenter says sent a shockwave through the industry.
Google Cloud Tech
MCP vs API: Understanding the Protocol Rewriting How AI Connects to Everything
The way AI models connect to tools, data, and systems is being rewritten around a new standard: the Model Context Protocol (MCP).
Eric Tech
The Complete OpenCode Tutorial: Keep Claude Code's Playbook (Skills, MCP, agents.md) While Freely Choosing Your Model
Coding agents tend to lock you into one provider and model, but OpenCode lets you freely choose between existing subscriptions, free models, API-key models, or local models -- while keeping the features popularized by Claude Code and Codex.