From 52cecd303fbbfba83fabd57c57de9199cb6dde03 Mon Sep 17 00:00:00 2001 From: MHSanaei Date: Wed, 27 May 2026 22:41:28 +0200 Subject: [PATCH] feat(install): random PostgreSQL role + post-install credentials display MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The local-Postgres installer used to bake in a static role name (`xui`) and only printed `PostgreSQL (xui@127.0.0.1:5432/xui)` at the end of install, leaving operators without the random password or any hint of how to connect from the shell. Two changes: - install_postgres_local now generates an 8-char random role name alongside the random password, and double-quotes identifiers in the CREATE/ALTER statements (a random alphanumeric may start with a digit, which Postgres rejects for unquoted identifiers). - After a successful local install, a dedicated "PostgreSQL Credentials" block is rendered in the summary — DB / user / pass / host / port / DSN / env-file path, plus ready-to-paste psql commands for both the postgres superuser and the new role. Credentials cross the subshell boundary via a 0600 tmpfile (PG_CRED_FILE) that the parent shell sources and unlinks; the PG_* vars are unset after display. Only fires for the local-install flow; the external-DSN path is unchanged. --- install.sh | 81 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/install.sh b/install.sh index f28751c3..c65b9963 100644 --- a/install.sh +++ b/install.sh @@ -111,10 +111,12 @@ gen_random_string() { } install_postgres_local() { - local pg_user="xui" - local pg_db="xui" - local pg_pass + local pg_user pg_pass + pg_user=$(gen_random_string 8) pg_pass=$(gen_random_string 24) + local pg_db="xui" + local pg_host="127.0.0.1" + local pg_port="5432" case "${release}" in ubuntu | debian | armbian) @@ -170,20 +172,34 @@ install_postgres_local() { sleep 1 done - # Idempotent role/db creation. + # Idempotent role/db creation. Identifiers are double-quoted because a + # random username may start with a digit, which Postgres rejects unquoted. sudo -u postgres psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='${pg_user}'" 2> /dev/null \ | grep -q 1 \ - || sudo -u postgres psql -c "CREATE USER ${pg_user} WITH PASSWORD '${pg_pass}';" >&2 || return 1 + || sudo -u postgres psql -c "CREATE USER \"${pg_user}\" WITH PASSWORD '${pg_pass}';" >&2 || return 1 sudo -u postgres psql -tAc "SELECT 1 FROM pg_database WHERE datname='${pg_db}'" 2> /dev/null \ | grep -q 1 \ - || sudo -u postgres psql -c "CREATE DATABASE ${pg_db} OWNER ${pg_user};" >&2 || return 1 + || sudo -u postgres psql -c "CREATE DATABASE \"${pg_db}\" OWNER \"${pg_user}\";" >&2 || return 1 - sudo -u postgres psql -c "ALTER USER ${pg_user} WITH PASSWORD '${pg_pass}';" >&2 || return 1 + sudo -u postgres psql -c "ALTER USER \"${pg_user}\" WITH PASSWORD '${pg_pass}';" >&2 || return 1 local pg_pass_enc pg_pass_enc=$(printf '%s' "${pg_pass}" | sed -e 's/%/%25/g' -e 's/:/%3A/g' -e 's/@/%40/g' -e 's|/|%2F|g' -e 's/?/%3F/g' -e 's/#/%23/g') - echo "postgres://${pg_user}:${pg_pass_enc}@127.0.0.1:5432/${pg_db}?sslmode=disable" + + if [[ -n "${PG_CRED_FILE:-}" ]]; then + umask 077 + cat > "${PG_CRED_FILE}" <