PostgreSQL MCP服务器
一个MCP服务器,将PostgreSQL数据库操作作为AI助手的工具公开。
什么是MCP?
模型上下文协议(MCP)将AI助手与外部工具和数据连接起来。该服务器允许AI助手执行SQL查询并检查PostgreSQL数据库模式。
设置
1.安装依赖项
注: 此项目使用Poetry进行依赖关系管理。如果您没有安装Poetry,可以使用以下命令安装: ``bash curl -sSL https://install.python-poetry.org | python3 - `` 请参阅 官方诗歌文献 用于替代安装方法。poetry install2.配置数据库
复制 .env.example 到 .env 并添加您的PostgreSQL凭据:
cp .env.example .env编辑 .env:
DB_NAME=your_database
DB_USER=postgres
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=5432测试
MCP检查员(推荐)
npx @modelcontextprotocol/inspector poetry run python postgres-mcp-server/main.py这将打开一个web UI,您可以在其中:
- 查看下的可用工具 工具 标签
- 测试
get_schema - 查看实时结果
快速测试
poetry run python postgres-mcp-server/main.py按 Ctrl+C 停止。无错误=工作正常。
可用工具
get_schema() -获取数据库架构
连接到光标
添加到光标MCP配置(全局设置):
{
"mcpServers": {
"postgres": {
"command": "poetry",
"args": ["-C", "/absolute/path/to/postgres-mcp-server", "run", "python", "postgres-mcp-server/main.py"]
}
}
}替换 /absolute/path/to/postgres-mcp-server 根据您的实际项目路径。
______________________________________________________________________
*面向未来的数据科学-教数据科学家使用人工智能优化工作流程*
