PostgreSQL MCP server

Provides a bridge to PostgreSQL databases for executing SQL queries, managing tables, and inspecting schemas with support for prepared statements and multiple parameter styles
Back to servers
Setup instructions
Provider
Anton O
Release date
Mar 25, 2025
Language
TypeScript
Package
Stats
4.5K downloads
5 stars

The MCP PostgreSQL Server enables AI models to interact with PostgreSQL databases through a standardized interface, providing database operations via the Model Context Protocol. This server acts as a bridge between AI systems and PostgreSQL databases for seamless data access and manipulation.

Installation

You can install the MCP PostgreSQL Server using npm:

npm install mcp-postgres-server

Alternatively, run it directly without installation using npx:

npx mcp-postgres-server

Configuration

The server requires PostgreSQL connection details through environment variables. Here's how to configure it:

{
  "mcpServers": {
    "postgres": {
      "type": "stdio",
      "command": "npx",
      "args": ["-y", "mcp-postgres-server"],
      "env": {
        "PG_HOST": "your_host",
        "PG_PORT": "5432",
        "PG_USER": "your_user",
        "PG_PASSWORD": "your_password",
        "PG_DATABASE": "your_database"
      }
    }
  }
}

Available Tools

connect_db

Establishes a connection to your PostgreSQL database:

use_mcp_tool({
  server_name: "postgres",
  tool_name: "connect_db",
  arguments: {
    host: "localhost",
    port: 5432,
    user: "your_user",
    password: "your_password",
    database: "your_database"
  }
});

query

Executes SELECT queries with optional prepared statement parameters:

use_mcp_tool({
  server_name: "postgres",
  tool_name: "query",
  arguments: {
    sql: "SELECT * FROM users WHERE id = $1",
    params: [1]
  }
});

execute

Performs INSERT, UPDATE, or DELETE operations:

use_mcp_tool({
  server_name: "postgres",
  tool_name: "execute",
  arguments: {
    sql: "INSERT INTO users (name, email) VALUES ($1, $2)",
    params: ["John Doe", "[email protected]"]
  }
});

list_schemas

Retrieves all schemas in the connected database:

use_mcp_tool({
  server_name: "postgres",
  tool_name: "list_schemas",
  arguments: {}
});

list_tables

Lists tables in the database:

// List tables in the 'public' schema (default)
use_mcp_tool({
  server_name: "postgres",
  tool_name: "list_tables",
  arguments: {}
});

// List tables in a specific schema
use_mcp_tool({
  server_name: "postgres",
  tool_name: "list_tables",
  arguments: {
    schema: "my_schema"
  }
});

describe_table

Gets the structure of a specific table:

// Describe a table in the 'public' schema (default)
use_mcp_tool({
  server_name: "postgres",
  tool_name: "describe_table",
  arguments: {
    table: "users"
  }
});

// Describe a table in a specific schema
use_mcp_tool({
  server_name: "postgres",
  tool_name: "describe_table",
  arguments: {
    table: "users",
    schema: "my_schema"
  }
});

Error Handling

The server provides detailed error messages for common issues:

  • Connection failures
  • Invalid queries
  • Missing parameters
  • Database errors

All tools use prepared statements to prevent SQL injection and support both PostgreSQL-style ($1, $2) and MySQL-style (?) parameter placeholders for flexibility in your queries.

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "postgres" '{"type":"stdio","command":"npx","args":["-y","mcp-postgres-server"],"env":{"PG_HOST":"your_host","PG_PORT":"5432","PG_USER":"your_user","PG_PASSWORD":"your_password","PG_DATABASE":"your_database"}}'

See the official Claude Code MCP documentation for more details.

For 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 > Tools & Integrations and click "New MCP Server".

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

{
    "mcpServers": {
        "postgres": {
            "type": "stdio",
            "command": "npx",
            "args": [
                "-y",
                "mcp-postgres-server"
            ],
            "env": {
                "PG_HOST": "your_host",
                "PG_PORT": "5432",
                "PG_USER": "your_user",
                "PG_PASSWORD": "your_password",
                "PG_DATABASE": "your_database"
            }
        }
    }
}

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 explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "postgres": {
            "type": "stdio",
            "command": "npx",
            "args": [
                "-y",
                "mcp-postgres-server"
            ],
            "env": {
                "PG_HOST": "your_host",
                "PG_PORT": "5432",
                "PG_USER": "your_user",
                "PG_PASSWORD": "your_password",
                "PG_DATABASE": "your_database"
            }
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

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