@@ -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" )
0 commit comments