readonly-postgres-mcp
A read-only MCP server for PostgreSQL that serves multiple databases from one process, enforcing read-only access via PostgreSQL grants rather than SQL string matching, and supports both local stdio and remote HTTP transports.
README
readonly-postgres-mcp
A read-only MCP server for PostgreSQL that serves many databases from one process, runs locally over stdio or remotely over HTTP, and enforces read-only access with PostgreSQL grants instead of SQL string matching.
docker run -p 8000:8000 \
-e PGMCP_DATABASES='[{"name":"app","url":"postgresql://mcp_ro:pw@db:5432/app"}]' \
-e PGMCP_TOKENS='[{"name":"laptop","sha256":"<sha256 of your token>"}]' \
ghcr.io/geolep/readonly-postgres-mcp:latest
claude mcp add --transport http pg --scope user \
https://your-host/mcp --header "Authorization: Bearer $TOKEN"
Why another Postgres MCP server
Most of them enforce "read-only" by pattern-matching the SQL string. That approach has already
failed in production: the official reference server was
archived after its read-only mode was found
to accept DROP SCHEMA. String matching cannot see through this, for example:
WITH deleted AS (DELETE FROM widgets RETURNING *) SELECT * FROM deleted
That statement is a SELECT. Every leading-keyword check accepts it, and it empties your table.
This server takes the guarantee out of the application entirely. It connects as a role that holds
SELECT and nothing else, with default_transaction_read_only set. A write does not fail because
the server rejected it — it fails because the database never granted it.
readonly-postgres-mcp role-sql generates that role for you, with statements to verify the grants
landed and to roll them back.
The security model in one table
| Layer | What it stops | Where it lives |
|---|---|---|
Role holds SELECT only |
every write, including ones the parser cannot see | PostgreSQL |
default_transaction_read_only = on |
data-modifying CTEs, SELECT INTO, CREATE TEMP TABLE |
PostgreSQL |
| Read-only transaction per connection | a session that somehow reset the role default | this server |
statement_timeout, CONNECTION LIMIT |
one runaway query taking the database with it | PostgreSQL |
Row cap with a truncated flag |
a SELECT * flooding the model's context |
this server |
| Statement guard | a clear error message instead of a driver stack trace | this server |
| Audit log with the token name | not knowing who read what | this server |
The first two rows are the security. The statement guard is ergonomics — see SECURITY.md and docs/security-model.md.
This is not a claim; it is a test suite. tests/test_readonly_enforcement.py provisions a real
PostgreSQL with a role built by role-sql, then sends each attack around the guard, straight
to the driver:
INSERT / UPDATE / DELETE / TRUNCATE → 25006 read_only_sql_transaction
WITH x AS (DELETE ... RETURNING *) ... → 25006 read_only_sql_transaction
SELECT ... INTO / CREATE TEMP TABLE → 25006 read_only_sql_transaction
DROP SCHEMA public CASCADE → 25006 read_only_sql_transaction
COPY ... TO PROGRAM / pg_read_file() → 42501 insufficient_privilege
Tools
| Tool | Description |
|---|---|
list_databases |
Databases this token can reach, with connection status |
query |
One read-only statement, with a row cap and a truncated flag |
list_tables |
Tables and views, optionally for one schema |
describe_table |
Columns, constraints and indexes |
get_schema_ddl |
Schema-only DDL via pg_dump |
generate_readonly_role_sql |
The provisioning SQL for a database, as a tool |
Setup
1. Provision the role
readonly-postgres-mcp role-sql app_production
Prints three blocks: grant (run it), verify (prove the grants landed) and revoke
(roll it back). To keep tables out of reach, add --exclude-tables users api_keys — the generator
then omits ALTER DEFAULT PRIVILEGES, so a table created later does not silently become readable.
2. Mint a token per consumer
readonly-postgres-mcp gen-token laptop --databases app --row-cap 500 --expires 2027-01-01
The token is shown once; only its SHA-256 goes in the config. One token per consumer means you can revoke one without disturbing the others.
3. Run it
Locally over stdio, with no token needed because the operating system is the boundary:
{
"mcpServers": {
"pg": {
"command": "readonly-postgres-mcp",
"args": ["serve"],
"env": { "PGMCP_DATABASES": "[{\"name\":\"app\",\"url\":\"postgresql://mcp_ro:pw@localhost/app\"}]" }
}
}
}
Remotely over HTTP — see docs/deploy-coolify.md for a full walkthrough, or docker-compose.example.yml for the short version. Put TLS in front of it: the bearer token is a shared secret.
Full reference: docs/configuration.md.
What it does not protect against
Prompt injection. Any agent holding a token can read everything its role can read. If the agent also processes untrusted input — scraped pages, user messages, third-party data — assume that input can steer the queries. Scope tokens to the fewest databases, and leave sensitive tables out of the grant.
Data reaching the model. Every row returned goes into an LLM context. Treat each granted table as disclosed to whatever the agent is connected to.
Development
python -m venv .venv && .venv/bin/pip install -e ".[dev]"
.venv/bin/pytest # integration tests need Docker
.venv/bin/pytest -m "not integration"
License
Apache-2.0 — see LICENSE.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。