Connect Cursor to PostgreSQL over MCP

Let Cursor's agent query your real PostgreSQL data while you build — read-only, logged and revocable. Here's the setup, and how it stays safe.

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

  1. Add your database in the AI2SQL Gateway — credentials are encrypted and stored server-side.
  2. Generate a gateway key; the wizard gives you the key and the connection id.
  3. Drop the MCP entry into .cursor/mcp.json (project) or ~/.cursor/mcp.json (global):
Cursor — .cursor/mcp.json
{
  "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

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.

Frequently asked

Where does the MCP config go in Cursor? +

Cursor reads MCP servers from .cursor/mcp.json in your project (or ~/.cursor/mcp.json for all projects). Add the ai2sql entry with your gateway key and connection id, then reload — Cursor lists the three tools automatically.

Will Cursor's agent be able to change my database? +

No. The gateway blocks every write, DDL and dangerous statement before it runs and executes queries in a read-only transaction. Cursor can read and query, not mutate.

Does my Postgres password end up in .cursor/mcp.json? +

No — only a revocable gateway key and a connection id. Your actual credentials are encrypted server-side in the AI2SQL dashboard and never reach Cursor.

Can I scope one key to one project's database? +

Yes. Create a separate key per project or per agent and revoke any of them independently. Every call is logged against the key that made it.

Give your agent a database it can't break

Connect a database, grab a key, paste one config. Read-only, logged and revocable from the first query.

Start free →

Related guides