
Let Claude query Postgres
People ask the engineering team for numbers that live in the database.
How it connects
This setup is for engineering and data teams who keep answering the same database questions for colleagues. Claude connects to Postgres through an MCP server with a read-only role, writes and runs SQL, and posts recurring numbers to Slack. You end up with a safe way for people to ask questions in plain language without access to production.
Set it up
1Create a read-only role
Create a dedicated Postgres role with LOGIN and grant it USAGE on the needed schemas and SELECT on the needed tables only. Set default_transaction_read_only and a statement_timeout on the role so long or writing queries fail fast. Never reuse your application's database user.
Tip: Run SET ROLE to the new role and try an INSERT to confirm it fails.
2Point it at a replica
Connect the agent to a read replica, or to a reporting database copied from production. If you only have a primary, use the read-only role plus a low connection limit. Restrict network access so only the machine running the MCP server can reach it.
Tip: A statement_timeout of a few seconds stops a runaway join from hogging resources.
3Register it in Claude
Add a Postgres MCP server, such as Postgres MCP Pro in restricted mode or another read-only server, with the read-only connection string. Register it in Claude Desktop's config, Cursor's MCP settings or with claude mcp add in Claude Code. Keep the connection string in an environment variable rather than a committed file.
Tip: Check the server's docs for a read-only or restricted flag and turn it on.
4Describe the schema
Write a short notes file that explains key tables, what a row represents, how tables join and which columns hold money or dates. Add it to a Claude project, CLAUDE.md or Cursor rules. Ask Claude to show the SQL it ran with every answer so someone can check it.
Tip: Include definitions of business terms like active customer so numbers match your dashboards.
5Share the answers
Connect the Slack MCP server and restrict it to one channel for metrics. Ask Claude to post the question, the number and the SQL used. For scheduled numbers, run Claude Code from cron or CI with the same configuration.
Tip: Post the SQL alongside the answer so anyone can reproduce it.
Then just ask
Good to know
What you need
- A Postgres database where you can create roles and grants
- Ideally a read replica or reporting copy
- Claude Desktop, Claude Code or Cursor
- A Slack channel for shared metrics
Limits
- Claude can write SQL that runs but answers the wrong question, so important numbers need a human check.
- Large result sets get truncated or summarised; ask for aggregates instead of raw rows.
- The agent knows only the schema and your notes; undocumented business logic in application code stays invisible to it.
Keep it safe
- Read-only database role only.
- Exclude tables containing personal data unless strictly needed.
- Prefer a replica over the primary.





