How to Connect PostgreSQL to AI Agents Using MCP
Step-by-step guide to setting up the PostgreSQL MCP server. Give your AI agent read-only access to query your PostgreSQL databases safely.
Generate & Test PostgreSQL MCP Server Config
One-click export with environment variables & path locators for Claude Desktop, Cursor, Windsurf, and OpenAI Codex CLI.
Connect PostgreSQL to AI Agents Using MCP
The PostgreSQL MCP server allows AI agents to inspect database schemas, visualize tables, and execute read-only SQL queries against your PostgreSQL databases. This turns your AI assistant into a powerful data analyst that can write complex JOINs and aggregations on the fly.
Prerequisites
- â–¸A PostgreSQL database running locally or remotely.
- â–¸Node.js 18+ installed on your host machine.
- â–¸An MCP-compatible client like Claude Desktop.
Security Best Practices
Before configuring your MCP server, you must establish strict access controls. Never grant your AI agent root or postgres admin access.
- â–¸Use a read-only database user: Create a dedicated user with
SELECT-only permissions. - â–¸Limit accessible schemas: Configure the user to only query the
publicschema. - ▸Use environment variables for connection strings—never hardcode credentials in your config.
-- Execute this to create a read-only user
CREATE USER mcp_reader WITH PASSWORD 'secure_password';
GRANT CONNECT ON DATABASE mydb TO mcp_reader;
GRANT USAGE ON SCHEMA public TO mcp_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_reader;Installation & Configuration
For production setups, we recommend injecting the database URL via environment variables rather than hardcoding it in the args array.
Update your Claude Desktop config:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"DATABASE_URL": "postgresql://mcp_reader:secure_password@/mydb"
}
}
}
}Available Tools
Once connected, the server exposes:
- â–¸
query— Execute read-only SQL queries. - ▸
list_tables— List all tables in the database. - ▸
describe_table— Get schema information and column types for a specific table.
Example Workflows
Once configured, you can prompt your AI agent naturally:
- â–¸"What tables exist in my database?"
- â–¸"Show me the schema for the users table."
- â–¸"What are the top 10 customers by order value this quarter? Write a SQL query, execute it, and format the results as a markdown table."
Troubleshooting
- â–¸
Connection refused: Ensure PostgreSQL is running and accepting connections on port5432. - â–¸
Authentication failed: Verify yourmcp_readercredentials and ensure yourpg_hba.confallows connections from your MCP server's IP. - â–¸
SSL required: If connecting to a cloud database (like AWS RDS or Supabase), append?sslmode=requireto yourDATABASE_URLstring.
Using with OpenAI Codex
You can use this MCP server with the OpenAI Codex CLI by adding it to your configuration:
codex mcp add --name postgres --command "npx -y @modelcontextprotocol/server-postgres postgresql://mcp_reader:your_password@/mydb"Note: Replace the connection string with your actual PostgreSQL URL.
For a full list of recommended servers, see Best MCP Servers for OpenAI Codex.
Build your full agent toolstack in the Visual Generator
Combine How to Connect PostgreSQL to AI Agents Using MCP with databases, search APIs, and memory graphs in a single configuration file.
Related Guides
How to Use the MySQL MCP Server for Database Queries
Connect MySQL databases to your AI agent for querying, schema exploration, and data analysis using the MySQL MCP server.
DatabasesHow to Use the SQLite MCP Server for Local Database Analysis
Learn how to connect SQLite databases to your AI agent for local data analysis, querying, and schema exploration using the MCP protocol.
DatabasesHow to Use the MongoDB MCP Server for NoSQL Data Access
Connect MongoDB databases to AI agents for document querying, aggregation pipelines, and schema exploration using MCP.