---
title: "Storing OpenAI embeddings in Postgres with pgvector"
description: "pgvector adds a vector data type and distance operators (Euclidean <->, negative inner product <#>, cosine <=>) to Postgres, so OpenAI text-embedding-ada-002 embeddings (1536 dimensions) can be stored and queried for similarity directly in the database."
url: "https://swe.quest/articles/storing-openai-embeddings-in-postgres-with-pgvector-19a5d03614f86fc6"
canonical: "https://supabase.com/blog/openai-embeddings-postgres-vector"
source: "Supabase"
published: "2023-02-06"
difficulty: "medium"
kind: "How-to"
reading_minutes: 11
tags: ["vector-search","postgres","rag","llms"]
---

# Storing OpenAI embeddings in Postgres with pgvector

A post on [Supabase](https://swe.quest/sources/supabase.md), published 2023-02-06, 11 min read, How-to. Original: [https://supabase.com/blog/openai-embeddings-postgres-vector](https://supabase.com/blog/openai-embeddings-postgres-vector)

## Summary

pgvector adds a vector data type and distance operators (Euclidean \<-\>, negative inner product \<#\>, cosine \<=\>) to Postgres, so OpenAI text-embedding-ada-002 embeddings (1536 dimensions) can be stored and queried for similarity directly in the database. The walkthrough creates a documents table, a match\_documents SQL function that filters by a cosine-similarity threshold, and an IVFFlat index sized at roughly 4 \* sqrt(rows) lists. It then generates embeddings in JavaScript and builds Deno Edge Functions for semantic search and for retrieval-augmented answers with text-davinci-003, keeping injected context under about 1500 tokens to fit the 4000-token limit.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Vector search](https://swe.quest/tags/vector-search.md) (Data & databases)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [RAG & retrieval](https://swe.quest/tags/rag.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)

## Prerequisites

- PostgreSQL basics ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- SQL functions ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- OpenAI API

## Keywords

pgvector, Supabase, OpenAI, text-embedding-ada-002, IVFFlat, Supabase Edge Functions, text-davinci-003, Deno

## Related posts

- [Building a RAG application with Llama 3.1 and pgvector](https://swe.quest/articles/building-a-rag-application-with-llama-3-1-and-pgvector-aa7660e0eb99da02.md): Neon · 2024-07-30 · Medium · 12 min read · RAG & retrieval, Vector search, PostgreSQL, LLMs, Python
  A small RAG pipeline built on Neon Postgres with pgvector as the vector store, OctoAI's gte-large embedding model producing 1024-dimensional vectors, and Llama 3.1 405B generating answers.
- [Semantic search using OpenAI, pg\_embedding and Neon](https://swe.quest/articles/semantic-search-using-openai-pg-embedding-and-neon-aeafbc27d93ca728.md): Neon · 2023-08-25 · Medium · 9 min read · Vector search, PostgreSQL, LLMs, Serverless, TypeScript
  A YC idea-matcher app implements semantic search by embedding ~4,400 YCombinator company descriptions with OpenAI's text-embedding-ada-002 (1536 dimensions) and storing them as real\[\] columns in Neon Postgres via the pg\_embedding extension.
- [ChatGPT plugins now support Postgres & Supabase](https://swe.quest/articles/chatgpt-plugins-now-support-postgres-supabase-db9d10a95a8d96b9.md): Supabase · 2023-05-25 · Medium · 7 min read · RAG & retrieval, Vector search, PostgreSQL, LLMs
  Supabase contributed Postgres and Supabase datastore providers to OpenAI's ChatGPT Retrieval Plugin, storing text-embedding-ada-002 embeddings in pgvector.
- [Hugging Face is now supported in Supabase](https://swe.quest/articles/hugging-face-is-now-supported-in-supabase-b922ce506c4fa31d.md): Supabase · 2023-08-07 · Medium · 7 min read · Vector search, PostgreSQL, Serverless, Python, JavaScript
  Supabase adds first-class Hugging Face support for generating embeddings stored in pgvector, as an open-source, self-hostable alternative to OpenAI's text-embedding-ada-002.
- [How to Generate & Store OpenAI Vector Embeddings with Turso](https://swe.quest/articles/how-to-generate-store-openai-vector-embeddings-with-turso-1144f075a7bcad32.md): Turso · 2024-07-01 · Medium · 5 min read · Vector search, SQLite, LLMs, JavaScript
  Turso's beta vector support stores OpenAI text-embedding-ada-002 embeddings in a libSQL F32\_BLOB(1536) column, whose size matches the model's 1536 dimensions.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
