> ## Documentation Index
> Fetch the complete documentation index at: https://docs.upsonic.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# PGVector

> Using PostgreSQL with pgvector extension as a vector database provider

## Overview

PGVector is a PostgreSQL extension that enables vector similarity search. It supports HNSW and IVFFlat indexes and integrates seamlessly with existing PostgreSQL infrastructure.

**Provider Class:** `PgVectorProvider`\
**Config Class:** `PgVectorConfig`

## Install

<Note>
  Install the PGVector optional dependency group:

  ```bash theme={null}
  uv pip install "upsonic[pgvector]"
  ```
</Note>

## Examples

```python theme={null}
from upsonic import Agent, Task, KnowledgeBase
from upsonic.embeddings import OpenAIEmbedding, OpenAIEmbeddingConfig
from upsonic.vectordb import PgVectorProvider, PgVectorConfig, HNSWIndexConfig
from pydantic import SecretStr

# Setup embedding provider
embedding = OpenAIEmbedding(OpenAIEmbeddingConfig())

# Create PGVector configuration
config = PgVectorConfig(
    collection_name="my_collection",
    vector_size=1536,
    connection_string=SecretStr("postgresql://user:password@localhost/dbname"),
    schema_name="public",
    index=HNSWIndexConfig(m=16, ef_construction=200)
)
vectordb = PgVectorProvider(config)

# Create knowledge base
kb = KnowledgeBase(
    sources="document.pdf",
    embedding_provider=embedding,
    vectordb=vectordb
)

# Use with Agent
agent = Agent("anthropic/claude-sonnet-4-5")
task = Task(
    description="Search the database",
    context=[kb]
)
result = agent.do(task)
```

## Parameters

### Base Parameters (from BaseVectorDBConfig)

| Parameter | Type | Description | Default | Required |
| - | - | - | - | - |
| `collection_name` | `str` | Name of the collection | `"default_collection"` | No |
| `vector_size` | `int` | Dimension of vectors | - | **Yes** |
| `distance_metric` | `DistanceMetric` | Similarity metric (`COSINE`, `EUCLIDEAN`, `DOT_PRODUCT`) | `COSINE` | No |
| `recreate_if_exists` | `bool` | Recreate collection if it exists | `False` | No |
| `default_top_k` | `int` | Default number of results | `10` | No |
| `default_similarity_threshold` | `Optional[float]` | Minimum similarity score (0.0-1.0) | `None` | No |
| `dense_search_enabled` | `bool` | Enable dense vector search | `True` | No |
| `full_text_search_enabled` | `bool` | Enable full-text search | `True` | No |
| `hybrid_search_enabled` | `bool` | Enable hybrid search | `True` | No |
| `default_hybrid_alpha` | `float` | Default alpha for hybrid search (0.0-1.0) | `0.5` | No |
| `default_fusion_method` | `Literal['rrf', 'weighted']` | Default fusion method for hybrid search | `'weighted'` | No |
| `provider_name` | `Optional[str]` | Provider name | `None` | No |
| `provider_description` | `Optional[str]` | Provider description | `None` | No |
| `provider_id` | `Optional[str]` | Provider ID | `None` | No |
| `default_metadata` | `Optional[Dict[str, Any]]` | Default metadata for all records | `None` | No |
| `indexed_fields` | `Optional[List[Union[str, Dict[str, Any]]]]` | Fields to index for filtering | `None` | No |

### PGVector-Specific Parameters

| Parameter | Type | Description | Default | Required |
| - | - | - | - | - |
| `connection_string` | `SecretStr` | PostgreSQL connection string | - | **Yes** |
| `schema_name` | `str` | PostgreSQL schema name | `"public"` | No |
| `table_name` | `Optional[str]` | Table name (uses `collection_name` if not specified) | `None` | No |
| `index` | `Union[HNSWIndexConfig, IVFIndexConfig]` | Index type configuration (FLAT not supported) | `HNSWIndexConfig()` | No |
| `content_language` | `str` | Language for full-text search (e.g., 'english', 'spanish') | `"english"` | No |
| `prefix_match` | `bool` | Enable prefix matching for full-text search (appends \* to words) | `False` | No |
| `schema_version` | `int` | Schema version for migrations | `1` | No |
| `auto_upgrade_schema` | `bool` | Automatically upgrade schema on version mismatch | `False` | No |
| `batch_size` | `int` | Batch size for upsert operations | `100` | No |
| `pool_size` | `int` | Connection pool size | `5` | No |
| `max_overflow` | `int` | Maximum pool overflow | `10` | No |
| `pool_timeout` | `float` | Pool timeout in seconds | `30.0` | No |
| `pool_recycle` | `int` | Pool recycle time in seconds | `3600` | No |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.