home / mcp / postgresql mcp server

PostgreSQL MCP Server

MCP Server for PostgreSQL

Installation
Add the following to your MCP client configuration file.

Configuration

View docs
{
  "mcpServers": {
    "asadudin-mcp-server-postgres": {
      "command": "python",
      "args": [
        "mcp_server_postgres.py"
      ],
      "env": {
        "HOST": "0.0.0.0",
        "PORT": "8056",
        "PG_HOST": "localhost",
        "PG_PORT": "5432",
        "PG_USER": "postgres",
        "PG_DATABASE": "postgres",
        "PG_PASSWORD": "YOUR_PASSWORD"
      }
    }
  }
}

You operate an MCP server that bridges your PostgreSQL databases with your MCP client, enabling safe, parameterized SQL queries, data manipulation, and schema management through a unified API. It streamlines database interactions and can be deployed locally or in containers for consistent development and production environments.

How to use

You will connect to the MCP server from your client and call the available endpoints to perform common database tasks. Use sql_query to fetch data, sql_execute to apply DML statements, create_database to add new databases, create_or_update_table to manage table schemas, and debug_postgres_connection to verify connectivity. Each operation accepts a set of parameters that you provide from your MCP client, and the server translates those requests into PostgreSQL actions.

How to install

Prerequisites you need before running the MCP server:

- Python 3.8 or newer

- PostgreSQL 10 or newer

- Docker (optional, for containerized deployment)

- Docker Compose (optional, for development)

Using Docker (Recommended)

# Step 1: Clone the MCP PostgreSQL server repository
git clone https://github.com/asadudin/mcp-server-postgres.git
cd mcp-server-postgres

# Step 2: Copy the example environment file
cp .env.example .env

# Step 3: Update the .env file with your PostgreSQL credentials
```

```env
PG_HOST=postgres
PG_PORT=5432
PG_USER=postgres
PG_PASSWORD=your_password
PG_DATABASE=your_database
HOST=0.0.0.0
PORT=8056
```

```bash
# Step 4: Start the service in detached mode
docker-compose up -d

Manual installation

# Step 1: Clone the repository
git clone https://github.com/asadudin/mcp-server-postgres.git
cd mcp-server-postgres

# Step 2: Create and activate a virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: .\venv\Scripts\activate

# Step 3: Install dependencies
pip install -r requirements.txt

# Step 4: Copy the example environment file and update it
cp .env.example .env
# Edit .env with your configuration

# Step 5: Run the MCP server
python mcp_server_postgres.py

Available tools

sql_query

Execute a SELECT query and return results as JSON.

sql_execute

Execute INSERT/UPDATE/DELETE statements with optional parameters.

create_database

Create a new PostgreSQL database.

create_or_update_table

Create or update a table schema using SQL statements.

debug_postgres_connection

Test and debug the PostgreSQL connection