autogen - ✅(Solved) Fix Autogen Studio: Alembic generate_revision fails with "password authentication failed" despite valid DATABASE_URI and successful psql connections [1 pull requests, 3 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
microsoft/autogen#7341Fetched 2026-04-08 00:39:47
View on GitHub
Comments
3
Participants
3
Timeline
9
Reactions
0
Author
Timeline (top)
commented ×3cross-referenced ×1issue_type_added ×1labeled ×1

Error Message

(Background on this error at: https://sqlalche.me/e/20/e3q8)

  • The UI otherwise works and I can interact with Autogen Studio normally, but the startup logs always show this error during the migration step. 2026-03-04 08:17:15.256 | ERROR | autogenstudio.database.schema_manager:generate_revision:475 - Failed to generate revision: (psycopg.OperationalError) connection failed: connection to server at "postgres" (172.20.0.4), port 5432 failed: FATAL: password authentication failed for user "autogen" (Background on this error at: https://sqlalche.me/e/20/e3q8)

Fix Action

Fixed

PR fix notes

PR #7349: fix: preserve database password in Alembic migration config

Description (problem / solution / changelog)

Summary

Fixes #7341

When Autogen Studio starts with a PostgreSQL database, schema_manager.generate_revision fails with "password authentication failed" even though the database credentials are correct and the main application connects successfully.

Root cause: In SchemaManager._generate_alembic_ini_content(), the database URL is serialized using str(self.engine.url). In SQLAlchemy 2.x, str() on a URL object obfuscates the password as ***, so the URL written to alembic.ini becomes:

postgresql+psycopg://autogen:***@postgres:5432/autogen

When Alembic reads this config to run generate_revision, it tries to authenticate with the literal string *** as the password, which naturally fails.

Fix: Replace str(self.engine.url) with self.engine.url.render_as_string(hide_password=False) to write the actual password into alembic.ini.

Test Plan

  • Verified with sqlalchemy.make_url that the old code produces *** for the password while the new code preserves it
  • Confirmed the fix also handles special characters in passwords (URL-encoded % chars are properly escaped for ConfigParser with %%)
  • All existing test_db_manager.py tests pass (6/6)

Changed files

  • python/packages/autogen-studio/autogenstudio/database/schema_manager.py (modified, +1/-1)

Code Example

services:
  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-autogen}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-autogen}
      POSTGRES_DB: ${POSTGRES_DB:-autogen}
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER}"]
      interval: 10s
      start_period: 40s
      timeout: 5s
      retries: 5

---

autogen-studio:
    build: .
    ports:
      - "8080:8080"
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-autogen}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-autogen}
      POSTGRES_DB: ${POSTGRES_DB:-autogen}
      POSTGRES_HOST: ${POSTGRES_HOST:-postgres}
      POSTGRES_PORT: ${POSTGRES_PORT:-5432}
      DATABASE_URI: ""
      OPENAI_API_KEY: ${OPENAI_API_KEY:-}
    depends_on:
      postgres:
        condition: service_healthy
    command: bash /autogen/scripts/studio-init.sh

---

#!/bin/bash

APP_DIR="/autogen"

# Build DATABASE_URI from environment variables
POSTGRES_USER=${POSTGRES_USER:-autogen}
POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-autogen}
POSTGRES_DB=${POSTGRES_DB:-autogen}
POSTGRES_HOST=${POSTGRES_HOST:-postgres}
POSTGRES_PORT=${POSTGRES_PORT:-5432}

DATABASE_URI="postgresql+psycopg://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
echo "Built DATABASE_URI from environment variables: $DATABASE_URI"

STUDIO_PORT="${STUDIO_PORT:-8080}"
STUDIO_HOST="${STUDIO_HOST:-0.0.0.0}"

autogenstudio ui \
  --database-uri "$DATABASE_URI" \
  --port "$STUDIO_PORT" --reload \
  --host "$STUDIO_HOST" --appdir "$APP_DIR"

---

cat /home/autogen/.autogenstudio/temp_env_vars.env

---

AUTOGENSTUDIO_HOST=0.0.0.0
AUTOGENSTUDIO_PORT=8080
AUTOGENSTUDIO_API_DOCS=True
AUTOGENSTUDIO_APPDIR=/autogen
AUTOGENSTUDIO_DATABASE_URI=postgresql+psycopg://autogen:autogen@postgres:5432/autogen

---

+ APP_DIR=/autogen
+ POSTGRES_USER=autogen
+ POSTGRES_PASSWORD=autogen
+ POSTGRES_DB=autogen
+ POSTGRES_HOST=postgres
+ POSTGRES_PORT=5432
Built DATABASE_URI from environment variables: postgresql+psycopg://autogen:autogen@postgres:5432/autogen
+ autogenstudio ui --database-uri postgresql+psycopg://autogen:autogen@postgres:5432/autogen --port 8080 --reload --host 0.0.0.0 --appdir /autogen
INFO:     Will watch for changes in these directories: ['/autogen']
INFO:     Loading environment from '/home/autogen/.autogenstudio/temp_env_vars.env'
INFO:     Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
INFO:     Started reloader process [7] using WatchFiles
...
2026-03-04 08:17:15.204 | INFO     | autogenstudio.database.db_manager:initialize_database:82 - Creating database tables...
2026-03-04 08:17:15.248 | INFO     | autogenstudio.database.schema_manager:_initialize_alembic:133 - Alembic initialization complete
2026-03-04 08:17:15.256 | ERROR    | autogenstudio.database.schema_manager:generate_revision:475 - Failed to generate revision: (psycopg.OperationalError) connection failed: connection to server at "postgres" (172.20.0.4), port 5432 failed: FATAL:  password authentication failed for user "autogen"
(Background on this error at: https://sqlalche.me/e/20/e3q8)
2026-03-04 08:17:15.257 | INFO     | autogenstudio.web.app:lifespan:39 - Application startup complete. Navigate to http://0.0.0.0:8080

---

docker exec -ti $(docker ps | grep docker-autogen-studio | awk '{print $1}') env | grep -E 'DATABASE_URI|POSTGRES_'
POSTGRES_USER=autogen
POSTGRES_PASSWORD=autogen
POSTGRES_DB=autogen
POSTGRES_HOST=postgres
POSTGRES_PORT=5432

---

# Direct psql test
psql -U autogen -d autogen -h $POSTGRES_HOST -c "SELECT current_user, current_database();"

# Output:
-- current_user | current_database
-- autogen      | autogen

# Non-interactive test with PGPASSWORD
PGPASSWORD=$POSTGRES_PASSWORD psql -U $POSTGRES_USER -d $POSTGRES_DB -h $POSTGRES_HOST -t -c "SELECT 'OK' WHERE 1=1;" && echo "✓ Credentials are correct"

# Output:
OK
Credentials are correct
RAW_BUFFERClick to expand / collapse

What happened?

Describe the bug When running Autogen Studio against a local PostgreSQL database in Docker, the UI works and the main database connections succeed (I can query the DB from inside the autogen-studio container using psql with the same credentials). However, during startup Autogen Studio’s Alembic-based migration step (schema_manager.generate_revision) fails with:

(psycopg.OperationalError) connection failed: connection to server at "postgres" (172.20.0.4), port 5432 failed: FATAL: password authentication failed for user "autogen" (Background on this error at: https://sqlalche.me/e/20/e3q8)

so:

  • The database is up and accepting connections.
  • The autogen user and password are correct and verified via psql.
  • Autogen Studio is started with the correct DATABASE_URI and also loads the same URI from temp_env_vars.env.
  • Yet Alembic’s generate_revision path still hits a password authentication failure.
  • The UI otherwise works and I can interact with Autogen Studio normally, but the startup logs always show this error during the migration step.

To Reproduce I’m running Autogen Studio in Docker with a co-located PostgreSQL 15 instance.

  1. Postgres service (from docker-compose.yml):
services:
  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-autogen}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-autogen}
      POSTGRES_DB: ${POSTGRES_DB:-autogen}
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER}"]
      interval: 10s
      start_period: 40s
      timeout: 5s
      retries: 5
  1. Autogen Studio service (relevant parts):
  autogen-studio:
    build: .
    ports:
      - "8080:8080"
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-autogen}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-autogen}
      POSTGRES_DB: ${POSTGRES_DB:-autogen}
      POSTGRES_HOST: ${POSTGRES_HOST:-postgres}
      POSTGRES_PORT: ${POSTGRES_PORT:-5432}
      DATABASE_URI: ""
      OPENAI_API_KEY: ${OPENAI_API_KEY:-}
    depends_on:
      postgres:
        condition: service_healthy
    command: bash /autogen/scripts/studio-init.sh
  1. Studio init script (studio-init.sh) builds the DB URI from env and starts Autogen Studio:
#!/bin/bash

APP_DIR="/autogen"

# Build DATABASE_URI from environment variables
POSTGRES_USER=${POSTGRES_USER:-autogen}
POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-autogen}
POSTGRES_DB=${POSTGRES_DB:-autogen}
POSTGRES_HOST=${POSTGRES_HOST:-postgres}
POSTGRES_PORT=${POSTGRES_PORT:-5432}

DATABASE_URI="postgresql+psycopg://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
echo "Built DATABASE_URI from environment variables: $DATABASE_URI"

STUDIO_PORT="${STUDIO_PORT:-8080}"
STUDIO_HOST="${STUDIO_HOST:-0.0.0.0}"

autogenstudio ui \
  --database-uri "$DATABASE_URI" \
  --port "$STUDIO_PORT" --reload \
  --host "$STUDIO_HOST" --appdir "$APP_DIR"
  1. Temp env file loaded by Autogen Studio: Inside the autogen-studio container:
cat /home/autogen/.autogenstudio/temp_env_vars.env

Output:

AUTOGENSTUDIO_HOST=0.0.0.0
AUTOGENSTUDIO_PORT=8080
AUTOGENSTUDIO_API_DOCS=True
AUTOGENSTUDIO_APPDIR=/autogen
AUTOGENSTUDIO_DATABASE_URI=postgresql+psycopg://autogen:autogen@postgres:5432/autogen
  1. Docker logs for autogen-studio startup:
+ APP_DIR=/autogen
+ POSTGRES_USER=autogen
+ POSTGRES_PASSWORD=autogen
+ POSTGRES_DB=autogen
+ POSTGRES_HOST=postgres
+ POSTGRES_PORT=5432
Built DATABASE_URI from environment variables: postgresql+psycopg://autogen:autogen@postgres:5432/autogen
+ autogenstudio ui --database-uri postgresql+psycopg://autogen:autogen@postgres:5432/autogen --port 8080 --reload --host 0.0.0.0 --appdir /autogen
INFO:     Will watch for changes in these directories: ['/autogen']
INFO:     Loading environment from '/home/autogen/.autogenstudio/temp_env_vars.env'
INFO:     Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
INFO:     Started reloader process [7] using WatchFiles
...
2026-03-04 08:17:15.204 | INFO     | autogenstudio.database.db_manager:initialize_database:82 - Creating database tables...
2026-03-04 08:17:15.248 | INFO     | autogenstudio.database.schema_manager:_initialize_alembic:133 - Alembic initialization complete
2026-03-04 08:17:15.256 | ERROR    | autogenstudio.database.schema_manager:generate_revision:475 - Failed to generate revision: (psycopg.OperationalError) connection failed: connection to server at "postgres" (172.20.0.4), port 5432 failed: FATAL:  password authentication failed for user "autogen"
(Background on this error at: https://sqlalche.me/e/20/e3q8)
2026-03-04 08:17:15.257 | INFO     | autogenstudio.web.app:lifespan:39 - Application startup complete. Navigate to http://0.0.0.0:8080
  1. Manual DB checks from inside the same autogen-studio container:
docker exec -ti $(docker ps | grep docker-autogen-studio | awk '{print $1}') env | grep -E 'DATABASE_URI|POSTGRES_'
POSTGRES_USER=autogen
POSTGRES_PASSWORD=autogen
POSTGRES_DB=autogen
POSTGRES_HOST=postgres
POSTGRES_PORT=5432

Then

# Direct psql test
psql -U autogen -d autogen -h $POSTGRES_HOST -c "SELECT current_user, current_database();"

# Output:
-- current_user | current_database
-- autogen      | autogen

# Non-interactive test with PGPASSWORD
PGPASSWORD=$POSTGRES_PASSWORD psql -U $POSTGRES_USER -d $POSTGRES_DB -h $POSTGRES_HOST -t -c "SELECT 'OK' WHERE 1=1;" && echo "✓ Credentials are correct"

# Output:
OK
✓ Credentials are correct

Screenshots

  • If helpful, I can add screenshots of:
  • The Autogen Studio UI running successfully.
  • The Postgres logs showing both “database is ready” and the password authentication failed line.
  • The container logs with the exact startup sequence shown above.

Additional context

  • This looks like a discrepancy between:
  • The main DB engine used by db_manager.initialize_database, and
  • The separate engine/URL used by Alembic’s generate_revision.
  • It feels like Alembic might be:
  • Constructing a slightly different URL internally (perhaps missing +psycopg or using a different env key), or
  • Falling back to a different config path when generate_revision is called.
  • I searched existing issues related to database problems in this repo and didn’t see this exact pattern reported yet (is:issue state:open database).

Which packages was the bug in?

AutoGen Studio (autogensudio)

AutoGen library version.

Python dev (main branch)

extent analysis

Fix Plan

The issue seems to be related to the database connection URL used by Alembic's generate_revision method. To fix this, we need to ensure that the DATABASE_URI environment variable is correctly set and used by Alembic.

Here are the steps to fix the issue:

  • Update the studio-init.sh script to set the DATABASE_URI environment variable before starting Autogen Studio:
#!/bin/bash

APP_DIR="/autogen"

# Build DATABASE_URI from environment variables
POSTGRES_USER=${POSTGRES_USER:-autogen}
POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-autogen}
POSTGRES_DB=${POSTGRES_DB:-autogen}
POSTGRES_HOST=${POSTGRES_HOST:-postgres}
POSTGRES_PORT=${POSTGRES_PORT:-5432}

DATABASE_URI="postgresql+psycopg://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
echo "Built DATABASE_URI from environment variables: $DATABASE_URI"

export DATABASE_URI  # Add this line to export the variable

STUDIO_PORT="${STUDIO_PORT:-8080}"
STUDIO_HOST="${STUDIO_HOST:-0.0.0.0}"

autogenstudio ui \
  --database-uri "$DATABASE_URI" \
  --port "$STUDIO_PORT" --reload \
  --host "$STUDIO_HOST" --appdir "$APP_DIR"
  • Update the Autogen Studio code to use the DATABASE_URI environment variable when creating the Alembic engine. This might involve updating the schema_manager module to use the os module to get the DATABASE_URI environment variable:
import os

# ...

def generate_revision(self):
    database_uri = os.environ.get('DATABASE_URI')
    # Use the database_uri to create the Alembic engine
    # ...

Verification

To verify that the fix worked, restart the Autogen Studio container and check the logs for any errors related to database connections. You can also use the psql command to test the database connection manually.

Extra Tips

  • Make sure to update the temp_env_vars.env file to include the correct DATABASE_URI environment variable.
  • If you're using a .env file to store environment variables, make sure to update it to include the correct DATABASE_URI variable.
  • You can use the python-dotenv library to load environment variables from a .env file in your Python code.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

autogen - ✅(Solved) Fix Autogen Studio: Alembic generate_revision fails with "password authentication failed" despite valid DATABASE_URI and successful psql connections [1 pull requests, 3 comments, 3 participants]