copy markdown
Ever wondered how seamless your data pipeline would be if you could invoke a deterministic large language model (LLM) directly from your Postgres database—without having to shuttle data back and forth through an application layer?
Meet postgres-llm: an open-source Postgres trigger function that brings AI to your database rows and columns, allowing for tasks like translation, sentiment analysis, OCR, and more, right into your SQL world.
Machine learning and NLP models are typically orchestrated from a backend server which means your data makes a round trip outside of Postgres and then returns. This is fine for batch jobs or when data security isn’t critical. But what if you want richer processing that’s:
postgres-llm allows you to do just that, with idiomatic Postgres triggers.
postgres-llm is implemented as a dynamic trigger function written in PL/pgSQL. It uses the http and hstore extensions for outbound API requests and flexible responses.
It’s built to work with any LLM provider that matches the OpenAI chat completion API including, by default, Interfaze. You can configure it for your chosen key and endpoint.
The main user workflow:
call_llm.Suppose you have a customer review system and want to analyze the sentiment of each review as it’s written.
1. Install Requirements
http and hstore extensions.2. Create the User Reviews Table
3. Set Up the LLM Call Function
call_llm.sql from the repo.API_KEY).4. Create a Trigger for Sentiment Analysis
call_llm has three parameters:
prompt: The prompt to send to the LLM.output_column: The column to write the LLM response to.input_column: The column to read the input as context for the prompt.For example, to create a trigger for sentiment analysis, you would do the following:
Once your trigger is set up, it will automatically process rows on both INSERT and UPDATE events.
Effect:
The trigger runs as the row is created. The sentiment column is populated via the LLM (e.g., "positive").
Effect:
The LLM re-analyzes the updated review text and updates the sentiment column accordingly (e.g., now "negative").
| id | review_text | sentiment |
|---|---|---|
| 1 | This product exceeded my expectations! | positive |
| 1 | The item arrived broken and late. | negative |
If you add a Spanish translation trigger:
Summarize a name:
OCR from image:
postgres-llm brings the power of LLMs right to where your data lives removing friction, reducing latency, and opening up a world of real-time AI automation possibilities.
No pipelines, no ETL, just pure Postgres and AI.
Check out the code and readme for all the details here:
👉 https://github.com/JigsawStack/postgres-llm