Claude Desktop Setup
This server can be used with Claude Desktop via the Model Context Protocol (MCP).
See also: claude_config_example.json in the repository.
Quick Setup
Option 1: Using npx (Recommended)
Add to your Claude Desktop configuration file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@calebmabry/postgres-mcp-server"
],
"env": {
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DB_USER": "your_username",
"DB_PASSWORD": "your_password",
"DB_NAME": "your_database",
"DB_SSL": "false",
"READ_ONLY": "true"
}
}
}
}
Option 2: Using Local Installation
If you've cloned this repository:
{
"mcpServers": {
"postgres": {
"command": "node",
"args": [
"/absolute/path/to/postgres-mcp-server/dist/index.js"
],
"env": {
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DB_USER": "your_username",
"DB_PASSWORD": "your_password",
"DB_NAME": "your_database",
"DB_SSL": "false",
"READ_ONLY": "true"
}
}
}
}
Environment Variables
Required
DB_HOST- PostgreSQL host (default: localhost)DB_PORT- PostgreSQL port (default: 5432)DB_USER- Database userDB_PASSWORD- Database passwordDB_NAME- Database name
Optional
DB_SSL- Enable SSL connection (default: false)READ_ONLY- Restrict to SELECT queries only (default: true)AUTO_LIMIT- Automatically add LIMIT to queries without one (default: true)DEFAULT_PAGE_SIZE- Default rows per page (default: 100)MAX_PAGE_SIZE- Maximum allowed page size (default: 500)
Security Notes
Read-Only Mode (Recommended)
When READ_ONLY=true (default):
- Only
SELECT,WITH(CTEs), andEXPLAINqueries are allowed - All write operations (
INSERT,UPDATE,DELETE) are blocked - Schema modifications (
CREATE,ALTER,DROP) are blocked
Write Mode
When READ_ONLY=false:
UPDATEandDELETErequireWHEREclauses- Prevents accidental mass updates/deletes
- Still blocks dangerous operations like
DROP,TRUNCATE
Available Tools
Once configured, Claude Desktop will have access to these PostgreSQL tools:
- query - Execute SQL queries with pagination
- describe_table - Get table structure and column details
- list_tables - List all tables in a schema
- list_schemas - List all database schemas
- list_indexes - List table or schema indexes
- list_views - List database views
- list_functions - List functions and procedures
- get_constraints - Get table constraints
- explain_query - Get query execution plan
- get_table_stats - Get table statistics and size
Troubleshooting
Server Not Starting
-
Check Claude Desktop logs:
- macOS:
~/Library/Logs/Claude/mcp*.log - Windows:
%APPDATA%\Claude\logs\mcp*.log
- macOS:
-
Verify database connection:
psql -h localhost -U your_username -d your_database -
Test the server manually:
npx -y @calebmabry/postgres-mcp-server
Permission Issues
Ensure your database user has appropriate permissions:
-- For read-only access
GRANT CONNECT ON DATABASE your_database TO your_username;
GRANT USAGE ON SCHEMA public TO your_username;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO your_username;
-- For write access (if READ_ONLY=false)
GRANT INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO your_username;
Connection Errors
- Verify PostgreSQL is running:
pg_isready -h localhost - Check firewall settings
- Confirm database credentials
- For SSL issues, set
DB_SSL=trueorDB_SSL=falseexplicitly
Example Usage in Claude
Once configured, you can ask Claude things like:
- "What tables are in the database?"
- "Show me the structure of the users table"
- "Run a query to find all active users"
- "Explain the performance of this query: SELECT * FROM orders WHERE created_at > '2024-01-01'"
- "What indexes exist on the products table?"
Advanced Configuration
Custom Port or SSL
{
"env": {
"DB_HOST": "db.example.com",
"DB_PORT": "5433",
"DB_SSL": "true",
"DB_USER": "readonly_user",
"DB_PASSWORD": "secure_password",
"DB_NAME": "production_db",
"READ_ONLY": "true",
"MAX_PAGE_SIZE": "1000"
}
}
Multiple Databases
You can configure multiple PostgreSQL connections:
{
"mcpServers": {
"postgres-prod": {
"command": "npx",
"args": ["-y", "@calebmabry/postgres-mcp-server"],
"env": {
"DB_NAME": "production",
"READ_ONLY": "true"
}
},
"postgres-dev": {
"command": "npx",
"args": ["-y", "@calebmabry/postgres-mcp-server"],
"env": {
"DB_NAME": "development",
"READ_ONLY": "false"
}
}
}
}
Restart Required
After modifying the configuration, restart Claude Desktop for changes to take effect.