postgres-mcp
Enables connecting to a PostgreSQL database hosted on DigitalOcean Managed Databases for schema exploration and querying.
Allows exploring PostgreSQL database schemas (tables, columns, constraints, indexes, triggers, functions, views, enums, stats) and executing guarded read/write queries with a dry-run and confirm workflow.
Enables connecting to a PostgreSQL database hosted on Supabase for schema exploration and querying.
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., "@postgres-mcplist all tables in the public schema"
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.
postgres-mcp
A fast, self-hostable PostgreSQL MCP server. Explore your database (schemas, tables, columns, constraints, relationships, indexes, triggers, functions/procedures, views, enums, stats) and run guarded read or write queries — with a selectable access mode so the same server can be locked to read-only or opened up for edits.
Built with Python + uv, FastMCP and psycopg3.
Runs equally well via uvx or Docker. Works with any PostgreSQL: local, Neon,
Supabase, Cloud SQL, RDS, DigitalOcean, …
Naming note: this is an independent project. There is a separate, unrelated PyPI package also called
postgres-mcp(Crystal DBA's "Postgres MCP Pro"). If you publish, pick a unique distribution name.
Quick start with Docker
# 1. Build the image
docker build -t postgres-mcp .
# 2. Add it to your MCP client (.mcp.json) — read-only by default:{
"mcpServers": {
"postgres": {
"command": "docker",
"args": ["run", "-i", "--rm", "-e", "DATABASE_URI", "-e", "PG_MCP_ACCESS_MODE", "postgres-mcp"],
"env": {
"DATABASE_URI": "postgresql://readonly_user:pass@host:5432/db",
"PG_MCP_ACCESS_MODE": "readonly"
}
}
}
}The server speaks MCP over stdio, so the container must be run with -i.
DATABASE_URI and PG_MCP_ACCESS_MODE are listed both in args (to forward the
names into the container) and in env (their values), so secrets stay out of
the image.
Try it instantly with a seeded demo database
docker compose --profile demo up -d demo-db # Postgres on localhost:55432, pre-seeded
docker build -t postgres-mcp .
# Point DATABASE_URI at: postgresql://readonly_user:readonly_pw@host.docker.internal:55432/appdbRelated MCP server: pg-mcp
Run without Docker (uv)
uv sync
DATABASE_URI=postgresql://readonly_user:pass@host:5432/db \
PG_MCP_ACCESS_MODE=readonly \
uv run postgres-mcpOr straight from a Git repo, no clone needed:
{
"mcpServers": {
"postgres": {
"command": "uvx",
"args": ["--from", "git+https://github.com/<you>/postgres-mcp", "postgres-mcp"],
"env": { "DATABASE_URI": "postgresql://readonly_user:pass@host:5432/db" }
}
}
}Access modes
Set with PG_MCP_ACCESS_MODE. Each mode adds tools; higher modes include the
lower ones. Choose the least privilege you need.
Mode | Read tools |
|
| DB session |
| ✅ | — | — | forced |
| ✅ | ✅ | — | normal |
| ✅ | ✅ | ✅ | normal |
The access mode is enforced two ways: write tools are not even registered in lower modes, and in
readonlythe database session itself rejects writes. For real safety, also connect with a DB role scoped to what you need.
Tools
Read (all modes)
Tool | Purpose |
| Current access mode + safety config |
| User schemas |
| Tables/views/matviews with size & row estimate |
| Columns, types, defaults, identity/generated, PK |
| PK / unique / FK / check / exclusion |
| Incoming & outgoing foreign keys |
| Index definitions |
| Trigger definitions (schema or one table) |
| Functions & procedures (signatures) |
| Full source of a function/procedure |
| View / materialized-view definitions |
| Enum types and labels |
| Sizes, live/dead rows, vacuum/scan stats |
| Guarded read-only query runner |
Write (mode-gated)
Tool | Mode | Purpose |
|
| INSERT / UPDATE / DELETE / MERGE |
|
| CREATE / ALTER / DROP / TRUNCATE / COMMENT / GRANT / REVOKE / REINDEX |
How writes stay safe — the dry-run + confirm workflow
Every write tool defaults to confirm=false, which performs a dry run:
Understand the data flow first. The model is instructed to inspect
describe_table,get_relations(cascading FKs) andlist_triggersbefore changing anything, so cascade/side-effects are known up front.Dry run. With
confirm=false, the statement runs inside a transaction that is rolled back. Because Postgres has transactional DDL, this both validates the statement and returns the exactaffected_rows— without persisting anything.Review. If
affected_rowsis larger than expected, fix theWHEREclause and dry-run again.UPDATE/DELETEwithout aWHEREis refused unlessallow_full_table_write=true.Commit. Re-run with
confirm=trueto apply the change.
On top of this, your MCP client (Claude Code, etc.) prompts the human to approve each tool call — so a real person is always in the loop before a commit.
Statements that can't run in a transaction (
CREATE INDEX CONCURRENTLY,CREATE/DROP DATABASE,VACUUM) can't be dry-run;execute_ddltells you to re-run withconfirm=true, non_transactional=true(no rollback safety).
Configuration
Env var | Default | Meaning |
| — |
|
|
|
|
|
| Per-statement timeout |
|
| Hard cap on returned rows |
|
| Max pooled connections |
TLS works out of the box (e.g. Neon: append ?sslmode=require to the URI).
Recommended DB roles
Read-only:
CREATE ROLE readonly_user LOGIN PASSWORD 'change_me';
GRANT CONNECT ON DATABASE mydb TO readonly_user;
GRANT USAGE ON SCHEMA public TO readonly_user;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly_user;
ALTER ROLE readonly_user SET statement_timeout = '15s';Read-write (add only what you need):
GRANT INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_user;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO app_user;Add your own tools
Drop a function in src/postgres_mcp/server.py:
@mcp.tool()
def biggest_tables(schema: str = "public", top: int = 10) -> list[dict]:
"""Largest tables in a schema by total size."""
return db.query(
"""SELECT c.relname AS name,
pg_size_pretty(pg_total_relation_size(c.oid)) AS size
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = %s AND c.relkind IN ('r','p','m')
ORDER BY pg_total_relation_size(c.oid) DESC LIMIT %s""",
(schema, top),
)Roadmap
☐ Cloud SQL connectivity guide (Cloud SQL Auth Proxy + gcloud/service account)
☐ Provider notes: DigitalOcean Managed Databases, AWS RDS/Aurora
☐ Optional published images (GHCR) and PyPI release
License
MIT
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Latest Blog Posts
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/gabriel-herencia/postgres-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server