How to Use PostgreSQL MCP Server with Your AI
Step-by-step guide to querying databases, exploring schemas, and managing PostgreSQL directly from your AI assistant using the PostgreSQL MCP server.
How to Use PostgreSQL MCP Server with Your AI
If you work with databases, you know the drill. Open a SQL client, remember the connection string, write a query, interpret the results. The PostgreSQL MCP server removes all that friction by letting your AI assistant talk directly to your Postgres database. Ask questions in plain English, get answers backed by real SQL queries.
This is one of the most powerful MCP servers available because it turns your AI into a database analyst who already knows your schema.
What It Does
The PostgreSQL MCP server gives your AI a direct connection to your Postgres database. It can explore your database schema, including tables, columns, types, and relationships. It can run read queries to answer questions about your data. It can describe table structures and indexes. And depending on your configuration, it can also run write operations like inserts and updates.
Your AI does not just run blind queries. It first inspects your schema to understand the structure, then writes appropriate SQL based on your natural language request.
Prerequisites
- A PostgreSQL database that is accessible from your machine (local or remote).
- A database connection string in the format
postgresql://user:password@host:port/database. - Node.js 18 or later installed on your machine.
- An MCP-compatible AI client such as Claude Desktop, Cursor, or Windsurf.
Step-by-Step Setup
1. Prepare Your Connection String
You need a standard PostgreSQL connection URI. For a local database, it might look like postgresql://myuser:mypassword@localhost:5432/mydb. For a remote database, replace localhost with your host address.
If your database requires SSL, append ?sslmode=require to the connection string.
2. Add the Server Configuration
In your AI client's MCP settings, add:
json{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://user:password@localhost:5432/mydb"
]
}
}
}
3. Consider Read-Only Access
For safety, especially with production databases, create a read-only database user. In psql, run:
sqlCREATE USER ai_reader WITH PASSWORD 'secure_password';
GRANT CONNECT ON DATABASE mydb TO ai_reader;
GRANT USAGE ON SCHEMA public TO ai_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO ai_reader;
Then use this user in your connection string.
4. Restart Your AI Client
Save the configuration and restart. Your AI will connect to the database and be ready to explore and query it.
Things to Try
Here are queries that demonstrate how natural the experience feels:
- "What tables are in this database and how are they related?" Your AI inspects the schema and gives you a clear overview of the database structure, including foreign key relationships.
- "How many users signed up last month, broken down by week?" The AI writes the appropriate SQL with date functions, runs it, and presents the results in a readable format.
- "Show me the top 10 customers by total order value, including their email and the number of orders they placed." Multi-table joins written automatically based on your schema.
- "Are there any orders in the last 30 days where the total doesn't match the sum of line items?" Data integrity checks become conversational.
- "What's the average response time for support tickets by priority level?" Aggregate queries with grouping, translated from plain English.
Tips and Tricks
Always use a read-only user for production databases. This is the single most important tip. Your AI cannot accidentally modify data if the database user only has SELECT permissions.
Let the AI explore first. When connecting to an unfamiliar database, start by asking "What tables exist and what do they contain?" The AI will map out the schema before you start asking specific questions.
Ask for the SQL too. If you want to learn or verify, ask your AI to show you the SQL it ran. This is a great way to improve your own SQL skills.
Use it for reporting. Instead of building dashboards for one-off questions, just ask your AI. "What was our revenue last quarter compared to the quarter before?" is faster than building a report.
Tools That Pair Well
The PostgreSQL MCP server pairs naturally with several tools on a-gnt. Use the GitHub MCP server to look up migration files when your AI finds schema issues. The Slack MCP server can post query results directly to a team channel. And if you are working with a full-stack app, the Filesystem MCP server lets your AI read your application code to understand how the database is being used.
Find It on a-gnt
Check out the full listing for the PostgreSQL MCP server on a-gnt for detailed setup instructions and user tips.
Ratings & Reviews
0.0
out of 5
0 ratings
No reviews yet. Be the first to share your experience.