PostgreSQL MCP Server

PostgreSQL MCP Server

Enables natural language interaction with PostgreSQL databases, supporting query execution, schema management, data operations, user management, and database maintenance with secure remote access via HTTP/SSE transport.

Category
访问服务器

README

PostgreSQL MCP Server

Model Context Protocol server for PostgreSQL with HTTP/SSE transport for remote deployment. Built with Python, FastAPI, and Docker for Elestio or any cloud platform.

Features

  • HTTP/SSE Transport: Remote access via HTTPS
  • API Key Authentication: Secure access control
  • Query Execution: Execute SELECT queries with natural language
  • Schema Management: Create, alter, and drop tables and indexes
  • Data Manipulation: Insert, update, and delete data
  • User Management: Create users and manage permissions
  • Database Maintenance: VACUUM, backups, and connection management
  • Secure: Read-only mode, connection pooling, and query timeouts

Quick Start

1. Build the Docker Image

docker build -t postgresql-mcp-server .

2. Configure Environment

Generate API key:

python3 -c "import secrets; print(secrets.token_urlsafe(32))"

Copy .env.example to .env and update:

cp .env.example .env
# Edit .env with your PostgreSQL and API key

3. Run with Docker Compose

docker-compose up -d

Server will be available at http://localhost:3000

4. Configure Cursor IDE

Add to Cursor MCP settings:

{
  "mcpServers": {
    "postgresql": {
      "url": "https://your-server.com:3000",
      "headers": {
        "Authorization": "Bearer your_api_key"
      }
    }
  }
}

See ELESTIO_DEPLOY.md for deployment guide.

Available Tools

Query Tools

  • execute_query - Run SELECT queries
  • execute_explain - Get query execution plans
  • list_databases - List all databases
  • list_tables - List tables in schema
  • list_columns - Show column information
  • get_table_info - Get detailed table metadata
  • get_database_size - Get database and table sizes

Schema Management (DDL)

  • create_table - Create new tables
  • drop_table - Remove tables
  • alter_table - Modify table structure
  • create_index - Create indexes
  • drop_index - Remove indexes
  • get_table_ddl - Generate CREATE TABLE statements

Data Operations (DML)

  • insert_data - Insert single row
  • bulk_insert - Insert multiple rows
  • update_data - Update rows
  • delete_data - Delete rows

User Management (DCL)

  • list_users - List database users
  • create_user - Create new users
  • grant_permissions - Grant privileges
  • revoke_permissions - Revoke privileges
  • list_permissions - Show user permissions

Maintenance

  • vacuum_analyze - Run VACUUM ANALYZE
  • backup_database - Create pg_dump backup
  • restore_database - Restore from backup
  • kill_connections - Terminate connections
  • get_active_connections - Show active connections
  • test_connection - Test database connection

Usage Examples

Once configured in Cursor, interact with your database naturally:

"Show me all tables in the database"
"What's the structure of the users table?"
"Find the top 10 customers by order count"
"Create a products table with id, name, and price columns"
"Insert a new user with email test@example.com"
"Show me all database users and their permissions"

Configuration Options

Environment variables:

  • POSTGRES_HOST - Database host (default: localhost)
  • POSTGRES_PORT - Database port (default: 5432)
  • POSTGRES_DB - Database name
  • POSTGRES_USER - Database user
  • POSTGRES_PASSWORD - Database password
  • POSTGRES_READONLY - Read-only mode (default: false)
  • POSTGRES_SSLMODE - SSL mode (default: prefer)
  • POSTGRES_POOL_MIN - Min connections (default: 1)
  • POSTGRES_POOL_MAX - Max connections (default: 10)
  • QUERY_TIMEOUT - Query timeout in seconds (default: 30)

Security Best Practices

  1. Use environment variables for credentials, never hardcode
  2. Enable read-only mode for exploration: POSTGRES_READONLY=true
  3. Create dedicated users with minimal required privileges
  4. Use SSL connections for production: POSTGRES_SSLMODE=require
  5. Set query timeouts to prevent long-running queries
  6. Limit connection pools based on your database capacity

Deployment Options

Option 1: Using Docker Compose (Recommended)

Edit .env with your PostgreSQL credentials:

docker-compose up -d

Option 2: Direct Docker Run

docker run -i --rm \
  -e POSTGRES_HOST=your_host \
  -e POSTGRES_PORT=5432 \
  -e POSTGRES_DB=your_db \
  -e POSTGRES_USER=your_user \
  -e POSTGRES_PASSWORD=your_password \
  postgresql-mcp-server

Project Structure

mcpg/
├── src/postgresql_mcp_server/
│   ├── __init__.py
│   ├── server.py              # Main MCP server
│   ├── db_manager.py           # Connection management
│   ├── query_executor.py       # Query execution
│   ├── schema_manager.py       # DDL operations
│   ├── data_manager.py         # DML operations
│   ├── user_manager.py         # User management
│   └── maintenance_manager.py  # Maintenance tools
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
├── .env.example
├── cursor-mcp-config.json
├── CURSOR_SETUP.md
└── README.md

Troubleshooting

Connection Issues

  1. Verify PostgreSQL is accessible:

    docker run -it --rm postgres:16-alpine psql -h host.docker.internal -U user -d db
    
  2. Check Docker network settings for container-to-container communication

  3. Review Cursor logs for MCP errors

Permission Errors

  • Verify database user has required privileges
  • Check PostgreSQL pg_hba.conf for access rules
  • Use test_connection tool to diagnose

Docker Issues

  • Ensure Docker daemon is running
  • Check for port conflicts (5432)
  • Verify image is built: docker images | grep postgresql-mcp-server

Development

Requirements

  • Python 3.11+
  • PostgreSQL 12+
  • Docker 20.10+

Local Development

# Install dependencies
pip install -r requirements.txt

# Set environment variables
export POSTGRES_HOST=localhost
export POSTGRES_PORT=5432
# ... other vars

# Run directly
python -m src.postgresql_mcp_server.server

License

MIT License - see LICENSE file for details

Contributing

Contributions welcome! Please open an issue or PR.

Support

For issues and questions:

Additional Documentation

推荐服务器

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

官方
精选