Skip to content

Commit dce6324

Browse files
authored
🔌 feat: Enable PostgreSQL Unix Socket Connection (#153)
1 parent 38b706c commit dce6324

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ The following environment variables are required to run the application:
4343
- `RAG_OPENAI_BASEURL`: (Optional) The base URL for your OpenAI API Embeddings
4444
- `RAG_OPENAI_PROXY`: (Optional) Proxy for OpenAI API Embeddings
4545
- `VECTOR_DB_TYPE`: (Optional) select vector database type, default to `pgvector`.
46+
- `POSTGRES_USE_UNIX_SOCKET`: (Optional) Set to "True" when connecting to the PostgreSQL database server with Unix Socket.
4647
- `POSTGRES_DB`: (Optional) The name of the PostgreSQL database, used when `VECTOR_DB_TYPE=pgvector`.
4748
- `POSTGRES_USER`: (Optional) The username for connecting to the PostgreSQL database.
4849
- `POSTGRES_PASSWORD`: (Optional) The password for connecting to the PostgreSQL database.

app/config.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ def get_env_variable(
5050
VECTOR_DB_TYPE = VectorDBType(
5151
get_env_variable("VECTOR_DB_TYPE", VectorDBType.PGVECTOR.value)
5252
)
53+
POSTGRES_USE_UNIX_SOCKET = (
54+
get_env_variable("POSTGRES_USE_UNIX_SOCKET", "False").lower() == "true"
55+
)
5356
POSTGRES_DB = get_env_variable("POSTGRES_DB", "mydatabase")
5457
POSTGRES_USER = get_env_variable("POSTGRES_USER", "myuser")
5558
POSTGRES_PASSWORD = get_env_variable("POSTGRES_PASSWORD", "mypassword")
@@ -69,8 +72,13 @@ def get_env_variable(
6972
env_value = get_env_variable("PDF_EXTRACT_IMAGES", "False").lower()
7073
PDF_EXTRACT_IMAGES = True if env_value == "true" else False
7174

72-
CONNECTION_STRING = f"postgresql+psycopg2://{urllib.parse.quote_plus(POSTGRES_USER)}:{urllib.parse.quote_plus(POSTGRES_PASSWORD)}@{DB_HOST}:{DB_PORT}/{urllib.parse.quote_plus(POSTGRES_DB)}"
73-
DSN = f"postgresql://{urllib.parse.quote_plus(POSTGRES_USER)}:{urllib.parse.quote_plus(POSTGRES_PASSWORD)}@{DB_HOST}:{DB_PORT}/{urllib.parse.quote_plus(POSTGRES_DB)}"
75+
if POSTGRES_USE_UNIX_SOCKET:
76+
connection_suffix = f"{urllib.parse.quote_plus(POSTGRES_USER)}:{urllib.parse.quote_plus(POSTGRES_PASSWORD)}@/{urllib.parse.quote_plus(POSTGRES_DB)}?host={urllib.parse.quote_plus(DB_HOST)}"
77+
else:
78+
connection_suffix = f"{urllib.parse.quote_plus(POSTGRES_USER)}:{urllib.parse.quote_plus(POSTGRES_PASSWORD)}@{DB_HOST}:{DB_PORT}/{urllib.parse.quote_plus(POSTGRES_DB)}"
79+
80+
CONNECTION_STRING = f"postgresql+psycopg2://{connection_suffix}"
81+
DSN = f"postgresql://{connection_suffix}"
7482

7583
## Logging
7684

0 commit comments

Comments
 (0)