Postgres MCP
Provides a structured, safe interface for PostgreSQL databases, enabling read-only schema introspection, query execution (read-only by default), and optional write operations via explicit opt-in.
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 database"
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.
English | π PortuguΓͺs
β¨ What it does
Most LLMs interact with databases by guessing - assuming table names, inventing column names, and writing queries that may fail or expose sensitive data. Postgres MCP solves this by giving the LLM a structured, safe interface to actually understand the database before touching it.
Built with @modelcontextprotocol/sdk and pg, it provides:
π§ Semantic knowledge graph - the LLM gets a complete map of schemas, tables, columns, foreign keys, inferred relations, risk levels, and business domains - built from the real schema, not invented
π‘οΈ Read-only by default - no writes, no DDL, no arbitrary SQL unless you explicitly opt in;
pg_classify_query_risklets the LLM check a query's safety before running itπ Runtime credential resolution - credentials are read from
.envat startup; nothing sensitive lives inmcp.jsonπ― Explicit tool selection - every tool is opt-in via
tool=<name>args, so the LLM only sees what you choose to expose
Related MCP server: mcp-enterprise-starter
π Requirements
βοΈ Node.js >= 18
π A
.envfile with database credentials (anywhere in the project tree - see .env Discovery)
π Installation
There are two ways to use this package. Choose the one that best fits your workflow.
Option 1 - No installation (via npx, recommended for quick start)
No installation needed. npx downloads and runs the package on demand. Add -y as the first argument to skip the confirmation prompt.
{
"servers": {
"Postgres Tools": {
"type": "stdio",
"command": "npx",
"args": [
"-y",
"@edelciomolina/postgres-mcp"
],
"env": {
"MCP_KEY_HOST": "DB_HOST",
"MCP_KEY_PORT": "DB_PORT",
"MCP_KEY_NAME": "DB_NAME",
"MCP_KEY_SSLMODE": "DB_SSLMODE",
"MCP_KEY_USER": "DB_USER",
"MCP_KEY_PASS": "DB_PASS"
}
}
}
}This starts the server with the default read-only tool set - no tool= arguments needed. To enable write-capable tools, see Write-capable tools.
π‘ Using Supabase, Neon, Railway or another platform that only provides a connection string? Use
MCP_KEY_URLpointing toDATABASE_URL(or whatever variable name the platform uses). The server will prioritize the URL and ignore the individual variables. See Connection via URL.
Option 2 - Install via VS Code (MCP extension marketplace)
VS Code supports discovering and installing MCP servers directly in the editor, without using the terminal.
Open the Command Palette (Cmd+Shift+P on Mac / Ctrl+Shift+P on Windows/Linux)
Run
MCP: Add ServerChoose "Browse MCP Servers" (or "From registry", depending on your VS Code version)
Search for
postgres-mcporedelciomolinaSelect Postgres MCP and follow the instructions - VS Code will add the entry to your
mcp.jsonautomatically
π‘ You can also open the MCP Servers panel via Copilot chat icon β Manage MCP Servers to browse, enable, or disable servers at any time.
After installing, edit the generated entry in .vscode/mcp.json to add your tool= arguments and env key mappings as shown in the Usage section below.
π Usage in VS Code (mcp.json)
Read-only (default - no tool= arguments needed):
{
"servers": {
"Postgres Tools": {
"type": "stdio",
"command": "npx",
"args": ["@edelciomolina/postgres-mcp"],
"env": {
"MCP_KEY_HOST": "DB_HOST",
"MCP_KEY_PORT": "DB_PORT",
"MCP_KEY_NAME": "DB_NAME",
"MCP_KEY_SSLMODE": "DB_SSLMODE",
"MCP_KEY_USER": "DB_USER",
"MCP_KEY_PASS": "DB_PASS"
}
}
}
}With write tools (explicit opt-in required):
{
"servers": {
"Postgres Tools": {
"type": "stdio",
"command": "npx",
"args": [
"@edelciomolina/postgres-mcp",
"tool=pg_manage_schema",
"tool=pg_manage_indexes"
],
"env": {
"POSTGRES_MCP_ALLOW_WRITE": "true",
"MCP_KEY_HOST": "DB_HOST",
"MCP_KEY_PORT": "DB_PORT",
"MCP_KEY_NAME": "DB_NAME",
"MCP_KEY_SSLMODE": "DB_SSLMODE",
"MCP_KEY_USER": "DB_USER",
"MCP_KEY_PASS": "DB_PASS"
}
}
}
}β οΈ Write-capable tools require
POSTGRES_MCP_ALLOW_WRITE=trueinenv. Without it, the server exits at startup.
The corresponding .env file at the root of your project:
DB_HOST=db.your-project.supabase.co
DB_PORT=5432
DB_NAME=postgres
DB_SSLMODE=require
DB_USER=readonly_user
DB_PASS=your_passwordβοΈ How mcp.json configuration works
ποΈ env - credential key mapping
The env block does not contain the actual credentials. It maps each MCP_KEY_* to the variable name in your .env file.
Key in | Points to | Example value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Priority: when
MCP_KEY_URL(orDATABASE_URL) is present, the server uses the URL directly and ignores the individual credential keys.
This indirection lets you use any variable name in your .env - useful when sharing a .env across multiple services with different naming conventions.
π§ args - tool selection via tool= prefix
Each enabled MCP tool is declared as a separate argument in the format tool=<name>:
"args": [
"-y",
"@edelciomolina/postgres-mcp",
"tool=pg_manage_schema",
"tool=pg_manage_indexes"
]This makes the tool list explicit and auditable directly in mcp.json - no hidden configuration files. π
π Connection via URL (DATABASE_URL)
In addition to individual credentials, you can provide a full connection string - the standard format on platforms like Supabase, Neon, and Railway.
.env:
DATABASE_URL=postgresql://user:password@host:5432/database?sslmode=requiremcp.json:
{
"servers": {
"Postgres Tools": {
"type": "stdio",
"command": "npx",
"args": ["-y", "@edelciomolina/postgres-mcp"],
"env": {
"MCP_KEY_URL": "DATABASE_URL"
}
}
}
}The variable mapped by MCP_KEY_URL has priority over the other keys (MCP_KEY_HOST, MCP_KEY_PORT, etc.). If the URL is present, the other variables are ignored.
If the platform uses a different name (e.g. DB_URL), just adjust the mapping:
"MCP_KEY_URL": "DB_URL"π‘οΈ Why read-only is the default
If you omit all tool= arguments, the server starts with a curated read-only set - all tools that can retrieve, analyze, or explain data, but nothing that can modify it.
β Included in defaults (read-only):
pg_execute_query pg_manage_query pg_inspect_schema
pg_get_setup_instructions pg_analyze_database
pg_monitor_database pg_debug_database
pg_inspect_database_graph pg_describe_table_semantics
pg_find_related_tables pg_classify_query_riskπ‘
pg_execute_queryrejectsINSERT,UPDATE,DELETE, DDL,ANALYZE,VACUUM,EXPLAIN ANALYZEand other write/maintenance commands before the database is queried.
π‘
pg_inspect_schemaprovides read-only schema introspection (get_info,get_enums). For DDL operations, usepg_manage_schemawith explicit opt-in.
β οΈ Excluded from defaults - require tool= argument AND POSTGRES_MCP_ALLOW_WRITE=true:
Tool | Operations |
| CREATE TABLE, ALTER TABLE, CREATE TYPE |
| CREATE INDEX, DROP INDEX, REINDEX |
| ADD CONSTRAINT, DROP CONSTRAINT |
| CREATE FUNCTION, DROP FUNCTION |
| CREATE TRIGGER, DROP TRIGGER, enable/disable |
| ENABLE/DISABLE RLS, CREATE/ALTER/DROP POLICY |
| CREATE/DROP/ALTER USER, GRANT, REVOKE |
| INSERT / UPDATE / DELETE / UPSERT |
| Arbitrary SQL with transaction support |
π .env file discovery
The server resolves the .env file in this order:
env-file=<path>argument - explicit path relative tocwd; takes priority over everythingUpward search - starting from
cwd, searches each parent directory until a.envis found or the filesystem root is reached
If no .env is found, the server exits with a clear error message.
Monorepos and subfolders
When VS Code starts the MCP process, cwd is typically the workspace root. If your .env is in a subfolder (e.g. functions/.env), use env-file= to point to it explicitly:
{
"servers": {
"Postgres Tools": {
"type": "stdio",
"command": "npx",
"args": [
"-y",
"@edelciomolina/postgres-mcp",
"env-file=functions/.env"
],
"env": {
"MCP_KEY_HOST": "DB_HOST",
"MCP_KEY_PORT": "DB_PORT",
"MCP_KEY_NAME": "DB_NAME",
"MCP_KEY_SSLMODE": "DB_SSLMODE",
"MCP_KEY_USER": "DB_USER",
"MCP_KEY_PASS": "DB_PASS"
}
}
}
}π‘ The upward search behavior handles the common case automatically. Use
env-file=when you need explicit control (CI, monorepos, Docker bind-mounts).
π§° Available tools
Read-only (enabled by default)
Tool | Description |
| SELECT / COUNT / EXISTS with write and multi-statement guards |
| EXPLAIN plans, slow query analysis, |
| Schema info and ENUM types (read-only introspection) |
| Setup instructions per platform |
| Performance, configuration, and storage analysis |
| Real-time monitoring of connections, queries, locks, and replication |
| Diagnose connections, locks, performance, and replication |
| Build a full knowledge graph of the database: schemas, tables, columns, FKs, indexes, inferred relations, and business domains |
| Describe a table with semantic context: risk level, column roles, sensitive columns, and related tables |
| Find tables related to a given table via explicit FKs and inferred naming patterns, with path explanation |
| Classify query risk ( |
Write-capable (opt-in via tool= argument + POSTGRES_MCP_ALLOW_WRITE=true)
Tool | Description |
| Schema info, create/alter tables, manage ENUMs |
| List, create, drop, reindex, analyze index usage |
| List, create, and drop constraints and foreign keys |
| List, create, and drop functions and procedures |
| List, create, drop, enable/disable triggers |
| Row-Level Security policies |
| User permissions, create/drop/alter users, grant/revoke |
| INSERT / UPDATE / DELETE / UPSERT with parameterized queries |
| Arbitrary SQL execution with optional transaction support |
π§ Semantic Layer
The four pg_*_graph / pg_*_semantics / pg_*_risk tools build an in-memory knowledge graph of your database at runtime. This gives the LLM a structured map - schemas, tables, columns, foreign keys, inferred relations, risk levels, and business domains - without executing any query against your data.
All inferred fields (column semantic roles, table probable types, inferred relations) are clearly tagged so the LLM knows to treat them as hints, not schema facts.
Optional configuration (mcp-config.json)
Place a mcp-config.json file beside your .env to tune the semantic layer and security limits. All fields are optional - omitting the file applies safe defaults.
{
"security": {
"defaultLimit": 100,
"maxLimit": 1000,
"blockedSchemas": ["pg_catalog", "information_schema"],
"blockedTables": [],
"requireLimit": true
},
"semanticLayer": {
"enabled": true,
"inferRelationsWithoutForeignKeys": true,
"inferBusinessEntities": true,
"sensitiveKeywords": ["password", "secret", "token", "api_key", "ssn", "hash"]
}
}ποΈ Architecture
For a detailed view of the communication flow between the MCP client, the proxy, and PostgreSQL - including the full sequence diagram - see ARCHITECT.md.
π License
MIT Β© Edelcio Molina
This 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.
Related MCP Servers
- Alicense-qualityCmaintenanceA production-ready MCP server that connects AI assistants to any PostgreSQL database with 25 tools and role-based access control.Last updated24MIT
- AlicenseAqualityDmaintenanceA production-grade MCP server that gives AI agents safe, authenticated access to a PostgreSQL database.Last updated3MIT
- Alicense-qualityBmaintenanceA read-only MCP server for PostgreSQL that enables safe database introspection and querying via natural language.Last updated668MIT
- Flicense-qualityDmaintenanceA Model Context Protocol (MCP) server that provides database operations as tools for LLM-powered applications. Supports PostgreSQL, MySQL, and MongoDB databases with connection pooling and flexible result formatting.Last updated
Related MCP Connectors
MCP server for managing Prisma Postgres.
MCP server for interacting with the Supabase platform
GibsonAI MCP server: manage your databases with natural language
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- 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/edelciomolina/postgres-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server