Query PostgreSQL Databases with Claude
Archived official MCP reference server giving Claude read-only PostgreSQL access for schema inspection and SELECT-only queries.
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.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/vb-postgres | bash Capabilities
Tools your agent gets
Execute a read-only SQL query against the PostgreSQL database.
List all tables available in the connected PostgreSQL database.
Get schema information and column details for a specific table.
Overview
PostgreSQL MCP
This MCP server gives Claude read-only PostgreSQL access - schema inspection, table listing, and SELECT-only query execution. Use it when you want Claude to explore and analyze data in an existing PostgreSQL database without any risk of writing to it.
What it does
The PostgreSQL MCP server gives Claude read-only access to PostgreSQL databases for data analysis and schema inspection. It exposes three tools: query (executes a read-only SQL query, restricted to SELECT statements for safety), list_tables (lists all tables in the database), and describe_table (returns column and schema information for a named table). It also exposes the database schema as resources - a full-schema resource and a per-table schema resource - so Claude can inspect structure without running a query first.
When to use - and when NOT to
Use it when you need Claude to analyze data or inspect schema in an existing PostgreSQL database without risk of writing to it. It is not for write operations: only SELECT queries are permitted, and the security guidance recommends connecting through a read-replica in production and limiting accessible schemas rather than granting broad access. This server is one of the official Model Context Protocol reference implementations, now maintained in the modelcontextprotocol/servers-archived repository, which is archived and no longer receiving updates.
Capabilities
Given a natural-language request, Claude inspects the relevant table schema, writes an appropriate SQL query, executes it through the query tool, and presents the results - for example, analyzing a users table to show signup distribution by month. The list_tables and describe_table tools let Claude discover database structure on its own before writing a query, rather than requiring the schema to be provided up front. The server also exposes the schema directly as resources - postgres://schema for the full database schema and postgres://tables/{table_name} for an individual table's schema - so Claude can pull structure without invoking a tool call.
How to install
Install globally via npm install -g @modelcontextprotocol/server-postgres, then add the server to your Claude Code MCP configuration with the connection string as an argument to npx.
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://user:password@localhost:5432/database"
]
}
}
}
Use a connection string with appropriately scoped credentials - read-only access is the default behavior.
Who it's for
Developers and analysts who want Claude to explore and query a PostgreSQL database directly - inspecting schema, running ad hoc SELECT queries, and summarizing results - without any risk of the connector writing to the database.
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
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.