# Postgres (read-only)

> Postgres (read-only) is an open-source MCP server for databases. Read-only Postgres access with four enforcement layers and a shared business-context file. It runs locally on your machine and is started by your AI client.

- **Kind:** MCP server
- **Pricing:** Open source
- **Platforms:** CLI, self-hosted
- **Website:** https://contextflo.com/postgres-mcp
- **Repository:** https://github.com/contextflo/postgres-mcp
- **MCP Registry name:** io.github.contextflo/postgres-mcp
- **Categories:** databases
- **Listing updated:** 2026-11-16

## About

The archived reference Postgres server let a COMMIT then DROP SCHEMA payload escape its read-only transaction, and this server exists as a drop-in replacement that stops it in four independent ways: the extended query protocol rejects multi-statement input, every statement runs in BEGIN READ ONLY and ends in ROLLBACK, the real Postgres parser walks the tree to catch writes hidden in CTEs, and a read-only database role is recommended. It admits a user-defined function called from a SELECT is not covered, and that read-only does not mean confidential.

The second idea is a context file, .contextflo/context.md, seeded from COMMENT ON values. It holds business definitions such as what revenue means, is handed to the model, and the agent can append gotchas with add_table_context for you to review in git. Tools are query, list_tables, get_table_context and add_table_context. Results cap at 1,000 rows and 50,000 characters by default and truncation is stated.

The package is MIT licensed and needs Node.js 20 or later. Contextflo sells a team product with access control and audit logs on top, but this server is standalone.

## Features

- **Four-layer read-only:** Protocol, connection, parser and role each block writes.
- **Context file:** Business definitions are handed to the model and editable in git.
- **Agent notes:** The agent can append gotchas for you to review.
- **Result caps:** Defaults to 1,000 rows and 50,000 characters.
- **Statement timeout:** 30 seconds by default, configurable.
- **Schema tools:** Lists tables and shows columns, keys and enum values.

## Good for

- Let an assistant query analytics data without write risk
- Replace the archived reference Postgres server
- Teach a model what business metrics mean
- Explore an unfamiliar schema

## Install

### Claude Code (Terminal)

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

### Claude Desktop (claude_desktop_config.json)

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

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

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

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

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

### Windsurf (~/.codeium/windsurf/mcp_config.json)

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

### Other clients (your client's MCP settings (most use this shape))

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

## Tools

- `query`: Runs one read-only statement: SELECT, WITH SELECT, EXPLAIN or SHOW.
- `list_tables`: Lists readable tables with descriptions.
- `get_table_context`: Returns columns, types, keys, enum values and descriptions.
- `add_table_context`: Lets the agent add a note to the context file.

## FAQ

### Is it really read-only?

It uses four layers, but the README says a read-only role is still recommended because functions called from a SELECT are not covered.

### What does it need?

Node.js 20 or later and a DATABASE_URL, preferably for a read-only role.

### Can I swap it for the reference server?

Yes. The tool is still query with an sql argument, with some stricter rules such as rejecting multi-statement SQL.

### How are results limited?

1,000 rows and 50,000 characters by default.

### Is Postgres (read-only) free?

Yes. Postgres (read-only) is open source, so you can use and self-host it for free.

### How do I add Postgres (read-only) to Claude Code?

Run this command in your terminal: claude mcp add postgres-mcp --env DATABASE_URL='<DATABASE_URL>' -- npx -y @contextflo/postgres-mcp

### What are the best alternatives to Postgres (read-only)?

The closest alternatives to Postgres (read-only) are Midplane, PlatypusDB, Lorebase, Sovaria and model2data studio.

## Alternatives

- [Midplane](https://ai-powered.com/mcp/midplane): Midplane is an open-source MCP server for databases.
- [PlatypusDB](https://ai-powered.com/mcp/platypusdb): PlatypusDB is an open-source MCP server for databases.
- [Lorebase](https://ai-powered.com/mcp/lorebase): Lorebase is an open-source MCP server for knowledge and memory.
- [Sovaria](https://ai-powered.com/mcp/sovaria): Sovaria is a freemium MCP server for databases.
- [model2data studio](https://ai-powered.com/mcp/model2data-studio): model2data studio is a freemium MCP server for databases.
- [semlayer](https://ai-powered.com/mcp/semlayer): semlayer is a free MCP server for databases.
- [bricksentry](https://ai-powered.com/mcp/bricksentry): bricksentry is an open-source MCP server for databases.
- [MCP Toolbox for Databases](https://ai-powered.com/mcp/mcp-toolbox-for-databases-2): MCP Toolbox for Databases is an open-source MCP server for databases.

---
Source: https://ai-powered.com/mcp/postgres-read-only
