PG_MCP_SERVER

PG_MCP_SERVER

cbc3929

Developer Tools
Visit Server

README

PG_MCP_SERVER

<div align="center">Custom Implementation for PostgreSQL + PostGIS Support</div>

Introduction

中文文档

A general-purpose PostgreSQL MCP Server. The MCP part is implemented using go-mcp, supporting Stdio and SSE transports.

The descriptions for PostGIS and PgVector come from another open-source project: https://github.com/stuzero/pg-mcp-server 🙏🙏🙏 This approach to providing context is refreshing.

⚠️ Database requires defining roles to prevent SQL injection. Grant SELECT permission to the public schema to prevent sensitive data exposure. ⚠️ Grant all permissions to the new role for the temp schema to ensure data isolation.

Features

When deploying LLMs locally, context needs to be managed efficiently. Reading the database schema on every call consumes time and significant token context. This project adopts a pre-processing approach. It natively supports fetching the table structure from the database and provides descriptive information to the LLM:

  • Utilizes Tool descriptions and input schemas to implicitly or explicitly convey schema information.
  • Leverages MCP Resources during initialization to read table names, columns, constraints, foreign keys, indexes, geometry types, and EPSG codes, creating a fundamental understanding for the large model.

Installation

In main.go:

// Here you can set the connection string for database interaction
schemaLoadConnID, err := dbService.RegisterConnection(tempCtx, "postgres://mcp_user:mcp123456@192.168.2.19:5432/postgres")

The format is postgres://user:pass@host:port/db
Alternatively, you can configure it via .env by uncommenting and setting the SCHEMA_LOAD_DB_URL variable. Here, RegisterConnection gives the server an initial connection string to cache database table information.
🏁 It is recommended to create a dedicated server role. The SQL is as follows:

-- Create a new role and set a password
CREATE ROLE mcp_user WITH LOGIN PASSWORD 'mcp123456';

-- Set basic permissions for mcp_user
GRANT CONNECT ON DATABASE postgres TO mcp_user;
GRANT USAGE ON SCHEMA public TO mcp_user;

-- Grant SELECT permission on all existing tables in the public schema
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_user;

-- Automatically grant SELECT on future tables in public schema to mcp_user
ALTER DEFAULT PRIVILEGES IN SCHEMA public
   GRANT SELECT ON TABLES TO mcp_user;

-- Create a temp schema
CREATE SCHEMA temp;

-- Grant all privileges on the temp schema to mcp_user
GRANT USAGE, CREATE ON SCHEMA temp TO mcp_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA temp TO mcp_user;

-- Grant all privileges on future tables in temp schema to mcp_user
ALTER DEFAULT PRIVILEGES IN SCHEMA temp
   GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO mcp_user;
Use code with caution.

Running

  • 🐋 Run with Docker
git clone https://github.com/cbc3929/pg_mcp_server.git
cd pg_mcp_server
docker build -t pg-mcp-server:latest .
docker run -d -p 8181:8181 --name my-mcp-server pg-mcp-server:latest
  • 🀄 Run Directly
  1. Clone the project
git clone https://github.com/cbc3929/pg_mcp_server.git
cd pg_mcp_server
  1. Install dependencies
go mod tidy

or

go mod download
  1. Run directly
    Ensure necessary environment variables are set or .env file is present
go run ./cmd/server/main.go
  1. Build (Optional)
go build -o pg_mcp_server ./cmd/server/main.go
./pg_mcp_server

Extension Support

PostGIS ✅
PgVector ✅
PgRouting ⭕

TODO / Unfinished

  • Management of tables in the temp schema: A mechanism for table cleanup/recycling should be implemented.
  • Unit testing.

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
MCP Package Docs Server

MCP Package Docs Server

Facilitates LLMs to efficiently access and fetch structured documentation for packages in Go, Python, and NPM, enhancing software development with multi-language support and performance optimization.

Featured
Local
TypeScript
Claude Code MCP

Claude Code MCP

An implementation of Claude Code as a Model Context Protocol server that enables using Claude's software engineering capabilities (code generation, editing, reviewing, and file operations) through the standardized MCP interface.

Featured
Local
JavaScript
@kazuph/mcp-taskmanager

@kazuph/mcp-taskmanager

Model Context Protocol server for Task Management. This allows Claude Desktop (or any MCP client) to manage and execute tasks in a queue-based system.

Featured
Local
JavaScript
Linear MCP Server

Linear MCP Server

Enables interaction with Linear's API for managing issues, teams, and projects programmatically through the Model Context Protocol.

Featured
JavaScript
mermaid-mcp-server

mermaid-mcp-server

A Model Context Protocol (MCP) server that converts Mermaid diagrams to PNG images.

Featured
JavaScript
Jira-Context-MCP

Jira-Context-MCP

MCP server to provide Jira Tickets information to AI coding agents like Cursor

Featured
TypeScript
Linear MCP Server

Linear MCP Server

A Model Context Protocol server that integrates with Linear's issue tracking system, allowing LLMs to create, update, search, and comment on Linear issues through natural language interactions.

Featured
JavaScript
Sequential Thinking MCP Server

Sequential Thinking MCP Server

This server facilitates structured problem-solving by breaking down complex issues into sequential steps, supporting revisions, and enabling multiple solution paths through full MCP integration.

Featured
Python