The PostgreSQL MCP Server provides an interface for LLMs to interact with PostgreSQL databases, enabling schema inspection, query execution, and CRUD operations. This server extends the base PostgreSQL MCP Server with additional functionality for creating tables, inserting entries, updating data, deleting entries, and dropping tables.
To install the PostgreSQL MCP Server:
git clone https://github.com/vignesh-codes/ai-agents-mcp-pg.git
docker run --name postgres-container -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin_password -e POSTGRES_DB=mydatabase -p 5432:5432 -d postgres:latest
docker build -t mcp/postgres -f src/Dockerfile .
To connect the MCP server with Claude Desktop, add the following to the "mcpServers" section of your claude_desktop_config.json
:
{
"mcpServers": {
"postgres": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mcp/postgres",
"postgresql://username:[email protected]:5432/mydatabase"
]
}
}
}
Note:
host.docker.internal
if the server is running on the host networkpostgresql://user:password@host:port/db-name
Execute read-only SQL queries against the database:
{
"sql": "SELECT * FROM users;"
}
Create new tables by specifying column definitions:
{
"tableName": "example_table",
"columns": [
{ "name": "id", "type": "SERIAL PRIMARY KEY" },
{ "name": "name", "type": "VARCHAR(255)" },
{ "name": "age", "type": "INTEGER" }
]
}
Add new data to existing tables:
{
"tableName": "example_table",
"values": {
"name": "John Doe",
"age": 30
}
}
Modify existing data based on conditions:
{
"tableName": "example_table",
"values": {
"age": 35
},
"conditions": "name = 'John Doe'"
}
Remove data from tables:
{
"tableName": "example_table",
"conditions": "name = 'John Doe'"
}
Remove tables from the database:
{
"tableName": "example_table"
}
The server automatically provides schema information for each table in the database through the resource postgres://<host>/<table>/schema
, which includes:
Here's an example workflow creating related tables and working with data:
{
"columns": [
{"name": "id", "type": "INTEGER PRIMARY KEY"},
{"name": "name", "type": "TEXT"},
{"name": "email", "type": "TEXT"},
{"name": "created_at", "type": "TEXT"}
],
"tableName": "users"
}
{
"columns": [
{"name": "id", "type": "INTEGER PRIMARY KEY"},
{"name": "user_id", "type": "INTEGER"},
{"name": "amount", "type": "REAL"},
{"name": "status", "type": "TEXT"},
{"name": "payment_date", "type": "TEXT"}
],
"tableName": "payments"
}
{
"values": {
"id": "1",
"name": "John Doe",
"email": "[email protected]",
"created_at": "2024-01-01"
},
"tableName": "users"
}
{
"sql": "SELECT u.name, p.amount, p.status FROM users u JOIN payments p ON u.id = p.user_id;"
}
{
"values": {
"status": "completed"
},
"tableName": "payments",
"conditions": {
"user_id": "3"
}
}
There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json
file so that it is available in all of your projects.
If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json
file.
To add a global MCP server go to Cursor Settings > MCP and click "Add new global MCP server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"cursor-rules-mcp": {
"command": "npx",
"args": [
"-y",
"cursor-rules-mcp"
]
}
}
}
To add an MCP server to a project you can create a new .cursor/mcp.json
file or add it to the existing one. This will look exactly the same as the global MCP server example above.
Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.
The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.
You can also explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.