PostgreSQL Explorer MCP server

Enables natural language interaction with PostgreSQL databases through tools for schema exploration, table inspection, relationship discovery, and SQL query execution
Back to servers
Setup instructions
Provider
gldc
Release date
Mar 29, 2025
Language
Python
Stats
11 stars

PostgreSQL MCP Server is an implementation of the Model Context Protocol (MCP) that enables seamless integration between LLM applications and PostgreSQL databases. This server provides a standardized interface for AI agents to interact with databases, allowing them to query data, explore schema information, and understand database structure.

Features

  • List database schemas
  • List tables within schemas
  • Describe table structures
  • List table constraints and relationships
  • Get foreign key information
  • Execute SQL queries

Installation

Installing via Smithery

To install PostgreSQL MCP Server automatically via Smithery:

npx -y @smithery/cli install @gldc/mcp-postgres --client claude

Manual Installation

  1. Clone the repository:
git clone <repository-url>
cd mcp-postgres
  1. Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows, use: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt

Usage

Starting the Server

You can start the MCP server in several ways:

# Without a connection string (server starts, DB-backed tools will return a friendly error)
python postgres_server.py

# Set the connection string via environment variable:
export POSTGRES_CONNECTION_STRING="postgresql://username:password@host:port/database"
python postgres_server.py

# Or pass it using the --conn flag:
python postgres_server.py --conn "postgresql://username:password@host:port/database"

Available Tools

The server provides the following tools:

  • query: Execute SQL queries against the database
  • list_schemas: List all available schemas
  • list_tables: List all tables in a specific schema
  • describe_table: Get detailed information about a table's structure
  • get_foreign_keys: Get foreign key relationships for a table
  • find_relationships: Discover both explicit and implied relationships for a table

Docker Support

Running with Docker

Build the Docker image:

docker build -t mcp-postgres .

Run without a database connection:

docker run -p 8000:8000 mcp-postgres

Run with a live PostgreSQL database:

docker run \
  -e POSTGRES_CONNECTION_STRING="postgresql://username:password@host:5432/database" \
  -p 8000:8000 \
  mcp-postgres

Integration with MCP-Compatible Tools

To integrate this server with MCP-compatible tools (like Cursor), add it to your ~/.cursor/mcp.json:

{
  "servers": {
    "postgres": {
      "command": "/path/to/venv/bin/python",
      "args": [
        "/path/to/postgres_server.py"
      ],
      "env": {
        "POSTGRES_CONNECTION_STRING": "postgresql://username:password@host:5432/database?ssl=true"
      }
    }
  }
}

Replace:

  • /path/to/venv with your virtual environment path
  • /path/to/postgres_server.py with the absolute path to the server script

Security Considerations

  • Never expose sensitive database credentials in your code
  • Use environment variables or secure configuration files for database connection strings
  • Consider using connection pooling for better resource management
  • Implement proper access controls and user authentication

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" '{"command":"/path/to/venv/bin/python","args":["/path/to/postgres_server.py"],"env":{"POSTGRES_CONNECTION_STRING":"postgresql://username:password@host:5432/database?ssl=true"}}'

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": {
            "command": "/path/to/venv/bin/python",
            "args": [
                "/path/to/postgres_server.py"
            ],
            "env": {
                "POSTGRES_CONNECTION_STRING": "postgresql://username:password@host:5432/database?ssl=true"
            }
        }
    }
}

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": {
            "command": "/path/to/venv/bin/python",
            "args": [
                "/path/to/postgres_server.py"
            ],
            "env": {
                "POSTGRES_CONNECTION_STRING": "postgresql://username:password@host:5432/database?ssl=true"
            }
        }
    }
}

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