~/tools / Embeddings and search (RAG) / Embeddings to pgvector INSERT
Embeddings to pgvector INSERT — Turn Ollama /api/embed results and texts into pgvector INSERT statements
Turn the texts you embedded and the response from Ollama's /api/embed (or an OpenAI-compatible /v1/embeddings endpoint) into INSERT statements for a pgvector table in PostgreSQL. Each text is paired with its vector in order, quotes are escaped, and the tool checks that the counts and dimensions match before you run anything. Nothing leaves your browser.
The example uses made-up, unit-length vectors with 4 dimensions so that you can read them. Real embeddings have hundreds or thousands of dimensions.
3 rows with 4 dimensions. Texts read as the /api/embed request body, vectors as an Ollama /api/embed response.
INSERT statements
Run it in psql or another client. The rows are inserted in one transaction.
BEGIN;
INSERT INTO documents (content, embedding) VALUES
('Embeddings turn text into numeric vectors.', '[0.620186,0.410123,0.550165,0.380114]'),
('pgvector stores vectors in PostgreSQL, next to the rest of your data.', '[0.578815,0.469039,0.518937,0.419142]'),
('Ollama''s /api/embed returns one vector per input, in the same order.', '[0.119982,0.809879,-0.329951,0.46993]');
COMMIT;Vector format from the pgvector README, checked against pgvector 0.8.7 on 2026-11-04. pgvector README
Everything is done in your browser. The tool writes SQL and connects to nothing.
After you embed your document chunks with Ollama, the vectors have to go into the database together with the texts they came from. Ollama's /api/embed returns one vector per input string, in the same order as the input, so the first text belongs to the first vector. This tool does the pairing and writes the SQL. Paste the texts, either as the JSON body you sent to /api/embed (the RAG Text Chunker on this site produces it), as a JSON array of strings, or one per line, and paste the response. The tool reads the Ollama response ({"embeddings": [[…], …]}) and the OpenAI-compatible one ({"data": [{"embedding": […]}]}, put back in index order). It writes the vectors in pgvector's text format, such as '[0.1,-0.2,0.3]', and writes each text as an SQL string constant: the PostgreSQL documentation says a single quote inside a string is written as two single quotes, and backslashes are kept as they are because standard_conforming_strings is on by default since PostgreSQL 9.1. Before writing anything, it checks what would make the INSERT fail or pair the wrong rows: a different number of texts and vectors, vectors with different dimensions, a dimension that differs from the column's vector(n), values that are not finite numbers (pgvector does not accept NaN or Infinity), base64 embeddings, and the NUL character, which PostgreSQL cannot store in text. The statements are wrapped in BEGIN and COMMIT, 100 rows per INSERT. We ran the output on pgvector 0.8.7 with PostgreSQL 16 using a real response from Ollama, including texts with quotes, backslashes, line breaks and emoji, and the texts came back unchanged.
The core logic of this tool is open source under the MIT License. github.com/seigo5555/toolkitbox-lib/src/pgvector-insert.ts
Use Cases
- Load the chunks of a manual or an FAQ and their embeddings from Ollama into PostgreSQL for a RAG search
- Try a search on a few real rows before you write the loading code for your application
- Find out why an insert fails: a vector with the wrong number of dimensions, or more texts than vectors
Usage
- Paste the texts you embedded: the JSON body from the RAG Text Chunker, a JSON array of strings, or one text per line.
- Send the same texts to /api/embed and paste the whole JSON response. Enter the table and column names, and optionally the dimensions of the vector(n) column.
- Copy the SQL and run it in psql or another client. The pgvector SQL Builder on this site writes a matching table and search query.
FAQ
How do I know which vector belongs to which text?
By order. Ollama's /api/embed returns the embeddings in the same order as the input array, so the tool pairs the first text with the first vector, the second with the second, and so on. If the counts differ, the tool stops instead of guessing.
My chunks contain line breaks. Can I paste them one per line?
No, a line break would split a chunk into two texts. Paste the JSON body you sent to /api/embed or a JSON array of strings instead; the line breaks inside each string are kept in the SQL.
Is it safe to paste text with quotes?
The tool doubles every single quote, which is how the PostgreSQL documentation says to write a quote inside a string constant. Backslashes are left unchanged, which is correct with the default standard_conforming_strings = on. If your server has it turned off, do not run the output. In application code, use your database driver's parameters instead of building SQL strings.
What happens if the dimensions do not match the column?
pgvector rejects the row, with an error such as "expected 384 dimensions, not 768". Enter the column's dimensions in the tool to catch this before you run the SQL. Different dimensions usually mean that the vectors came from a different model or a different dimensions setting.
How many rows can I convert at once?
Up to 1,000 texts and vectors. For larger loads, the pgvector README shows bulk loading with COPY.
Is my data sent anywhere?
No. The conversion runs in your browser. The tool never connects to Ollama or to a database.
Related articles
- Store and search Ollama embeddings in PostgreSQL with pgvectorKeep Ollama embeddings in PostgreSQL with pgvector, search them with HNSW or IVFFlat, and pass the results to /api/chat so that the answer cites its sources.
- Embeddings with Ollama: the basics for search and RAGMake embeddings with Ollama's /api/embed: the models and their sizes, truncate and dimensions, unit-length vectors and cosine similarity, from the official docs.