How to connect Cursor to PostgreSQL safely

6 min read · Updated November 14, 2026

Connecting Cursor to the database takes five minutes: a PostgreSQL MCP server and a connection string in a config file. The problem is what comes with that connection string.

How most teams do it today

Cursor talks to databases through MCP (Model Context Protocol) servers. The usual setup lives in the project or user .cursor/mcp.json file, and the PostgreSQL server receives the full connection string, with user and password.

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["<postgres-mcp-server>", "postgresql://app_user:PASSWORD@db.internal:5432/app"]
    }
  }
}

It works. From that moment the database password sits in plain text on the laptop, often for a user with write access, shared by the whole team.

The four risks of this setup

  • The password leaves the company network with the laptop, and can end up in a repository if the file is committed.
  • The agent inherits everything that password allows. If the user can delete, the agent can delete, even when the person asking should not.
  • Sensitive data opens with it: finance, payroll, and personal data are within reach of the same credential.
  • With a shared credential the database always logs the same user. When something disappears, nobody can tell who asked the agent.

What you can do with PostgreSQL alone

Before any tool, reduce the possible damage. Create a user only for the agent, with minimum privilege and time limits:

CREATE ROLE agent_read LOGIN PASSWORD 'change-this-password';
GRANT CONNECT ON DATABASE app TO agent_read;
GRANT USAGE ON SCHEMA public TO agent_read;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO agent_read;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO agent_read;
ALTER ROLE agent_read SET statement_timeout = '15s';
ALTER ROLE agent_read SET default_transaction_read_only = on;

What really protects you is the GRANT: without write privilege, the database refuses. default_transaction_read_only helps, but the session itself can turn it off, so do not rely on it as a barrier.

Keep sensitive tables in a schema without USAGE for this user, and point the agent at a replica or a staging database whenever you can.

Where a read-only user falls short

  • Developers need to create and update data. A read-only user blocks the work, and a user with write access brings back the original problem.
  • The password is still on each person's laptop.
  • There is no middle ground: the agent can delete or it cannot. There is no way to say "it can, if someone approves".
  • The database audit still shows one user for everyone.

How to do it with GatekeeperID Agents

Agents sits between Cursor and PostgreSQL, inside your network. The database password stays in the Connector, and each person gets a personal token. In Cursor, the config only has the internal address and that token:

{
  "mcpServers": {
    "gatekeeper-agents": {
      "url": "https://agents.yourcompany.internal/mcp",
      "headers": { "Authorization": "Bearer <your-personal-token>" }
    }
  }
}

From then on, analysts query, developers query, create, and update, and DELETE, DROP, TRUNCATE, or unknown commands become an approval request. Once approved, that exact call runs once. Every decision is audited with the name of who asked and who approved.

Take the database password off your team's laptops

In 20 minutes we show Cursor being blocked, the approval in the panel, and the audit with names.

Book a 20-minute demo

Questions about this guide

Can I put the connection string in an environment variable instead of mcp.json?

Yes, and it is better than leaving it in a versioned file. But the password is still on the laptop and the agent still has everything it allows. The environment variable hides the password from Git, not from the agent.

Is a read-only user already safe?

It greatly reduces the risk of deleting data, but reads can also leak sensitive data. Combine it with separate schemas, a query timeout, and ideally a replica.

Does GatekeeperID Agents work with Cursor today?

Yes. Cursor was the first supported client, speaking MCP to the Connector. PostgreSQL is the first supported database.

More guides