Store vector embeddings

This page shows you how to use AlloyDB as a vector database with the pgvector extension that includes pgvector functions and operators. These functions and operators let you store embeddings as vector values.

Required database extension

Use the vector extension, version 0.5.0.google-1 or later, which includes pgvector functions and operators, to store generated embeddings as vector values. This is a version of pgvector that Google has extended with optimizations specific to AlloyDB.

CREATE EXTENSION IF NOT EXISTS vector;

Store generated embeddings

Ensure that you have already created a table in your AlloyDB database.

To store vector embeddings, do the following:

  1. Create a vector[] column in your table to store your embeddings:

    ALTER TABLE TABLE ADD COLUMN EMBEDDING_COLUMN vector(DIMENSIONS);
    

    Replace the following:

    • TABLE: the table name

    • EMBEDDING_COLUMN: the name of the new embedding column

    • DIMENSIONS: the number of dimensions that the model supports.

      For example, if you are using one of the textembedding-geckoEnglish models—for example, textembedding-gecko@004 with Vertex AI, specify 768.

  2. Copy the vectors to the vector column. The following example assumes your embeddings are available in a .csv file:

    COPY TABLE (EMBEDDING_COLUMN) FROM 'PATH_TO_VECTOR_CSV (FORMAT CSV);
    

    Replace the following:

    • PATH_TO_VECTOR_CSV: the full path of where you have stored your .csv file.

After you store the embeddings, you can use a version of the pgvector PostgreSQL extension optimized for AlloyDB or the alloydb_scann extension to create indexes for faster query performance.

What's next