PostgreSQL MCP server

Access and analyze Postgres databases with read-only queries.
Back to servers
Provider
Anthropic
Release date
Nov 19, 2024
Language
TypeScript
Package
Stats
137.9K downloads
40.6K stars

The PostgreSQL MCP server provides read-only access to PostgreSQL databases, allowing LLMs to inspect database schemas and execute read-only SQL queries. This server acts as a bridge between language models and your PostgreSQL databases, making it easier to work with database content directly in AI interfaces.

Installation Options

Using Docker

Docker provides an isolated environment to run the MCP server without installing dependencies directly on your system.

Note: When using Docker on macOS and connecting to a PostgreSQL server on your host machine, use host.docker.internal instead of localhost in the connection URL.

Configure with Claude Desktop

Add the following to the "mcpServers" section of your claude_desktop_config.json:

{
  "mcpServers": {
    "postgres": {
      "command": "docker",
      "args": [
        "run", 
        "-i", 
        "--rm", 
        "mcp/postgres", 
        "postgresql://host.docker.internal:5432/mydb"]
    }
  }
}

Using NPX

NPX allows you to run the server directly from npm without a global installation.

Configure with Claude Desktop

Add the following to the "mcpServers" section of your claude_desktop_config.json:

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

Connection URL Format

The connection URL follows this format:

postgresql://user:password@host:port/database-name

VS Code Integration

Manual Configuration

To configure the PostgreSQL MCP server in VS Code:

  1. Press Ctrl + Shift + P and type Preferences: Open User Settings (JSON)
  2. Add one of the following configuration blocks:

Using Docker with VS Code

{
  "mcp": {
    "inputs": [
      {
        "type": "promptString",
        "id": "pg_url",
        "description": "PostgreSQL URL (e.g. postgresql://user:[email protected]:5432/mydb)"
      }
    ],
    "servers": {
      "postgres": {
        "command": "docker",
        "args": [
          "run",
          "-i",
          "--rm",
          "mcp/postgres",
          "${input:pg_url}"
        ]
      }
    }
  }
}

Using NPX with VS Code

{
  "mcp": {
    "inputs": [
      {
        "type": "promptString",
        "id": "pg_url",
        "description": "PostgreSQL URL (e.g. postgresql://user:pass@localhost:5432/mydb)"
      }
    ],
    "servers": {
      "postgres": {
        "command": "npx",
        "args": [
          "-y",
          "@modelcontextprotocol/server-postgres",
          "${input:pg_url}"
        ]
      }
    }
  }
}

Alternatively, you can add this configuration to .vscode/mcp.json in your workspace to share it with others (without the "mcp" root key).

Using the MCP Server

Once configured, your AI assistant can:

Query the Database

The server provides a query tool that allows executing read-only SQL queries against the connected database:

  • All queries are executed within a READ ONLY transaction
  • SQL injection protection is built-in

Access Table Schemas

The server automatically discovers and provides schema information for each table in the database:

  • Available at postgres://<host>/<table>/schema
  • Includes column names and data types
  • Helps AI assistants understand your database structure

When interacting with an AI that has access to this MCP server, you can ask it to examine your database schema or run queries to retrieve information.

How to add this MCP server to Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > MCP and click "Add new global MCP server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-mcp"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later