> ## Documentation Index
> Fetch the complete documentation index at: https://neuraltrust-92b43583-develop.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# PostgreSQL + pgvector

> Connect a PostgreSQL pgvector store as a TrustTest knowledge base

`PgVectorKnowledgeBase` is a PostgreSQL-backed vector knowledge base. It uses the [`pgvector`](https://github.com/pgvector/pgvector) extension for similarity search.

Import it from the connector submodule. There is no `trusttest.kb` package and no `PostgresKnowledgeBase` class.

```python theme={null}
from trusttest.knowledge_base.pgvector import PgVectorKnowledgeBase
```

## Installation

```bash theme={null}
uv add "trusttest[rag-postgres]"
```

## Constructor

| Parameter           | Type               | Default                                       | Description                                                   |
| ------------------- | ------------------ | --------------------------------------------- | ------------------------------------------------------------- |
| `connection_string` | `str`              | Required                                      | SQLAlchemy/psycopg URL, e.g. `postgresql://user:pass@host/db` |
| `table_name`        | `str`              | `"documents"`                                 | Table that holds documents                                    |
| `fields_mapping`    | `dict[str, str]`   | `id` / `content` / `embeddings` → `embedding` | Column names in your table                                    |
| `embeddings_model`  | `EmbeddingsModel`  | From `set_config`                             | Used for query embeddings                                     |
| `llm_client`        | `LLMClient`        | From `set_config`                             | Topic summarization                                           |
| `seed_topics`       | `list[str]`        | `None`                                        | Skip auto-clustering when set                                 |
| `language`          | `LanguageType`     | Detected                                      | Document language                                             |
| `language_detector` | `LanguageDetector` | FastText if installed                         | Optional override                                             |
| `saved_topics_path` | `str`              | `None`                                        | Persist clustered topics                                      |

Default `fields_mapping` is `{"id": "id", "content": "content", "embeddings": "embedding"}`.

## Example

```python theme={null}
from trusttest.embeddings import get_embeddings_model
from trusttest.knowledge_base.pgvector import PgVectorKnowledgeBase

kb = PgVectorKnowledgeBase(
    connection_string="postgresql://postgres:password@localhost/music",
    table_name="Song",
    fields_mapping={
        "id": "id",
        "content": "lyrics",
        "embeddings": "embedding",
    },
    embeddings_model=get_embeddings_model(
        provider="openai",
        model="text-embedding-3-small",
    ),
    seed_topics=["love", "party", "nature", "sadness"],
)

results = kb.search("ocean")
```

Configure embeddings globally with `trusttest.set_config({"embeddings": {"provider": "openai", "model": "text-embedding-3-small"}})` if you omit `embeddings_model`.

## Notes

* Enable the extension: `CREATE EXTENSION IF NOT EXISTS vector;`
* If the embeddings column is missing, semantic search is disabled.
