How to connect Claude to Postgres without giving it write access
Jump to a section
Postgres is where most companies keep their core data, so once your team adopts Claude, one of the first things you'll want is to ask it questions about that data. Most tutorials still point at the official MCP server, which is archived and whose read-only guard can be bypassed with one stacked query. There are two safer ways to do it:
- An open-source MCP server. Runs on your machine with a read-only user. Free, and best for one person.
- A connector platform like Contextflo. Connect once for the whole team, with shared context, per-person table access and an audit trail.
I tried both, so this post covers the pros and cons of each, starting with the open-source route.
Step 1: create a read-only user, on a replica
Do this before any MCP config.
CREATE ROLE claude_ro LOGIN PASSWORD '<generated>';
GRANT CONNECT ON DATABASE mydb TO claude_ro;
GRANT USAGE ON SCHEMA public TO claude_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO claude_ro;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO claude_ro;
The ALTER DEFAULT PRIVILEGES line is what stops new tables from being invisible next month. Default privileges only cover objects created by the role that set them, so if your migrations run as a different user, run it as that user.
Point it at a read replica, not your primary. An exploratory aggregation over a few million rows is a perfectly reasonable question, and it can also slow your app down. RDS, Supabase, Neon and most hosted providers give you a replica, so use it. People skip this because everything works fine until the one query that doesn't.
Step 2: skip the official server
The package most tutorials still show is @modelcontextprotocol/server-postgres. Don't use it.
Anthropic archived the repo in May 2025 and deprecated the npm package on July 10, 2025. The published version (0.6.2) has a confirmed SQL-injection vulnerability: a stacked query like COMMIT; DROP SCHEMA public CASCADE; ends its READ ONLY transaction and runs the rest with whatever the role can do. It still got over 105,000 npm downloads in the week of September 19-25, 2026, unpatched.
Sources: Datadog Security Labs, the archived repo and the npm downloads API (accessed September 26, 2026).
If you copied that snippet from somewhere else, fix it today. We compare the maintained alternatives in best Postgres MCP servers, and how its read-only default stacks up against the official BigQuery, Databricks, Redshift and ClickHouse servers in our read-only defaults table.
Step 3: run a maintained server
We wrote a replacement: @contextflo/postgres-mcp, open source under MIT. It's a drop-in swap for the archived server, with the same query tool and the connection string as the first argument.
npx @contextflo/postgres-mcp postgresql://claude_ro:pass@replica-host:5432/mydb
In Claude Code:
claude mcp add postgres -- npx -y @contextflo/postgres-mcp postgresql://claude_ro:pass@replica-host:5432/mydb
In Claude Desktop:
// claude_desktop_config.json
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@contextflo/postgres-mcp",
"postgresql://claude_ro:pass@replica-host:5432/mydb"
]
}
}
}
The open-source server runs on your machine. Contextflo, the hosted product, manages the connection, shared context and team access for you.
Read-only is enforced in four layers, and each one stops the stacked-query exploit on its own. The exploit is a test case in the repo.
- User SQL goes over Postgres's extended query protocol, so Postgres itself rejects multi-statement input.
- The connection starts with
default_transaction_read_only=on, and every statement runs insideBEGIN READ ONLYand ends inROLLBACK. - Statements are checked against an allowlist using the real Postgres parser, walking the whole tree, so a data-modifying CTE hidden inside a
SELECTgets caught. - The read-only role from Step 1, which Postgres enforces no matter what the code above gets wrong.
It's v0.1.0, and it has limits. The parser can't see inside your own functions, so a SECURITY DEFINER function called from a SELECT can do whatever its body does. That's why the read-only role isn't optional. Setup details and the full list are on the postgres-mcp page. If you'd rather use something we didn't write, @bytebase/dbhub also runs with npx and a read-only DSN.
Once connected, Claude can read the schema and run SQL. Ask "how many users signed up this week?" and it writes the query and answers. For one person exploring a schema they already know, that's all you need.
Before you trust it, run three checks:
- A read. Ask how many users signed up this week, and check it gets an answer.
- A blocked write. Ask it to delete a test row, and confirm it's refused.
- An answer you already know. Ask for a number you trust from somewhere else, and compare.
Want help connecting it and checking the first answer? Book 20 minutes and we'll do it with you.
Tip: give Claude some context. Create a Claude Project, add notes about your schema (table descriptions, metric definitions, business rules) and query from inside it. Our server also reads a .contextflo/context.md file you edit, seeded by npx @contextflo/postgres-mcp init. Accuracy goes up noticeably either way.
To see why, ask it for last week's revenue. Does it know whether your net amount column includes tax, or which status values mean completed?
The catch is that those notes live in your Claude account or on your machine, and your teammates don't see them.
When the answers trace back to you
Postgres usually belongs to whoever is closest to it: the one person who wrote half the migrations, owns the replica and gets every "can you pull this?" in Slack. Hook Claude up and those requests stop arriving. The answers still carry your name, though, and when they drift, nothing fails loudly.
The answers stop agreeing
Production Postgres schemas are messy. They grow over years, with abbreviated column names, legacy tables and business logic nobody wrote down. Claude sees this:
usr_acct (id, sts, crt_at, upd_at, tier_id, ref_src, acq_ch)
ord (id, usr_id, amt_gross, amt_net, disc_cd, sts, crt_at)
sub (id, usr_id, plan_id, mrr_cents, churn_at, cancel_rsn)
Without context it guesses. Does amt_net include tax? Which values of sts mean completed?
Your head of growth asks what revenue was last week, and Claude computes it from ord.amt_net. Your finance lead asks the same question and gets a different number, because their notes say something else. Both numbers look confident, and nobody knows they disagree until they're in the same meeting.
The context becomes somebody's second job
The notes workaround means context is maintained by hand. A column gets renamed, a table gets added, a metric definition changes, and someone has to remember to update the notes in every teammate's separate project.

The config sprawls
- Everyone edits a JSON file containing database credentials. Your head of ops isn't doing that.
- Everyone gets identical access. Restricting who sees what means a separate Postgres user per person, with the grants and rotation that come with it.
- Credentials sit on laptops in plaintext. Revoking one person means changing the password for everyone.
- Changing provider breaks everybody. Move from Supabase to Neon, or RDS to Railway, and every person updates their config.
- A support contractor needs the tickets table for three months and nothing near payments. That's another role, another set of grants, and a password you have to remember to revoke.
Nobody can tell you who asked
Postgres knows what ran. pg_stat_statements keeps the normalized query text, call counts and timings, and the server log can record which role connected. But every teammate connects as the same claude_ro, so neither one says which person asked, what Claude told them, or which reading of amt_net it used. Our server can keep a local query log too, but only on the machine it runs on.
So when your CEO asks why the number in Claude doesn't match the one in the board deck, you're the one reconstructing it, from a query you never saw and a chat you can't open.
Keep the control, hand off the upkeep
Contextflo connects to Postgres once, with read-only credentials, and runs the MCP server for you. It reads the schema plus any source code and docs you point it at, and generates what each table and column means, how they relate, and how key metrics are calculated. Your team asks in Claude, ChatGPT or any assistant that supports MCP. What changes is what the assistant knows, who's allowed to ask what, and that you can see both.


It works with any hosted Postgres: AWS RDS, Supabase, Neon, PlanetScale, Google Cloud SQL, Azure Postgres, Railway, Render, or self-hosted.
The generated context is a draft. It gets structure right and business meaning wrong often enough that someone who knows the data should read it once. Reviewing it takes an afternoon, where writing it from scratch takes weeks, and you don't have to do it all at once: start with one table, one definition and one answer you've verified, and the rest can wait.
Here is Contextflo in action.
How we think about it
Most people running Postgres for a company are also shipping the product on top of it. We built Contextflo so the analytics side doesn't become a second product to maintain:
- Read-only twice. Contextflo only runs SELECT queries, and we still want the read-only role from Step 1 underneath it. Belt and braces is the right default for a production database.
- A name on every query. Each question is logged with the person who asked it and the SQL that ran, which is exactly what
pg_stat_statementscan't give you. - Access per person, not per Postgres role. Give the contractor the tickets table and nothing else, without creating a role you'll forget to drop.
- Review once, everyone benefits. You correct the generated context in one place, and every teammate's assistant picks it up. No more notes in twelve separate Claude Projects.
- You don't have to watch it. A weekly digest sums up what the team has been asking, so you see what people need without reading logs.
What stays yours is the schema itself: the migrations, the tables and what amt_net actually means. We make the model you already have safe for everyone to use.
If someone has to sign off before your team gets access, this is the section to send them.
Which one to use
| Direct MCP | Contextflo | |
|---|---|---|
| Setup | JSON config, per person | Connect once, invite the team |
| Schema context | Raw names, plus your own notes | Generated, then reviewed |
| Metric consistency | Whatever each person's notes say | Defined once, shared |
| Access control | Same DB user for everyone | Per-table, per-user |
| Credentials | On every laptop | Centralised, never exposed |
| Changing provider | Update every config | Update one connection |
| Audit trail | Local log at best | Every question and query |
Just you, on a schema you know: run a maintained server. A replica, a read-only role and @contextflo/postgres-mcp or dbhub, and you're done.
Other people start asking: use Contextflo. The first time a teammate makes a call on a number you never saw, that number is yours to defend. That's the point to put access, a record of who asked what, and shared context in the middle, before it turns into your second job.
You do the modeling, we do the rest. Connect Postgres free, or grab 20 minutes and I'll set it up with you.
FAQ
How do I connect Claude to Postgres? Create a read-only Postgres user, run a maintained Postgres MCP server pointed at your database, and add it to Claude as a connector. Claude can then list tables, inspect the schema, and run SQL from chat. For a team, a managed context layer like Contextflo runs the connection and adds shared definitions and per-user access control.
Is there a Postgres MCP server or connector for Claude? Yes. Several maintained Postgres MCP servers let you connect Claude to Postgres, such as @bytebase/dbhub run with npx and a read-only DSN. Avoid the archived @modelcontextprotocol/server-postgres, which has a confirmed SQL-injection vulnerability.
How do I connect Claude to Postgres without writing code? A managed layer like Contextflo connects to Postgres with read-only credentials and runs the MCP server for you, so your whole team can query PostgreSQL in Claude with no config files or JSON editing.
Is the official Postgres MCP server safe to use? No. Anthropic archived @modelcontextprotocol/server-postgres in May 2025 and deprecated it on npm in July 2025, and its published version has a confirmed SQL-injection vulnerability that can bypass the read-only guard. Use a maintained open-source server or a managed layer, and never point the archived package at production.
Can I see who ran a query through Claude on Postgres? Not from Postgres alone. pg_stat_statements and the server log show the query and the connecting role, but if everyone connects as the same read-only user, nothing ties a query to the person who asked. Contextflo logs each question with the person who asked it and the SQL that ran, across Claude, ChatGPT or any assistant that supports MCP.
What is the best Postgres MCP server for Claude? It depends on whether you need a quick solo connection or a team-ready setup with access control and shared context. See our comparison of Postgres MCP servers for the current maintained options and which one to avoid.



