docker run -itd --name postgres -e POSTGRES_PASSWORD=12345678 -p 5432:5432 -d postgres
psql -h 主机地址 -p 端口号 -U 用户名 -d 数据库名`
create database mydb;
\c mydb
-- 创建学生表
CREATETABLEstudents (
idSERIALPRIMARYKEY,
nameVARCHAR(100)NOTNULL,
ageINTNOTNULL
);
-- 插入 10 条模拟数据
INSERTINTOstudents (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)
打开Cherry Studio,点击“设置”--->“MCP服务器”填写对应内容:
配置完后,可以开始使用了,首先问有几张表:
接着让它查询表的数据:
到此,Postgres MCP的配置和使用就完成了。
如果是使用了vscode、trae、cursor之类的IDE,在配置文件中写入下面内容即可:
{
"mcpServers": {
"postgres": {
"command":"npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://postgres:12345678@127.0.0.1:5432/mydb"
]
}
}
}
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |