Discover read-only PostgreSQL access for schemas and queries with Model Context Protocol server integrations
PostgreSQL is an MCP (Model Context Protocol) server tailored to provide read-only access to database systems, enabling advanced Artificial Intelligence (AI) applications like Claude Desktop and Continue to inspect schema information and execute read-only SQL queries. This server acts as a bridge between these AI tools and the underlying PostgreSQL databases, facilitating seamless data integration without compromising security or data integrity.
The PostgreSQL MCP Server supports executing SQL queries within a READ ONLY transaction. The query
tool is designed to interact with the connected database by accepting an input string representing the SQL query to be executed. This feature enables AI applications to perform data analyses, fetch specific records, and other read-only operations without making permanent changes.
PostgreSQL MCP Server provides a comprehensive schema for each table in the connected PostgreSQL databases. The server automatically discovers and maps out columns, their names, and associated data types. These resources can be accessed via postgres://<host>/<table>/schema
, offering structured data information that is essential for AI applications needing to understand database schemas before querying.
The PostgreSQL server is built with a microservices architecture, leveraging Docker and Node.js for deployment flexibility. This setup ensures that the server can be easily customized and integrated into various development pipelines and environments. The server implements the Model Context Protocol (MCP), adhering strictly to the standard API and data transmission rules defined by MCP.
All interactions with the PostgreSQL database are conducted within a READ ONLY transaction, ensuring no harmful changes can be made through query execution. This feature aligns with security best practices for AI applications that require read-only access to sensitive databases.
To set up and use this server, follow these steps:
For users running Docker on macOS or Windows:
host.docker.internal
as the hostname if the server is running on the host network (e.g., localhost).{
"mcpServers": {
"postgres": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mcp/postgres",
"postgresql://host.docker.internal:5432/mydb"
]
}
}
}
Replace /mydb
with your database name.
Alternatively, the server can be run using a Node.js package manager like npx
.
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://localhost/mydb"
]
}
}
}
Again, replace /mydb
with your database name.
Imagine an AI application that needs to analyze customer data from a PostgreSQL database during the training phase. The PostgreSQL MCP Server can be configured to provide read-only access to specific tables, allowing the AI to query this data without altering any records.
A developer using an AI tool like Continue might want to monitor changes in a database schema frequently without having direct read permissions on the production database. The PostgreSQL MCP Server can be set up to provide temporary or long-term access based on specific needs, ensuring that sensitive data remains protected.
PostgreSQL MCP Server seamlessly integrates with several popular MCP clients:
+----------------+------------+-------+---------+
| MCP Client | Resources | Tools | Prompts |
+----------------+------------+-------+---------+
| Claude Desktop | ✅ | ✅ | ✅ |
| Continue | ✅ | ✅ | ✅ |
| Cursor | ❌ | ✅ | ❌ |
+----------------+------------+-------+---------+
To further enhance security and performance, users can customize the server configuration using environment variables. For example:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://localhost/mydb"
],
"env": {
"API_KEY": "your-api-key"
}
}
}
}
Why is the server limited to read-only access?
Can I use this with other MCP clients besides those listed?
How do I secure my API key when using environment variables?
Is it possible to configure multiple database connections in a single configuration file?
mcpServers
.What happens if the server is shut down abruptly while running queries?
For developers looking to contribute to or extend this PostgreSQL MCP Server project:
Fork the Repository: Clone this repository from GitHub.
Set Up Local Environment: Install Docker and Node.js dependencies as needed.
Run Tests: Ensure that all test cases pass before making changes.
Commit Changes: Follow the commit message guidelines, providing clear descriptions of your contributions.
Pull Requests: Submit pull requests for new features or bug fixes, ensuring comprehensive documentation updates are included.
Stay connected with the broader Model Context Protocol community:
By leveraging the PostgreSQL MCP Server, developers can integrate powerful database capabilities into their AI applications, ensuring that data is accessed securely while maintaining the integrity of the underlying systems.
RuinedFooocus is a local AI image generator and chatbot image server for seamless creative control
Simplify MySQL queries with Java-based MysqlMcpServer for easy standard input-output communication
Learn to set up MCP Airflow Database server for efficient database interactions and querying airflow data
Build stunning one-page websites track engagement create QR codes monetize content easily with Acalytica
Explore CoRT MCP server for advanced self-arguing AI with multi-LLM inference and enhanced evaluation methods
Access NASA APIs for space data, images, asteroids, weather, and exoplanets via MCP integration