Skip to main content
Glama
PostgreSQL MCP Server

PostgreSQL MCP Server

README.md
# PostgreSQL MCP Server

A Model Context Protocol (MCP) server that provides tools for interacting with PostgreSQL databases.

## Tools

- **read_query** - Execute SELECT queries to read data from the database
- **list_tables** - Get a list of all tables in the database
- **describe_table** - View schema information for a specific table

## Installation

### 1. Create and activate a virtual environment

```bash
cd <path-to-project>
python3 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
```

### 2. Install the package

```bash
pip install -e .
```

## Usage

The server supports two transport modes:
- **stdio** (default) - Standard input/output, for direct MCP client integration
- **http** - HTTP with streamable transport, for standalone server mode

### Command Line

```bash
postgres-mcp --host <host> --port <port> --database <database> --user <user> --password <password> [--transport stdio|http]
```

**Arguments:**

| Argument | Required | Default | Description |
|----------|----------|---------|-------------|
| `--host` | Yes | - | PostgreSQL host |
| `--port` | No | 5432 | PostgreSQL port |
| `--database` | Yes | - | Database name |
| `--user` | Yes | - | Database user |
| `--password` | Yes | - | Database password |
| `--transport` | No | stdio | Transport type: `stdio` or `http` |
| `--http-host` | No | 127.0.0.1 | Host for HTTP server |
| `--http-port` | No | 8080 | Port for HTTP server |

**Example (stdio mode):**

```bash
postgres-mcp --host localhost --port 5432 --database mydb --user admin --password secret
```

**Example (HTTP mode):**

```bash
postgres-mcp --host localhost --database mydb --user admin --password secret --transport http --http-port 8080
```

### As a Python module

```bash
python -m postgres_mcp --host localhost --database mydb --user admin --password secret
```

### Using run.sh (for AWS RDS with IAM authentication)

The `run.sh` script handles AWS RDS IAM authentication and SSL configuration:

```bash
./run.sh
```

This script:
- Sets up SSL certificates for RDS
- Generates a temporary IAM auth token via `aws-vault`
- Launches the MCP server in HTTP mode on `http://127.0.0.1:8080/mcp`
- You need to configure the environment variables for credentials and RDS connection details before running the script.

## MCP Client Configuration

### HTTP Mode (recommended for AWS RDS / complex environments)

1. Start the server in a terminal with the correct environment:
   ```bash
   ./run.sh
   ```

2. Configure your MCP client:

   **For clients that support HTTP directly (e.g., Cursor):**
   ```json
   {
     "mcpServers": {
       "postgres": {
         "type": "streamable-http",
         "url": "http://127.0.0.1:8080/mcp"
       }
     }
   }
   ```

   **For clients that only support stdio (e.g., Claude Code):**

   Use the stdio-to-HTTP proxy:
   ```json
   {
     "mcpServers": {
       "postgres": {
         "command": "<path-to-project>/.venv/bin/postgres-mcp-proxy",
         "args": ["--server-url", "http://127.0.0.1:8080/mcp"]
       }
     }
   }
   ```

### Stdio Mode (for simple setups)

#### Direct execution

**Claude Desktop** (`~/Library/Application Support/Claude/claude_desktop_config.json`):

```json
{
  "mcpServers": {
    "postgres": {
      "command": "<path-to-project>/.venv/bin/postgres-mcp",
      "args": [
        "--host", "localhost",
        "--port", "5432",
        "--database", "mydb",
        "--user", "admin",
        "--password", "secret"
      ]
    }
  }
}
```

**Claude Code:**

```json
{
  "mcpServers": {
    "postgres": {
      "command": "<path-to-project>/.venv/bin/postgres-mcp",
      "args": [
        "--host", "localhost",
        "--port", "5432",
        "--database", "mydb",
        "--user", "admin",
        "--password", "secret"
      ]
    }
  }
}
```

## Development

### Install in development mode

```bash
pip install -e .
```

### Project structure

```
db-mcp/
├── pyproject.toml
├── README.md
├── run.sh                   # AWS RDS launcher script
└── src/
    └── postgres_mcp/
        ├── __init__.py
        ├── __main__.py      # CLI entry point
        ├── database.py      # Connection pool management
        ├── server.py        # MCP server implementation
        └── proxy.py         # Stdio-to-HTTP proxy for Claude Code
```