Category Archives: Entity Framework

Local RAG with .NET, Postgres, and Ollama: Postgres with pgvector Setup (Part 1)

Step 1: Set Up Postgres Locally with the pgvector Extension

In this series, we’ll build a RAG (Retrieval-Augmented Generation) application that runs completely on your local machine. RAG systems use AI to answer questions based on your specific documents or data. The first component we need is a vector database to store and search through our document embeddings1. We’ll use Postgreswith the pgvector extension, which adds support for vector similarity search.

To make things easy to start, I am going to use a Docker container to run PostgreSQL with the pgvector extension instead of installing and configuring Postgres locally. This makes sure we all have the same setup and avoids configuration issues across different operating systems.

To make it easy to start, I am going to use a Docker container to run Postgres with the pgvector extension instead of installing and configuring it locally.

In a powershell prompt, I pull the image:

docker pull pgvector/pgvector:pg16

This pulled the code from docker hub here : https://hub.docker.com/r/pgvector/pgvector

At the time or this post, I got an image ‘pgvector/pgvector’ with tag pg16. You can see this by running this command in PowerShell:

docker images

Once you have the image, you can run it – starting the container with this command:

docker run -d --name postgres__with_pgvector -e POSTGRES_PASSWORD=password99 -e POSTGRES_USER=postgres -e POSTGRES_DB=vectordb -p 5432:5432 pgvector/pgvector:pg16

You can also do this start-up visually using Docker Desktop if you prefer:

Click on the images link on the left menu on Docker Desktop and then click the run triangle next to the image you just downloaded. You will put the parameters in the command line above into environment variables like this:

Click on the Containers cube link on the left and you will see your container is running.

Next, you can connect to postgres in the container with the command:

 docker exec -it postgres_with_pgvector psql -U postgres

That should give you a postgres prompt ‘postgres=#’

Let’s test that everything is connected and working now with these commands (hitting enter after each line):

CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE items (
    id bigserial PRIMARY KEY,
    embedding vector(3)
);
INSERT INTO items (embedding) VALUES ('[1,2,3]');
SELECT * FROM items;

The select should return:

 id | embedding
----+-----------
  1 | [1,2,3]
(1 row)

To get out of the sql prompt, type ‘\q’

Let’s review what we’ve accomplished:

  • Postgres is running in a Docker container
  • The pgvector extension is installed and working
  • We’ve verified we can store and retrieve vector data

In Part 2, we’ll set up Ollama to run an AI model locally. This will allow us to generate the vector embeddings that we’ll store in our Postgres database. Then in Part 3, we’ll create a .NET application that brings these components together into a complete RAG system.

If you need to stop the container, you can use:

docker stop postgres__with_pgvector

Or use Docker Desktop to stop it. Your data will persist for next time.

See you in Part 2!
-Jim

  1. Embeddings are mathematical representations of objects like text, images, and audio. They are used by machine learning (ML) and artificial intelligence (AI) systems to understand complex relationships in data.  ↩︎

Entity Framework Validation Errors

Found this lovely little code snipped for interrogating the database validation errors sent back from an Entity Framework Save:


catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;

Enjoy,

-Jim