BACK TO DIRECTORY
mcpintermediate

PostgreSQL

Provides Claude Code with read-only or read-write access to a PostgreSQL database. Enables running SQL queries, inspecting table schemas, viewing indexes, analyzing query plans, and browsing data. Essential for database-driven development workflows where Claude needs to understand your data model and write accurate queries.

1.5k STARS
21.2k DOWNLOADS
claude-templates
postgressqldatabasequeriesschema

INSTALL COMMAND

npx @anthropic-ai/claude-code mcp add postgres -- npx -y @modelcontextprotocol/server-postgres postgresql://user:pass@localhost:5432/dbname

CONFIGURATION

json
1{
2 "mcpServers": {
3 "postgres": {
4 "command": "npx",
5 "args": [
6 "-y",
7 "@modelcontextprotocol/server-postgres",
8 "postgresql://user:password@localhost:5432/mydb"
9 ]
10 }
11 }
12}
13
14// Connection string format:
15// postgresql://[user]:[password]@[host]:[port]/[database]?[params]
16//
17// For SSL connections:
18// postgresql://user:pass@host:5432/db?sslmode=require
19//
20// Available tools:
21// - query: Execute a SQL query (SELECT, INSERT, UPDATE, DELETE)
22// - describe_table: Get table schema (columns, types, constraints)
23// - list_tables: List all tables in the database
24// - get_indexes: List indexes for a table
25// - explain_query: Get query execution plan (EXPLAIN ANALYZE)
26//
27// Safety notes:
28// - Use a read-only database user for safety
29// - Set statement_timeout to prevent long-running queries
30// - Never expose production credentials in config files
31// - Use environment variables for connection strings:
32// "args": ["$DATABASE_URL"]
33//
34// Recommended PostgreSQL user setup:
35// CREATE ROLE claude_readonly WITH LOGIN PASSWORD 'secure_pass';
36// GRANT CONNECT ON DATABASE mydb TO claude_readonly;
37// GRANT USAGE ON SCHEMA public TO claude_readonly;
38// GRANT SELECT ON ALL TABLES IN SCHEMA public TO claude_readonly;