# Building safe MCP servers for your PostgreSQL database

DevFeed: [Building safe MCP servers for your PostgreSQL database](<https://devfeed.tech/articles/building-safe-mcp-servers-for-your-postgresql-database-21752.md>)

Original publisher: [Read original article](<http://blog.pamelafox.org/2026/08/building-safe-mcp-servers-for-your.html>)

Author: Pamela Fox (noreply@blogger.com)

Published: 2026-08-12T18:07:30Z

Content type: article

Language: en

Sources: [Pamela Fox](<https://devfeed.tech/sources/pamela-fox.md>)

Topics: [Model Context Protocol](<https://devfeed.tech/topics/model-context-protocol.md>), [MCP Server](<https://devfeed.tech/topics/mcp-server.md>), [PostgreSQL](<https://devfeed.tech/topics/postgresql.md>), [SQL](<https://devfeed.tech/topics/sql.md>), [Python](<https://devfeed.tech/topics/python.md>), [GitHub Copilot](<https://devfeed.tech/topics/github-copilot.md>), [Open Source](<https://devfeed.tech/topics/open-source.md>), [LangChain](<https://devfeed.tech/topics/langchain.md>)

Tags: [github-copilot](<https://devfeed.tech/tags/github-copilot.md>), [langchain](<https://devfeed.tech/tags/langchain.md>), [mcp](<https://devfeed.tech/tags/mcp.md>), [mcp-server](<https://devfeed.tech/tags/mcp-server.md>), [open-source](<https://devfeed.tech/tags/open-source.md>), [postgresql](<https://devfeed.tech/tags/postgresql.md>), [python](<https://devfeed.tech/tags/python.md>), [sql](<https://devfeed.tech/tags/sql.md>)

## AI overview

This tutorial explains how to build MCP servers for PostgreSQL databases and how to control the level of agent access. It compares flexible servers that accept generated SQL with stricter servers using typed tools and templated queries, using Python and FastMCP examples.

## Source excerpt

Model Context Protocol (MCP) is an open protocol that describes how agents can connect to external tools and data sources, and is now widely supported by the most popular coding agents (like GitHub Copilot, Claude Code, and Codex) and agent frameworks (like LangChain and Pydantic AI). If you want to give agents a standard way to access the data in a database, you can build your own MCP server and expose tools for the agent to query or even modify data. But you need to design your MCP server carefully, to ensure that agents can do everything that users want - but nothing that you don't want them to do! In this blog post, we'll walk through the range of ways to build MCP servers on top of a PostgreSQL database, since PostgreSQL is the most popular open source database and is production-ready with hosted offerings like Azure Database for PostgreSQL. You can apply these same principles to any database, however. There's a spectrum of ways to build MCP servers on top of a database. We'll start with the most flexible option, exploratory servers that allow the agent to generate full SQL queries, conclude with the strictest option, fully typed tools for templated queries, and explore options in the middle too. Free-form SQL Let's take a look at a simple MCP server that gives the agent as much information and control as possible. For all of our examples, we use the Python language and the FastMCP package, but SDKs are available in multiple languages. All code is available in the GitHub repository. We start off by giving the server a name, which the agent will see and consider when deciding which MCP server to invoke for a given user query: mcp = FastMCP("Bees database MCP server") For this example, my database stores observations of bees, so I name it accordingly. We then define an execute_sql tool that accepts any SQL string, executes it against the database, and returns the rows. @mcp.tool() async def execute_sql(sql: str) -> str: """Execute a SQL query against the database