The PostgreSQL MCP Server provides a Model Context Protocol implementation that allows Language Models to interact with PostgreSQL databases. This enhanced version enables both reading and writing capabilities, including data querying, data modification, and schema management operations.
You can use the PostgreSQL MCP Server with Claude Desktop by adding it to your configuration in one of two ways:
{
"mcpServers": {
"postgres": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mcp/postgres",
"postgresql://host.docker.internal:5432/mydb"]
}
}
}
Notes:
host.docker.internal
if the server is running on the host networkpostgresql://user:password@host:port/db-name
?sslmode=no-verify
to bypass SSL certificate verification if needed{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://localhost/mydb"
]
}
}
}
Remember to replace mydb
with your actual database name.
The PostgreSQL MCP Server provides several tools that let you interact with your database.
To execute read-only SQL queries:
/query SELECT * FROM users LIMIT 5
All queries are executed within a READ ONLY transaction.
To run SQL statements that modify data:
/execute INSERT INTO users (name, email) VALUES ('Jane Doe', '[email protected]')
To insert data using a simplified command:
/insert table="users", data={"name": "John Doe", "email": "[email protected]"}
To update existing records:
/update table="users", data={"status": "inactive"}, where="id='123'"
To delete records:
/delete table="users", where="email='[email protected]'"
/createTable tableName="tasks", columns=[
{"name": "id", "type": "SERIAL", "constraints": "PRIMARY KEY"},
{"name": "title", "type": "VARCHAR(100)", "constraints": "NOT NULL"},
{"name": "created_at", "type": "TIMESTAMP", "constraints": "DEFAULT CURRENT_TIMESTAMP"}
]
/createFunction name="update_timestamp", parameters="", returnType="TRIGGER", language="plpgsql", body="BEGIN NEW.updated_at = NOW(); RETURN NEW; END;"
/createTrigger name="set_timestamp", tableName="tasks", functionName="update_timestamp", when="BEFORE", events=["UPDATE"], forEach="ROW"
/createIndex tableName="users", indexName="idx_user_email", columns=["email"], unique=true
/alterTable tableName="users", operation="ADD COLUMN", details="login_count INTEGER DEFAULT 0"
The server automatically exposes schema information for all tables in the connected database:
postgres://<host>/<table>/schema
The PostgreSQL MCP Server includes several security features:
To add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "postgres" '{"command":"npx","args":["-y","@modelcontextprotocol/server-postgres","postgresql://localhost/mydb"]}'
See the official Claude Code MCP documentation for more details.
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 > Tools & Integrations and click "New MCP Server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://localhost/mydb"
]
}
}
}
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 explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.
To add this MCP server to Claude Desktop:
1. Find your configuration file:
~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
~/.config/Claude/claude_desktop_config.json
2. Add this to your configuration file:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://localhost/mydb"
]
}
}
}
3. Restart Claude Desktop for the changes to take effect