Skip to content

Commit a001c9a

Browse files
committed
chore: Pgvector - document expected connection string and raise informative error
1 parent 0d95514 commit a001c9a

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

integrations/pgvector/src/haystack_integrations/document_stores/pgvector/document_store.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ def __init__(
101101
A specific table to store Haystack documents will be created if it doesn't exist yet.
102102
103103
:param connection_string: The connection string to use to connect to the PostgreSQL database, defined as an
104-
environment variable. It can be provided in either URI format
105-
e.g.: `PG_CONN_STR="postgresql://USER:PASSWORD@HOST:PORT/DB_NAME"`, or keyword/value format
106-
e.g.: `PG_CONN_STR="host=HOST port=PORT dbname=DBNAME user=USER password=PASSWORD"`
104+
environment variable. Supported formats:
105+
- URI, e.g. `postgresql://USER:PASSWORD@HOST:PORT/DB_NAME` (use percent-encoding for special characters)
106+
- keyword/value format, e.g. `PG_CONN_STR="host=HOST port=PORT dbname=DBNAME user=USER password=PASSWORD"`
107107
See [PostgreSQL Documentation](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING)
108108
for more details.
109109
:param create_extension: Whether to create the pgvector extension if it doesn't exist.
@@ -360,7 +360,15 @@ def _ensure_db_setup(self):
360360
logger.debug("Failed to close connection: {e}", e=str(e))
361361

362362
conn_str = self.connection_string.resolve_value() or ""
363-
connection = Connection.connect(conn_str)
363+
try:
364+
connection = Connection.connect(conn_str)
365+
except Error as e:
366+
msg = (
367+
"Failed to connect to PostgreSQL database. Ensure the connection string follows the "
368+
"PostgreSQL connection specification: "
369+
"https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING."
370+
)
371+
raise DocumentStoreError(msg) from e
364372
connection.autocommit = True
365373
if self.create_extension:
366374
connection.execute("CREATE EXTENSION IF NOT EXISTS vector")
@@ -394,7 +402,15 @@ async def _ensure_db_setup_async(self):
394402
await self._async_connection.close()
395403

396404
conn_str = self.connection_string.resolve_value() or ""
397-
async_connection = await AsyncConnection.connect(conn_str)
405+
try:
406+
async_connection = await AsyncConnection.connect(conn_str)
407+
except Error as e:
408+
msg = (
409+
"Failed to connect to PostgreSQL database. Ensure the connection string follows the "
410+
"PostgreSQL connection specification: "
411+
"https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING."
412+
)
413+
raise DocumentStoreError(msg) from e
398414
await async_connection.set_autocommit(True)
399415
if self.create_extension:
400416
await async_connection.execute("CREATE EXTENSION IF NOT EXISTS vector")

integrations/pgvector/tests/test_document_store.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import psycopg
88
import pytest
99
from haystack.dataclasses.document import ByteStream, Document
10-
from haystack.document_stores.errors import DuplicateDocumentError
10+
from haystack.document_stores.errors import DocumentStoreError, DuplicateDocumentError
1111
from haystack.document_stores.types import DuplicatePolicy
1212
from haystack.testing.document_store import CountDocumentsTest, DeleteDocumentsTest, WriteDocumentsTest
1313
from haystack.utils import Secret
@@ -60,6 +60,13 @@ def test_delete_all_documents(self, document_store: PgvectorDocumentStore) -> No
6060
document_store.write_documents([Document(id="1")])
6161
assert document_store.count_documents() == 1
6262

63+
def test_invalid_connection_string(self, monkeypatch):
64+
monkeypatch.setenv("PG_CONN_STR", "invalid_connection_string")
65+
document_store = PgvectorDocumentStore()
66+
with pytest.raises(DocumentStoreError) as e:
67+
document_store._ensure_db_setup()
68+
assert "Failed to connect to PostgreSQL database" in str(e)
69+
6370

6471
@pytest.mark.usefixtures("patches_for_unit_tests")
6572
def test_init(monkeypatch):

integrations/pgvector/tests/test_document_store_async.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import psycopg
88
import pytest
99
from haystack.dataclasses.document import ByteStream, Document
10-
from haystack.document_stores.errors import DuplicateDocumentError
10+
from haystack.document_stores.errors import DocumentStoreError, DuplicateDocumentError
1111
from haystack.document_stores.types import DuplicatePolicy
1212
from haystack.utils import Secret
1313
from psycopg.sql import SQL
@@ -101,6 +101,13 @@ async def test_delete_all_documents_async(self, document_store: PgvectorDocument
101101
document_store.write_documents([Document(id="1")])
102102
assert document_store.count_documents() == 1
103103

104+
async def test_invalid_connection_string(self, monkeypatch):
105+
monkeypatch.setenv("PG_CONN_STR", "invalid_connection_string")
106+
document_store = PgvectorDocumentStore()
107+
with pytest.raises(DocumentStoreError) as e:
108+
await document_store._ensure_db_setup_async()
109+
assert "Failed to connect to PostgreSQL database" in str(e)
110+
104111

105112
@pytest.mark.integration
106113
@pytest.mark.asyncio

0 commit comments

Comments
 (0)