Set up PostgreSQL MCP in Claude Code
Query and interact with PostgreSQL databases, inspect schemas, and run SQL directly from your AI editor.
1
Locate the config file
Claude Code reads MCP server configuration from the following file:
~/.claude.jsonCreate this file if it does not already exist.
2
Add the configuration
You can configure PostgreSQL MCP using the CLI command or by editing the config file directly.
Option A: CLI command
bash
claude mcp add postgres-mcp -e POSTGRES_CONNECTION_STRING=YOUR_POSTGRES_CONNECTION_STRING -- npx -y @modelcontextprotocol/server-postgres postgresql://localhost/mydbOption B: Config file
json
{
"mcpServers": {
"postgres-mcp": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://localhost/mydb"
],
"env": {
"POSTGRES_CONNECTION_STRING": "YOUR_POSTGRES_CONNECTION_STRING"
}
}
}
}3
Set up environment variables
PostgreSQL MCP requires the following environment variables:
POSTGRES_CONNECTION_STRINGRequiredPostgreSQL connection string (e.g. postgresql://user:pass@localhost:5432/mydb)
4
Verify it works
Restart Claude Code. PostgreSQL MCP should appear in your MCP server list with 8 tools available.
This server adds approximately 4,120 tokens to your context window for tool definitions.