How to Store Embeddings and Find Similar Rows with pgvector
Store embeddings in PostgreSQL, retrieve nearby rows by cosine distance, and check whether a matching pgvector HNSW index is used.

If your application already produces embeddings, store each one in a vector column with the model’s dimension, then order rows by distance from the query embedding. For cosine-distance retrieval, pgvector uses <=>; an HNSW index intended for that query needs vector_cosine_ops. Without an index, the search scans rows for exact nearest neighbors. With the index, nearest-neighbor search is approximate. Check the plan and results on your own data before choosing between them.
Set the dimension in the target database
This example uses PostgreSQL 17.9 and pgvector 0.8.2 as a source-reported test environment, not as a compatibility boundary. The SQL assumes pgvector is available to the server. Enable it in the database that will hold the table; enabling is a per-database operation.
CREATE EXTENSION IF NOT EXISTS vector;
If pgvector is not yet available, see the EDB installation guide for its covered PostgreSQL distributions rather than treating CREATE EXTENSION as a package-installation step.
Store complete embedding values
The following small teaching example assumes a model that emits three components. The numbers are illustrative complete vectors, not outputs attributed to a particular model or a tested dataset. For an application, set vector(n) to the dimension of its actual model and insert that model’s complete output. A dimension mismatch at insert time raises an error.
CREATE TABLE documents (
id bigint PRIMARY KEY,
content text NOT NULL,
embedding vector(3) NOT NULL
);
INSERT INTO documents (id, content, embedding)
VALUES
(1, 'First document', '[0.12,-0.04,0.88]'::vector),
(2, 'Second document', '[0.20,0.10,0.75]'::vector),
(3, 'Third document', '[-0.30,0.45,0.60]'::vector);
The insert contains numeric vector values rather than an ellipsis or a placeholder. In an application, supply its model-produced values at this point; do not silently change the column dimension to accommodate a different model. The dimension guidance is straightforward: VECTOR(n) must match the model output.
Retrieve nearest rows by cosine distance
Use an incoming query embedding with the same dimension. This example requests two rows and returns their distances so the ordering can be inspected:
SELECT id, content,
embedding <=> '[0.12,-0.04,0.88]'::vector AS cosine_distance
FROM documents
ORDER BY embedding <=> '[0.12,-0.04,0.88]'::vector
LIMIT 2;
Before adding a nearest-neighbor index, pgvector’s search is exact and scans the rows. Run this query first if you want an exact result to compare with the indexed query. The three-row table is only a syntax and correctness example, not evidence about performance or recall.
Match the HNSW index to the query
For the cosine-distance operator <=>, create the HNSW index with vector_cosine_ops:
CREATE INDEX documents_embedding_cosine_idx
ON documents USING hnsw (embedding vector_cosine_ops);
The operator-class guidance notes that a mismatch between <=> and the index operator class can cause the index to be skipped. Keep the indexed column expression in ORDER BY aligned with the index definition. HNSW provides approximate nearest-neighbor search, so an indexed result need not reproduce every row selected by the exact scan. There is no supported latency or recall figure for this example.
Check the rows and the measured plan
Repeat the SELECT to inspect the returned IDs, count and distance ordering. Then check how PostgreSQL executed that same retrieval:
EXPLAIN ANALYZE
SELECT id, content,
embedding <=> '[0.12,-0.04,0.88]'::vector AS cosine_distance
FROM documents
ORDER BY embedding <=> '[0.12,-0.04,0.88]'::vector
LIMIT 2;
Look for an Index Scan using documents_embedding_cosine_idx or a Seq Scan followed by a sort. EXPLAIN ANALYZE runs the query and reports execution, rather than only an estimated plan. A sequential scan on this tiny table is a plausible planner choice, not proof of a broken index. Check again with representative row counts and embeddings, and record the settings and conditions if you compare execution times. If the representative plan skips the index, inspect the full plan and confirm that the query still orders directly by embedding <=> and that the index uses vector_cosine_ops before changing anything else.

