Integrations
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.
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:Copy
- Build the server:Copy
- Add 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:
CopyExample using environment variable (if not using CLI arg):
CopyUsing the
--connection-string
CLI argument or thePOSTGRES_CONNECTION_STRING
environment variable makes theconnectionString
argument optional for most tool calls. - Tool-Specific Argument: If a
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.Examplemcp-tools.json
:Copy - 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).
- The server determines the database connection string based on the following precedence:
- 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.
This server cannot be installed
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.
A Model Context Protocol server that enables powerful PostgreSQL database management capabilities including analysis, schema management, data migration, and monitoring through natural language interactions.
Related MCP Servers
- -securityAlicense-qualityA Model Context Protocol server that provides read-only access to PostgreSQL databases. This server enables LLMs to inspect database schemas and execute read-only queries.Last updated -114,98144,966JavaScriptMIT License
- -securityAlicense-qualityA Model Context Protocol server that provides read-only access to PostgreSQL databases with enhanced multi-schema support, allowing LLMs to inspect database schemas across multiple namespaces and execute read-only queries while maintaining schema isolation.Last updated -132JavaScriptMIT License
- -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 -4JavaScript
- -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 -TypeScript