Get the FREE Ultimate OpenClaw Setup Guide →

mcp-postgres

MCP PostgreSQL Server

Installation
Run this command in your terminal to add the MCP server to Claude Code.
Run in terminal:
Command
claude mcp add --transport stdio antonorlov-mcp-postgres-server npx -y mcp-postgres-server \
  --env PG_HOST="your_host" \
  --env PG_PORT="5432" \
  --env PG_USER="your_user" \
  --env PG_DATABASE="your_database" \
  --env PG_PASSWORD="your_password"

How to use

This MCP server provides a PostgreSQL interface through a standardized Model Context Protocol (MCP) API. It exposes tools to connect to a PostgreSQL database, run queries, and manage schema and table information. Use it to securely connect, execute prepared statements, and retrieve results in a structured format. The available tools include: connect_db to establish a database connection with credentials, query to run SELECT statements with optional parameters, execute to perform INSERT/UPDATE/DELETE operations with prepared statements, list_schemas to list all schemas, list_tables to list tables in a schema, and describe_table to get the structure of a specific table. These tools are designed to support both PostgreSQL-style placeholders ($1, $2) and MySQL-style (?) placeholders, with automatic handling of parameter binding. You can mix and match tools to construct complex workflows, such as connecting to a database, listing available tables, describing a table, and then executing parameterized updates or queries as part of an automated process.

How to install

Prerequisites:

  • Node.js and npm installed on your system
  • Access to install npm packages

Installation steps:

  1. Install the MCP PostgreSQL server package locally: npm install mcp-postgres-server

  2. OR run directly with npx (no local install required): npx mcp-postgres-server

  3. Configure the MCP server by setting the required environment variables as shown in the configuration example. These variables provide the connection details to your PostgreSQL database.

  4. If you are using MCP in another orchestration, ensure the mcp_config format is loaded by your MCP runner, providing the postgres server entry with the proper environment values.

Additional notes

Tips and considerations:

  • Ensure PG_HOST, PG_PORT, PG_USER, PG_PASSWORD, and PG_DATABASE are correctly set to connect to your PostgreSQL instance.
  • The server supports prepared statements to mitigate SQL injection risks; use the query and execute tools with the params array for safe parameter binding.
  • Placeholders can be PostgreSQL-style ($1, $2) or MySQL-style (?); the tool will adapt accordingly.
  • Connections are automatically managed and cleaned up after operations to prevent leaks.
  • If you encounter connection errors, verify network access to the PostgreSQL host and confirm that the database user has appropriate privileges.
  • For security, avoid embedding plaintext credentials in logs; rely on environment variables and secure secret management when deploying.
  • The server supports multiple schemas and provides list_schemas, list_tables, and describe_table to explore database metadata before performing data operations.

Related MCP Servers

Sponsor this space

Reach thousands of developers