MCP PostgreSQL Server

MCP PostgreSQL Server

Enables secure read-only access to PostgreSQL databases, allowing users to list tables, query schemas, execute SELECT statements, and inspect table structures through natural language interactions.

Category
访问服务器

README

MCP PostgreSQL Server

A Model Context Protocol (MCP) server that provides PostgreSQL database access and operations.

Installation

You can use this MCP server with any MCP-compatible client by installing it via npm:

npm install -g mcp-postgres

Or run it directly with npx:

npx mcp-postgres@latest

Configuration

MCP Client Configuration

Add this to your MCP client configuration (e.g., .kiro/settings/mcp.json):

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["mcp-postgres@latest"],
      "env": {
        "DB_HOST": "localhost",
        "DB_PORT": "5432",
        "DB_USER": "postgres",
        "DB_PASSWORD": "your_password",
        "DB_NAME": "your_database",
        "DB_SSL_MODE": "require"
      },
      "disabled": false,
      "autoApprove": ["list_tables", "get_schema"]
    }
  }
}

Alternative using DATABASE_URL:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["mcp-postgres@latest"],
      "env": {
        "DATABASE_URL": "postgresql://username:password@localhost:5432/database_name?sslmode=require"
      },
      "disabled": false,
      "autoApprove": ["list_tables", "get_schema"]
    }
  }
}

Environment Variables

The server supports multiple configuration methods:

Option 1: Individual Environment Variables (Recommended)

DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=your_database
DB_SSL_MODE=require  # Optional: require, disable, or omit for default

Alternative PostgreSQL-style variable names are also supported:

POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_password
POSTGRES_DB=your_database
POSTGRES_SSL_MODE=require  # Optional: require, disable, or omit for default

Option 2: DATABASE_URL (Fallback)

DATABASE_URL=postgresql://username:password@localhost:5432/database_name?sslmode=require

Option 3: Config File

Create a config.json file in your working directory:

{
  "db": {
    "host": "localhost",
    "port": 5432,
    "user": "postgres",
    "password": "your_password",
    "database": "your_database",
    "sslmode": "require"
  }
}

SSL Configuration

The server supports SSL connections with the following modes:

  • require - Forces SSL connection (useful for cloud databases)
  • disable - Explicitly disables SSL (default for local development)
  • Omit the SSL mode for default behavior (no SSL)

SSL can be configured via:

  • Environment variables: DB_SSL_MODE or POSTGRES_SSL_MODE
  • DATABASE_URL parameter: ?sslmode=require
  • Config file: "sslmode": "require"

AWS RDS Auto-Configuration

The server automatically detects AWS RDS endpoints (hosts containing .rds.amazonaws.com) and:

  1. Automatically downloads the AWS RDS Global Certificate Bundle from https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem
  2. Caches the certificate locally in .aws-certs/ directory for 30 days
  3. Configures SSL with proper certificate validation using the downloaded bundle
  4. Re-downloads the certificate automatically if it's older than 30 days
  5. Graceful fallback to basic SSL if certificate download fails

This means you can connect to AWS RDS instances without manually downloading or configuring SSL certificates. Simply provide your RDS endpoint and the server handles the rest:

DB_HOST=mydb.cluster-xyz.us-east-1.rds.amazonaws.com
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=your_database
# No need to set DB_SSL_MODE - automatically configured for RDS

Features:

  • Persistent disk caching: Certificate is saved to .aws-certs/rds-global-bundle.pem and persists between sessions
  • 30-day cache duration: Certificate is automatically refreshed after 30 days
  • Cache validation: Verifies cached certificates aren't corrupted before use
  • Connection retry: Automatic retry logic with 3 attempts and 2-second delays
  • Error handling: Falls back to basic SSL if certificate download fails
  • Performance: Certificate is cached in memory after first read to avoid repeated file operations
  • Cache monitoring: Use the check_certificate_cache tool to view cache status

The auto-configuration ensures secure, verified connections to AWS RDS while maintaining convenience and reliability.

Available Tools

list_tables

Lists all tables in the database with their types.

get_schema

Gets database schema information including tables and columns.

  • Optional parameter: table_name - Get schema for a specific table

execute_query

Executes a SQL query (SELECT statements only for safety).

  • Required parameter: query - The SQL SELECT query to execute

describe_table

Get detailed information about a specific table including indexes and constraints.

  • Required parameter: table_name - Name of the table to describe

get_table_sample

Gets a sample of rows from a table.

  • Required parameter: table_name - Name of the table to sample
  • Optional parameter: limit - Number of rows to return (default: 10, max: 100)

check_certificate_cache

Checks the status of the AWS RDS certificate cache.

  • Shows cache location, age, expiration status, and file details
  • Useful for troubleshooting SSL connection issues with RDS

Security

For security reasons, only SELECT queries are allowed through the execute_query tool. This prevents accidental data modification through the MCP interface.

Testing

Testing with MCP Inspector

You can test the server locally using the MCP Inspector tool:

# Install the MCP inspector
npm install -g @modelcontextprotocol/inspector

# Set your database credentials
$env:DB_HOST="localhost"
$env:DB_USER="postgres"
$env:DB_PASSWORD="your_password"
$env:DB_NAME="your_database"

# Run the inspector
mcp-inspector node server.mjs

The inspector opens a web UI where you can interactively test each tool and see the responses.

Testing in Kiro IDE

Once configured in your .kiro/settings/mcp.json, you can test the tools directly:

  • "List all tables in the database"
  • "Show me the schema for the users table"
  • "Execute this query: SELECT * FROM products WHERE price > 100"

License

MIT

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选