Postgres MCP Server

Provides secure database access to PostgreSQL using the Kysely ORM.

Postgres MCP Server

npm version Tests GitHub issues

A Model Context Protocol (MCP) server that provides secure database access to PostgreSQL through Kysely ORM. This server enables Claude Desktop to interact with PostgreSQL databases using natural language.

Features

  • MCP Tools: Query execution, table listing, schema inspection, and constraint information
  • Type Safety: Full TypeScript support with typed inputs/outputs
  • Connection Pooling: Configurable connection limits with idle timeout
  • Error Handling: Graceful error messages for connection and query issues
  • Security: Parameterized queries to prevent SQL injection

Installation

npx postgres-mcp-server

Configuration

Create a .env file with your database credentials:

DB_HOST=127.0.0.1
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password_here
DB_NAME=postgres
DB_SSL=true

Available Tools

ToolDescriptionRequired ParametersOptional Parameters
queryExecute SQL queries with pagination supportsql (string)pageSize (1-500), offset (number), parameters (array)
describe_tableGet table structure and column detailsschema (string), table (string)-
list_tablesList all tables in a schemaschema (string)-
list_schemasList all schemas in the database-includeSystemSchemas (boolean)
get_constraintsGet table constraints (PK, FK, etc.)schema (string), table (string)-
list_indexesList indexes for a table or schemaschema (string)table (string)
list_viewsList views in a schemaschema (string)-
list_functionsList functions and proceduresschema (string)-
explain_queryGet query execution plansql (string)analyze (boolean), format (text/json/xml/yaml)
get_table_statsGet table size and statisticsschema (string)table (string)

Key Features

  • Pagination: Query tool supports up to 500 rows per page with automatic LIMIT/OFFSET handling
  • Security: Parameterized queries prevent SQL injection, READ_ONLY mode by default
  • Type Safety: Full TypeScript support with Zod schema validation

Claude Desktop Configuration

Add this server to your Claude Desktop configuration file:

Edit claude_desktop_config.json:

{
  "mcpServers": {
    "postgres-mcp-server": {
      "command": "npx",
      "args": ["postgres-mcp-server"],
      "env": {
        "DB_HOST": "127.0.0.1",
        "DB_PORT": "5432",
        "DB_USER": "postgres",
        "DB_PASSWORD": "your_password_here",
        "DB_NAME": "your_database_name",
        "DB_SSL": "true"
      }
    }
  }
}

Configuration File Locations

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json

Development

# Clone and install dependencies
git clone https://github.com/abiswas97/postgres-mcp-server.git
cd postgres-mcp-server
npm install

# Run in development mode with hot reload
npm run dev

# Build for production
npm run build

# Run tests
npm run test

# Run specific test suites
npm run test:unit
npm run test:integration

Environment Variables

VariableDefaultDescription
DB_HOST127.0.0.1PostgreSQL host
DB_PORT5432PostgreSQL port
DB_USERpostgresDatabase user
DB_PASSWORDrequiredDatabase password
DB_NAMEpostgresDatabase name
DB_SSLtrueEnable SSL connection
READ_ONLYtrueRestrict to SELECT/WITH/EXPLAIN queries
QUERY_TIMEOUT30000Query timeout in milliseconds
MAX_PAGE_SIZE500Maximum rows per page
DEFAULT_PAGE_SIZE100Default page size when not specified

License

ISC

Máy chủ liên quan

NotebookLM Web Importer

Nhập trang web và video YouTube vào NotebookLM chỉ với một cú nhấp. Được tin dùng bởi hơn 200.000 người dùng.

Cài đặt tiện ích Chrome