claude-examples

MCP servers

How do I connect Claude Code to my database via MCP?

Wire up the official Postgres MCP server in read-only mode so Claude can query schemas and inspect rows without risking writes.

By Kalle · intermediate · Updated 2026-04-30

MCP servers expose tools to Claude over a structured protocol. The Postgres one is a good first MCP server: real value, low risk if you keep it read-only.

Heads up: Anthropic’s reference Postgres MCP server has been moved to the servers-archived repo. The npm package below still installs and runs, but it isn’t actively maintained. For long-term use, consider a community-maintained Postgres MCP server instead — the wiring below is identical, only the package name changes.

1. Add the server

Edit your project’s .mcp.json (committed) or your user-level ~/.claude.json (private):

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://readonly_user:pw@localhost:5432/myapp"
      ]
    }
  }
}

The connection string user should be readonly_user — a Postgres role with SELECT and nothing else. Don’t connect with a write-capable user “just in case.” MCP tools are a real attack surface; the database doesn’t know it’s talking to an LLM.

2. Restart Claude Code

The MCP server starts on the next session. /mcp lists connected servers and which tools they expose.

3. Use it

In a session:

What’s the schema of the orders table? Then count orders per status for the last 7 days.

Claude calls the Postgres tool, gets the schema, runs a SELECT, summarizes. You see every query in the tool log.

4. Sandbox harder if needed

For real production data, add layers:

  • A separate read-replica, never primary.
  • Row-level security on the readonly user.
  • A statement timeout so a bad SELECT can’t hang the connection.
  • The /mcp permission prompt — answer “ask every time” until you trust the workflow.

When MCP earns its keep

If you’re constantly copying-pasting query results into the chat, you want MCP. If you only query the database once a week, you don’t.

Sources

mcp postgres database