← Back to MCP servers

Model Context Protocol (Official)

PostgreSQL MCP Server

MCP server that gives AI coding agents read-only access to PostgreSQL databases for schema inspection, query execution, and data analysis during development.

Last reviewed Feb 26, 2026

What it does

The PostgreSQL MCP Server connects AI coding agents to a PostgreSQL database. Agents can inspect table schemas, run read-only queries, and understand the data model while writing application code. This eliminates guesswork about column names, types, relationships, and existing data when generating queries, migrations, or ORM models.

Available tools

Tool What it does
query Execute a read-only SQL query and return results

The server also exposes resources that agents can request:

Resource What it provides
postgres://<host>/schemas List all schemas in the database
postgres://<host>/schema/<name> Full table definitions for a specific schema

Setup by tool

Cursor

Create .cursor/mcp.json in your project root:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://user:password@localhost:5432/mydb"
      ]
    }
  }
}

Claude Code

Add to your Claude configuration file:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://user:password@localhost:5432/mydb"
      ]
    }
  }
}

Replace the connection string with your actual database credentials. For development databases, localhost with default ports is typical.

When to use this

  • Query generation: Agents write SQL against real schemas instead of guessing column names
  • ORM model generation: Create Prisma schemas, TypeORM entities, or Drizzle tables that match your actual database
  • Migration authoring: Agents inspect current table structures before generating ALTER statements
  • Data exploration: Ask agents to summarize data distributions or find anomalies during debugging
  • API development: Generate CRUD endpoints that match your actual data model

Security considerations

  • Use read-only credentials: Create a dedicated PostgreSQL role with SELECT-only permissions
  • Never connect to production databases: Use development or staging databases for agent access
  • Connection strings contain passwords: Store them in environment variables, not committed files
  • Network access: Ensure the database is accessible from the machine running the MCP server
  • Row-level security: Consider enabling RLS if agents should only see specific data subsets