PostgreSQL Read-Only MCP Server
README.md
# PostgreSQL Read-Only MCP Server
A Model Context Protocol (MCP) server that provides safe, read-only access to one configured PostgreSQL database.
The server exposes schema inspection, data preview, custom `SELECT` query execution, relationship discovery, database statistics, safe connection metadata, and a `connection_info` prompt for MCP clients such as Claude Desktop.
## Features
- Single database per MCP server instance
- Strict PostgreSQL read-only validation (`SELECT` only)
- Schema-aware tools with optional `schema` input, defaulting to `public`
- Server-side row limits and query timeout protection
- Data preview with optional column selection, basic `where` filtering, and text truncation
- Foreign key relationship discovery
- Database statistics with largest tables by rows and by size
- Safe `db_info` output that never exposes passwords
- `connection_info` MCP prompt with connection context and usage guidance
## Installation
Use directly with `npx` in your MCP client config:
```bash
npx -y postgres-readonly-mcp
```
Or install globally:
```bash
npm install -g postgres-readonly-mcp
postgres-readonly-mcp
```
For local development only:
```bash
git clone https://github.com/beydemirfurkan/postgres-readonly-mcp.git
cd postgres-readonly-mcp
npm install
npm run build
```
## Claude Desktop Config
Recommended `npx` setup:
```json
{
"mcpServers": {
"postgres-readonly": {
"command": "npx",
"args": ["-y", "postgres-readonly-mcp"],
"env": {
"POSTGRES_HOST": "127.0.0.1",
"POSTGRES_PORT": "5432",
"POSTGRES_USER": "postgres",
"POSTGRES_PASSWORD": "your_password",
"POSTGRES_DATABASE": "your_database",
"POSTGRES_SSL": "true",
"POSTGRES_SSL_REJECT_UNAUTHORIZED": "true"
}
}
}
}
```
Connection string setup:
```json
{
"mcpServers": {
"postgres-readonly": {
"command": "npx",
"args": ["-y", "postgres-readonly-mcp"],
"env": {
"DATABASE_URL": "postgresql://USER:PASSWORD@HOST:5432/DB_NAME",
"POSTGRES_SSL": "true"
}
}
}
}
```
Restart Claude Desktop after editing the config.
## Multiple Databases
One server instance maps to one PostgreSQL database. To expose multiple databases, add multiple MCP server entries with different names and environment variables:
```json
{
"mcpServers": {
"postgres-crm": {
"command": "npx",
"args": ["-y", "postgres-readonly-mcp"],
"env": {
"DATABASE_URL": "postgresql://USER:PASSWORD@HOST:5432/crm"
}
},
"postgres-analytics": {
"command": "npx",
"args": ["-y", "postgres-readonly-mcp"],
"env": {
"DATABASE_URL": "postgresql://USER:PASSWORD@HOST:5432/analytics"
}
}
}
}
```
## Environment Variables
- `POSTGRES_HOST`: PostgreSQL host, default `localhost`
- `POSTGRES_PORT`: PostgreSQL port, default `5432`
- `POSTGRES_USER`: PostgreSQL user, default `postgres`
- `POSTGRES_PASSWORD`: PostgreSQL password, default empty
- `POSTGRES_DATABASE`: PostgreSQL database name, default `postgres`
- `POSTGRES_SSL`: Enable TLS, default `true`
- `POSTGRES_SSL_REJECT_UNAUTHORIZED`: Reject unauthorized TLS certificates, default `true`
- `POSTGRES_QUERY_TIMEOUT_MS`: Query timeout in milliseconds, minimum `1000`, default `30000`
- `DATABASE_URL`: PostgreSQL connection URL fallback
- `POSTGRES_URL`: PostgreSQL connection URL alias
Explicit `POSTGRES_*` host fields override URL values when both are present.
If your password includes special characters (`/`, `@`, `:`), URL encode it in connection strings.
## Tools
### `db_info`
Returns safe connection metadata for the configured database.
Input:
```json
{}
```
### `list_tables`
Lists tables/views in a schema.
Input:
```json
{ "schema": "public" }
```
### `describe_table`
Describes columns, primary key, foreign keys, and indexes.
Input:
```json
{
"schema": "public",
"table": "users"
}
```
### `preview_data`
Previews rows from a table with optional column selection and basic filtering.
Input:
```json
{
"schema": "public",
"table": "users",
"columns": ["id", "email", "created_at"],
"where": "created_at >= '2026-01-01'",
"limit": 10
}
```
### `run_query`
Runs a custom read-only SQL query.
Input:
```json
{
"query": "SELECT id, email FROM \"public\".\"users\" ORDER BY id DESC",
"limit": 100
}
```
### `show_relations`
Shows foreign key relations for a table.
Input:
```json
{
"schema": "public",
"table": "orders"
}
```
### `db_stats`
Returns database size, table count, row estimates, `largestByRows`, and `largestBySize`.
Input:
```json
{}
```
## Prompts
### `connection_info`
Provides safe PostgreSQL connection context and read-only usage guidance. Passwords are never included.
## Safety Model
- Only `SELECT` statements are allowed
- Multiple SQL statements in one request are blocked
- DML, DDL, grant/revoke, maintenance, and admin keywords are blocked
- Risky PostgreSQL functions such as `pg_sleep`, `dblink`, and file-read helpers are blocked
- Results are capped server-side even if a query requests more rows
- Connection/runtime errors are sanitized to avoid credential leakage
## Limits
- `preview_data` default: `10`, max: `100`
- `run_query` default: `1000`, max: `5000`
- Query timeout: `30000ms` by default
- Long text truncation: `200` chars
## Development
```bash
npm install
npm run build
npm run dev
npm test
```
## License
MIT