PostgreSQL MCP Server
Uses Docker for running test containers, enabling comprehensive testing with real PostgreSQL database instances.
Supports configuration through environment variables using .env files, allowing users to easily configure database connection details and other settings.
Uses npm for package management, installation, and running scripts for development, testing, and production builds.
Provides AI assistants with secure, structured access to PostgreSQL databases. Includes tools for querying, inserting, updating, and deleting data with schema introspection, filtering, pagination, sorting, and safety features.
Built with TypeScript, providing type safety and better code organization for the MCP server implementation.
Includes comprehensive testing using Vitest in combination with Testcontainers for real PostgreSQL database testing.
Implements input validation with Zod schemas for comprehensive error handling and type safety.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@PostgreSQL MCP Server show me the schema for the users table"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
PostgreSQL MCP Server
A TypeScript-based Model Context Protocol (MCP) server that provides AI assistants with secure, structured access to PostgreSQL databases.
Features
Safe Database Operations: All queries use parameterized statements to prevent SQL injection
Comprehensive Tools: Query, insert, update, delete data with full schema introspection
Flexible Querying: Support for filtering, pagination, sorting, and complex WHERE conditions
Schema Discovery: Get detailed information about tables, columns, constraints, and indexes
Connection Pooling: Efficient database connection management
Error Handling: Comprehensive error reporting without exposing sensitive information
Safety Checks: Required WHERE clauses for updates/deletes, confirmation for large operations
Related MCP server: SQL MCP Server
Tools Available
query-table
Query data from a specific table with filtering, pagination, and sorting.
Parameters:
table(string, required): Table name to querycolumns(string[], optional): Specific columns to select (default: all)where(object, optional): WHERE conditions (supports equality, arrays for IN, wildcards for LIKE)pagination(object, optional):{limit: number, offset: number}sort(object, optional):{column: string, direction: "ASC"|"DESC"}
get-schema
Get database schema information including tables, columns, and constraints.
Parameters:
schema_name(string, optional): Schema to inspect (default: "public")table_pattern(string, optional): LIKE pattern for table namesinclude_columns(boolean, optional): Include column details (default: true)include_constraints(boolean, optional): Include constraint details (default: false)
execute-query
Execute a parameterized SQL query with safety checks.
Parameters:
query(string, required): SQL query with parameter placeholders ($1, $2, etc.)params(any[], optional): Parameters for the queryexplain(boolean, optional): Include execution plan (default: false)
insert-data
Insert new records into a table.
Parameters:
table(string, required): Target table namedata(object|object[], required): Data to insert (single record or array)on_conflict(string, optional): Conflict resolution: "error", "ignore", "update" (default: "error")conflict_columns(string[], optional): Columns to check for conflictsreturning(string[], optional): Columns to return (default: ["*"])
update-data
Update existing records in a table.
Parameters:
table(string, required): Target table namedata(object, required): Data to updatewhere(object, required): WHERE conditions (required for safety)returning(string[], optional): Columns to return (default: ["*"])
delete-data
Delete records from a table.
Parameters:
table(string, required): Target table namewhere(object, required): WHERE conditions (required for safety)confirm_delete(boolean, optional): Bypass confirmation for large deletesreturning(string[], optional): Columns to return from deleted records
get-table-info
Get detailed information about a specific table.
Parameters:
table(string, required): Table nameschema_name(string, optional): Schema name (default: "public")include_statistics(boolean, optional): Include size and row count stats (default: true)
connection-status
Check database connection status, view error details, and retry connection.
Parameters:
retry(boolean, optional): Attempt to reconnect if connection is currently failed (default: false)
Returns:
Current connection status ("connected", "failed", or "unknown")
Error details if connection failed
Last connection attempt timestamp
Troubleshooting information for failed connections
Result of retry attempt if retry was requested
Installation
npm installConfiguration
Create a .env file based on .env.example:
cp .env.example .envSet your PostgreSQL connection details:
# Required: PostgreSQL connection string
DATABASE_URL=postgresql://username:password@localhost:5432/database_name
# Optional: Individual connection parameters
# POSTGRES_HOST=localhost
# POSTGRES_PORT=5432
# POSTGRES_DB=database_name
# POSTGRES_USER=username
# POSTGRES_PASSWORD=password
# Optional: Environment and debugging
NODE_ENV=development
DEBUG=postgres-mcp*
# Optional: Connection pool settings
MAX_CONNECTIONS=20
QUERY_TIMEOUT=30000Usage
Development
# Start in development mode with auto-reload
npm run dev
# Or start normally
npm startProduction
# Build the project
npm run build
# Run the built version
node dist/index.jsAs an MCP Server
Add to your MCP client configuration (e.g., Claude Desktop):
{
"mcpServers": {
"postgres": {
"command": "node",
"args": ["/path/to/postgres-mcp/dist/index.js"],
"env": {
"DATABASE_URL": "postgresql://username:password@localhost:5432/database_name"
}
}
}
}Security Considerations
Parameterized Queries: All SQL operations use parameter binding to prevent injection attacks
Identifier Validation: Table and column names are validated against PostgreSQL naming rules
Required WHERE Clauses: UPDATE and DELETE operations require WHERE conditions for safety
Large Operation Warnings: Confirmation required for operations affecting >100 rows
Connection Security: Use SSL connections in production environments
Access Control: Configure database-level permissions appropriately
Database Permissions
The database user should have appropriate permissions for the operations you want to allow:
-- For read-only access
GRANT SELECT ON ALL TABLES IN SCHEMA public TO your_user;
-- For full access
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO your_user;
-- For schema introspection
GRANT USAGE ON SCHEMA information_schema TO your_user;
GRANT SELECT ON ALL TABLES IN SCHEMA information_schema TO your_user;Example Usage
Once connected through an MCP client:
AI: Can you show me the structure of the users table?
Assistant: I'll get the table information for you.
[Uses get-table-info tool]
The users table has the following structure:
- id (integer, primary key)
- email (varchar, unique, not null)
- name (varchar)
- created_at (timestamp with time zone)
...AI: Find all users created in the last 7 days
Assistant: I'll query the users table for recent records.
[Uses query-table tool with WHERE condition]
Found 15 users created in the last 7 days:
...Development
Testing
This project includes comprehensive tests using Vitest and Testcontainers for real PostgreSQL database testing.
Prerequisites:
Docker must be installed and running (for testcontainers)
Run all tests:
npm testRun tests in watch mode:
# Watch mode for development
npm run test:watchType Checking
npm run typecheckBuilding
npm run buildArchitecture
index.ts: Main server entry point and tool registration
tools/utils.ts: Shared utilities, database connection, and helper functions
tools/*.ts: Individual tool implementations
tests/: Comprehensive test suite
tsup.config.ts: Build configuration
tsconfig.json: TypeScript configuration
vitest.config.ts: Test configuration
Error Handling
All tools include comprehensive error handling:
Input validation with Zod schemas
Database connection error handling
SQL execution error handling
Graceful error responses to MCP clients
Graceful startup: Server starts even if database is unavailable
Connection recovery: Ability to retry connections without restarting
Connection Troubleshooting
If the database connection fails at startup or during operation, the server will continue running and provide helpful error information through the connection-status tool.
Common connection issues:
Database server not running: Ensure PostgreSQL is running and accessible
Invalid credentials: Check username, password, and database name in your configuration
Network connectivity: Verify host, port, and firewall settings
SSL/TLS issues: Check SSL configuration for production environments
Connection string format: Ensure DATABASE_URL follows the correct format
To diagnose and fix connection issues:
Check connection status:
Use the connection-status tool to see current status and error detailsVerify configuration:
# Check your .env file or environment variables echo $DATABASE_URL # Should look like: postgresql://username:password@host:port/databaseTest manually:
# Test connection with psql psql $DATABASE_URL -c "SELECT 1;"Retry connection:
Use the connection-status tool with retry: true to attempt reconnection
Graceful degradation:
If database connection fails, all database tools will return helpful error messages
Error messages include specific troubleshooting steps
Tools automatically guide users to use
connection-statusfor diagnosis and retryOnce connection is restored (via retry), all tools resume normal operation
Contributing
Follow the existing code patterns
Add proper TypeScript types
Include error handling
Test with a real PostgreSQL database
Update documentation as needed
License
MIT License
Available Tools
8 toolsconnection-statusA
Check database connection status, view error details, and retry connection. Use retry: true to attempt reconnection when database is unavailable.
Inputs: retry.
delete-dataA
Delete records from a table. Requires WHERE conditions for safety. Includes confirmation prompt for large deletions.
Inputs: table (required), where (required), confirm_delete, returning.
execute-queryC
Execute a parameterized SQL query with safety checks. Supports SELECT, INSERT, UPDATE, DELETE operations with parameter binding to prevent SQL injection.
Inputs: query (required), params, explain.
get-schemaB
Get database schema information including tables, columns, data types, and optionally constraints. Useful for understanding database structure.
Inputs: schema_name, table_pattern, include_columns, include_constraints.
get-table-infoB
Get detailed information about a specific table including columns, constraints, indexes, and optionally statistics like row count and size.
Inputs: table (required), schema_name, include_statistics.
insert-dataC
Insert new records into a table. Supports single or multiple records, conflict resolution (ignore/update), and returning inserted data.
Inputs: table (required), data (required), on_conflict, conflict_columns, +1 more.
query-tableC
Query data from a specific table with filtering, pagination, and sorting. Supports WHERE conditions with exact matches, arrays (IN), and LIKE patterns.
Inputs: table (required), columns, where, pagination, +1 more.
update-dataB
Update existing records in a table. Requires WHERE conditions for safety. Supports complex WHERE clauses and returns updated records.
Inputs: table (required), data (required), where (required), returning.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 8 tools
Most tools have distinct purposes, but there is some overlap between 'execute-query' and 'query-table' that could cause confusion. 'execute-query' handles general SQL operations, while 'query-table' is specifically for querying tables with filtering, but both can be used for SELECT queries, potentially leading to misselection. Other tools like 'insert-data', 'update-data', and 'delete-data' are clearly differentiated by their CRUD operations.
Tool names follow a consistent verb-noun pattern with hyphens throughout, such as 'execute-query', 'get-schema', and 'update-data'. All tools adhere to this naming convention, making them predictable and easy to understand. There are no deviations or mixed styles like camelCase or snake_case, ensuring high readability.
With 8 tools, the count is well-scoped for a PostgreSQL server, covering essential database operations like connection management, CRUD, schema inspection, and query execution. Each tool serves a clear purpose without redundancy, and the number is neither too thin nor overwhelming, fitting typical server needs in the 3-15 tool range.
The tool set provides comprehensive coverage for core PostgreSQL operations, including connection status, schema retrieval, CRUD operations, and query execution. Minor gaps exist, such as the lack of tools for managing database objects (e.g., creating/dropping tables or indexes) or handling transactions, but agents can work around these using 'execute-query' for most missing functions.
Maintenance
Related MCP Connectors
Hosted PostgreSQL MCP server for Claude, ChatGPT and Cursor, with each action switched per account.
A comprehensive Model Context Protocol (MCP) server that enables AI assistants to interact with yo…
Xata MCP server lets AI agents interact with your Xata projects, and Postgres database branches.
Text-to-SQL MCP server: read-only queries on PostgreSQL, MySQL and SQL Server with your real schema
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceA Model Context Protocol server providing both read and write access to PostgreSQL databases, enabling LLMs to query data, modify records, and manage database schemas.241 npm10MIT
- AlicenseNot gradedqualityDmaintenanceA TypeScript implementation of a Model Context Protocol server that enables language models to securely query PostgreSQL databases, including those behind SSH bastion tunnels.27 npm1MIT
- AlicenseNot gradedqualityDmaintenanceA Model Context Protocol server providing dual transport (HTTP and Stdio) access to PostgreSQL databases, allowing AI assistants to query databases and fetch schema information through natural language.49 npm31MIT
- AlicenseNot gradedqualityDmaintenanceA Model Context Protocol server for PostgreSQL databases that enables AI agents to connect, query, and explore multiple databases with schema discovery and extension context.541MIT