readonly-postgres-mcp
Provides read-only access to PostgreSQL databases, with tools for running safe queries, listing tables, describing table structures, fetching schema DDL, and generating read-only roles.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@readonly-postgres-mcpShow me the schema for the customers table"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
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:latestclaude 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 deletedThat 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.
Related MCP server: pg-mcp
The security model in one table
Layer | What it stops | Where it lives |
Role holds | every write, including ones the parser cannot see | PostgreSQL |
| data-modifying CTEs, | PostgreSQL |
Read-only transaction per connection | a session that somehow reset the role default | this server |
| one runaway query taking the database with it | PostgreSQL |
Row cap with a | a | 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_privilegeTools
Tool | Description |
| Databases this token can reach, with connection status |
| One read-only statement, with a row cap and a |
| Tables and views, optionally for one schema |
| Columns, constraints and indexes |
| Schema-only DDL via |
| The provisioning SQL for a database, as a tool |
Setup
1. Provision the role
readonly-postgres-mcp role-sql app_productionPrints 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-01The 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.
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- Alicense-qualityDmaintenanceA production-ready MCP server that enables safe, read-only SQL SELECT queries against PostgreSQL databases with built-in security validation. It features connection pooling, automatic row limits, and structured logging to ensure secure and reliable database interactions.42ISC
- AlicenseBqualityDmaintenanceA lightweight Postgres MCP server for safe database exploration and query analysis, read-only by default, with multi-database support.43MIT
- Alicense-qualityCmaintenanceRead-only PostgreSQL MCP server that enables running SELECT queries, listing tables and schemas, and describing columns, with built-in protection against writes and malicious SQL attacks.727MIT
- AlicenseAqualityDmaintenanceA secure, read-only PostgreSQL MCP server that provides safe database introspection and querying capabilities.1412MIT
Related MCP Connectors
MCP server for managing Prisma Postgres.
Read-only MCP server for ClassQuill, a tutoring-business-management platform.
MCP server for interacting with the Supabase platform
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/geolep/readonly-postgres-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server