🐘

PostgreSQL MCP

Featured

Model Context Protocol server for PostgreSQL databases. Execute queries, explore schemas, and analyze data securely.

by Anthropic
Updated about 8 hours ago

Quick Install

Terminal
$ npx @modelcontextprotocol/server-postgres
Runs MCP server via npx

PostgreSQL MCP Server

Official MCP server for PostgreSQL database integration.

Installation

npm install -g @modelcontextprotocol/server-postgres

Configuration

Add to your Claude configuration:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://user:password@localhost:5432/database"
      ]
    }
  }
}

Available Tools

query

Execute a SQL query.

{
  "sql": "SELECT * FROM users WHERE active = true LIMIT 10"
}

list_tables

List all tables in the database.

{}

describe_table

Get table schema information.

{
  "table": "users"
}

Resources

The server exposes database schema as resources:

  • postgres://tables - List of all tables
  • postgres://schema/{table} - Schema for specific table

Security Best Practices

  1. Use Read-Only Credentials
    sql
    CREATE ROLE claude_readonly LOGIN PASSWORD 'secure_password';
    GRANT CONNECT ON DATABASE mydb TO claude_readonly;
    GRANT USAGE ON SCHEMA public TO claude_readonly;
    GRANT SELECT ON ALL TABLES IN SCHEMA public TO claude_readonly;

  2. Limit Access

    • Only grant access to necessary schemas
    • Exclude sensitive tables
    • Use connection pooling
  3. Audit Queries

    • Enable PostgreSQL query logging
    • Monitor for unusual patterns

Use Cases

  • Data exploration and analysis
  • Schema documentation
  • Query optimization suggestions
  • Data quality checks
  • Report generation

Example Workflows

Analyze Table Structure

"Describe the users table and suggest indexes for common query patterns"

Data Quality Check

"Find any NULL values in required fields across all tables"

Query Optimization

"Analyze this slow query and suggest improvements"

Source

GitHub Repository

Raw Content

# PostgreSQL MCP Server

Official MCP server for PostgreSQL database integration.

## Installation

```bash
npm install -g @modelcontextprotocol/server-postgres
```

## Configuration

Add to your Claude configuration:

```json
{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://user:password@localhost:5432/database"
      ]
    }
  }
}
```

## Available Tools

### query
Execute a SQL query.

```json
{
  "sql": "SELECT * FROM users WHERE active = true LIMIT 10"
}
```

### list_tables
List all tables in the database.

```json
{}
```

### describe_table
Get table schema information.

```json
{
  "table": "users"
}
```

## Resources

The server exposes database schema as resources:

- `postgres://tables` - List of all tables
- `postgres://schema/{table}` - Schema for specific table

## Security Best Practices

1. **Use Read-Only Credentials**
   ```sql
   CREATE ROLE claude_readonly LOGIN PASSWORD 'secure_password';
   GRANT CONNECT ON DATABASE mydb TO claude_readonly;
   GRANT USAGE ON SCHEMA public TO claude_readonly;
   GRANT SELECT ON ALL TABLES IN SCHEMA public TO claude_readonly;
   ```

2. **Limit Access**
   - Only grant access to necessary schemas
   - Exclude sensitive tables
   - Use connection pooling

3. **Audit Queries**
   - Enable PostgreSQL query logging
   - Monitor for unusual patterns

## Use Cases

- Data exploration and analysis
- Schema documentation
- Query optimization suggestions
- Data quality checks
- Report generation

## Example Workflows

### Analyze Table Structure
```
"Describe the users table and suggest indexes for common query patterns"
```

### Data Quality Check
```
"Find any NULL values in required fields across all tables"
```

### Query Optimization
```
"Analyze this slow query and suggest improvements"
```

## Source

[GitHub Repository](https://github.com/modelcontextprotocol/servers)