mcp-server-postgresql: A PostgreSQL MCP Server
The Model Context Protocol (MCP) is an open protocol that enables seamless integration between LLM applications and external data sources and tools. Whether you're building an AI-powered IDE, enhancing a chat interface, or creating custom AI workflows, MCP provides a standardized way to connect LLMs with the context they need.
This repository is an example of how to create an MCP server for managing PostgreSQL databases.
Overview
A Model Context Protocol server for interacting with PostgreSQL. It provides tools for executing queries, creating tables, inserting rows of data, and retrieving results.
Components
Tools
The server implements the following tools:
-
execute_query: Execute an arbitrary SQL query- Takes a
querystring - Returns results for SELECT-like statements, or a success message for others
- Takes a
-
create_table: Create a new table- Takes a
nameand aschema(column definitions) - Creates the specified table
- Takes a
-
insert_data: Insert a row of data into a table- Takes a
tablename and adatadict of column values - Inserts the specified row
- Takes a
-
fetch_data: Fetch data by executing a SELECT query- Takes a
querystring - Returns matching rows as a result set
- Takes a
Configuration
The server connects to PostgreSQL using the following environment variables (all are required):
PG_HOST: PostgreSQL hostPG_PORT: PostgreSQL portPG_USER: PostgreSQL userPG_PASSWORD: PostgreSQL passwordPG_DATABASE: PostgreSQL database name
If any of these variables are missing, the server will raise an error during startup. Make sure to set each of these before running the server.
Quickstart
Claude Desktop
On MacOS: ~/Library/Application\ Support/Claude/claude_desktop_config.json
On Windows: %APPDATA%/Claude/claude_desktop_config.json
{
"postgresql": {
"command": "uvx",
"args": [
"mcp-server-postgresql"
]
}
}
Development
Building and Publishing
To prepare the package for distribution:
- Sync dependencies and update lockfile:
uv sync
- Build package distributions:
uv build
This will create source and wheel distributions in the dist/ directory.
- Publish to PyPI:
uv publish
Note: You'll need to set PyPI credentials via environment variables or command flags:
- Token:
--tokenorUV_PUBLISH_TOKEN - Or username/password:
--username/UV_PUBLISH_USERNAMEand--password/UV_PUBLISH_PASSWORD
Debugging
Since MCP servers run over stdio, debugging can be challenging. For the best debugging experience, we strongly recommend using the MCP Inspector.
You can launch the MCP Inspector via npm with this command:
npx @modelcontextprotocol/inspector uv --directory $(PWD) run mcp-server-postgresql
Upon launching, the Inspector will display a URL that you can access in your browser to begin debugging.
Metadata
Release files for mcp-server-postgresql 0.1.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mcp_server_postgresql-0.1.9.tar.gz | 16.0 kB | Without provenance | Details for mcp_server_postgresql-0.1.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mcp_server_postgresql-0.1.9-py3-none-any.whl | Python 3 | none | any | Details for mcp_server_postgresql-0.1.9-py3-none-any.whl |
Total release size: 20.7 kB
Release files / mcp_server_postgresql-0.1.9.tar.gz
| Download URL | mcp_server_postgresql-0.1.9.tar.gz |
|---|---|
| Size | 16.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ad02281aed2c64ac2ba4389ff83eee66b722d9ec993d9397a1f1c1fb61f72c3d
|
|
BLAKE2b-256 checksum How to use checksums |
63a4559553c520573c28108fc65a392a7393b935e542c6367cae3b37648a2a9b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.26
|
Release files / mcp_server_postgresql-0.1.9-py3-none-any.whl
| Download URL | mcp_server_postgresql-0.1.9-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
75e148cde0d7033319edb632d4fad01ec62332023f2b405f43956e53518d4300
|
|
BLAKE2b-256 checksum How to use checksums |
bd40832bc329371021a37b0474352e7d252696eeafa46bad40043ac124654e6c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.26
|
Security for mcp-server-postgresql 0.1.9
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.