PostgreSQL MCP Server is an implementation of the Model Context Protocol (MCP) that enables seamless integration between LLM applications and PostgreSQL databases. This server provides a standardized interface for AI agents to interact with databases, allowing them to query data, explore schema information, and understand database structure.
To install PostgreSQL MCP Server automatically via Smithery:
npx -y @smithery/cli install @gldc/mcp-postgres --client claude
git clone <repository-url>
cd mcp-postgres
python -m venv venv
source venv/bin/activate # On Windows, use: venv\Scripts\activate
pip install -r requirements.txt
You can start the MCP server in several ways:
# Without a connection string (server starts, DB-backed tools will return a friendly error)
python postgres_server.py
# Set the connection string via environment variable:
export POSTGRES_CONNECTION_STRING="postgresql://username:password@host:port/database"
python postgres_server.py
# Or pass it using the --conn flag:
python postgres_server.py --conn "postgresql://username:password@host:port/database"
The server provides the following tools:
Build the Docker image:
docker build -t mcp-postgres .
Run without a database connection:
docker run -p 8000:8000 mcp-postgres
Run with a live PostgreSQL database:
docker run \
-e POSTGRES_CONNECTION_STRING="postgresql://username:password@host:5432/database" \
-p 8000:8000 \
mcp-postgres
To integrate this server with MCP-compatible tools (like Cursor), add it to your ~/.cursor/mcp.json
:
{
"servers": {
"postgres": {
"command": "/path/to/venv/bin/python",
"args": [
"/path/to/postgres_server.py"
],
"env": {
"POSTGRES_CONNECTION_STRING": "postgresql://username:password@host:5432/database?ssl=true"
}
}
}
}
Replace:
/path/to/venv
with your virtual environment path/path/to/postgres_server.py
with the absolute path to the server scriptTo add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "postgres" '{"command":"/path/to/venv/bin/python","args":["/path/to/postgres_server.py"],"env":{"POSTGRES_CONNECTION_STRING":"postgresql://username:password@host:5432/database?ssl=true"}}'
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": "/path/to/venv/bin/python",
"args": [
"/path/to/postgres_server.py"
],
"env": {
"POSTGRES_CONNECTION_STRING": "postgresql://username:password@host:5432/database?ssl=true"
}
}
}
}
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": "/path/to/venv/bin/python",
"args": [
"/path/to/postgres_server.py"
],
"env": {
"POSTGRES_CONNECTION_STRING": "postgresql://username:password@host:5432/database?ssl=true"
}
}
}
}
3. Restart Claude Desktop for the changes to take effect