oedb-backend/DB_CONNECTION_FIX.md
2025-09-15 23:54:04 +02:00

3.3 KiB

PostgreSQL Authentication Fix

Issue Description

The server was failing to connect to the PostgreSQL database with the following error:

[OEDB] [ERROR] Failed to connect to PostgreSQL database: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "cipherbliss"

This error occurs because the application was trying to connect to PostgreSQL using Unix socket connections, which default to "peer" authentication (using the operating system username). However, the database was configured to use password authentication, as evidenced by the fact that DBeaver could connect using the password from the .env file.

Solution

The fix involves modifying the database connection logic in oedb/utils/db.py to:

  1. Force TCP/IP connections instead of Unix socket connections by setting the host to "localhost" when it's empty
  2. Add appropriate connection parameters for local connections
  3. Improve debug logging to help with troubleshooting

These changes ensure that the application uses password authentication instead of peer authentication when connecting to the database.

Testing the Fix

A test script has been created to verify that the database connection works with the new changes:

./test_db_connection.py

This script attempts to connect to the database using the same connection logic as the main application and reports whether the connection was successful.

Technical Details

Changes Made

  1. Modified db_connect() function to set host to "localhost" when it's empty:

    # If host is empty, set it to localhost to force TCP/IP connection
    # instead of Unix socket (which uses peer authentication)
    if not host:
        host = "localhost"
    
  2. Added appropriate connection parameters for local connections:

    # For localhost connections, add additional parameters to ensure proper connection
    if host in ('localhost', '127.0.0.1'):
        logger.debug("Using TCP/IP connection with additional parameters")
        return psycopg2.connect(
            dbname=dbname,
            host=host,
            password=password,
            user=user,
            options="-c client_encoding=utf8 -c statement_timeout=3000",
            connect_timeout=3,
            application_name="oedb",
            # Disable SSL for local connections
            sslmode='disable')
    
  3. Improved debug logging to help with troubleshooting:

    logger.debug(f"Connecting to database: {dbname} on {host} as user {user}")
    

Why This Works

By setting the host to "localhost" when it's empty, we force psycopg2 to use TCP/IP connections instead of Unix socket connections. TCP/IP connections typically use password authentication, which is what the database is configured to use.

The additional connection parameters for local connections help ensure that the connection is established correctly and provide useful information for debugging.

Additional Notes

If you continue to experience authentication issues, check the following:

  1. Make sure your .env file contains the correct database credentials:

    DB_NAME=oedb
    DB_USER=cipherbliss
    POSTGRES_PASSWORD=your_password
    
  2. Verify that your PostgreSQL server is configured to allow password authentication for the specified user.

  3. Check the PostgreSQL logs for more detailed error messages.