How to query a Database with an AI agent
Point your agent at a read-only database user through an MCP server. For Postgres, DBHub works in Claude Code, Codex and Cursor with one connection string. For Supabase, use its hosted MCP server with read_only=true. Then ask questions in plain English and check the SQL it runs.
Grok Bot steps tested by us on Oct 26, 2026 while building this page. Claude Code, Codex and Cursor steps checked against official docs on Oct 26, 2026.
Get the free starter kit. One short email a day when our setups change.
Free starter kit on signup: 2 Claude Code skills and 1 agent persona from our paid packs. By subscribing you agree to get The AI Loadout Daily, one email each morning. No spam, we never sell your email, and every email has a one-click unsubscribe link.
Claude Code
Create a read-only database userclaude mcp add --transport stdio db -- npx -y @bytebase/dbhub --dsn "postgresql://readonly:pass@host:5432/db"Supabase instead: claude mcp add --transport http supabase "https://mcp.supabase.com/mcp?project_ref=YOUR_REF&read_only=true"Ask: "How many signups did we get per day last week? Show the SQL."
Codex
codex mcp add db -- npx -y @bytebase/dbhub --dsn "postgresql://readonly:pass@host:5432/db"Or for Supabase: codex mcp add supabase --url "https://mcp.supabase.com/mcp?project_ref=YOUR_REF&read_only=true"codex mcp login supabase if it asks you to sign in
Cursor
Add to .cursor/mcp.json: {"mcpServers":{"supabase":{"url":"https://mcp.supabase.com/mcp?project_ref=YOUR_REF&read_only=true"}}}Or a DBHub command entry with your --dsn
Grok Bot
Connect Supabase to Grok BotAsk your question and it runs the SQL and shows it to youIt asks before anything that writes data
Gotchas
- Never give an agent a production admin login. Use read_only=true or a read-only role.
- Row data can contain prompt injection. Do not let the agent act on text found in rows.
- Ask for the SQL every time so you can check the logic.
The loadout for this job
- Tools: Claude, Cursor
- Repo: MCP Servers
- Model: Claude Sonnet 5.5
- Bot: Grok Bot
- Full setup: Best AI setup for data analysis
FAQ
Can Claude Code query Postgres?
Yes, through an MCP server like DBHub with a connection string.
Is the Supabase MCP server safe?
Use read_only=true and project_ref to scope it to one project with read-only access.
Related how-tos
Sources
Commands change. Check the linked docs before you run anything. All how-to guides