Labsco
abiswas97 logo

Postgres MCP Server

โ˜… 1

from abiswas97

Provides secure database access to PostgreSQL using the Kysely ORM.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedAccount requiredNeeds API keys

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

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

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