postgres-mcp
Provides tools to execute queries, explore database structure (tables, columns, indexes, foreign keys), analyze query performance with EXPLAIN plans, and retrieve full schema context for PostgreSQL databases.
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., "@postgres-mcplist all tables in the public schema"
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.
postgres-mcp
!!! DO NOT USE IN PRODUCTION !!! !!! ONLY FOR LOCAL DEV & DB !!!
A PostgreSQL MCP (Model Context Protocol) server that enables Claude to safely interact with PostgreSQL databases via stdio/JSON-RPC 2.0.
Features
Execute queries — run parameterized SELECT statements with row limits
Explore structure — list tables/views and inspect columns, indexes, foreign keys
Analyze performance — generate EXPLAIN / EXPLAIN ANALYZE plans
Schema context — expose full DDL-like schema as an MCP resource
Security — read-only mode, schema whitelisting, SQL injection prevention
Related MCP server: PostgreSQL MCP Server
Requirements
Node.js ≥ 18 (ESM)
PostgreSQL database
Installation
npm installConfiguration
Copy .env.example to .env and fill in your values:
POSTGRES_URL=postgresql://user:password@localhost:5432/dbname
POSTGRES_SCHEMAS=public
POSTGRES_READONLY=true
POSTGRES_QUERY_TIMEOUT=30000
POSTGRES_MAX_ROWS=500Variable | Default | Description |
| (required) | PostgreSQL connection string |
|
| Comma-separated list of allowed schemas |
|
| Block any write statement |
|
| Query timeout in milliseconds |
|
| Maximum rows returned per query |
Usage
Integration with Claude Code
The server is designed to be run via npx directly from its local directory — no global install needed.
Add it to your Claude Code config (~/.claude/settings.json for global use, or .claude/settings.json at project level):
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["/absolute/path/to/postgres_mcp"],
"env": {
"POSTGRES_URL": "postgresql://user:password@localhost:5432/dbname",
"POSTGRES_SCHEMAS": "public",
"POSTGRES_READONLY": "true"
}
}
}
}Replace /absolute/path/to/postgres_mcp with the actual path to this repository on your machine. All configuration is passed via env — no .env file required when using this approach.
Note:
npxrunsnpm installautomatically on first launch ifnode_modulesis absent.
Testing with MCP Inspector
The MCP Inspector provides a web UI to test the server interactively without Claude Code.
POSTGRES_URL=postgresql://user:password@localhost:5432/dbname \
npx @modelcontextprotocol/inspector npx /absolute/path/to/postgres_mcpThen open http://localhost:5173 in your browser. From there you can:
Call any tool with custom parameters and see the response
Read the
postgres://schemaresourceInspect the raw JSON-RPC messages exchanged
Tools
query
Execute a SQL SELECT query.
{
"sql": "SELECT * FROM users WHERE id = $1",
"params": [42]
}Returns: rows, rowCount, fields, truncated (if row limit was hit).
list_tables
List all accessible tables and views.
Returns an array of { schema, name, type } objects.
describe_table
Get the full structure of a table: columns, indexes, and foreign keys.
{
"table": "users",
"schema": "public"
}Returns: columns (name, type, nullable, default, primary_key), indexes, foreign_keys.
explain_query
Generate an execution plan for a SQL query.
{
"sql": "SELECT * FROM orders WHERE user_id = 1",
"analyze": false
}Set analyze: true to run EXPLAIN ANALYZE (actually executes the query).
Resource
postgres://schema
Auto-loaded resource exposing the full database schema in DDL-like format. Gives Claude upfront context about all accessible tables and their columns.
Security
Read-only mode (default): blocks INSERT, UPDATE, DELETE, DROP, CREATE, ALTER, TRUNCATE, GRANT, REVOKE, and other write operations via regex validation
Schema isolation: queries are restricted to schemas listed in
POSTGRES_SCHEMASParameterized queries: prevents SQL injection
Connection pooling: max 5 connections with idle/connection timeouts
Row and time limits: configurable caps prevent runaway queries
Tests
npm testTests use Vitest with mocked database calls — no live database required.
Project Structure
src/
├── index.js # MCP server entry point
├── db.js # Connection pool & configuration
├── security.js # SQL and schema validation
├── tools/
│ ├── query.js
│ ├── list_tables.js
│ ├── describe_table.js
│ └── explain_query.js
└── resources/
└── schema.js
tests/
├── security.test.js
├── query.test.js
├── list_tables.test.js
├── describe_table.test.js
├── explain_query.test.js
└── schema_resource.test.jsThis server cannot be installed
Maintenance
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
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
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/nicolas-canfrere/postgres-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server