Postgres
FreeMaintainedEnables AI agents to interact with PostgreSQL databases through schema intelligence, query execution, and DBA tooling including index analysis and health monito
About
Enables AI agents to interact with PostgreSQL databases through schema intelligence, query execution, and DBA tooling including index analysis and health monitoring. Features configurable access levels and audit logging for secure database operations.
README
mcp-postgres
MCP server for PostgreSQL. Gives AI agents schema intelligence, query execution, and DBA tooling — through the Model Context Protocol.
Unlike generic database MCP servers, mcp-postgres is Postgres-native. It extracts table/column comments, understands Postgres-specific catalog views, provides index analysis, and ships with configurable access levels so you don't hand an LLM unrestricted database access.
Features
Schema Intelligence
- List schemas, tables, views with sizes and row counts
- Full table descriptions: columns, types, constraints, indexes, foreign keys
- Extracts
COMMENT ONmetadata — gives the LLM semantic context about what columns mean - Search objects by name or comment across the entire database
Query Execution
- Read-only
querytool with automatic row limiting - Write-capable
executetool gated by access level EXPLAIN ANALYZEwith human-readable output
DBA Tooling
- Table stats: live/dead tuples, bloat percentage, vacuum history, scan patterns
- Index analysis: usage stats, unused index detection, missing index suggestions
- Database health: connections, cache hit ratio, long-running queries, throughput
Safety
- Four access levels:
readonly,readwrite,admin,unrestricted - SQL statement classification (SELECT, DML, DDL, admin) with enforcement
- Audit logging to stderr (JSON, one entry per query)
Quick Start
npx mcp-postgres --connection-string "postgres://user:pass@localhost:5432/mydb"
Or with environment variables:
DATABASE_URL="postgres://user:pass@localhost:5432/mydb" npx mcp-postgres
Claude Desktop
Add to your claude_desktop_config.json:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"mcp-postgres",
"--connection-string",
"postgres://user:pass@localhost:5432/mydb"
]
}
}
}
Claude Code
Add to your project's .mcp.json:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "mcp-postgres"],
"env": {
"DATABASE_URL": "postgres://user:pass@localhost:5432/mydb"
}
}
}
}
Tools
| Tool | Description | Access |
|---|---|---|
list_schemas |
List schemas with table counts and sizes | readonly |
list_tables |
List tables with comments, row counts, sizes | readonly |
describe_table |
Full table description with columns, indexes, FKs, comments | readonly |
search_objects |
Search objects by name or comment | readonly |
query |
Execute SELECT queries | readonly |
execute |
Execute INSERT/UPDATE/DELETE/CREATE/etc | varies |
explain_query |
EXPLAIN (ANALYZE) with readable output | readonly* |
table_stats |
Table statistics, bloat, vacuum info | readonly |
index_analysis |
Index usage, unused indexes, missing index hints | readonly |
database_health |
Connections, cache ratio, long queries, bloat | readonly |
*explain_query with analyze=true executes the query, so it respects the statement's access level.
Resources
| URI | Description |
|---|---|
postgres://schema/{name} |
Full DDL for a schema (CREATE TABLE statements with comments) |
postgres://extensions |
Installed PostgreSQL extensions |
Prompts
| Prompt | Description |
|---|---|
explore-database |
Guided database exploration — schemas, tables, relationships |
optimize-query |
Analyze a slow query with EXPLAIN, indexes, and recommendations |
health-check |
Comprehensive database health assessment |
Configuration
CLI Options
--connection-string PostgreSQL connection URL
--access-level readonly|readwrite|admin|unrestricted (default: readonly)
--row-limit Max rows returned per query (default: 500)
--schema Default schema filter (default: public)
--audit Enable query audit logging to stderr
Environment Variables
| Variable | Description |
|---|---|
DATABASE_URL |
PostgreSQL connection URL |
POSTGRES_URL |
Alternative connection URL |
MCP_POSTGRES_ACCESS_LEVEL |
Access level override |
MCP_POSTGRES_ROW_LIMIT |
Row limit override |
Access Levels
| Level | SELECT | INSERT/UPDATE/DELETE | CREATE/ALTER/DROP | TRUNCATE/DROP DATABASE |
|---|---|---|---|---|
readonly |
yes | no | no | no |
readwrite |
yes | yes | no | no |
admin |
yes | yes | yes | no |
unrestricted |
yes | yes | yes | yes |
Default is readonly. Use the minimum level needed.
Audit Logging
Enable with --audit. Logs every tool invocation to stderr as JSON:
{"timestamp":"2026-04-03T12:00:00.000Z","tool":"query","sql":"SELECT * FROM users","statementType":"select","accessLevel":"readonly","allowed":true,"durationMs":12,"rowCount":42}
Pipe stderr to a file to capture: mcp-postgres --audit 2>audit.log
Architecture
src/
├── index.ts Entry point and CLI
├── server.ts MCP server setup
├── config.ts Configuration parsing
├── db/
│ ├── pool.ts Connection pool management
│ └── query.ts Query execution with timing
├── tools/
│ ├── schema.ts Schema exploration tools
│ ├── query.ts Query execution tools
│ └── performance.ts DBA and health tools
├── resources/
│ └── schema.ts Schema DDL resources
├── prompts/
│ └── index.ts Prompt templates
└── safety/
├── classifier.ts SQL statement classification
├── access.ts Access level enforcement
└── audit.ts Audit logging
Development
npm install
npm test # run tests
npm run build # compile TypeScript
npm run dev -- --connection-string "postgres://..." # run in dev mode
License
MIT
Install Postgres in Claude Desktop, Claude Code & Cursor
unyly install mcp-postgresInstalls into Claude Desktop, Claude Code, Cursor & VS Code — handles npx, uvx and build-from-source repos for you.
First time? Get the CLI: curl -fsSL https://unyly.org/install | sh
Or configure manually
Run in your terminal:
claude mcp add mcp-postgres --env DATABASE_URL="" --env DB_HOST="" --env DB_NAME="" --env DB_PASSWORD="" --env DB_PORT="" --env DB_READ_ONLY="" --env DB_SSL_MODE="" --env DB_STATEMENT_TIMEOUT="" --env DB_USER="" --env POSTGRES_DB="" --env POSTGRES_HOST="" --env POSTGRES_PASSWORD="" --env POSTGRES_PORT="" --env POSTGRES_SSL_MODE="" --env POSTGRES_USER="" -- npx -y mcp-postgresStep-by-step: how to install Postgres
FAQ
Is Postgres MCP free?
Yes, Postgres MCP is free — one-click install via Unyly at no cost.
Does Postgres need an API key?
Yes, it requires environment variables: DATABASE_URL, DB_HOST, DB_NAME, DB_PASSWORD, DB_PORT, DB_READ_ONLY, DB_SSL_MODE, DB_STATEMENT_TIMEOUT, DB_USER, POSTGRES_DB, POSTGRES_HOST, POSTGRES_PASSWORD, POSTGRES_PORT, POSTGRES_SSL_MODE, POSTGRES_USER. Unyly injects them into the config during install.
Is Postgres hosted or self-hosted?
A hosted option is available: Unyly runs the server in the cloud, no local setup required.
How do I install Postgres in Claude Desktop, Claude Code or Cursor?
Open Postgres on unyly.org, pick your client tab (Claude Desktop, Claude Code, Cursor) and press Install — the config is generated automatically, no JSON editing.
Changes
Versions and requested access over time.
- New version published
- New version published
Related MCPs
wenb1n-dev/SmartDB_MCP
A universal database MCP server supporting simultaneous connections to multiple databases. It provides tools for database operations, health analysis, SQL optim
by wenb1n-devPostgres Server
This server enables interaction with PostgreSQL databases through the Model Context Protocol, optimized for the AWS Bedrock AgentCore Runtime. It provides tools
by madhurprashPostgres
Query your database in natural language
by AnthropicPostgreSQL
Read-only database access with schema inspection.
by modelcontextprotocolRedis
Interact with Redis key-value stores.
by modelcontextprotocolSQLite
Database interaction and business intelligence capabilities.
by modelcontextprotocolmxcp
Open-source framework for building enterprise-grade MCP servers using just YAML, SQL, and Python, with built-in auth, monitoring, ETL and policy enforcement.
by raw-labstadas-github/a2asearch-mcp
MCP server to search 4,800+ MCP servers, AI agents, CLI tools and agent skills. Install: npx -y a2asearch-mcp. Ask Claude: "Find MCP servers for database access
by tadas-githubjulien040/anyquery
Query more than 40 apps with one binary using SQL. It can also connect to your PostgreSQL, MySQL, or SQLite compatible database. Local-first and private by desi
by julien040drakonkat/wizzy-mcp-tmdb
A MCP server for The Movie Database API that enables AI assistants to search and retrieve movie, TV show, and person information.
by drakonkatCompare Postgres with
Not sure what to pick?
Find your stack in 60 seconds
Author?
Embed badge for your README
Browse similar
All data MCPs
