home / mcp / postgresql mcp server
Provides a PostgreSQL-backed MCP server that exposes resources and supports executing SQL queries with retry logic.
Configuration
View docs{
"mcpServers": {
"endaoment-endaoment-postgres-mcp": {
"command": "node",
"args": [
"server.js"
],
"env": {
"DB_CREDENTIALS": "{\"DB_USER\":\"user\",\"DB_PASSWORD\":\"pass\",\"DB_HOST\":\"localhost\",\"DB_PORT\":\"5433\",\"DB_NAME\":\"db\"}"
}
}
}
}This MCP (Model Context Protocol) server lets AI models interact with your PostgreSQL database through a standardized, reliable interface. It manages connections with pooling, exposes database resources, and supports executing SQL queries with built‑in retry and error handling to keep conversations with models smooth and resilient.
Start by running the MCP server locally. You can launch it directly with Node.js or via npm. Once running, you expose the server to your MCP client so the AI model can list tables, read schemas, and execute queries against your PostgreSQL database.
Two straightforward ways to start are shown here. Choose the one you prefer.
node server.js
```
```javascript
npm startPrerequisites you need before installing are Node.js 20 or higher and a PostgreSQL database with reachable credentials.
Follow these concrete steps to set up and start the MCP server.
# 1) Install dependencies
npm install
# 2) Start the server directly with Node.js
node server.js
# Or start via npm script
npm startThe server reads database connection details from an environment source. You provide credentials as a JSON string via the DB_CREDENTIALS environment variable or through shell configuration files if the env file is absent.
Basic credential format to place in a shell or env file: the JSON string should include your user, password, host, port, and database name.
# Example environment variable (use in your shell or in a .env file)
export DB_CREDENTIALS='{"DB_USER":"your-username","DB_PASSWORD":"your-password","DB_HOST":"your-host","DB_PORT":"5433","DB_NAME":"your-database"}'
```
If you prefer a different variable name, you can start the server with a credentials flag
```bash
node server.js --credentials-var MY_CUSTOM_DB_CREDS
```
In this case, define MY_CUSTOM_DB_CREDS in your environment or shell config.If you encounter issues, verify that PostgreSQL is reachable, credentials are correct, and the Node.js version is 20 or newer. You can enable verbose logging to get more insight into startup and request handling.
# Enable verbose logging when starting
node server.js -v
```
Or with npm
```bash
npm start -- --verboseThis MCP server integrates with Cursor. You can manually add the server to Cursor by configuring the MCP entry with a stdio command that points to your local startup script.
Example manual configuration for Cursor: enter the following details in Cursor MCP settings.
Name: Postgres MCP
Type: stdio
Command: node /full/path/to/server.js
```
You can also rely on the pre-configured Cursor setup that ships with the project to automate integration inside Cursor.The server exposes database resources such as tables and their schemas to AI models. It also provides a tool to execute SQL queries with retry logic.
The server includes connection retry logic, detailed error logging, and graceful shutdown handling to minimize disruption and aid debugging.
Common PostgreSQL problems include permission issues, missing relations, or misconfigured connections. Ensure the database user has appropriate permissions, verify the target database and schema, and confirm connectivity with a tool like psql.
To avoid large results that slow down responses, consider applying LIMITs in queries when working with AI models. Always monitor logs for any startup or request handling issues and restart the server if necessary.
Execute SQL queries with retry logic