# PostgreSQL MCP — SQL Database Server for AI Agents > MCP server that gives AI agents direct access to PostgreSQL databases. Run queries, explore schemas, manage tables, and analyze data through natural language. 3,000+ stars. ## Install Merge the JSON below into your `.mcp.json`: ## Quick Use Add to your `.mcp.json`: ```json { "mcpServers": { "postgres": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-postgres"], "env": { "POSTGRES_CONNECTION_STRING": "postgresql://user:pass@localhost:5432/mydb" } } } } ``` Restart Claude Code. Ask: "Show all tables in the database" or "Find the top 10 customers by revenue." --- ## Intro PostgreSQL MCP is a Model Context Protocol server that gives AI agents like Claude Code direct, read/write access to PostgreSQL databases with 3,000+ GitHub stars. Run SQL queries, explore schemas, create tables, analyze data, and generate reports — all through natural language. Your AI agent becomes a database-aware assistant that understands your data model. Best for developers working with PostgreSQL who want AI-assisted data exploration and database operations. Works with: Claude Code, Cursor, any MCP client. Setup time: under 1 minute. --- ## Available Tools ### Schema Exploration ``` "Show all tables and their column types" "Describe the users table" "What foreign keys reference the orders table?" "Show all indexes on the products table" ``` ### Query Execution ``` "Find all users who signed up in the last 30 days" "What is the average order value by country?" "Show the top 10 products by revenue this quarter" "Count active vs inactive users" ``` ### Data Analysis ``` "Analyze the orders table and find trends in monthly revenue" "Find customers with declining purchase frequency" "Show the distribution of order sizes" ``` ### Schema Management ``` "Create a table for tracking user preferences with columns: user_id, key, value, updated_at" "Add an index on the email column of the users table" "Add a nullable column 'phone' to the customers table" ``` ### Migration Generation ``` "Generate a migration to add soft-delete to the users table" "Create a migration that adds a full-text search index on products.description" ``` ## Safety Features ### Read-Only Mode ```json { "env": { "POSTGRES_CONNECTION_STRING": "postgresql://readonly_user:pass@host/db" } } ``` ### Query Confirmation The agent shows the SQL before executing write operations — you approve or reject. ### Key Stats - 3,000+ GitHub stars - Official MCP server - Full SQL execution - Schema exploration - Read-only mode available ### FAQ **Q: What is PostgreSQL MCP?** A: An MCP server that gives AI agents direct access to PostgreSQL databases for querying data, exploring schemas, and managing tables via natural language. **Q: Is it safe?** A: Use a read-only database user for exploration. Write operations require confirmation. Never use superuser credentials. **Q: Is PostgreSQL MCP free?** A: Yes, open-source under MIT license. --- ## Source & Thanks > Part of the [MCP Servers](https://github.com/modelcontextprotocol/servers) collection. Licensed under MIT. > > [server-postgres](https://github.com/modelcontextprotocol/servers) — ⭐ 3,000+ Thanks for making databases conversational. --- ## 快速使用 将以下配置添加到 `.mcp.json`: ```json { "mcpServers": { "postgres": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-postgres"], "env": { "POSTGRES_CONNECTION_STRING": "postgresql://user:pass@localhost/mydb" } } } } ``` --- ## 简介 PostgreSQL MCP 是一个让 AI Agent 直接访问 PostgreSQL 数据库的 MCP 服务器,GitHub 3,000+ stars。运行查询、探索表结构、管理数据。支持只读模式保障安全。 --- ## 来源与感谢 > Part of [MCP Servers](https://github.com/modelcontextprotocol/servers). Licensed under MIT. --- Source: https://tokrepo.com/en/workflows/faa28c56-1377-4109-9e8f-a9b2be119d16 Author: MCP Hub