PostgreSQL MCP Server

PostgreSQL MCP Server

Enables secure querying of PostgreSQL databases through MCP-compatible clients. Supports read-only SQL execution, table exploration, and connection management with built-in security validation.

Category
访问服务器

README

PostgreSQL MCP Server

<img src="icon.png" alt="Postgres Icon" width="150"/>

An MCP (Model Context Protocol) server for querying a PostgreSQL database. Execute SQL queries, view table details, and manage database connections through MCP-compatible clients.

Key Features

  • Database Connection: Connect to PostgreSQL databases with authentication
  • SQL Execution: Execute SELECT queries with security validation
  • Connection Testing: Test database connection status

Installation

Claude Desktop

Follow these steps for installation:

Step 1: Download the MCP Server

Download the postgres-connector.mcpb file from this repository (located in the root directory).

Step 2: Install the MCP Server

  1. Open Claude Desktop
  2. Navigate to Settings > Features
  3. Click "Install Extension" and select the postgres-connector.mcpb file

Installation Step 2 Screenshot placeholder: Installing the .mcpb file

Step 3: Configure Database Connection

After installation, the PostgreSQL Connector will appear in your Claude Desktop with default environment variables that you can configure with your database details.

Configuration Screen Screenshot placeholder: Database configuration interface showing environment variables

Step 4: Test the database connection

Send a query "test the database connection" Test Database Connection

Configuration

Required Database Settings

You'll need to provide the following information for your PostgreSQL database:

Environment Variable Description Required Default
POSTGRES_HOST The hostname or IP address of your PostgreSQL server ✅ Yes -
POSTGRES_PORT The port number for your PostgreSQL server ❌ No 5432
POSTGRES_DB The name of the specific database to connect to ✅ Yes -
POSTGRES_USER Your PostgreSQL username ✅ Yes -
POSTGRES_PASSWORD Your PostgreSQL password ✅ Yes -

These environment variables will be automatically available for configuration in the extension settings panel after installing the extension in Claude Desktop.

Available Tools

1. Test PostgreSQL Connection

Tool: test-postgres-connection

Tests your database connection and displays the current configuration (password hidden for security).

Example Output:

✅ Database connection successful!

Configuration:
{
  "host": "localhost",
  "port": "5432",
  "database": "myapp_production",
  "username": "postgres",
  "password": "***hidden***"
}

Connection test result: [
  {
    "test": 1
  }
]

2. Get All Tables

Tool: get-all-tables

Retrieves a list of all tables in the public schema of your database.

Example Output:

[
  {
    "table_name": "users"
  },
  {
    "table_name": "orders"
  },
  {
    "table_name": "products"
  },
  {
    "table_name": "categories"
  }
]

3. Execute SQL Query

Tool: execute-sql-query

Executes SELECT queries on your database with security validation.

Input:

  • sql: The SQL query to execute (SELECT statements only)

Example Usage:

SELECT id, name, email FROM users LIMIT 5;

Example Output:

[
  {
    "id": 1,
    "name": "John Doe",
    "email": "john@example.com"
  },
  {
    "id": 2,
    "name": "Jane Smith",
    "email": "jane@example.com"
  }
]

Security & Safety

SQL Query Validation

The PostgreSQL MCP server includes security measures:

  • READ-ONLY Operations: Only SELECT statements are permitted
  • Dangerous Keywords Blocked: DROP, DELETE, TRUNCATE, ALTER, CREATE, INSERT, UPDATE operations are automatically blocked
  • Statement-Level Validation: Each SQL statement is validated individually
  • Safe Error Handling: Database errors are caught and displayed safely

Best practice is to create a read-only database user for the postgres connector to use.

Blocked Operations

The following SQL operations are NOT ALLOWED for security:

  • DROP (deleting tables/databases)
  • DELETE (removing data)
  • TRUNCATE (clearing tables)
  • ALTER (modifying structure)
  • CREATE (creating new objects)
  • INSERT (adding data)
  • UPDATE (modifying data)

Usage Examples

Get Database Tables

Use the get-all-tables tool to retrieve all tables in your database schema.

Execute Queries

Use the execute-sql-query tool with SELECT statements:

SELECT * FROM users LIMIT 10;

Troubleshooting

Connection Issues

Problem: "Database connection failed"

  • Check: Verify your host, port, database name, username, and password
  • Check: Ensure your PostgreSQL server is running and accessible
  • Check: Verify that your database user has the necessary permissions

Problem: "Connection timeout"

  • Check: Your database server might be behind a firewall
  • Check: The host address and port number are correct
  • Check: Network connectivity to the database server

Query Issues

Problem: "Dangerous operation detected"

  • Cause: You're trying to execute a non-SELECT statement
  • Solution: Only SELECT queries are allowed for security reasons

Problem: "Query execution failed"

  • Check: Your SQL syntax is correct
  • Check: The tables and columns you're referencing exist
  • Check: Your database user has SELECT permissions on the tables

Configuration Issues

Problem: MCP server not appearing in Claude Desktop client

  • Solution: Restart Claude Desktop
  • Check: Verify the .mcpb file was installed correctly
  • Check: Look for any error messages in Claude Desktop's console

Problem: Environment variables not showing up

  • Solution: Restart Claude Desktop after installation
  • Check: Ensure the extension is enabled in Settings > Features

Problem: Cannot configure database settings

  • Solution: Make sure all required environment variables are filled out
  • Check: Verify your database credentials are correct

Technical Details

System Requirements

  • PostgreSQL database (version 9.0 or higher)
  • Network access to your PostgreSQL server

Dependencies

  • Node.js runtime
  • PostgreSQL client libraries
  • MCP (Model Context Protocol) framework

Project Structure

postgres-connector/
├── src/
│   └── server.ts          # Main MCP server implementation
├── package.json           # Node.js dependencies
├── manifest.json          # MCP extension manifest
├── icon.png              # Extension icon
└── postgres-connector.mcpb # Compiled extension bundle

License

MIT License - see the LICENSE file for details.

Author

Marty Markenson


Need help? Open an issue in this repository or reach out to martymarkenson

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选