Connect AI agents to Postgres databases via MCP protocol for schema-aware querying and data exploration
pg-mcp-server is a Model Context Protocol server that bridges AI agents and PostgreSQL databases. It exposes schema metadata (tables, columns, indexes, foreign keys) as MCP resources, and lets agents execute read-only SQL queries or transactional writes. Ideal for developers who want Claude, Cursor, or other LLM-powered tools to answer questions about a live database without manual SQL. Supports connection string configuration, SSL modes, and Row-level security awareness.
Use cases
- Ask an AI agent to explain the schema and relationships in an unfamiliar Postgres database
- Generate Prisma migrations from an existing live database structure
- Write and validate complex SQL queries through natural language
- Debug data inconsistencies by querying specific rows referenced in a ticket
- Explore table statistics and index effectiveness with AI assistance
Key features
- Schema introspection: exposes tables, columns, indexes, and foreign keys as MCP resources
- Read and write query execution with parameterized SQL support
- Transaction support for safe multi-step data modifications
- Row-level security (RLS) policy visibility for Postgres Pro users
- Connection pooling awareness via libpq environment variables