Read-Only Database Access for Agents via MCP
Give an agent safe SQL querying through an MCP server connected with a read-only database role.
Enforce safety in the database, not only in the prompt.
1. Create a read-only role
CREATE ROLE agent_ro LOGIN PASSWORD 'change-me';
GRANT CONNECT ON DATABASE app TO agent_ro;
GRANT USAGE ON SCHEMA public TO agent_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO agent_ro;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO agent_ro;
ALTER ROLE agent_ro SET statement_timeout = '5s';Better still: point it at a read replica and grant only on views that exclude sensitive columns.
2. MCP config
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres",
"postgresql://agent_ro:change-me@localhost:5432/app"]
}
}
}The reference Postgres server is archived (github.com/modelcontextprotocol/servers-archived) and the npm package @modelcontextprotocol/server-postgres is marked deprecated ("no longer supported"); the config above still shows the pattern, but for production use a maintained Postgres MCP server, and keep the same read-only-role principle.
Failure modes
- The reference server package is deprecated; plan a maintained replacement.
- Expensive queries can load production; use a replica and statement_timeout.
- Views that include sensitive columns leak data; grant only on safe views.
Markdown version: /blueprints/mcp-postgres-readonly.md
Want a deeper build of this?
Vote for an extended, fully tested version (more tools, a full workflow file). Votes decide what we build next.
Get one fully built blueprint a week
A weekly email: one blueprint with config, cost, setup time and failure modes, plus a vote on what we build next.
Related blueprints
Build a Custom MCP Server in Python (MCPServer, formerly FastMCP)
Expose your own functions as MCP tools and resources using the official Python SDK in about 20 lines.
Build a Custom MCP Server in TypeScript
A minimal stdio MCP server using @modelcontextprotocol/sdk with a zod-validated tool.
GitHub MCP Server with a Scoped Token
Run GitHub's official MCP server in Docker with a fine-grained, least-privilege personal access token.