Repository: zulip/zulip

Issue:
Add Support for Custom Database Names & Users
Add Support for Custom Database Names & Users

As per - https://chat.zulip.org/#narrow/stream/2-general/topic/Change.20DB.20user.20and.20name &  https://github.com/zulip/docker-zulip/issues/289

@zulipbot claim


Discussion:
@zulipbot add "area: production installer"
Hello @zulip/server-production members, this issue was labeled with the "area: production installer" label, so you may want to check it out!

<!-- areaLabelAddition -->
Hello @adambirds, you claimed this issue to work on it, but this issue and any referenced pull requests haven't been updated for 10 days. Are you still working on this issue?

If so, please update this issue by leaving a comment on this issue to let me know that you're still working on it. Otherwise, I'll automatically remove you from this issue in 4 days.

If you've decided to work on something else, simply comment `@zulipbot abandon` so that someone else can claim it and continue from where you left off.

Thank you for your valuable contributions to Zulip!

<!-- inactiveWarning -->

Resolving patch:
diff --git a/scripts/lib/zulip_tools.py b/scripts/lib/zulip_tools.py
--- a/scripts/lib/zulip_tools.py
+++ b/scripts/lib/zulip_tools.py
@@ -581,8 +581,10 @@ def get_deploy_options(config_file: configparser.RawConfigParser) -> List[str]:
 
 
 def run_psql_as_postgres(
+    config_file: configparser.RawConfigParser,
     sql_query: str,
 ) -> None:
+    dbname = get_config(config_file, "postgresql", "database_name", "zulip")
     subcmd = " ".join(
         map(
             shlex.quote,
@@ -590,8 +592,8 @@ def run_psql_as_postgres(
                 "psql",
                 "-v",
                 "ON_ERROR_STOP=1",
-                # TODO: Stop hardcoding the database name.
-                "zulip",
+                "-d",
+                dbname,
                 "-c",
                 sql_query,
             ],
diff --git a/scripts/setup/generate_secrets.py b/scripts/setup/generate_secrets.py
--- a/scripts/setup/generate_secrets.py
+++ b/scripts/setup/generate_secrets.py
@@ -7,6 +7,7 @@
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
 sys.path.append(BASE_DIR)
 from scripts.lib.setup_path import setup_path
+from scripts.lib.zulip_tools import get_config, get_config_file
 
 setup_path()
 
@@ -101,6 +102,13 @@ def add_secret(name: str, value: str) -> None:
     if development and need_secret("local_database_password"):
         add_secret("local_database_password", random_token())
 
+    # We only need a secret if the database username does not match
+    # the OS username, as identd auth works in that case.
+    if get_config(
+        get_config_file(), "postgresql", "database_user", "zulip"
+    ) != "zulip" and need_secret("postgres_password"):
+        add_secret("postgres_password", random_token())
+
     # The core Django SECRET_KEY setting, used by Django internally to
     # secure sessions.  If this gets changed, all users will be logged out.
     if need_secret("secret_key"):
diff --git a/zproject/computed_settings.py b/zproject/computed_settings.py
--- a/zproject/computed_settings.py
+++ b/zproject/computed_settings.py
@@ -281,8 +281,8 @@ def get_dirs(self) -> List[PosixPath]:
 DATABASES: Dict[str, Dict[str, Any]] = {
     "default": {
         "ENGINE": "django.db.backends.postgresql",
-        "NAME": "zulip",
-        "USER": "zulip",
+        "NAME": get_config("postgresql", "database_name", "zulip"),
+        "USER": get_config("postgresql", "database_user", "zulip"),
         # Password = '' => peer/certificate authentication (no password)
         "PASSWORD": "",
         # Host = '' => connect to localhost by default
@@ -314,7 +314,12 @@ def get_dirs(self) -> List[PosixPath]:
         DATABASES["default"]["OPTIONS"]["sslmode"] = REMOTE_POSTGRES_SSLMODE
     else:
         DATABASES["default"]["OPTIONS"]["sslmode"] = "verify-full"
-
+elif get_config("postgresql", "database_user") != "zulip":
+    if get_secret("postgres_password") is not None:
+        DATABASES["default"].update(
+            PASSWORD=get_secret("postgres_password"),
+            HOST="localhost",
+        )
 POSTGRESQL_MISSING_DICTIONARIES = bool(get_config("postgresql", "missing_dictionaries", None))
 
 DEFAULT_AUTO_FIELD = "django.db.models.AutoField"


Test patch: