DEV Community

Cover image for Installing PostgreSQL + pgvector on Debian
farez
farez

Posted on

Installing PostgreSQL + pgvector on Debian

If you're building any kind of AI app that requires storage of vectors for embeddings, and you need an open source storage solution, it's hard to go wrong with PostgreSQL + the pgvector extension.

You could go with a hosted solution, such as Supabase's vector storage. You can also host it yourself.

It's easy and I show you how to do it for the Debian OS below, which would allow you to add this to a Debian hosted website. This is also the exact setup I'm using for clarifypdf.com, a "Chat with PDF" AI app.

Prerequisities

You'll need root access to your Debian environment.

Steps

SSH into your server, with your username and server's IP address



$ ssh user@ip.add.re.ss


Enter fullscreen mode Exit fullscreen mode

Install pgvector



sudo apt install postgresql-15-pgvector


Enter fullscreen mode Exit fullscreen mode

Login to PostgreSQL



$ psql -U db-username db-name


Enter fullscreen mode Exit fullscreen mode

Enable pgvector



# CREATE EXTENSION pgvector;


Enter fullscreen mode Exit fullscreen mode

Check that it's enabled



# \dx


Enter fullscreen mode Exit fullscreen mode

You should see the extensions list, with pgvector in it.

Screen shot of PostgreSQL extensions list showing that pgvector is enabled.

That's it! You're ready to create embeddings with PostgreSQL!

Top comments (7)

Collapse
 
dasheck0 profile image
Stefan Neidig

Would love to see it in action. Any tutorials on how to use it in a ML context you are willing to share?

Collapse
 
farez profile image
farez

Sure! I'm using this exact setup in production for clarifypdf.com. So the DB contains embeddings for all documents and using it to store document sumarries.

Is there a specific aspect of the setup you're interested in?

Collapse
 
dasheck0 profile image
Stefan Neidig

Just a production ready setup would be interesting. I setup this myself using AWS RDS (with pgvector) and pipedream. It works but I am not sure whether this would be production ready or not. So having a point of reference would be really helpful :)

Thread Thread
 
farez profile image
farez

Sorry, missed this reply.

We don't have any fancy setup TBH. It's a PostgreSQL db with pgvector extension. The main "meat" of it is a Documents table and Embeddings table (one to many relation from Documents to Embeddings).

Documents store references to uploaded PDFs. Embeddings store content from the PDF. Each row in Embeddings represent one page of the PDF.

Embeddings has a 'content' column that stores the content from the PDF and an 'embeddings' column that stores the embeddings generated from the content.

When querying, we do a 'cosine similarity' search on the embeddings, which returns the rows from Embeddings table, whose 'content' values we then use to create the prompt for GPT (up to the token limit).

That's about it!

Thread Thread
 
dasheck0 profile image
Stefan Neidig

Interesting. We have the exact same setup :D I stumbled across Quivr in the meantime. This is an open source project that does basically this but utilizes Supabase and its vector databases. I use it ever since.

Collapse
 
samswen profile image
Sam Wen

I run Postgresql 16; I have to use

CREATE EXTENSION vector;

Replace pgvector with vector to enable pgvector

Collapse
 
antonio_solis_3500884bd25 profile image
Antonio Solis

cuando instalo la extensión me lo instala perfecto pero cuando lo doy ejecutar me da error!!!! ¿ tendrán alguna solución posible?