Supports PostgreSQL installation and configuration on Linux platforms with customized setup instructions tailored to Linux environments.
Provides platform-specific PostgreSQL installation and configuration guidance for macOS systems.
Requires Node.js runtime environment for server operation, with specific version requirements (≥ 18.0.0) for proper functionality.
Provides comprehensive PostgreSQL database management capabilities, including database analysis, schema management, data migration, and real-time monitoring. Enables analyzing configurations, debugging issues, creating and altering tables, exporting/importing data between databases, and monitoring performance metrics.
PostgreSQL MCP Server
A Model Context Protocol (MCP) server that provides PostgreSQL database management capabilities. This server assists with analyzing existing PostgreSQL setups, providing implementation guidance, debugging database issues, managing schemas, migrating data, and monitoring database performance.
Version 0.2.0
Features
The server provides the following tools:
1. Database Analysis and Setup
1.1. Analyze Database (analyze_database
)
Analyzes PostgreSQL database configuration and performance metrics:
Configuration analysis
Performance metrics
Security assessment
Recommendations for optimization
1.2. Get Setup Instructions (get_setup_instructions
)
Provides step-by-step PostgreSQL installation and configuration guidance:
Platform-specific installation steps
Configuration recommendations
Security best practices
Post-installation tasks
1.3. Debug Database (debug_database
)
Debug common PostgreSQL issues:
Connection problems
Performance bottlenecks
Lock conflicts
Replication status
2. Schema Management
2.1. Get Schema Information (get_schema_info
)
Get detailed schema information for a database or specific table:
List of tables in a database
Column definitions
Constraints (primary keys, foreign keys, etc.)
Indexes
2.2. Create Table (create_table
)
Create a new table with specified columns:
Define column names and types
Set nullable constraints
Set default values
2.3. Alter Table (alter_table
)
Modify existing tables:
Add new columns
Modify column types or constraints
Drop columns
2.4. Get Enums (get_enums
)
Get information about PostgreSQL ENUM types.
2.5. Create Enum (create_enum
)
Create a new ENUM type in the database.
3. Data Migration
3.1. Export Table Data (export_table_data
)
Export table data to JSON or CSV format:
Filter data with WHERE clause
Limit number of rows
Choose output format
3.2. Import Table Data (import_table_data
)
Import data from JSON or CSV files:
Optionally truncate table before import
Support for different formats
Custom CSV delimiters
3.3. Copy Between Databases (copy_between_databases
)
Copy data between two PostgreSQL databases:
Filter data with WHERE clause
Optionally truncate target table
4. Monitoring
4.1. Monitor Database (monitor_database
)
Real-time monitoring of PostgreSQL database:
Database metrics (connections, cache hit ratio, etc.)
Table metrics (size, row counts, dead tuples)
Active query information
Lock information
Replication status
Configurable alerts
5. Functions
5.1. Get Functions (get_functions
)
Get information about PostgreSQL functions.
5.2. Create Function (create_function
)
Create or replace a PostgreSQL function.
5.3. Drop Function (drop_function
)
Drop a PostgreSQL function.
6. Row-Level Security (RLS)
6.1. Enable RLS (enable_rls
)
Enable Row-Level Security on a table.
6.2. Disable RLS (disable_rls
)
Disable Row-Level Security on a table.
6.3. Create RLS Policy (create_rls_policy
)
Create a Row-Level Security policy.
6.4. Edit RLS Policy (edit_rls_policy
)
Edit an existing Row-Level Security policy.
6.5. Drop RLS Policy (drop_rls_policy
)
Drop a Row-Level Security policy.
6.6. Get RLS Policies (get_rls_policies
)
Get Row-Level Security policies.
7. Triggers
7.1. Get Triggers (get_triggers
)
Get information about PostgreSQL triggers.
7.2. Create Trigger (create_trigger
)
Create a PostgreSQL trigger.
7.3. Drop Trigger (drop_trigger
)
Drop a PostgreSQL trigger.
7.4. Set Trigger State (set_trigger_state
)
Enable or disable a PostgreSQL trigger.
Prerequisites
Node.js >= 18.0.0
PostgreSQL server (for target database operations)
Network access to target PostgreSQL instances
Installation
Installing via Smithery
To install postgresql-mcp-server for Claude Desktop automatically via Smithery:
Manual Installation
Clone the repository
Install dependencies:
npm installBuild the server:
npm run buildAdd to MCP settings file (e.g., in your IDE's settings or a global MCP configuration):
There are a few ways to configure the connection string for the server, with the following order of precedence:
Tool-Specific Argument: If a
connectionString
is provided directly in the arguments when calling a specific tool, that value will be used for that call.CLI Argument: You can provide a default connection string when starting the server using the
-cs
or--connection-string
argument.Environment Variable: If neither of the above is provided, the server will look for a
POSTGRES_CONNECTION_STRING
environment variable.
If no connection string is found through any of these methods, tools requiring a database connection will fail.
Example using CLI argument in MCP settings:
{ "mcpServers": { "postgresql-mcp": { "command": "node", "args": [ "/path/to/postgresql-mcp-server/build/index.js", "--connection-string", "postgresql://username:password@server:port/dbname" // Optionally, add "--tools-config", "/path/to/your/mcp-tools.json" ], "disabled": false, "alwaysAllow": [] // Note: 'env' block for POSTGRES_CONNECTION_STRING can still be used as a fallback // if --connection-string is not provided in args. } } }Example using environment variable (if not using CLI arg):
{ "mcpServers": { "postgresql-mcp": { "command": "node", "args": [ "/path/to/postgresql-mcp-server/build/index.js" // Optionally, add "--tools-config", "/path/to/your/mcp-tools.json" ], "disabled": false, "alwaysAllow": [], "env": { "POSTGRES_CONNECTION_STRING": "postgresql://username:password@server:port/dbname" } } } }Using the
Tool Configuration
The server supports filtering which tools are enabled via an external JSON configuration file.
CLI Option: Use
-tc <path>
or--tools-config <path>
to specify the path to your tools configuration file.File Format: The JSON file should contain an object with an
enabledTools
key, which holds an array of tool name strings.Example
{ "enabledTools": [ "get_schema_info", "analyze_database", "export_table_data" ] }Behavior:
If the configuration file is provided and valid, only the listed tools will be enabled.
If the file is not provided, is invalid, or cannot be read, all tools will be enabled by default.
The server will log which tools are enabled based on this configuration.
Development
npm run dev
- Start development server with hot reloadnpm run lint
- Run ESLintnpm test
- Run tests (if configured)
Security Considerations
Connection Security
The server determines the database connection string based on the following precedence:
connectionString
provided directly in a tool's arguments.--connection-string
CLI argument used when starting the server.POSTGRES_CONNECTION_STRING
environment variable.
Ensure that connection strings (especially those with credentials) are managed securely.
Uses connection pooling via
pg
(previously@vercel/postgres
).Validates connection strings.
Supports SSL/TLS connections (configure via connection string).
Query Safety
Executes predefined operations; avoids arbitrary SQL execution where possible.
Uses parameterized queries where applicable to prevent SQL injection.
Logs operations for auditing.
Authentication
Relies on PostgreSQL's authentication mechanisms via the connection string.
Securely manage your database credentials. Do not hardcode them in client requests if avoidable; prefer using the
--connection-string
CLI option or thePOSTGRES_CONNECTION_STRING
environment variable when configuring the server.
Best Practices
Configure the default database connection string securely using the
--connection-string
CLI option or thePOSTGRES_CONNECTION_STRING
environment variable.If a tool needs to connect to a different database than the default, provide the
connectionString
directly in that tool's arguments.Always use secure connection strings with proper credentials, preferably configured via the
POSTGRES_CONNECTION_STRING
environment variable.Follow production security recommendations for sensitive environments.
Regularly monitor and analyze database performance using the
monitor_database
tool.Keep PostgreSQL version up to date.
Implement proper backup strategies independently.
Use connection pooling for better resource management (handled internally).
Implement proper error handling and logging.
Regular security audits and updates.
Error Handling
The server implements error handling for:
Connection failures
Query errors
Invalid inputs
Permission issues
Errors are returned in the standard MCP error format.
Contributing
Fork the repository
Create a feature branch
Commit your changes
Push to the branch
Create a Pull Request
License
This project is licensed under the AGPLv3 License - see LICENSE file for details.
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
Tools
A Model Context Protocol server that enables powerful PostgreSQL database management capabilities including analysis, schema management, data migration, and monitoring through natural language interactions.
- Version 0.2.0
- Features
- Prerequisites
- Installation
- Tool Configuration
- Development
- Security Considerations
- Best Practices
- Error Handling
- Contributing
- License
Related Resources
Related MCP Servers
- -securityFlicense-qualityA Model Context Protocol server providing both read and write access to PostgreSQL databases, enabling LLMs to query data, modify records, and manage database schemas.Last updated -5837
- -securityFlicense-qualityA Model Context Protocol server that enables performing PostgreSQL database operations (create, read, update, delete) on User and Post entities through MCP tools.Last updated -
- -securityAlicense-qualityA Model Context Protocol server that enables interaction with PostgreSQL databases to list tables, retrieve schemas, and execute read-only SQL queries.Last updated -26MIT License
- AsecurityAlicenseAqualityA Model Context Protocol server that enables interaction with PostgreSQL databases for analyzing setups, debugging issues, managing schemas, migrating data, and monitoring performance.Last updated -101MIT License