Execute read-only SQL queries against the connected database
The PostgreSQL MCP server provides a secure interface for LLMs to interact with PostgreSQL databases in a read-only capacity. It enables AI assistants to inspect database schemas and execute SQL queries without modifying data, making it ideal for data analysis and exploration tasks. This server automatically discovers and exposes table schemas from your connected database, allowing LLMs to understand your data structure. All queries are executed within READ ONLY transactions, ensuring data integrity while providing valuable insights from your PostgreSQL databases.
The PostgreSQL MCP server creates a bridge between AI assistants and your PostgreSQL databases, allowing for safe, read-only access to your data. This enables AI models to help with data analysis, query optimization, and database exploration without risking data modifications.
You can run the PostgreSQL MCP server using either Docker or NPX. Choose the method that best fits your environment.
To use this server with Claude Desktop, add the following configuration to the "mcpServers" section of your claude_desktop_config.json
:
{
"mcpServers": {
"postgres": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mcp/postgres",
"postgresql://host.docker.internal:5432/mydb"
]
}
}
}
Note for macOS users: When running Docker on macOS, use
host.docker.internal
instead oflocalhost
if the PostgreSQL server is running on your host machine.
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://localhost:5432/mydb"
]
}
}
}
For VS Code integration, you can add the configuration to your User Settings (JSON) file or to a .vscode/mcp.json
file in your workspace.
{
"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}"
]
}
}
}
}
{
"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}"
]
}
}
}
}
The PostgreSQL connection URL follows this format:
postgresql://[user[:password]@][host][:port][/database]
Examples:
postgresql://localhost:5432/mydb
(default user, no password)postgresql://user:password@localhost:5432/mydb
(with credentials)postgresql://host.docker.internal:5432/mydb
(for Docker on macOS)Once configured, the PostgreSQL MCP server provides:
Table Schema Information: The server automatically discovers and exposes schema information for all tables in your database, including column names and data types.
Read-Only Query Execution: You can execute SQL queries against your database. All queries are executed within a READ ONLY transaction to ensure data safety.
If you prefer to build the Docker image yourself:
docker build -t mcp/postgres -f src/postgres/Dockerfile .