The MCP PostgreSQL Server enables AI models to interact with PostgreSQL databases through a standardized interface, providing database operations via the Model Context Protocol. This server acts as a bridge between AI systems and PostgreSQL databases for seamless data access and manipulation.
You can install the MCP PostgreSQL Server using npm:
npm install mcp-postgres-server
Alternatively, run it directly without installation using npx:
npx mcp-postgres-server
The server requires PostgreSQL connection details through environment variables. Here's how to configure it:
{
"mcpServers": {
"postgres": {
"type": "stdio",
"command": "npx",
"args": ["-y", "mcp-postgres-server"],
"env": {
"PG_HOST": "your_host",
"PG_PORT": "5432",
"PG_USER": "your_user",
"PG_PASSWORD": "your_password",
"PG_DATABASE": "your_database"
}
}
}
}
Establishes a connection to your PostgreSQL database:
use_mcp_tool({
server_name: "postgres",
tool_name: "connect_db",
arguments: {
host: "localhost",
port: 5432,
user: "your_user",
password: "your_password",
database: "your_database"
}
});
Executes SELECT queries with optional prepared statement parameters:
use_mcp_tool({
server_name: "postgres",
tool_name: "query",
arguments: {
sql: "SELECT * FROM users WHERE id = $1",
params: [1]
}
});
Performs INSERT, UPDATE, or DELETE operations:
use_mcp_tool({
server_name: "postgres",
tool_name: "execute",
arguments: {
sql: "INSERT INTO users (name, email) VALUES ($1, $2)",
params: ["John Doe", "[email protected]"]
}
});
Retrieves all schemas in the connected database:
use_mcp_tool({
server_name: "postgres",
tool_name: "list_schemas",
arguments: {}
});
Lists tables in the database:
// List tables in the 'public' schema (default)
use_mcp_tool({
server_name: "postgres",
tool_name: "list_tables",
arguments: {}
});
// List tables in a specific schema
use_mcp_tool({
server_name: "postgres",
tool_name: "list_tables",
arguments: {
schema: "my_schema"
}
});
Gets the structure of a specific table:
// Describe a table in the 'public' schema (default)
use_mcp_tool({
server_name: "postgres",
tool_name: "describe_table",
arguments: {
table: "users"
}
});
// Describe a table in a specific schema
use_mcp_tool({
server_name: "postgres",
tool_name: "describe_table",
arguments: {
table: "users",
schema: "my_schema"
}
});
The server provides detailed error messages for common issues:
All tools use prepared statements to prevent SQL injection and support both PostgreSQL-style ($1, $2) and MySQL-style (?) parameter placeholders for flexibility in your queries.
To add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "postgres" '{"type":"stdio","command":"npx","args":["-y","mcp-postgres-server"],"env":{"PG_HOST":"your_host","PG_PORT":"5432","PG_USER":"your_user","PG_PASSWORD":"your_password","PG_DATABASE":"your_database"}}'
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": {
"type": "stdio",
"command": "npx",
"args": [
"-y",
"mcp-postgres-server"
],
"env": {
"PG_HOST": "your_host",
"PG_PORT": "5432",
"PG_USER": "your_user",
"PG_PASSWORD": "your_password",
"PG_DATABASE": "your_database"
}
}
}
}
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": {
"type": "stdio",
"command": "npx",
"args": [
"-y",
"mcp-postgres-server"
],
"env": {
"PG_HOST": "your_host",
"PG_PORT": "5432",
"PG_USER": "your_user",
"PG_PASSWORD": "your_password",
"PG_DATABASE": "your_database"
}
}
}
}
3. Restart Claude Desktop for the changes to take effect