# Postgres (read-only) MCP server

> Enables AI agents to perform secure, read-only analytics on PostgreSQL databases with shared schema context files.

- Repository: https://github.com/contextflo/postgres-mcp
- Page: https://hostedmcp.org/servers/contextflo/postgres-mcp
- Transport: stdio
- Stars: 4
- License: MIT
- Latest version: 0.1.4

## Install

### Claude Code

```sh
claude mcp add --env 'DATABASE_URL=<YOUR_DATABASE_URL>' postgres-mcp -- npx -y @contextflo/postgres-mcp
```

### Cursor (`~/.cursor/mcp.json`)

```json
{
  "mcpServers": {
    "postgres-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "@contextflo/postgres-mcp"
      ],
      "env": {
        "DATABASE_URL": "<YOUR_DATABASE_URL>"
      }
    }
  }
}
```

### VS Code (`.vscode/mcp.json`)

```json
{
  "servers": {
    "postgres-mcp": {
      "type": "stdio",
      "command": "npx",
      "args": [
        "-y",
        "@contextflo/postgres-mcp"
      ],
      "env": {
        "DATABASE_URL": "<YOUR_DATABASE_URL>"
      }
    }
  }
}
```

### VS Code CLI

```sh
code --add-mcp '{"name":"postgres-mcp","type":"stdio","command":"npx","args":["-y","@contextflo/postgres-mcp"],"env":{"DATABASE_URL":"<YOUR_DATABASE_URL>"}}'
```

### Claude Desktop (`claude_desktop_config.json`)

```json
{
  "mcpServers": {
    "postgres-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "@contextflo/postgres-mcp"
      ],
      "env": {
        "DATABASE_URL": "<YOUR_DATABASE_URL>"
      }
    }
  }
}
```

### Codex

```sh
codex mcp add postgres-mcp --env 'DATABASE_URL=<YOUR_DATABASE_URL>' -- npx -y @contextflo/postgres-mcp
```

### Codex config.toml (`~/.codex/config.toml`)

```toml
[mcp_servers.postgres-mcp]
command = "npx"
args = ["-y", "@contextflo/postgres-mcp"]

[mcp_servers.postgres-mcp.env]
DATABASE_URL = "<YOUR_DATABASE_URL>"
```

### Gemini CLI

```sh
gemini mcp add -s user -e 'DATABASE_URL=<YOUR_DATABASE_URL>' postgres-mcp npx -- -y @contextflo/postgres-mcp
```

### Gemini settings.json (`~/.gemini/settings.json`)

```json
{
  "mcpServers": {
    "postgres-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "@contextflo/postgres-mcp"
      ],
      "env": {
        "DATABASE_URL": "<YOUR_DATABASE_URL>"
      }
    }
  }
}
```

## Environment variables

| Name | Required | Secret | Description |
|---|---|---|---|
| `DATABASE_URL` | yes | yes | Postgres connection string. Use a read-only role. |
| `TEST_DATABASE_URL` | no | no |  |

## Tools (4)

- `query`
- `list_tables`
- `get_table_context`
- `add_table_context`

## FAQ

### How is the read-only restriction enforced?

It is enforced through four independent layers: the extended query protocol, connection-level read-only settings, a statement allowlist using the Postgres C parser, and a dedicated read-only database role.

### Can the AI agent update the database schema context?

Yes, the agent can use the `add_table_context` tool to append notes about the schema to a markdown context file, which can then be reviewed by a human.

### What happens if a query is too large or takes too long?

The server includes a default statement timeout of 30 seconds and caps results at 1000 rows and 50,000 characters to prevent excessive resource usage.

### How do I set up the server with a connection string?

You can provide the connection string as a command line argument or set a `DATABASE_URL` variable in a `.env` file in your project directory.
