This section walks you through setting up the PGvector VectorStore to store document embeddings and perform similarity searches.

PGvector is an open-source extension for PostgreSQL that enables storing and searching over machine learning-generated embeddings. It provides different capabilities that let users identify both exact and approximate nearest neighbors. It is designed to work seamlessly with other PostgreSQL features, including indexing and querying.


First you need an access to PostgreSQL instance with enabled vector, hstore and uuid-ossp extensions.

The setup local Postgres/PGVector appendix shows how to set up a DB locally with a Docker container.

On startup, the PgVectorStore will attempt to install the required database extensions and create the required vector_store table with an index.

Optionally, you can do this manually like so:


	id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
	content text,
	metadata json,
	embedding vector(1536) // 1536 is the default embedding dimension

CREATE INDEX ON vector_store USING HNSW (embedding vector_cosine_ops);
replace the 1536 with the actual embedding dimension if you are using a different dimension.

Next if required, an API key for the EmbeddingClient to generate the embeddings stored by the PgVectorStore.


Then add the PgVectorStore boot starter dependency to your project:


or to your Gradle build.gradle build file.

dependencies {
    implementation ''

The Vector Store, also requires an EmbeddingClient instance to calculate embeddings for the documents. You can pick one of the available EmbeddingClient Implementations.

For example to use the OpenAI EmbeddingClient add the following dependency to your project:


or to your Gradle build.gradle build file.

dependencies {
    implementation ''
Refer to the Dependency Management section to add the Spring AI BOM to your build file. Refer to the Repositories section to add Milestone and/or Snapshot Repositories to your build file.

To connect to and configure the PgVectorStore, you need to provide access details for your instance. A simple configuration can either be provided via Spring Boot’s application.yml

    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: postgres
		index-type: HNSW
		distance-type: COSINE_DISTANCE
		dimension: 1536
Check the list of configuration parameters to learn about the default values and configuration options.

Now you can Auto-wire the PgVector Store in your application and use it

@Autowired VectorStore vectorStore;

// ...

List <Document> documents = List.of(
    new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
    new Document("The World is Big and Salvation Lurks Around the Corner"),
    new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));

// Add the documents to PGVector

// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));

Manual Configuration

Instead of using the Spring Boot auto-configuration, you can manually configure the PgVectorStore. For this you need to add the PostgreSQL connection and JdbcTemplate auto-configuration dependencies to your project:



Refer to the Dependency Management section to add the Spring AI BOM to your build file.

To configure PgVector in your application, you can use the following setup:

public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingClient embeddingClient) {
	return new PgVectorStore(jdbcTemplate, embeddingClient);

Metadata filtering

You can leverage the generic, portable metadata filters with the PgVector store.

For example, you can use either the text expression language:

    .withQuery("The World")
    .withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'"));

or programmatically using the Filter.Expression DSL:

FilterExpressionBuilder b = new FilterExpressionBuilder();

    .withQuery("The World")
    .withFilterExpression(b.and("john", "jill"),
        b.eq("article_type", "blog")).build()));
These filter expressions are converted into the equivalent PgVector filters.

PgVectorStore properties

You can use the following properties in your Spring Boot configuration to customize the PGVector vector store.

Property Description Default value

Nearest neighbor search index type. Options are NONE - exact nearest neighbor search, IVFFlat - index divides vectors into lists, and then searches a subset of those lists that are closest to the query vector. It has faster build times and uses less memory than HNSW, but has lower query performance (in terms of speed-recall tradeoff). HNSW - creates a multilayer graph. It has slower build times and uses more memory than IVFFlat, but has better query performance (in terms of speed-recall tradeoff). There’s no training step like IVFFlat, so the index can be created without any data in the table.


Search distance type. Defaults to COSINE_DISTANCE. But if vectors are normalized to length 1, you can use EUCLIDEAN_DISTANCE or NEGATIVE_INNER_PRODUCT for best performance.


Embeddings dimension. If not specified explicitly the PgVectorStore will retrieve the dimensions form the provided EmbeddingClient. Dimensions are set to the embedding column the on table creation. If you change the dimensions your would have to to re-create the vector_store table as well.


Deletes the existing vector_store table on start up.


Run Postgres & PGVector DB locally

docker run -it --rm --name postgres -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres ankane/pgvector

You can connect to this server like this:

psql -U postgres -h localhost -p 5432