The Wren Journal
Tutorial

How to use OpenAI GPT-4o to query your database?

Today, OpenAI released its latest LLM model, GPT-4o. People are sharing crazy applications built on top of this groundbreaking model. By…

Howard Chi

Howard Chi

Updated: Dec 18, 2025
Published: Aug 16, 2024

How to use OpenAI GPT-4o to query your database?

Note: this tutorial predates Agentic GenBI. The walkthrough below still works, but it frames Wren AI as a text-to-SQL tool. Today Wren AI is an agent that reasons over a governed context layer, with skills and memory, for humans and agents alike. Read "Introducing Agentic GenBI" to see how the product works now.

Today, OpenAI released its latest LLM model, GPT-4o. People are sharing crazy applications built on top of this groundbreaking model. By using GPT-4o, we see improvements in data retrieval, which could benefit users with faster response and be more cost-efficient (Cost less than GPT -4 Turbo).

1_iOIKqWXPzESA2iHbiK9bjQ.webp
1_iOIKqWXPzESA2iHbiK9bjQ.webp
Model comparison from OpenAI website

Within 12 hours, our team at Wren AI released our latest version, to support the GPT-4o LLM model for querying your own database. In this post, we will guide you through setting up using gpt-4o to query your own PostgreSQL database!

0_wc1t6QmOO7trn1QV.webp
0_wc1t6QmOO7trn1QV.webp

Wren AI is an open-source text-to-SQL solution for data teams to get results and insights faster by asking business questions without writing SQL.

Let’s get started!

1_Op_xmMAgAcGPg-r9ILL8Hg.webp
1_Op_xmMAgAcGPg-r9ILL8Hg.webp

Using Wren AI with GPT-4o to query PostgreSQL

Setup your PostgreSQL database

Start your PostgreSQL server, and enter your PostgreSQL database using the psql client.

1_keNQ7lDEXWcgtAcW6mCFrA.webp
1_keNQ7lDEXWcgtAcW6mCFrA.webp

Create a database in PostgreSQL here, we called ecom-wrenai

Connect to the created database ecom_wrenai using \c <database_name>

Now, you want to create tables of this dataset and import the data. Please check the SQL script here in the gist.

You can either copy the script to your psqlor Import SQL via psql

If you successfully import the data, you will see the information below.

1_PwsymRp1L1OCcXKrE6EOdw.webp
1_PwsymRp1L1OCcXKrE6EOdw.webp

Import data successfully

Installing Wren AI

Next, let’s start installing Wren AI; before we start, you need to install Docker.

1. Install Docker Desktop on your local computer.

Please ensure the version of Docker Desktop is at least >= 4.17.

2. Prepare an OpenAI API key

Please ensure that your Open API key has Full Permission(All).

Visit the OpenAI developer platform.

1_gLJ2kGHWmlcVBbhjOu78tA.webp
1_gLJ2kGHWmlcVBbhjOu78tA.webp

Enter the OpenAI API key page

Generate a new API key for Wren AI with full permission

1_A5kAe5Sb-5gEFW8CZ27Bzg.webp
1_A5kAe5Sb-5gEFW8CZ27Bzg.webp
Generate your OpenAI API key with full permission

3. Install Wren AI Launcher

If you are on Mac(using Windows or Linux check here) enter the below command to install the latest Wren AI Launcher.

curl -L https://github.com/Canner/WrenAI/releases/latest/download/wren-launcher-darwin.tar.gz | tar -xz && ./wren-launcher-darwin

The launcher will then ask for your OpenAI API key as below, paste your key into the command and hit enter.

Now you can select gpt-4o , gpt-4-turbo , gpt-3.5-turbo of OpenAI’s generation model in Wren AI.

1_JYL_eRRpxfqZcE7NuIubtw.webp
1_JYL_eRRpxfqZcE7NuIubtw.webp

Install Wren AI with CLI

Now you’ll see we are running docker-compose on your computer; after the installation, the tool will automatically open up your browser to access Wren AI.

1_1aGd3y750DBq_5Ik_mzbIQ.webp
1_1aGd3y750DBq_5Ik_mzbIQ.webp

Running Docker Compose on your computer

Connect your Data Source

While the terminal is successfully installed, it will launch the browser

1_JlQyJICH4CsACYc2I2trQQ.webp
1_JlQyJICH4CsACYc2I2trQQ.webp

First-time launching Wren AI

Select PostgreSQL database connector

1_zXw75bSs4NOxeC8CMtOOog.webp
1_zXw75bSs4NOxeC8CMtOOog.webp

Fill out the connection information

If you use the Mac local PostgreSQL database, enter docker.for.mac.localhost to access the database.

1_zYI11A-hNSf54VOGmOlwBQ.webp
1_zYI11A-hNSf54VOGmOlwBQ.webp

Select data models in your database.

Next step, define relationships to the semantic models; this could help LLMs to understand more about your semantic data structure.

1_4GwRiQyKxkBx955f-CgD0Q.webp
1_4GwRiQyKxkBx955f-CgD0Q.webp

Add semantic relationships

Now, you are all set!

You can view all your data models in the Wren AI UI.

1_tf_AKbgWR0JkxCBSUVUDBA.webp
1_tf_AKbgWR0JkxCBSUVUDBA.webp

Complete the setup

Playing around with Wren AI

Wren AI UI semantic modeling

With Wren AI UI, you can model your data models in a semantic context, such as adding descriptions, relationships, calculations, etc. The context could help LLMs learn your business terminologies and KPI definitions and reduce hallucinations when joining multiple tables; LLMs know the data structure hierarchy by learning through relationships such as whether it is a many-to-one , one-to-many , or many-to-many relationships between tables.

1_tf_AKbgWR0JkxCBSUVUDBA.webp
1_tf_AKbgWR0JkxCBSUVUDBA.webp
Semantic modeling through Wren AI UI

Define your business KPIs and formulas via calculations in Wren AI.

1_tJcNiHtebNldSsdAxTezvQ.webp
1_tJcNiHtebNldSsdAxTezvQ.webp

Add calculations in the data model

Adding semantic relationships between tables.

1_5JKyxk4WSD8AqHYl6PzFkQ.webp
1_5JKyxk4WSD8AqHYl6PzFkQ.webp

Add relationships in the data model.

Ask business questions and Follow-up insights with Wren AI

Now you can switch to the Home page in Wren AI UI, by clicking New Thread you can start asking any of your business questions, Wren AI search for relevant semantics through the vector store and provides recommendations.

1_tmZnhCF0IQ9TO50z6A8yJg.webp
1_tmZnhCF0IQ9TO50z6A8yJg.webp

Ask any business questions to Wren AI

Wren AI will show up to 3 options for your business questions; you can select the most relevant question to generate the final result.

1_gBcOtaxg4eZEI1WP5HHLHg.webp
1_gBcOtaxg4eZEI1WP5HHLHg.webp

Wren AI searches for the most relevant result

As you select an option, Wren AI will display a step-by-step breakdown of the summary, including SQL and data, allowing you to verify if the result is what you were looking for.

1_JD8GLyNVL1bLGRwf1akQDw.webp
1_JD8GLyNVL1bLGRwf1akQDw.webp

Step-by-step breakdown of the SQL summary

You can ask follow-up questions to gain deeper insights into the result.

1_eGc2e3nJAnADcyKOScB6ZA.webp
1_eGc2e3nJAnADcyKOScB6ZA.webp

Ask follow-up questions to gain more insights.

Let’s about it! Hope you are having fun with gpt-4o!

Keep reading