442 từ
2 phút đọc
PostgreSQL MCP Server: Nói Chuyện Với Database Bằng Natural Language

AI agent có thể viết SQL hoàn hảo. Nhưng nó không connect được database. Bạn copy-paste query, nó phân tích, bạn copy-paste kết quả lại. Back-and-forth này tốn thời gian và ngắt flow.

PostgreSQL MCP Server giải quyết bằng cách cho agent direct, read-only database access. Nó có thể inspect schemas, chạy SELECT queries, phân tích data patterns, và suggest optimizations — tất cả qua natural language.


Cài Đặt#

CrystalDBA/postgres-mcp (Docker, Khuyến nghị)#

Terminal window
claude mcp add postgres \
--command docker \
--args "run -i --rm -e DATABASE_URL postgres-mcp" \
--env DATABASE_URL="postgresql://user:password@localhost:5432/mydb"

Official MCP Server (npx, không Docker)#

Terminal window
claude mcp add postgres \
--env DATABASE_URL="postgresql://user:password@localhost:5432/mydb" \
-- npx -y @modelcontextprotocol/server-postgres

Neon (Serverless PostgreSQL)#

Terminal window
claude mcp add neon \
--transport http \
neon https://mcp.neon.tech/YOUR_NEON_API_KEY/v1/mcp

Tools Overview#

ToolChức năngVí dụ
queryChạy SELECT”Top 10 khách hàng theo doanh thu”
list_tablesList tất cả tables”Có tables nào trong public schema?“
describe_tableShow columns, types, constraints”Mô tả bảng orders”
get_schemaFull database schema”Cho xem toàn bộ cấu trúc database”

Security: Read-Only User#

Đây là security consideration quan trọng nhất của database MCP:

Tạo read-only user trước#

CREATE USER mcp_reader WITH PASSWORD 'secure' LOGIN;
GRANT pg_read_all_data TO mcp_reader;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO mcp_reader;

Không bao giờ dùng admin user#

Terminal window
# ❌ Sai — full write access
DATABASE_URL="postgresql://admin:super_secret@prod-db:5432/production"
# ✅ Đúng — read-only
DATABASE_URL="postgresql://mcp_reader:secure@localhost:5432/mydb?sslmode=require"
MeasureWhy
Read-only userNgăn accidental writes, truncation, drops
SSL mode requiredMã hóa data in transit
Statement timeoutSET statement_timeout = '30s' — ngăn runaway queries

Token Cost Warning#

PostgreSQL MCP có vấn đề: token explosion từ large schemas.

ScenarioToken Cost
Query 3-4 joins500 - 2,000 tokens
List 20 tables1,000 - 3,000 tokens
Describe wide table (30+ columns)3,000 - 8,000 tokens
Full schema (50+ tables)10,000 - 50,000+ tokens ⚠️

Mitigations: Chỉ describe tables cần thiết, dùng describe_table orders thay vì get_schema, connect tới views-only schema nếu có thể.


Workflows Thực Tế#

Workflow 1: Ad-Hoc Analytics#

Terminal window
$ claude "MRR trend 6 tháng gần đây?"
Agent:
1. Inspect: subscriptions, payments tables
2. Chạy query với date_trunc, GROUP BY
3. Trả về: trend 6 tháng kèm % tăng trưởng

Workflow 2: Schema Understanding#

Terminal window
$ claude "Cần xây API endpoint cho user profiles. Database có gì?"
Agent:
1. List_tables users, profiles, addresses
2. Describe từng table
3. "Data model dùng 1:1 giữa users và profiles.
Tôi sẽ gen endpoint với joins chính xác."

Workflow 3: Data Validation#

Terminal window
$ claude "Vừa chạy schema migration. Verify data integrity."
Agent:
So sánh row counts, check foreign keys, validate null percentages
"Migration clean. 15,342 users migrated. 0 orphaned profiles."

Tổng Kết#

PostgreSQL MCP Server biến database từ tool riêng thành extension của bộ não AI agent. Nó inspect schemas, phân tích data, validate migrations, suggest optimizations — qua natural language.

Nguyên tắc vàng: tạo read-only database user riêng cho MCP. Agent phân tích được mọi thứ nhưng không sửa được gì.


Series: Practical MCP Servers for Developers — 2026 Edition. Day 5 of 6.

Advertisement

PostgreSQL MCP Server: Nói Chuyện Với Database Bằng Natural Language
https://minixium.com/vi/posts/postgresql-mcp-database-ai-agent-guide-2026-vi/
Tác giả
Minixium
Đăng vào lúc
2026-05-04
Giấy phép bản quyền
CC BY-NC-SA 4.0

Advertisement