pgsql-mcp-server

MCP.Pizza Chef: leixiaotian1

pgsql-mcp-server is a Golang-based MCP server enabling AI models to interact with PostgreSQL databases. It supports executing SELECT, INSERT, UPDATE, DELETE queries, creating tables, listing tables, and explaining SQL queries. This server facilitates real-time database operations through the MCP protocol, making it ideal for AI assistants to manage and analyze PostgreSQL data efficiently.

Use This MCP server To

Execute SELECT queries on PostgreSQL databases via MCP Create new tables in PostgreSQL through AI-driven commands Insert, update, or delete data in PostgreSQL using MCP List all user tables in a PostgreSQL database with schema filters Analyze and explain SQL queries for optimization and debugging

README

PostgreSQL MCP Server

Stars Forks

English | 中文

A Model Context Protocol (MCP) server that provides tools for interacting with a PostgreSQL database. This server enables AI assistants to execute SQL queries, create tables, and list database tables through the MCP protocol.

Features

The server provides the following tools:

  • read_query: Execute SELECT queries on the PostgreSQL database
  • write_query: Execute INSERT, UPDATE, or DELETE queries on the PostgreSQL database
  • create_table: Create a new table in the PostgreSQL database
  • list_tables: List all user tables in the database (with optional schema filtering)
  • explain_query:EXPLAIN a query on the PostgreSQL database

Installation

Prerequisites

  • Go 1.23 or later
  • PostgreSQL database server

Steps

  1. Clone the repository:

    git clone https://github.com/sql-mcp-server.git
    cd sql-mcp-server
  2. Install dependencies:

    go mod download
  3. Build the server:

    go build -o sql-mcp-server

Configuration

The server requires database connection details through environment variables. Create a .env file in the project root with the following variables:

DB_HOST=localhost      # PostgreSQL server host
DB_PORT=5432           # PostgreSQL server port
DB_NAME=postgres       # Database name
DB_USER=your_username  # Database user
DB_PASSWORD=your_pass  # Database password
DB_SSLMODE=disable     # SSL mode (disable, require, verify-ca, verify-full)

Usage

Running the Server

./sql-mcp-server

MCP Configuration

To use this server with an AI assistant that supports MCP, add the following to your MCP configuration:

{
  "mcpServers": {
    "pgsql-mcp-server": {
      "command": "/path/to/sql-mcp-server",
      "args": [],
      "env": {
        "DB_HOST": "localhost",
        "DB_PORT": "5432",
        "DB_NAME": "postgres",
        "DB_USER": "your_username",
        "DB_PASSWORD": "your_password",
        "DB_SSLMODE": "disable"
      },
      "disabled": false,
      "autoApprove": []
    }
  }
}

Tool Examples

List Tables

List all user tables in the database:

{
  "server_name": "pgsql-mcp-server",
  "tool_name": "list_tables",
  "arguments": {}
}

List tables in a specific schema:

{
  "server_name": "pgsql-mcp-server",
  "tool_name": "list_tables",
  "arguments": {
    "schema": "public"
  }
}

Create Table

Create a new table:

{
  "server_name": "pgsql-mcp-server",
  "tool_name": "create_table",
  "arguments": {
    "schema": "CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(100), email VARCHAR(100) UNIQUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"
  }
}

Read Query

Execute a SELECT query:

{
  "server_name": "pgsql-mcp-server",
  "tool_name": "read_query",
  "arguments": {
    "query": "SELECT * FROM users LIMIT 10"
  }
}

Write Query

Execute an INSERT query:

{
  "server_name": "pgsql-mcp-server",
  "tool_name": "write_query",
  "arguments": {
    "query": "INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')"
  }
}

Execute an UPDATE query:

{
  "server_name": "pgsql-mcp-server",
  "tool_name": "write_query",
  "arguments": {
    "query": "UPDATE users SET name = 'Jane Doe' WHERE id = 1"
  }
}

Execute a DELETE query:

{
  "server_name": "pgsql-mcp-server",
  "tool_name": "write_query",
  "arguments": {
    "query": "DELETE FROM users WHERE id = 1"
  }
}

Security Considerations

  • The server validates query types to ensure that only appropriate operations are performed with each tool.
  • Input sanitization is performed for schema names to prevent SQL injection.
  • Consider using a dedicated database user with limited permissions for this server.
  • In production environments, enable SSL by setting DB_SSLMODE to require or higher.

Dependencies

License

[Add license information here]

Contributing

[Add contribution guidelines here]

pgsql-mcp-server FAQ

How do I install pgsql-mcp-server?
Clone the GitHub repo, ensure Go 1.23+ and PostgreSQL are installed, then build and run the server.
Can pgsql-mcp-server handle complex SQL queries?
Yes, it supports executing complex SELECT, INSERT, UPDATE, DELETE, and EXPLAIN queries on PostgreSQL.
Is schema filtering supported when listing tables?
Yes, you can optionally filter tables by schema when using the list_tables tool.
What programming language is pgsql-mcp-server written in?
It is implemented in Golang for performance and concurrency.
Can this server be integrated with multiple LLM providers?
Yes, it works with any MCP-compatible client, enabling integration with OpenAI, Claude, Gemini, and others.
Does pgsql-mcp-server support real-time query analysis?
Yes, it includes an explain_query tool to analyze and optimize SQL statements.
What are the prerequisites for running pgsql-mcp-server?
You need Go 1.23 or later and a running PostgreSQL database server.
How secure is the interaction between the MCP client and pgsql-mcp-server?
MCP protocol ensures scoped, secure, and observable interactions between clients and servers.