Pgvector 0.1.1

pgvector-dotnet

pgvector support for C#

Supports Npgsql

Build Status

Getting Started

Run:

dotnet add package Pgvector

And follow the instructions for your database library:

Npgsql

Import the library

using Pgvector.Npgsql;

Create a table

await using (var cmd = new NpgsqlCommand("CREATE TABLE items (embedding vector(3))", conn))
{
    await cmd.ExecuteNonQueryAsync();
}

Insert a vector

await using (var cmd = new NpgsqlCommand("INSERT INTO items (embedding) VALUES ($1)", conn))
{
    var embedding = new Vector(new float[] { 1, 1, 1 });
    cmd.Parameters.AddWithValue(embedding);
    await cmd.ExecuteNonQueryAsync();
}

Get the nearest neighbors

await using (var cmd = new NpgsqlCommand("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", conn))
{
    var embedding = new Vector(new float[] { 1, 1, 1 });
    cmd.Parameters.AddWithValue(embedding);

    await using (var reader = await cmd.ExecuteReaderAsync())
    {
        while (await reader.ReadAsync())
            Console.WriteLine((Vector) reader.GetValue(0));
    }
}

Add an approximate index

await using (var cmd = new NpgsqlCommand("CREATE INDEX my_index ON items USING ivfflat (embedding vector_l2_ops)", conn))
{
    await cmd.ExecuteNonQueryAsync();
}

Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance

See a full example

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/pgvector/pgvector-dotnet.git
cd pgvector-dotnet
createdb pgvector_dotnet_test
dotnet test

Showing the top 20 packages that depend on Pgvector.

Packages Downloads
Microsoft.SemanticKernel.Connectors.Postgres
Postgres(with pgvector extension) connector for Semantic Kernel plugins and semantic memory
7
Microsoft.SemanticKernel.Connectors.Postgres
Postgres(with pgvector extension) connector for Semantic Kernel plugins and semantic memory
6
Microsoft.SemanticKernel.Connectors.Postgres
Postgres(with pgvector extension) connector for Semantic Kernel plugins and semantic memory
5

.NET 7.0

Version Downloads Last updated
0.3.2 7 08/10/2025
0.3.1 5 08/10/2025
0.3.0 5 08/10/2025
0.2.0 4 08/10/2025
0.2.0-rc.2 5 08/11/2025
0.2.0-rc.1 5 08/11/2025
0.1.4 5 08/10/2025
0.1.3 5 08/10/2025
0.1.2 6 08/10/2025
0.1.1 5 08/10/2025
0.1.0 5 08/10/2025