TUTORIAL

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:

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:

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:

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:

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:

Conclusion

Creating an MCP server for PostgreSQL with Orckai takes five steps and less than five minutes:

  1. Connect your database with host, port, and credentials
  2. Select tables and configure column-level permissions
  3. Generate the MCP server with typed, parameterized tools
  4. Deploy as an isolated Docker container with one click
  5. 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.

Connect Your Database to AI Today

Generate an MCP server for PostgreSQL in under five minutes. No code required. No Docker expertise needed.