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.
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 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.
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://cursor_ro:change_me@localhost:5432/appdb"
]
}
}
}.cursor/mcp.json to your .gitignore, or reference the connection string from an environment variable instead of pasting it inline.Confirm the server starts
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.
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
33:42
41:18
28:05
3:12
26:54
39:10