[GH-ISSUE #4876] [Bug]: Error initializing database #3408

Closed
opened 2026-10-31 20:32:13 +07:00 by bsakaguchi · 4 comments
Owner

Originally created by @madcat-314 on GitHub (Jun 3, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/4876

Describe the bug

Immediately after a fresh installation of 3x-ui with PostgreSQL on AlmaLinux 10.2 — I encountered an error: the panel failed to launch.
The logs showed the following entry:

Jun 03 21:13:20 SRV-NAME x-ui[3047]: 2026/06/03 21:13:20 Error initializing database: failed to connect to user=PG-USER database=xui: 127.0.0.1:5432 (127.0.0.1): server error: FATAL: Ident authentication failed for user "PG-USER" (SQLSTATE 28000)

This indicates that PostgreSQL was attempting to authenticate the user via "ident" authentication — that is, by verifying the correspondence between the Linux system user and the PostgreSQL role. For an application, however, password-based authentication (specifically md5 or scram-sha-256) is almost always required instead of ident.

After I edited the /var/lib/pgsql/data/pg_hba.conf file by adding the following lines (above more general rules):

host    xui     PG-USER     127.0.0.1/32     scram-sha-256
host    xui     PG-USER     ::1/128          scram-sha-256

everything started working immediately.

How to reproduce the problem

  1. On AlmaLinux 10.2 run install script.
  2. Choose PostgreSQL as db.
  3. Follow other steps.
  4. Run x-ui.
  5. Check logs.

Expected behavior

Normal running.

Actual behavior

Just after install:

Panel state: Not running
Start automatically: Yes
xray state: Not running

Relevant logs


Screenshots

No response

3x-ui version

3.2.6

Xray-core version

No response

How did you install 3x-ui?

install.sh script

Operating system

AlmaLinux 10.2

Which parts of the panel are affected?

No response

Browser (only if it is a UI bug)

No response

Is the panel behind a reverse proxy or CDN?

None

Before submitting

  • I searched existing issues and this bug has not been reported.
  • I am running the latest released version of 3x-ui (or have verified the bug still exists on it).
  • This bug report is written in English.
  • I have redacted any sensitive data (tokens, real domains, client UUIDs).
Originally created by @madcat-314 on GitHub (Jun 3, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/4876 ### Describe the bug Immediately after a fresh installation of 3x-ui with PostgreSQL on AlmaLinux 10.2 — I encountered an error: the panel failed to launch. The logs showed the following entry: `Jun 03 21:13:20 SRV-NAME x-ui[3047]: 2026/06/03 21:13:20 Error initializing database: failed to connect to `user=PG-USER database=xui`: 127.0.0.1:5432 (127.0.0.1): server error: FATAL: Ident authentication failed for user "PG-USER" (SQLSTATE 28000)` This indicates that PostgreSQL was attempting to authenticate the user via "ident" authentication — that is, by verifying the correspondence between the Linux system user and the PostgreSQL role. For an application, however, password-based authentication (specifically `md5` or `scram-sha-256`) is almost always required instead of `ident`. After I edited the `/var/lib/pgsql/data/pg_hba.conf` file by adding the following lines (above more general rules): ``` host xui PG-USER 127.0.0.1/32 scram-sha-256 host xui PG-USER ::1/128 scram-sha-256 ``` everything started working immediately. ### How to reproduce the problem 1. On AlmaLinux 10.2 run install script. 2. Choose PostgreSQL as db. 3. Follow other steps. 4. Run x-ui. 5. Check logs. ### Expected behavior Normal running. ### Actual behavior Just after install: Panel state: Not running Start automatically: Yes xray state: Not running ### Relevant logs ```shell ``` ### Screenshots _No response_ ### 3x-ui version 3.2.6 ### Xray-core version _No response_ ### How did you install 3x-ui? install.sh script ### Operating system AlmaLinux 10.2 ### Which parts of the panel are affected? _No response_ ### Browser (only if it is a UI bug) _No response_ ### Is the panel behind a reverse proxy or CDN? None ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this bug has not been reported. - [x] I am running the latest released version of 3x-ui (or have verified the bug still exists on it). - [x] This bug report is written in English. - [x] I have redacted any sensitive data (tokens, real domains, client UUIDs).
bsakaguchi 2026-10-31 20:32:13 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Jun 3, 2026):

Root cause confirmed

This is a genuine bug in install_postgres_local() in install.sh. The function installs PostgreSQL, creates the role/database, and builds a TCP DSN (postgres://USER:PASS@127.0.0.1:5432/xui?sslmode=disable), but it never modifies pg_hba.conf to permit password-based authentication over that TCP connection.

On RHEL-family distros (AlmaLinux, Rocky Linux, CentOS, Fedora, RHEL), postgresql-setup --initdb produces a default pg_hba.conf whose TCP rules use ident authentication. That means PostgreSQL checks whether the connecting Linux username matches the PostgreSQL role name. Since x-ui runs as root (or another system user) and the PostgreSQL role has a random 8-character name, the check always fails:

FATAL: Ident authentication failed for user "PG-USER" (SQLSTATE 28000)

On Debian/Ubuntu the defaults are different (local connections use peer, TCP uses md5), so the same installer works there without modification.


Your fix is correct

Adding host-specific scram-sha-256 (or md5) rules for the application user above the generic ident line is the right solution:

# /var/lib/pgsql/data/pg_hba.conf — add these above the generic "ident" lines
host    xui     PG-USER     127.0.0.1/32     scram-sha-256
host    xui     PG-USER     ::1/128          scram-sha-256

After editing, reload PostgreSQL so the new rules take effect:

sudo systemctl reload postgresql

What should be fixed in the installer

install_postgres_local() should, after creating the role and database, locate pg_hba.conf via PostgreSQL's own SHOW hba_file and inject the password-auth entries before the generic ident rules, then reload PostgreSQL. Without this step the installer is broken for all RHEL-family distros.

This is a real bug affecting anyone who chooses the "install PostgreSQL locally" option on AlmaLinux, Rocky Linux, CentOS, Fedora, or RHEL.

<!-- gh-comment-id:4616163922 --> @github-actions[bot] commented on GitHub (Jun 3, 2026): ## Root cause confirmed This is a genuine bug in `install_postgres_local()` in `install.sh`. The function installs PostgreSQL, creates the role/database, and builds a TCP DSN (`postgres://USER:PASS@127.0.0.1:5432/xui?sslmode=disable`), but it **never modifies `pg_hba.conf`** to permit password-based authentication over that TCP connection. On **RHEL-family distros** (AlmaLinux, Rocky Linux, CentOS, Fedora, RHEL), `postgresql-setup --initdb` produces a default `pg_hba.conf` whose TCP rules use `ident` authentication. That means PostgreSQL checks whether the connecting Linux username matches the PostgreSQL role name. Since x-ui runs as root (or another system user) and the PostgreSQL role has a random 8-character name, the check always fails: ``` FATAL: Ident authentication failed for user "PG-USER" (SQLSTATE 28000) ``` On Debian/Ubuntu the defaults are different (local connections use `peer`, TCP uses `md5`), so the same installer works there without modification. --- ## Your fix is correct Adding host-specific `scram-sha-256` (or `md5`) rules for the application user **above** the generic `ident` line is the right solution: ``` # /var/lib/pgsql/data/pg_hba.conf — add these above the generic "ident" lines host xui PG-USER 127.0.0.1/32 scram-sha-256 host xui PG-USER ::1/128 scram-sha-256 ``` After editing, reload PostgreSQL so the new rules take effect: ```bash sudo systemctl reload postgresql ``` --- ## What should be fixed in the installer `install_postgres_local()` should, after creating the role and database, locate `pg_hba.conf` via PostgreSQL's own `SHOW hba_file` and inject the password-auth entries before the generic `ident` rules, then reload PostgreSQL. Without this step the installer is broken for all RHEL-family distros. This is a real bug affecting anyone who chooses the "install PostgreSQL locally" option on AlmaLinux, Rocky Linux, CentOS, Fedora, or RHEL.
Author
Owner

@madcat-314 commented on GitHub (Jun 4, 2026):

I am not a professional programmer, so I consulted a neural network; I tested the following fix locally, and it worked. Perhaps this could serve as a basis for a more canonical solution.

  1. Create a "configure_postgres_hba_for_local_xui()" function.
configure_postgres_hba_for_local_xui() {
    local pg_db="${1:?PostgreSQL database name is required}"
    local pg_user="${2:?PostgreSQL user is required}"

    case "${release}" in
        fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol | centos)
            ;;
        *)
            return 0
            ;;
    esac

    echo "Configuring PostgreSQL host authentication for ${pg_user}@${pg_db}..." >&2

    local hba_file
    hba_file=$(sudo -u postgres psql -tAc "SHOW hba_file;" 2>/dev/null | xargs)

    if [[ -z "${hba_file}" || ! -f "${hba_file}" ]]; then
        echo "Warning: could not determine pg_hba.conf location; skipping PostgreSQL hba configuration" >&2
        return 0
    fi

    local server_version_num
    server_version_num=$(sudo -u postgres psql -tAc "SHOW server_version_num;" 2>/dev/null | xargs)

    local auth_method="md5"
    if [[ "${server_version_num}" =~ ^[0-9]+$ ]] && (( server_version_num >= 100000 )); then
        auth_method="scram-sha-256"
        sudo -u postgres psql -tAc "ALTER SYSTEM SET password_encryption = 'scram-sha-256'; SELECT pg_reload_conf();" >/dev/null 2>&1 || true
    fi

    local hba_rule="host    ${pg_db}     ${pg_user}     127.0.0.1/32     ${auth_method}"

    if grep -Fxq "${hba_rule}" "${hba_file}"; then
        echo "PostgreSQL pg_hba.conf already contains required x-ui rule" >&2
        return 0
    fi

    cp "${hba_file}" "${hba_file}.bak.$(date +%Y%m%d%H%M%S)" || return 1

    awk -v rule="${hba_rule}" '
        BEGIN { inserted = 0 }

        /^[[:space:]]*host[[:space:]]+/ && inserted == 0 {
            print rule
            inserted = 1
        }

        { print }

        END {
            if (inserted == 0) {
                print rule
            }
        }
    ' "${hba_file}" > "${hba_file}.tmp" || return 1

    mv "${hba_file}.tmp" "${hba_file}" || return 1
    chown postgres:postgres "${hba_file}" 2>/dev/null || true
    chmod 600 "${hba_file}" 2>/dev/null || true

    sudo -u postgres psql -tAc "SELECT pg_reload_conf();" >/dev/null 2>&1 || true

    echo "PostgreSQL pg_hba.conf configured for ${pg_user}@${pg_db} using ${auth_method}" >&2
}
  1. Add a call to this function inside install_postgres_local
    —specifically, after this line:

sudo -u postgres psql -c "CREATE DATABASE \"${pg_db}\" OWNER \"${pg_user}\";" >&2 || return 1

add

configure_postgres_hba_for_local_xui "${pg_db}" "${pg_user}" || return 1

<!-- gh-comment-id:4619146502 --> @madcat-314 commented on GitHub (Jun 4, 2026): I am not a professional programmer, so I consulted a neural network; I tested the following fix locally, and it worked. Perhaps this could serve as a basis for a more canonical solution. 1. Create a "configure_postgres_hba_for_local_xui()" function. ``` configure_postgres_hba_for_local_xui() { local pg_db="${1:?PostgreSQL database name is required}" local pg_user="${2:?PostgreSQL user is required}" case "${release}" in fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol | centos) ;; *) return 0 ;; esac echo "Configuring PostgreSQL host authentication for ${pg_user}@${pg_db}..." >&2 local hba_file hba_file=$(sudo -u postgres psql -tAc "SHOW hba_file;" 2>/dev/null | xargs) if [[ -z "${hba_file}" || ! -f "${hba_file}" ]]; then echo "Warning: could not determine pg_hba.conf location; skipping PostgreSQL hba configuration" >&2 return 0 fi local server_version_num server_version_num=$(sudo -u postgres psql -tAc "SHOW server_version_num;" 2>/dev/null | xargs) local auth_method="md5" if [[ "${server_version_num}" =~ ^[0-9]+$ ]] && (( server_version_num >= 100000 )); then auth_method="scram-sha-256" sudo -u postgres psql -tAc "ALTER SYSTEM SET password_encryption = 'scram-sha-256'; SELECT pg_reload_conf();" >/dev/null 2>&1 || true fi local hba_rule="host ${pg_db} ${pg_user} 127.0.0.1/32 ${auth_method}" if grep -Fxq "${hba_rule}" "${hba_file}"; then echo "PostgreSQL pg_hba.conf already contains required x-ui rule" >&2 return 0 fi cp "${hba_file}" "${hba_file}.bak.$(date +%Y%m%d%H%M%S)" || return 1 awk -v rule="${hba_rule}" ' BEGIN { inserted = 0 } /^[[:space:]]*host[[:space:]]+/ && inserted == 0 { print rule inserted = 1 } { print } END { if (inserted == 0) { print rule } } ' "${hba_file}" > "${hba_file}.tmp" || return 1 mv "${hba_file}.tmp" "${hba_file}" || return 1 chown postgres:postgres "${hba_file}" 2>/dev/null || true chmod 600 "${hba_file}" 2>/dev/null || true sudo -u postgres psql -tAc "SELECT pg_reload_conf();" >/dev/null 2>&1 || true echo "PostgreSQL pg_hba.conf configured for ${pg_user}@${pg_db} using ${auth_method}" >&2 } ``` 2. Add a call to this function inside `install_postgres_local` —specifically, after this line: `sudo -u postgres psql -c "CREATE DATABASE \"${pg_db}\" OWNER \"${pg_user}\";" >&2 || return 1` add `configure_postgres_hba_for_local_xui "${pg_db}" "${pg_user}" || return 1`
Author
Owner

@madcat-314 commented on GitHub (Jun 4, 2026):

Version 3.2.7 is affected by this same bug. The solution is the same as in the previous version.

<!-- gh-comment-id:4623087612 --> @madcat-314 commented on GitHub (Jun 4, 2026): Version 3.2.7 is affected by this same bug. The solution is the same as in the previous version.
Author
Owner

@MHSanaei commented on GitHub (Jun 4, 2026):

fixed

<!-- gh-comment-id:4623428982 --> @MHSanaei commented on GitHub (Jun 4, 2026): fixed
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#3408
No description provided.