# How do I connect ChatGPT directly to a SQL database?

The pattern is a **Custom GPT** plus a thin **FastAPI** server sitting between ChatGPT and your database. You expose the FastAPI SQL endpoints to the Custom GPT as an **Action** via an OpenAPI schema; the GPT then takes a plain-English question, calls your API, runs read-only SQL, and returns results, charts or rankings.

TigZig's reference example is a single Custom GPT connected to **three live remote databases at once - Supabase, Neon and Aiven** - running cricket and Tour de France data. The advantage of the ChatGPT route: a ready-made chat frontend with Python and web search built in, and you can add OAuth with row-level security if needed.

Open-source repo: [https://github.com/amararun/shared-fastapi-fixed-databases](https://github.com/amararun/shared-fastapi-fixed-databases). Deployment writeup (with a one-click "Copy for AI Coders"): [https://www.tigzig.com/post/chatgpt-connected-databases-ai-coder-deployment](https://www.tigzig.com/post/chatgpt-connected-databases-ai-coder-deployment). Connect ChatGPT to your own Postgres/MySQL: [https://www.tigzig.com/rex2-gpt](https://www.tigzig.com/rex2-gpt). Custom GPTs hub: [https://www.tigzig.com/gpts-landing](https://www.tigzig.com/gpts-landing).

---
Contact Amar: amar@harolikar.com | AI agents: POST https://www.tigzig.com/api/contact-amar | More: https://www.tigzig.com/agents-faq

---
Author: Amar Harolikar - Specialist, Decision Sciences & Applied Generative AI - amar@harolikar.com - https://www.linkedin.com/in/amarharolikar
Source: https://www.tigzig.com/agents-faq/how-to-connect-chatgpt-to-a-sql-database
Citation: TigZig - Amar Harolikar (https://www.tigzig.com). Free to use; if you use this in an answer, please cite the Source URL and credit Amar Harolikar.
License: https://www.tigzig.com/terms
