PostgreSQL MCP 服务器 logo

PostgreSQL MCP 服务器

Community
leixiaotian1

一款用golang实现的操作pg数据库的mcp server,支持创建表、查询表、插入数据,查看数据,分析sql语句等功能,欢迎共建!!!

Publisherleixiaotian1
Repositorypgsql-mcp-server
LanguageGo
Forks
2
Stars
12
Available tools
0
Transport typestdio
Categories
LicenseMIT
Links
  • Connect tools to AI workflows

    PostgreSQL MCP 服务器 exposes MCP capabilities that can be used by compatible AI clients and agents.

  • 0 available tools

    Browse the callable actions below, including names and descriptions when provided by the server.

  • Ready-to-copy setup

    Use the installation snippets to configure this server in your preferred MCP client.

  • Open source signals

    12 stars and 2 forks from the linked repository.

PostgreSQL MCP Server

GoDoc Stars Forks

中文 | English

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

✨ Features

  • Interact with Databases via AI: Enables LLMs to perform database operations through a structured protocol.
  • Secure Toolset: Separates read and write operations into distinct, authorizable tools (read_query, write_query).
  • Schema Management: Allows for table creation (create_table) and listing (list_tables).
  • Query Analysis: Provides a tool to analyze query execution plans (explain_query).
  • Multiple Transport Modes: Supports stdio, Server-Sent Events (sse), and streamableHttp for flexible client integration.
  • Environment-Based Configuration: Easily configurable using a .env file.

🛠️ Available Tools

The server exposes the following tools for MCP clients to invoke:

Tool NameDescriptionParameters
read_queryExecutes a SELECT SQL query.query (string, required): The SELECT statement to execute.
write_queryExecutes an INSERT, UPDATE, or DELETE SQL query.query (string, required): The INSERT/UPDATE/DELETE statement to execute.
create_tableExecutes a CREATE TABLE SQL statement.schema (string, required): The CREATE TABLE statement.
list_tablesLists all user-created tables in the database.schema (string, optional): The schema name to filter tables by.
explain_queryReturns the execution plan for a given SQL query.query (string, required): The query to explain (must start with EXPLAIN).

🚀 Quick Start

Prerequisites

  • Go 1.23 or later
  • A PostgreSQL database server

Installation

  1. Clone the repository:

    bash
    git clone https://github.com/leixiaotian1/pgsql-mcp-server.git
    cd pgsql-mcp-server
  2. Install dependencies:

    bash
    go mod download
  3. Build the MCP server:

    bash
    go build -o pgsql-mcp-server

Configuration

The pg-mcp-server requires database connection details to be provided via 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)
SERVER_MODE=stdio      # Server mode (stdio, sse, streamableHttp)

Usage

Running the Server

bash
./pgsql-mcp-server

MCP Configuration

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

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

DOCKER DEPLOYMENT

Prerequisites

  • Docker installed

Deployment Steps

  1. Clone the project

    bash
    git clone https://github.com/leixiaotian1/pgsql-mcp-server.git
    cd pgsql-mcp-server
  2. Configure .env file

    Create a .env file in the project root directory. This file stores database connection information. Ensure the DB_HOST value matches the database container name you'll start later.

    properties
    DB_HOST=postgres
    DB_PORT=5432
    DB_NAME=postgres
    DB_USER=user
    DB_PASSWORD=password
    DB_SSLMODE=disable
    SERVER_MODE=sse
  3. Create Docker network

    To enable communication between the application container and database container, create a shared Docker network. This command only needs to run once.

    bash
    docker network create sql-mcp-network
  4. Start PostgreSQL database container

    Use this command to start a PostgreSQL container and connect it to our network.

    Note:

    • --name postgres: Container name, must exactly match the DB_HOST in your .env file.
    • --network sql-mcp-network: Connect to the shared network.
    • -p 5432:5432: Maps host's 5432 port to container's 5432 port. This means you can connect from your computer (e.g., using DBeaver) via localhost:5432, while the app container will access 5432 port directly through the internal network.
    bash
    docker run -d \
      --name postgres \
      --network sql-mcp-network \
      -e POSTGRES_USER=user \
      -e POSTGRES_PASSWORD=password \
      -e POSTGRES_DB=postgres \
      -p 5432:5432 \
      postgres
  5. Build and run the application

    Now you can use commands from the Makefile to manage the application.

    • Build image and run container:

      bash
      make build
      make run

      This will automatically stop old containers, build a new image, and start a new container.

    • View application logs:

      bash
      make logs

      If you see Successfully connected to database, everything is working correctly.

    • Stop the application:

      bash
      make stop

🔌 Server Modes

You can select the transport protocol by setting the SERVER_MODE environment variable.

stdio

The server communicates over standard input and output. This is the default mode and is ideal for local testing or direct integration with command-line-based MCP clients.

sse

The server communicates using Server-Sent Events (SSE). When this mode is enabled, the server will start an HTTP service and listen for connections.

  • SSE Endpoint: http://localhost:8088/sse
  • Message Endpoint: http://localhost:8088/message

streamableHttp

The server uses the Streamable HTTP transport, a more modern and flexible HTTP-based transport for MCP.

  • Endpoint: http://localhost:8088/mcp

🤝 Contributing

Contributions are welcome! If you find any bugs, have feature requests, or suggestions for improvement, please feel free to submit a Pull Request or open an Issue.

  1. Fork the Project.
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature).
  3. Commit your Changes (git commit -m 'Add some AmazingFeature').
  4. Push to the Branch (git push origin feature/AmazingFeature).
  5. Open a Pull Request.

📄 License

This project is open source and is licensed under the MIT License.

Installation

TypingMind
{
  "mcpServers": {
    "pgsql-mcp-server": {
      "command": "./sql-mcp-server",
      "args": [],
      "env": {
        "DB_HOST": "<DB_HOST>",
        "DB_PORT": "<DB_PORT>",
        "DB_NAME": "<DB_NAME>",
        "DB_USER": "<DB_USER>",
        "DB_PASSWORD": "<DB_PASSWORD>",
        "DB_SSLMODE": "<DB_SSLMODE>"
      }
    }
  }
}

Use PostgreSQL MCP 服务器 MCP with multiple AI models

TypingMind connects MCP tools at the workspace level, so once PostgreSQL MCP 服务器 is connected, you can use it with different AI models in TypingMind instead of setting it up separately for each model. This MCP runs locally through the TypingMind MCP connector on your device.

Setup guide to use the local connector

Use this when the MCP server needs access to local files, apps, or private resources on your computer.

1

Open the MCP settings

In TypingMind, go to Settings, Advanced Settings, then Model Context Protocol and choose Setup Connector.

  1. Open TypingMind in your browser.
  2. Click the Settings icon.
  3. Go to Advanced Settings.
  4. Open the Model Context Protocol section.
  5. Click Setup Connector and choose This Device.
TypingMind MCP connector setup screen with This Device selected
2

Run the connector command

Choose This Device, copy the command from TypingMind, and run it in Terminal. Keep the process running while you use MCP.

  1. Copy the setup command shown by TypingMind.
  2. Open Terminal on macOS or Windows Terminal on Windows.
  3. Paste and run the command.
  4. Approve the package install if Terminal asks you to proceed.
  5. Keep the Terminal window running while using MCP tools.
3

Add PostgreSQL MCP 服务器 as a server

When the connector status is Ready, click Edit Servers and paste the MCP server configuration.

  1. Wait until the connector status shows Ready.
  2. Click Edit Servers.
  3. Paste the PostgreSQL MCP 服务器 MCP server configuration.
  4. Save the server list.
  5. Refresh if you want to confirm the connector is still ready.
TypingMind MCP settings showing active server and Edit Servers button
{
  "mcpServers": {
    "pgsql-mcp-server": {
      "command": "npx",
      "args": [
        "-y",
        "<mcp-server-package>"
      ]
    }
  }
}
4

Use it across models

Save the server list, open Plugins, enable the PostgreSQL MCP 服务器 MCP tools, then select any supported AI model in TypingMind and use the tools in chat or assign them to an AI agent.

  1. Open the Plugins page in TypingMind.
  2. Enable the PostgreSQL MCP 服务器 MCP tools.
  3. Start a chat and choose the AI model you want to use.
  4. Use the MCP tools in chat or assign them to an AI agent.
  5. Switch to another AI model whenever needed without reconnecting MCP.
TypingMind chat using enabled MCP tools with a selected AI model
Can you use PostgreSQL MCP 服务器 to help me with this task?
PostgreSQL MCP 服务器
Sure. I read it.
Here is what I found using PostgreSQL MCP 服务器.

Frequently asked questions

What is the PostgreSQL MCP 服务器 MCP server used for?

PostgreSQL MCP 服务器 is an MCP server that lets compatible AI clients connect to external tools and context. In TypingMind, you can add this MCP server once and make its tools available in your AI workspace.

Can I use PostgreSQL MCP 服务器 MCP with multiple AI models in TypingMind?

Yes. TypingMind connects MCP tools at the workspace level, so you can use PostgreSQL MCP 服务器 with different AI models such as Claude, ChatGPT, Gemini, or other models you have configured in TypingMind without setting up the MCP server separately for each model.

Why use PostgreSQL MCP 服务器 MCP with TypingMind?

TypingMind is one of the best frontends for LLM chat because it brings multiple AI models, prompts, plugins, AI agents, API keys, and MCP tools into one workspace. With PostgreSQL MCP 服务器 connected, you can use its MCP tools across your preferred models while keeping your chat workflow organized in TypingMind.

How do I connect PostgreSQL MCP 服务器 MCP to TypingMind?

PostgreSQL MCP 服务器 runs through the TypingMind local MCP connector. This is best when the MCP server needs access to local files, desktop apps, command-line tools, or private resources on your computer.

What tools does PostgreSQL MCP 服务器 MCP provide in TypingMind?

PostgreSQL MCP 服务器 exposes MCP capabilities that can be enabled from the TypingMind Plugins page and used in chat or assigned to AI agents.

Do I need to share my API keys with TypingMind to use PostgreSQL MCP 服务器 MCP?

No. TypingMind is local-first and lets you keep your model providers, API keys, prompts, and MCP configuration under your control. If PostgreSQL MCP 服务器 requires authentication, add the required headers, OAuth settings, or local configuration for that MCP server when you create the connection.

Related MCP Servers

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇