mcp-postgres
Enables AI agents to interact with PostgreSQL databases, providing schema intelligence (table/column exploration with comment extraction, catalog views), query execution with configurable access levels (readonly to unrestricted), and DBA tooling including index analysis, table statistics, bloat detection, and database health monitoring.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@mcp-postgresshow me the schema for the users table including indexes and foreign keys"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
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 meanSearch objects by name or comment across the entire database
Query Execution
Read-only
querytool with automatic row limitingWrite-capable
executetool gated by access levelEXPLAIN 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,unrestrictedSQL 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-postgresClaude 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 with table counts and sizes | readonly |
| List tables with comments, row counts, sizes | readonly |
| Full table description with columns, indexes, FKs, comments | readonly |
| Search objects by name or comment | readonly |
| Execute SELECT queries | readonly |
| Execute INSERT/UPDATE/DELETE/CREATE/etc | varies |
| EXPLAIN (ANALYZE) with readable output | readonly* |
| Table statistics, bloat, vacuum info | readonly |
| Index usage, unused indexes, missing index hints | readonly |
| 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 |
| Full DDL for a schema (CREATE TABLE statements with comments) |
| Installed PostgreSQL extensions |
Prompts
Prompt | Description |
| Guided database exploration — schemas, tables, relationships |
| Analyze a slow query with EXPLAIN, indexes, and recommendations |
| 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 stderrEnvironment Variables
Variable | Description |
| PostgreSQL connection URL |
| Alternative connection URL |
| Access level override |
| Row limit override |
Access Levels
Level | SELECT | INSERT/UPDATE/DELETE | CREATE/ALTER/DROP | TRUNCATE/DROP DATABASE |
| yes | no | no | no |
| yes | yes | no | no |
| yes | yes | yes | no |
| 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 loggingDevelopment
npm install
npm test # run tests
npm run build # compile TypeScript
npm run dev -- --connection-string "postgres://..." # run in dev modeLicense
MIT
This server cannot be installed
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Latest Blog Posts
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/divinedev111/mcp-postgres'
If you have feedback or need assistance with the MCP directory API, please join our Discord server