When you're writing code against a database, it helps if the AI can actually see it — the real schema, the real column names, the real row counts. Cursor speaks MCP, so you can wire it to your PostgreSQL database and let its agent answer data questions and check its own assumptions instead of guessing at your table structure.
The catch is doing it safely. Point Cursor at a raw Postgres MCP server and you've put your database credentials in a project file and given an autonomous agent the ability to run any SQL it writes. The AI2SQL Gateway keeps the convenience and removes both risks.
Set it up in 3 steps
- Add your database in the AI2SQL Gateway — credentials are encrypted and stored server-side.
- Generate a gateway key; the wizard gives you the key and the connection id.
- Drop the MCP entry into
.cursor/mcp.json(project) or~/.cursor/mcp.json(global):
{
"mcpServers": {
"ai2sql": {
"url": "https://builder.ai2sql.io/api/mcp",
"headers": {
"x-ai2sql-key": "$AI2SQL_KEY",
"x-ai2sql-connection": "$CONNECTION_ID"
}
}
}
} ✓ ai2sql connected — 3 tools available
run_query · describe_schema · list_connections
Get $AI2SQL_KEY
and $CONNECTION_ID
from the AI2SQL Gateway — connect a database and the
setup wizard generates both for you.
Reload Cursor and the ai2sql server shows up with run_query,
describe_schema and list_connections. Now when you ask the agent
"does the orders table have a shipped_at column?" it checks instead of
hallucinating.
What stays safe
- Read-only. Writes,
DROP,ALTERand dangerous functions are blocked at the gateway — a coding agent can't accidentally migrate your schema. - No credentials in the repo.
.cursor/mcp.jsonholds a revocable key, not your Postgres password. Safe to gitignore either way. - Bounded. Results cap at 1,000 rows and queries time out, so an agent loop can't drain the database.
- Logged. Every query the agent runs is recorded, so you can see exactly what it touched.
A note on production databases
For a local dev database, a raw Postgres MCP server is fine. For a shared staging or production database, the guardrails matter — that's the whole reason the gateway exists. If you're weighing the options, see AI2SQL Gateway vs a raw Postgres MCP server.