This PostgreSQL MCP server enables AI models to interact with your PostgreSQL database through the Model Context Protocol, allowing for database schema information retrieval and SQL query execution with robust error handling.
Install the server by cloning the repository and installing dependencies:
npm install
The server requires database credentials to connect to your PostgreSQL database. These can be provided in several ways.
Create a .env
file in the project root directory:
touch .env
Add your database credentials as a JSON string:
export DB_CREDENTIALS='{"DB_USER":"your-username","DB_PASSWORD":"your-password","DB_HOST":"your-host","DB_PORT":"5433","DB_NAME":"your-database"}'
If no .env
file is found, the server will look for credentials in your shell configuration files in this order:
~/.zshrc
~/.bashrc
~/.bash_profile
~/.profile
To add credentials to a shell config file:
nano ~/.zshrc
# or
nano ~/.bashrc
Then add your database credentials:
export DB_CREDENTIALS='{"DB_USER":"your-username","DB_PASSWORD":"your-password","DB_HOST":"your-host","DB_PORT":"5433","DB_NAME":"your-database"}'
You can specify a custom environment variable name using the --credentials-var
flag:
node server.js --credentials-var MY_CUSTOM_DB_CREDS
Start the MCP server using one of these commands:
# Using Node.js directly
node server.js
# Using npm
npm start
Enable verbose logging to see detailed information:
# With verbose logging
node server.js --verbose
# or with short flag
node server.js -v
# With npm
npm start -- --verbose
This server works with Cursor AI through the Model Context Protocol.
A pre-configured .cursor/mcp.json
file is included for automatic setup with Cursor.
To manually add this server to Cursor:
node /full/path/to/server.js
The PostgreSQL MCP server provides these features to AI models:
If the server fails to connect to your database:
pg_isready -h localhost -p 5433
psql
to verify accessIf the server can't find your credentials:
.env
file is in the project root directoryDB_CREDENTIALS
is validnode -e "console.log(JSON.parse(process.env.DB_CREDENTIALS))" < .env
If you encounter problems with Cursor:
.cursor/mcp.json
file exists and is formatted correctlynode server.js -v
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.