Skip to content

Commit 58de0f3

Browse files
kushalbakshiclaude
andcommitted
feat: add dbname setting for PostgreSQL connections
Add database.dbname config option (env: DJ_DBNAME) to specify which PostgreSQL database to connect to. Defaults to 'postgres' if not set (existing behavior preserved). Required where the primary database has a non-default name. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1e957ef commit 58de0f3

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/datajoint/connection.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ def __init__(
180180
port = int(port)
181181
elif port is None:
182182
port = self._config["database.port"]
183-
self.conn_info = dict(host=host, port=port, user=user, passwd=password)
183+
dbname = self._config.get("database.dbname")
184+
self.conn_info = dict(host=host, port=port, user=user, passwd=password, dbname=dbname)
184185
if use_tls is not False:
185186
# use_tls can be: None (auto-detect), True (enable), False (disable), or dict (custom config)
186187
if isinstance(use_tls, dict):
@@ -224,14 +225,17 @@ def connect(self) -> None:
224225
warnings.filterwarnings("ignore", ".*deprecated.*")
225226
try:
226227
# Use adapter to create connection
227-
self._conn = self.adapter.connect(
228+
connect_kwargs = dict(
228229
host=self.conn_info["host"],
229230
port=self.conn_info["port"],
230231
user=self.conn_info["user"],
231232
password=self.conn_info["passwd"],
232233
charset=self._config["connection.charset"],
233234
use_tls=self.conn_info.get("ssl"),
234235
)
236+
if self.conn_info.get("dbname"):
237+
connect_kwargs["dbname"] = self.conn_info["dbname"]
238+
self._conn = self.adapter.connect(**connect_kwargs)
235239
except Exception as ssl_error:
236240
# If SSL fails, retry without SSL (if it was auto-detected)
237241
if self.conn_info.get("ssl_input") is None:
@@ -240,14 +244,17 @@ def connect(self) -> None:
240244
"To require SSL, set use_tls=True explicitly.",
241245
ssl_error,
242246
)
243-
self._conn = self.adapter.connect(
247+
connect_kwargs = dict(
244248
host=self.conn_info["host"],
245249
port=self.conn_info["port"],
246250
user=self.conn_info["user"],
247251
password=self.conn_info["passwd"],
248252
charset=self._config["connection.charset"],
249253
use_tls=False, # Explicitly disable SSL for fallback
250254
)
255+
if self.conn_info.get("dbname"):
256+
connect_kwargs["dbname"] = self.conn_info["dbname"]
257+
self._conn = self.adapter.connect(**connect_kwargs)
251258
else:
252259
raise
253260
self._is_closed = False # Mark as connected after successful connection

src/datajoint/settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ class DatabaseSettings(BaseSettings):
196196
description="Database backend: 'mysql' or 'postgresql'",
197197
)
198198
port: int | None = Field(default=None, validation_alias="DJ_PORT")
199+
dbname: str | None = Field(
200+
default=None,
201+
validation_alias="DJ_DBNAME",
202+
description="Database name for PostgreSQL connections. "
203+
"Defaults to 'postgres' if not set.",
204+
)
199205
reconnect: bool = True
200206
use_tls: bool | None = Field(default=None, validation_alias="DJ_USE_TLS")
201207
database_prefix: str = Field(

0 commit comments

Comments
 (0)