The PostgreSQL MCP server provides read-only access to PostgreSQL databases, allowing LLMs to inspect database schemas and execute read-only SQL queries. This server acts as a bridge between language models and your PostgreSQL databases, making it easier to work with database content directly in AI interfaces.
Docker provides an isolated environment to run the MCP server without installing dependencies directly on your system.
Note: When using Docker on macOS and connecting to a PostgreSQL server on your host machine, use host.docker.internal
instead of localhost
in the connection URL.
Add the following to the "mcpServers" section of your claude_desktop_config.json
:
{
"mcpServers": {
"postgres": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mcp/postgres",
"postgresql://host.docker.internal:5432/mydb"]
}
}
}
NPX allows you to run the server directly from npm without a global installation.
Add the following to the "mcpServers" section of your claude_desktop_config.json
:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://localhost/mydb"
]
}
}
}
The connection URL follows this format:
postgresql://user:password@host:port/database-name
To configure the PostgreSQL MCP server in VS Code:
Ctrl + Shift + P
and type Preferences: Open User Settings (JSON)
{
"mcp": {
"inputs": [
{
"type": "promptString",
"id": "pg_url",
"description": "PostgreSQL URL (e.g. postgresql://user:[email protected]:5432/mydb)"
}
],
"servers": {
"postgres": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mcp/postgres",
"${input:pg_url}"
]
}
}
}
}
{
"mcp": {
"inputs": [
{
"type": "promptString",
"id": "pg_url",
"description": "PostgreSQL URL (e.g. postgresql://user:pass@localhost:5432/mydb)"
}
],
"servers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"${input:pg_url}"
]
}
}
}
}
Alternatively, you can add this configuration to .vscode/mcp.json
in your workspace to share it with others (without the "mcp"
root key).
Once configured, your AI assistant can:
The server provides a query tool that allows executing read-only SQL queries against the connected database:
The server automatically discovers and provides schema information for each table in the database:
postgres://<host>/<table>/schema
When interacting with an AI that has access to this MCP server, you can ask it to examine your database schema or run queries to retrieve information.
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.