PostgreSQL Database MCP server

Enables interaction with PostgreSQL databases for executing SQL queries, exploring table schemas, and performing data analysis with robust error handling and connection pooling.
Back to servers
Provider
Endaoment
Release date
Mar 09, 2025
Language
TypeScript
Stats
1 star

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.

Prerequisites

  • Node.js 20 or higher
  • PostgreSQL database
  • Access credentials for the database

Installation

Install the server by cloning the repository and installing dependencies:

npm install

Configuration

The server requires database credentials to connect to your PostgreSQL database. These can be provided in several ways.

Using .env File

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"}'

Using Shell Configuration Files

If no .env file is found, the server will look for credentials in your shell configuration files in this order:

  1. ~/.zshrc
  2. ~/.bashrc
  3. ~/.bash_profile
  4. ~/.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"}'

Using Custom Credentials Variable

You can specify a custom environment variable name using the --credentials-var flag:

node server.js --credentials-var MY_CUSTOM_DB_CREDS

Running the Server

Start the MCP server using one of these commands:

# Using Node.js directly
node server.js

# Using npm
npm start

Logging Options

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

Integration with Cursor

This server works with Cursor AI through the Model Context Protocol.

Automatic Configuration

A pre-configured .cursor/mcp.json file is included for automatic setup with Cursor.

Manual Configuration

To manually add this server to Cursor:

  1. Go to Cursor Settings → Features → MCP
  2. Click "+ Add New MCP Server"
  3. Enter the following details:
    • Name: Postgres MCP
    • Type: stdio
    • Command: node /full/path/to/server.js

Server Capabilities

The PostgreSQL MCP server provides these features to AI models:

  • SQL Query Execution: Execute SQL queries with automatic retry logic
  • Database Schema Access: View schema information for all tables
  • Table Listing: List all available tables in the database

Troubleshooting

Connection Issues

If the server fails to connect to your database:

  • Ensure PostgreSQL is running: pg_isready -h localhost -p 5433
  • Verify your credentials are correct
  • Check that your IP address has access to the database
  • Try connecting with psql to verify access

Environment Variable Problems

If the server can't find your credentials:

  • Make sure your .env file is in the project root directory
  • Check that the JSON structure in DB_CREDENTIALS is valid
  • Test with: node -e "console.log(JSON.parse(process.env.DB_CREDENTIALS))" < .env

Cursor Integration Issues

If you encounter problems with Cursor:

  • Make sure the .cursor/mcp.json file exists and is formatted correctly
  • Try restarting Cursor
  • Run the server manually with verbose logging: node server.js -v
  • Check if database credentials are accessible in the Cursor environment

How to add this MCP server to Cursor

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.

Adding an MCP server to Cursor globally

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"
            ]
        }
    }
}

Adding an MCP server to a project

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.

How to use the MCP server

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.

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later