mcp-postgres
Read-only PostgreSQL MCP server that enables running SELECT queries, listing tables and schemas, and describing columns, with built-in protection against writes and malicious SQL attacks.
README
mcp-postgres
Read-only PostgreSQL MCP server for Claude Desktop (and any MCP-compatible client).
Tools exposed
| Tool | Description |
|---|---|
query |
Run a SELECT query |
list_tables |
List all user tables with row-count estimate and size |
describe_table |
Show columns, types, nullability, PK for a table |
list_schemas |
List user-defined schemas |
Security protections
| Attack | Example | Status |
|---|---|---|
| Direct writes | DROP, DELETE, UPDATE, INSERT, COPY |
✅ Blocked |
| Multiple statements | SELECT 1; DROP TABLE t |
✅ Blocked |
| System functions | pg_read_file, pg_sleep, dblink |
✅ Blocked |
| DoS | pg_sleep(10), GENERATE_SERIES(1,99999999) |
✅ Blocked |
| Dollar-quoted bypass | $$DROP TABLE t$$ |
✅ Blocked |
| Whitespace bypass | tabs and newlines between keywords | ✅ Blocked |
| Comment bypass | /* */ and -- before keywords |
✅ Blocked |
| Session write mode | Enforced via READ ONLY transaction |
✅ Enforced |
| Valid queries | SELECT, SELECT ... LIMIT, COUNT, WHERE |
✅ Allowed |
Environment variables
PostgreSQL connection
| Variable | Required | Default | Description |
|---|---|---|---|
PG_HOST |
✅ | — | Server hostname or IP |
PG_DATABASE |
✅ | — | Database name |
PG_USER |
✅ | — | PostgreSQL username |
PG_PASSWORD |
✅ | — | PostgreSQL password |
PG_PORT |
5432 |
TCP port | |
PG_SSL |
false |
Enable SSL (true/false) |
|
PG_SSL_REJECT_UNAUTHORIZED |
true |
Reject self-signed certs |
SSH tunnel (optional)
Set PG_SSH_HOST to activate the tunnel. Authentication requires either a private key file or a password.
| Variable | Required | Default | Description |
|---|---|---|---|
PG_SSH_HOST |
— | — | SSH server hostname (activates tunnel when set) |
PG_SSH_PORT |
22 |
SSH server port | |
PG_SSH_USER |
✅ (if tunnel) | — | SSH username |
PG_SSH_PRIVATE_KEY |
✅ or password | — | Path to private key file (e.g. ~/.ssh/id_rsa) |
PG_SSH_KEY_PASSPHRASE |
— | Passphrase for the private key (if encrypted) | |
PG_SSH_PASSWORD |
✅ or key | — | SSH password (used when no key file is provided) |
PG_SSH_REMOTE_HOST |
PG_HOST |
Postgres host as seen from the SSH server |
Build
npm install
npm run build
Claude Desktop configuration
Add to %APPDATA%\Claude\claude_desktop_config.json:
Conexão direta:
{
"mcpServers": {
"postgres": {
"command": "node",
"args": ["C:/Projetos/mcp-postgres-readonly/dist/index.js"],
"env": {
"PG_HOST": "localhost",
"PG_DATABASE": "mydb",
"PG_USER": "postgres",
"PG_PASSWORD": "yourpassword"
}
}
}
}
Conexão direta com SSL (certificado auto-assinado):
{
"mcpServers": {
"postgres": {
"command": "node",
"args": ["C:/Projetos/mcp-postgres-readonly/dist/index.js"],
"env": {
"PG_HOST": "db.example.com",
"PG_DATABASE": "mydb",
"PG_USER": "postgres",
"PG_PASSWORD": "yourpassword",
"PG_SSL": "true",
"PG_SSL_REJECT_UNAUTHORIZED": "false"
}
}
}
}
Via SSH tunnel (private key):
{
"mcpServers": {
"postgres": {
"command": "node",
"args": ["C:/Projetos/mcp-postgres-readonly/dist/index.js"],
"env": {
"PG_HOST": "localhost",
"PG_DATABASE": "mydb",
"PG_USER": "postgres",
"PG_PASSWORD": "yourpassword",
"PG_SSH_HOST": "bastion.example.com",
"PG_SSH_USER": "ubuntu",
"PG_SSH_PRIVATE_KEY": "C:/Users/you/.ssh/id_rsa"
}
}
}
}
Via SSH tunnel (password):
{
"mcpServers": {
"postgres": {
"command": "node",
"args": ["C:/Projetos/mcp-postgres-readonly/dist/index.js"],
"env": {
"PG_HOST": "localhost",
"PG_DATABASE": "mydb",
"PG_USER": "postgres",
"PG_PASSWORD": "yourpassword",
"PG_SSH_HOST": "bastion.example.com",
"PG_SSH_USER": "ubuntu",
"PG_SSH_PASSWORD": "sshpassword"
}
}
}
}
Tip: When
PG_SSH_REMOTE_HOSTis omitted, the tunnel forwards toPG_HOSTas seen from the SSH server. If Postgres runs on a private address only reachable from the bastion (e.g.db.internal), setPG_SSH_REMOTE_HOST=db.internalandPG_HOSTto anything (it will be overridden).
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.