postgresql.com
postgresql.com logo

PostgreSQL

Enables direct PostgreSQL database interaction with tools for executing SQL queries, creating tables, and listing databa...

Created byApr 23, 2025

PostgreSQL MCP Server

[![Build](https://github.com/mark3labs/mcp-go/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/mark3labs/mcp-go/actions/workflows/ci.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/mark3labs/mcp-go?cache)](https://goreportcard.com/report/github.com/mark3labs/mcp-go) [![GoDoc](https://pkg.go.dev/badge/github.com/mark3labs/mcp-go.svg)](https://pkg.go.dev/github.com/mark3labs/mcp-go) ![Stars](https://img.shields.io/github/stars/leixiaotian1/pgsql-mcp-server) ![Forks](https://img.shields.io/github/forks/leixiaotian1/pgsql-mcp-server)
English | [ ](readme_zh_CH.md)
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: ```bash git clone https://github.com/sql-mcp-server.git cd sql-mcp-server ```
  1. Install dependencies: ```bash go mod download ```
  1. Build the server: ```bash 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:

Usage

Running the Server

MCP Configuration

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

Tool Examples

List Tables

List all user tables in the database:
List tables in a specific schema:

Create Table

Create a new table:

Read Query

Execute a SELECT query:

Write Query

Execute an INSERT query:
Execute an UPDATE query:
Execute a DELETE query:

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

  • [github.com/joho/godotenv](https://github.com/joho/godotenv) - For loading environment variables from .env file
  • [github.com/lib/pq](https://github.com/lib/pq) - PostgreSQL driver for Go
  • [github.com/mark3labs/mcp-go](https://github.com/mark3labs/mcp-go) - Go SDK for Model Context Protocol

License

[Add license information here]

Contributing

[Add contribution guidelines here]