PostgreSQL MCP Server (Enhanced)
A Model Context Protocol server providing both read and write access to PostgreSQL databases, enabling LLMs to query data, modify records, and manage database schemas.
GarethCott
README
PostgreSQL MCP Server (Enhanced)
A Model Context Protocol server that provides both read and write access to PostgreSQL databases. This server enables LLMs to inspect database schemas, execute queries, modify data, and create/modify database schema objects.
Note: This is an enhanced version of the original PostgreSQL MCP server by Anthropic. The original server provides read-only access, while this enhanced version adds write capabilities and schema management.
Components
Tools
Data Query
- query
- Execute read-only SQL queries against the connected database
- Input:
sql
(string): The SQL query to execute - All queries are executed within a READ ONLY transaction
Data Modification
-
execute
- Execute a SQL statement that modifies data (INSERT, UPDATE, DELETE)
- Input:
sql
(string): The SQL statement to execute - Executed within a transaction with proper COMMIT/ROLLBACK handling
-
insert
- Insert a new record into a table
- Input:
table
(string): The table namedata
(object): Key-value pairs where keys are column names and values are the data to insert
-
update
- Update records in a table
- Input:
table
(string): The table namedata
(object): Key-value pairs for the fields to updatewhere
(string): The WHERE condition to identify records to update
-
delete
- Delete records from a table
- Input:
table
(string): The table namewhere
(string): The WHERE condition to identify records to delete
Schema Management
-
createTable
- Create a new table with specified columns and constraints
- Input:
tableName
(string): The table namecolumns
(array): Array of column definitions with name, type, and optional constraintsconstraints
(array): Optional array of table-level constraints
-
createFunction
- Create a PostgreSQL function/procedure
- Input:
name
(string): Function nameparameters
(string): Function parametersreturnType
(string): Return typelanguage
(string): Language (plpgsql, sql, etc.)body
(string): Function bodyoptions
(string): Optional additional function options
-
createTrigger
- Create a trigger on a table
- Input:
name
(string): Trigger nametableName
(string): Table to apply trigger tofunctionName
(string): Function to callwhen
(string): BEFORE, AFTER, or INSTEAD OFevents
(array): Array of events (INSERT, UPDATE, DELETE)forEach
(string): ROW or STATEMENTcondition
(string): Optional WHEN condition
-
createIndex
- Create an index on a table
- Input:
tableName
(string): Table nameindexName
(string): Index namecolumns
(array): Columns to indexunique
(boolean): Whether the index is uniquetype
(string): Optional index type (BTREE, HASH, GIN, GIST, etc.)where
(string): Optional condition
-
alterTable
- Alter a table structure
- Input:
tableName
(string): Table nameoperation
(string): Operation (ADD COLUMN, DROP COLUMN, etc.)details
(string): Operation details
Resources
The server provides schema information for each table in the database:
- Table Schemas (
postgres://<host>/<table>/schema
)- JSON schema information for each table
- Includes column names and data types
- Automatically discovered from database metadata
Usage with Claude Desktop
To use this server with the Claude Desktop app, add the following configuration to the "mcpServers" section of your claude_desktop_config.json
:
Docker
- when running docker on macos, use host.docker.internal if the server is running on the host network (eg localhost)
- username/password can be added to the postgresql url with
postgresql://user:password@host:port/db-name
- add
?sslmode=no-verify
if you need to bypass SSL certificate verification
{
"mcpServers": {
"postgres": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mcp/postgres",
"postgresql://host.docker.internal:5432/mydb"]
}
}
}
NPX
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://localhost/mydb"
]
}
}
}
Replace /mydb
with your database name.
Example Usage
Query Data
/query SELECT * FROM users LIMIT 5
Insert Data
/insert table="users", data={"name": "John Doe", "email": "john@example.com"}
Update Data
/update table="users", data={"status": "inactive"}, where="id='123'"
Create a Table
/createTable tableName="tasks", columns=[
{"name": "id", "type": "SERIAL", "constraints": "PRIMARY KEY"},
{"name": "title", "type": "VARCHAR(100)", "constraints": "NOT NULL"},
{"name": "created_at", "type": "TIMESTAMP", "constraints": "DEFAULT CURRENT_TIMESTAMP"}
]
Create a Function and Trigger
/createFunction name="update_timestamp", parameters="", returnType="TRIGGER", language="plpgsql", body="BEGIN NEW.updated_at = NOW(); RETURN NEW; END;"
/createTrigger name="set_timestamp", tableName="tasks", functionName="update_timestamp", when="BEFORE", events=["UPDATE"], forEach="ROW"
Building
Docker:
docker build -t mcp/postgres -f Dockerfile .
Security Considerations
- All data modification operations use transactions with proper COMMIT/ROLLBACK handling
- Each operation returns the SQL that was executed for transparency
- The server uses parameterized queries for insert/update operations to prevent SQL injection
License
This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository.
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
MCP Package Docs Server
Facilitates LLMs to efficiently access and fetch structured documentation for packages in Go, Python, and NPM, enhancing software development with multi-language support and performance optimization.
Claude Code MCP
An implementation of Claude Code as a Model Context Protocol server that enables using Claude's software engineering capabilities (code generation, editing, reviewing, and file operations) through the standardized MCP interface.
@kazuph/mcp-taskmanager
Model Context Protocol server for Task Management. This allows Claude Desktop (or any MCP client) to manage and execute tasks in a queue-based system.
Linear MCP Server
Enables interaction with Linear's API for managing issues, teams, and projects programmatically through the Model Context Protocol.
mermaid-mcp-server
A Model Context Protocol (MCP) server that converts Mermaid diagrams to PNG images.
Jira-Context-MCP
MCP server to provide Jira Tickets information to AI coding agents like Cursor

Linear MCP Server
A Model Context Protocol server that integrates with Linear's issue tracking system, allowing LLMs to create, update, search, and comment on Linear issues through natural language interactions.

Sequential Thinking MCP Server
This server facilitates structured problem-solving by breaking down complex issues into sequential steps, supporting revisions, and enabling multiple solution paths through full MCP integration.