Postgres MCP配置使用

AI资讯 12小时前 charles
260 0

本文预计阅读时间:3分钟

环境准备

运行数据库容器

docker run -itd --name postgres -e POSTGRES_PASSWORD=12345678 -p 5432:5432 -d postgres

创建数据

  • 连接数据库
psql -h 主机地址 -p 端口号 -U 用户名 -d 数据库名`
  • 创建数据库
create database mydb;
c mydb
  • 创建表并增加数据
-- 创建学生表
CREATETABLE students (
    idSERIAL PRIMARY KEY,
    nameVARCHAR(100NOTNULL,
    age INTNOTNULL
);

-- 插入 10 条模拟数据
INSERTINTO students (name, age)
VALUES
    ('Alice'20),
    ('Bob'21),
    ('Charlie'22),
    ('David'20),
    ('Eve'23),
    ('Frank'21),
    ('Grace'22),
    ('Heidi'20),
    ('Ivan'23),
    ('Judy'21);    

mydb=# select * from students;
 id |  name   | age 
----+---------+-----
  1 | Alice   |  20
  2 | Bob     |  21
  3 | Charlie |  22
  4 | David   |  20
  5 | Eve     |  23
  6 | Frank   |  21
  7 | Grace   |  22
  8 | Heidi   |  20
  9 | Ivan    |  23
 10 | Judy    |  21
(10 rows)

配置Postgres MCP

  • Cherry Studio配置

打开Cherry Studio,点击“设置”--->“MCP服务器”填写对应内容:Postgres MCP配置使用

使用Postgres MCP

配置完后,可以开始使用了,首先问有几张表:Postgres MCP配置使用

接着让它查询表的数据:Postgres MCP配置使用

到此,Postgres MCP的配置和使用就完成了。

其他IDE配置Postgres MCP

如果是使用了vscode、trae、cursor之类的IDE,在配置文件中写入下面内容即可:

{
  "mcpServers": {
    "postgres": {
      "command""npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://postgres:12345678@127.0.0.1:5432/mydb"
      ]
    }
  }
}

版权声明:charles 发表于 2025年6月23日 am12:50。
转载请注明:Postgres MCP配置使用 | AI工具大全&导航

相关文章