How to Create an MCP Server for PostgreSQL (Step-by-Step)
Why Connect AI to Your PostgreSQL Database?
PostgreSQL is the backbone of thousands of applications — from SaaS platforms and analytics pipelines to e-commerce backends and financial systems. The data sitting inside those tables holds answers to questions your team asks every day: What were last month's top-selling products? Which customers are at risk of churning? How does this quarter compare to the same period last year?
Traditionally, getting AI to answer those questions meant building custom scripts, writing API wrappers, or manually exporting data into CSV files for analysis. Each approach creates maintenance overhead, security gaps, and brittle integrations that break whenever the schema changes.
The Model Context Protocol (MCP) changes this entirely. MCP provides a standardized way for AI models to discover and call tools — including tools that query your database. Instead of writing glue code, you generate an MCP server that exposes your PostgreSQL tables as structured, permission-controlled tools that any AI agent can use.
With Orckai's MCP Server Generator, you can go from a PostgreSQL connection string to a fully deployed, Docker-containerized MCP server in under five minutes. Here is exactly how to do it.
Prerequisites
Before you begin, make sure you have the following:
- An Orckai account — sign up at orckai.app if you do not have one yet. The free tier includes MCP server generation.
- A PostgreSQL database — version 12 or later. You will need the host, port, database name, username, and password. If your database is behind a firewall, ensure Orckai's IP can reach it, or use a tunnel.
- A basic understanding of what you want AI to access — think about which tables contain the data your agents need, and which columns might contain sensitive information you want to exclude.
Step 1: Connect Your Database
Log in to Orckai and navigate to MCP Servers in the left sidebar. Click Create MCP Server and select Database as the source type. Choose PostgreSQL from the database dropdown.
Enter your connection details:
- Host — the hostname or IP address of your PostgreSQL server (e.g.,
db.example.comor10.0.1.50) - Port — the port PostgreSQL is listening on (default is
5432) - Database — the name of the database to connect to (e.g.,
production_db) - Username — a database user with read access to the tables you want to expose
- Password — the password for that user
Click Test Connection. Orckai will attempt to connect to your PostgreSQL instance, verify credentials, and read the information_schema to discover all available tables and columns. If the connection succeeds, you will see a green checkmark and a summary of discovered tables.
Tip: Use a dedicated read-only database user for your MCP server. This limits what the AI can do even if a tool definition is misconfigured, and it keeps your audit trail clean.
Step 2: Select Tables and Permissions
After a successful connection, Orckai displays every table and view in your database as a tree structure. Each table can be toggled on or off, and each column within a table can be individually included or excluded.
This is where you make critical security decisions:
- Include only the tables your agents need. If your agents are answering sales questions, include
orders,products, andcustomers— but excludeemployee_salariesoraudit_logs. - Hide PII columns. Exclude columns like
email,ssn,phone_number, orcredit_cardfrom any table where they exist. The generated MCP server will not have tools or parameters for these columns — the data never reaches the AI model. - Set read-only vs. read-write per table. For most use cases, read-only access is sufficient and far safer. Only enable write access if your agent genuinely needs to insert or update records.
Take your time with this step. The permissions you configure here are baked into the generated server code at design time, not enforced at runtime. That means there is no filter to bypass — excluded data simply does not exist in the MCP server.
Step 3: Generate the MCP Server
Once you have selected your tables and configured column permissions, click Generate. Orckai reads your permission selections and generates a complete MCP server with the following:
- Typed tool definitions — one tool per included table with input parameters matching the exposed columns
- SQL parameterization — all queries use parameterized statements to prevent SQL injection, even if the AI model passes unexpected input
- Input validation — each parameter is validated against its PostgreSQL column type (text, integer, timestamp, boolean, etc.)
- Pagination support — tools accept
limitandoffsetparameters to handle large result sets without overwhelming the AI context window
Generation typically completes in under 30 seconds. Orckai shows you a preview of the generated tool definitions so you can verify everything looks correct before deployment.
Step 4: Deploy as a Docker Container
Click Deploy. Orckai packages your MCP server into a Docker image, builds the container, connects it to the internal network, and starts it — all automatically. You do not need to write a Dockerfile, configure networking, or manage container registries.
Within seconds, the MCP server is running and healthy. You can verify this on the MCP Servers dashboard, where each server shows its status (running, stopped, error), container ID, and the list of tools it exposes. A built-in health endpoint is polled continuously so you are alerted if anything goes wrong.
Step 5: Attach to an Agent
Now that your MCP server is running, you need to connect it to an AI agent so it can actually use those tools. Navigate to Agents in the sidebar, then either create a new agent or edit an existing one.
In the agent configuration, scroll to the Tools section and click Add MCP Server. Select your newly created PostgreSQL MCP server from the dropdown. The agent now has access to every tool defined in that server — it can query your customers table, filter your orders table, aggregate your products table, and more.
Write a system prompt that tells the agent what it should do with the data. For example:
You are a sales analytics assistant. Use the PostgreSQL tools
to answer questions about orders, customers, and products.
Always include specific numbers and date ranges in your answers.
If data is unavailable, say so rather than guessing.
Save the agent and test it with a question like "What were the top 5 products by revenue last month?" The agent will call the appropriate MCP tool, receive the query results, and generate a natural language summary.
Example: Building a Sales Reporting Agent
Let us walk through a concrete example. Suppose you have a PostgreSQL database with three tables: sales_orders, customers, and products. You want an AI agent that can answer ad-hoc questions from your sales team.
After following Steps 1 through 5, your agent has tools like query_sales_orders, query_customers, and query_products. A sales manager opens the agent chat and asks:
"Show me total revenue by region for Q4 2025, and flag any region where revenue dropped more than 10% compared to Q3."
The agent calls query_sales_orders with date filters for Q3 and Q4, groups the results by the region column from the customers table, calculates the percentage change, and produces a formatted summary with the flagged regions highlighted. No dashboards to build, no SQL to write, no export to spreadsheet — just a question and an answer backed by live data.
Security Best Practices
Giving AI access to production data requires care. Follow these practices to keep your deployment secure:
- Use a read-only database user. Create a PostgreSQL role with
SELECTpermissions only. Even if the MCP server is configured as read-only, a dedicated DB user adds a second layer of protection. - Exclude sensitive columns at generation time. Do not rely on the AI model to "know" it should not return PII. Remove the columns from the MCP server entirely so the data never leaves your database for that tool.
- Monitor query logs. Enable PostgreSQL's
log_statementor usepgAuditto track every query the MCP server executes. This gives you a full audit trail of what data the AI accessed and when. - Rotate credentials regularly. Update your MCP server's database password on a schedule. Orckai lets you edit connection details and redeploy without regenerating the entire server.
- Use network isolation. Keep your PostgreSQL instance on a private network. The MCP server container connects to it via internal Docker networking — there is no need to expose the database to the public internet.
Conclusion
Creating an MCP server for PostgreSQL with Orckai takes five steps and less than five minutes:
- Connect your database with host, port, and credentials
- Select tables and configure column-level permissions
- Generate the MCP server with typed, parameterized tools
- Deploy as an isolated Docker container with one click
- Attach the server to an AI agent and start asking questions
No custom code, no API wrappers, no ongoing maintenance. The MCP protocol handles the communication between your AI agents and your data, and Orckai handles the infrastructure.
Ready to connect your PostgreSQL database to AI? Get started with Orckai or explore the full MCP Server Generator feature page to learn about all supported databases and API integrations.