Query PostgreSQL Databases with Claude
MCP server for read-only PostgreSQL access - inspect table schemas and run SELECT-only queries via a single query tool, safely inside a read-only transaction.
1.0.0Add to Favorites
Why it matters
Enable Claude to directly query PostgreSQL databases for data analysis and schema inspection. Access and understand your database content without manual SQL writing.
Outcomes
What it gets done
Execute read-only SQL queries on PostgreSQL.
List all tables within the database.
Retrieve schema information for specific tables.
Analyze database content by generating and executing SQL.
Source
Get it from source
Spark does not host a copy of it.
Open sourceReports
Agent outcome reports
No reports yet
Capabilities
Tools your agent gets
Execute read-only SQL queries against the connected PostgreSQL database
Overview
PostgreSQL MCP
An MCP server exposing a single read-only query tool and per-table schema resources for a PostgreSQL database, with every query run inside a read-only transaction. Use when an LLM needs to explore a PostgreSQL schema or answer questions from live data with no risk of writes - not for anything requiring modification.
What it does
This MCP server gives an LLM read-only access to a PostgreSQL database: it exposes a single tool, query, which executes a SQL statement inside a READ ONLY transaction and takes one input, sql (the query string). It also exposes each table's schema as a resource at postgres://<host>/<table>/schema - column names and data types, auto-discovered from the database's own metadata - so a client can inspect structure before writing a query.
When to use - and when NOT to
Use it when an LLM needs to inspect a PostgreSQL database's schema and run read-only SQL against it - answering questions from live data, exploring an unfamiliar schema, or building reports - without any risk of the model modifying data, since every query runs inside a read-only transaction. Not suited to anything requiring writes; this server has no write tool at all.
Inputs and outputs
Input: a connection string in postgresql://user:pass@host:port/db-name format, supplied once at server start (as a command-line argument or, in VS Code's variant, an interactively-prompted pg_url input). Output: schema JSON for the postgres://<host>/<table>/schema resources, and query results from the query tool.
How to install
Via Docker:
docker run -i --rm mcp/postgres postgresql://host.docker.internal:5432/mydb
Or via npx:
npx -y @modelcontextprotocol/server-postgres postgresql://localhost/mydb
Both are added under mcpServers in claude_desktop_config.json. VS Code has one-click install buttons for the npx and Docker variants, or manual setup via a .vscode/mcp.json workspace file using an ${input:pg_url} prompt instead of a hardcoded URL. When connecting to a PostgreSQL server on the host machine from inside Docker, use host.docker.internal in place of localhost. The image can also be built locally with docker build -t mcp/postgres -f src/postgres/Dockerfile .. The server itself is MIT-licensed.
Who it's for
Developers and analysts who want an LLM to explore a PostgreSQL database's schema and answer questions from live data conversationally, with the read-only transaction boundary as a safety guarantee against accidental writes.
Source README
PostgreSQL
A Model Context Protocol server that provides read-only access to PostgreSQL databases. This server enables LLMs to inspect database schemas and execute read-only queries.
Components
Tools
- query
- Execute read-only SQL queries against the connected database
- Input:
sql(string): The SQL query to execute - All queries are executed within a READ ONLY transaction
Resources
The server provides schema information for each table in the database:
- Table Schemas (
postgres://<host>/<table>/schema)- JSON schema information for each table
- Includes column names and data types
- Automatically discovered from database metadata
Configuration
Usage with Claude Desktop
To use this server with the Claude Desktop app, add the following configuration to the "mcpServers" section of your claude_desktop_config.json:
Docker
- when running docker on macos, use host.docker.internal if the server is running on the host network (eg localhost)
- username/password can be added to the postgresql url with
postgresql://user:password@host:port/db-name
{
"mcpServers": {
"postgres": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mcp/postgres",
"postgresql://host.docker.internal:5432/mydb"]
}
}
}
NPX
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://localhost/mydb"
]
}
}
}
Replace /mydb with your database name.
Usage with VS Code
For quick installation, use one of the one-click install buttons below...
For manual installation, add the following JSON block to your User Settings (JSON) file in VS Code. You can do this by pressing Ctrl + Shift + P and typing Preferences: Open User Settings (JSON).
Optionally, you can add it to a file called .vscode/mcp.json in your workspace. This will allow you to share the configuration with others.
Note that the
mcpkey is not needed in the.vscode/mcp.jsonfile.
Docker
Note: When using Docker and connecting to a PostgreSQL server on your host machine, use host.docker.internal instead of localhost in the connection URL.
{
"mcp": {
"inputs": [
{
"type": "promptString",
"id": "pg_url",
"description": "PostgreSQL URL (e.g. postgresql://user:pass@host.docker.internal:5432/mydb)"
}
],
"servers": {
"postgres": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mcp/postgres",
"${input:pg_url}"
]
}
}
}
}
NPX
{
"mcp": {
"inputs": [
{
"type": "promptString",
"id": "pg_url",
"description": "PostgreSQL URL (e.g. postgresql://user:pass@localhost:5432/mydb)"
}
],
"servers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"${input:pg_url}"
]
}
}
}
}
Building
Docker:
docker build -t mcp/postgres -f src/postgres/Dockerfile .
FAQ
Common questions
Trust
How it checks out
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.