LlamaIndex KVStore Postgres Integration

raw JSON →
0.5.0 verified Fri May 01 auth: no python

PostgreSQL-backed key-value store for LlamaIndex, enabling persistent storage of index metadata and documents. Currently at version 0.5.0, follows LlamaIndex's release cadence but is a relatively stable integration.

pip install llama-index-storage-kvstore-postgres
error ModuleNotFoundError: No module named 'llama_index.kvstore'
cause Import path changed in 0.5.0
fix
Use from llama_index.storage.kvstore.postgres import PostgresKVStore
error sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "kvstore" does not exist
cause Table not created automatically
fix
Set create_table=True or pre-create the table: CREATE TABLE IF NOT EXISTS public.kvstore (key TEXT PRIMARY KEY, data JSONB);
breaking In version 0.5.0, the import path changed from llama_index.kvstore.postgres to llama_index.storage.kvstore.postgres.
fix Update imports: from llama_index.storage.kvstore.postgres import PostgresKVStore
gotcha PostgresKVStore does not automatically create the specified table; it must exist or be created via the `create_table` parameter or manually.
fix Set create_table=True on initialization or run CREATE TABLE IF NOT EXISTS statement.
deprecated The `async_put` and `async_get` methods are deprecated in favor of synchronous versions; async support may be removed in future releases.
fix Use synchronous put/get methods; if async needed, use asyncio loop with run_in_executor.

Initialize a PostgresKVStore with connection string and perform basic put/get operations.

from llama_index.storage.kvstore.postgres import PostgresKVStore

kvstore = PostgresKVStore(
    connection_string="postgresql://user:pass@localhost:5432/dbname",
    table_name="kvstore",
    schema_name="public"
)
# Example: set and get
kvstore.put(key="test_key", val={"data": "value"})
result = kvstore.get(key="test_key")
print(result)