CursorIntermediate

How to Query Your Database from Cursor with a Postgres MCP Server

Wire a Postgres MCP server into Cursor so the agent can inspect your schema and run read-only queries.

10 minIntermediate

Giving Cursor access to your database schema changes how it writes queries. Instead of guessing at table and column names, the agent can read the real schema through a Postgres MCP server and write SQL that actually matches your tables. This guide connects a Postgres server with a read-only connection so you can explore safely.

  • Cursor with MCP support
  • A reachable Postgres database and its connection string
  • A read-only database user (strongly recommended)
  • Node.js with npx available

Create a read-only database user

Before connecting any tool to a real database, make a user that cannot modify data. This keeps an over-eager agent from running an UPDATE or DROP against production.

create-readonly.sql
CREATE USER cursor_ro WITH PASSWORD 'change_me';
GRANT CONNECT ON DATABASE appdb TO cursor_ro;
GRANT USAGE ON SCHEMA public TO cursor_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO cursor_ro;

Add the server to mcp.json

Add a Postgres entry to .cursor/mcp.json. Pass the connection string for the read-only user. Note the user, not the password, sits in this file, so keep the file out of version control.

.cursor/mcp.json
{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://cursor_ro:change_me@localhost:5432/appdb"
      ]
    }
  }
}
Never commit credentials
Add .cursor/mcp.json to your .gitignore, or reference the connection string from an environment variable instead of pasting it inline.

Confirm the server starts

Cursor Settings - MCP
MCP Servers
------------------------------
* postgres [connected]
tools: query
resources: schema (public)
The Postgres server exposes a query tool once it connects.

Ask the agent about your schema

Open the agent and ask a question that requires real schema knowledge. The agent reads the table structure, then writes a query that fits your columns rather than inventing names.

Cursor Agent
You
How many orders were placed last week? Use the orders table.
Agent
Reading schema... orders(id, user_id, total, created_at). Running: SELECT count(*) FROM orders WHERE created_at >= now() - interval '7 days'; Result: 1,284.
The agent reads the schema before composing a query.
Point it at a copy first
For anything beyond simple reads, connect the server to a staging database or a snapshot. You will move faster knowing nothing can touch production.

Result: Cursor now writes SQL grounded in your actual schema and can answer data questions inline, all through a connection that physically cannot modify your data.

Watch related tutorials

Tags
#cursor#mcp#postgres#database