What goes wrong when Claude or ChatGPT queries Postgres tables directly?
When Claude or ChatGPT queries Postgres tables directly, it writes SQL from table and column names. Names don't say what the business means, so the AI has to guess which joins are safe and which values to match. A wrong guess still runs and returns a result, with no error. Two examples from our blog posts:
- A join that counts things twice. Six orders total $955 across twelve line items. Join orders to line items and sum the order amount, and the result is $1,445, because each order's amount repeats for every item. (From The Semantic Layer Is an Interface, Not a Compiler.)
- A filter that matches nothing. In one ecommerce dataset, 255,883 rows have the status
'Complete'. A filter for'complete', lowercase, matches none of them. The query returns zero rows instead of an error, and an agent can report that zero as a fact. (From How an agent turns a question into a trustworthy answer.)
Why AI numbers don't match your dashboard covers the other common causes and a five-minute check for any AI answer.
What's a better way to give an AI business context than markdown files?
A data model is better for two reasons: the AI can't skip it, and the definitions are managed like code.
- The AI can't skip it. A markdown file is notes. The AI may read them or not, and it still writes every query itself, so nothing checks its SQL against them. With a data model, every query goes through Malloy. The AI asks for a metric, and the query is built from that metric's definition. A query that names a field the model doesn't have fails with an error.
- The definitions are managed like code. The model is a set of files in git. A change to what revenue means is reviewed before it ships, an earlier version is there to go back to, and the model's access rules apply to every query that goes through it.
A data model holds:
- Entities and how they relate, including which side of a join has many rows.
- Each metric's definition, such as net revenue without returned or cancelled items.
- Business rules and edge cases, such as trial churn not counting, or the fiscal year starting in February.
- A description of each field, including what its values look like.
Asked for total revenue on six orders that total $955 across twelve line items, a Malloy model returns $955 even with the line items joined, because each measure knows which table its values come from. Malloy vs. SQL explains how.
You don't write the model from scratch. An agent drafts it from the tables and from your answers to a few questions, such as what counts as revenue. A person who owns the numbers confirms what's right.
How do you set up Claude or ChatGPT on Postgres?
You can do it with open-source tools, with nothing to buy. Malloy is MIT-licensed, and Malloy Publisher serves a semantic data model to Claude, ChatGPT or any other MCP client. MCP (Model Context Protocol) is the standard way AI apps connect to outside data and tools. It takes four steps:
- Connect Postgres. Add a Postgres connection to Publisher's config. The Malloy connection docs show the format.
- Build the model. An agent using the open-source Malloy skills drafts sources, joins and measures from your tables, and you correct the definitions. The tutorial walks through it.
- Connect the AI. Claude Code on the same machine connects directly. Claude and ChatGPT in the browser need Publisher at an address they can reach over the internet. ChatGPT can also use OpenAI's Secure MCP Tunnel to reach a private server.
- Test it on your own questions. Keep a list of questions with verified answers, and re-run it after every change. In one run, an agent read "summer sales" as June to August and came out 20% low. A few lines in the model stating the company's season fixed it.
Credible runs the same models and skills as a managed service. It holds the Postgres connection, signs each person in so their access rules apply, and serves the model to Claude, ChatGPT, coding agents and Slack. The docs cover connecting Postgres, building a model with the agent and connecting Claude or ChatGPT. See also pricing.
Frequently asked questions
Is there a better way to give Claude context about a database than markdown files?
Yes: a data model that Claude queries over MCP. Claude reads markdown files as notes, and nothing checks its SQL against them. With a Malloy data model, Claude asks for a named metric and the query is built from that metric's definition. A query that names a field the model doesn't have fails with an error that names the field. The model is files in git, so a change to a definition is reviewed before it ships.
Can I connect ChatGPT to a Postgres database?
Yes, through a remote MCP server that connects to Postgres. As of December 2026, OpenAI's help center says ChatGPT Business, Enterprise and Edu accounts can add a custom MCP server on the web, from the Plugins page. On Business, only admins and owners can add one. Pro accounts can add one with read and fetch actions only. If that server serves a data model rather than raw tables, ChatGPT answers from the same definitions as every other tool that queries the model.
Can Claude answer questions about Salesforce or HubSpot data?
Yes, when the CRM data sits where Claude can query it. The simplest route is a CSV export: export deals, contacts or another object from Salesforce or HubSpot, add the file to the data model's package, and replace it with a newer export when the numbers need updating. The data model then defines pipeline, won deals and the other CRM metrics.
Is there an open source way to let Claude Code query Postgres?
Yes. Malloy Publisher is open source and runs on a laptop with one npx command. Point a connection in its config at Postgres, and it serves a Malloy data model over MCP. On startup it writes the .mcp.json file Claude Code reads, so a Claude Code session started in that folder afterward has the tools.