ruvnet/RuView · error · ValueError

Database port must be between 1 and 65535

Error message

Database port must be between 1 and 65535

What it means

This ValueError is raised by the Pydantic field_validator validate_db_port on the Settings model in archive/v1/src/config/settings.py. When the db_port field (default 5432) is set outside the range 1..65535, the validator raises ValueError, which Pydantic v2 re-wraps into a ValidationError at Settings instantiation time. It is a fail-fast guard so an invalid port never reaches SQLAlchemy connection setup.

Source

Thrown at archive/v1/src/config/settings.py:231

        """Validate port number."""
        if not 1 <= v <= 65535:
            raise ValueError("Port must be between 1 and 65535")
        return v
    
    @field_validator("workers")
    @classmethod
    def validate_workers(cls, v):
        """Validate worker count."""
        if v < 1:
            raise ValueError("Workers must be at least 1")
        return v
    
    @field_validator("db_port")
    @classmethod
    def validate_db_port(cls, v):
        """Validate database port."""
        if not 1 <= v <= 65535:
            raise ValueError("Database port must be between 1 and 65535")
        return v
    
    @field_validator("redis_port")
    @classmethod
    def validate_redis_port(cls, v):
        """Validate Redis port."""
        if not 1 <= v <= 65535:
            raise ValueError("Redis port must be between 1 and 65535")
        return v
    
    @field_validator("db_pool_size")
    @classmethod
    def validate_db_pool_size(cls, v):
        """Validate database pool size."""
        if v < 1:
            raise ValueError("Database pool size must be at least 1")
        return v
    

View on GitHub (pinned to 4685618388)

Solutions

  1. Set db_port to a valid TCP port, normally 5432 for PostgreSQL (e.g. DB_PORT=5432 in `.env`).
  2. Check the runtime environment for DB_PORT overrides: print the resolved value with `echo $DB_PORT` (or the container/compose env section) before the app starts.
  3. If the port comes from a URL string, parse it with urllib.parse and pass only the numeric `.port`, or set database_url directly so db_port is unused.
  4. If you deliberately want to disable the DB, do not use port 0; instead leave environment=development (SQLite default) or set enable_database_failsafe and omit db_host/db_user.

Example fix

# before
DB_PORT=0
settings = Settings()  # ValidationError: Database port must be between 1 and 65535

# after
DB_PORT=5432
settings = Settings()
Defensive patterns

Strategy: validation

Validate before calling

import os

raw = os.environ.get('DB_PORT', '5432')
try:
    db_port = int(raw)
except ValueError:
    raise SystemExit(f'DB_PORT must be an integer, got {raw!r}')
if not 1 <= db_port <= 65535:
    raise SystemExit(f'DB_PORT must be 1..65535, got {db_port}')

Try / catch

from pydantic import ValidationError

try:
    settings = Settings()
except ValidationError as e:
    # e.errors()[0]['msg'] == 'Value error, Database port must be between 1 and 65535'
    for err in e.errors():
        print(err['loc'], err['msg'])
    raise SystemExit('Fix DB_PORT before starting')

Prevention

When it happens

Trigger: Constructing the Settings object (directly or via get_settings()) while db_port resolves to 0, a negative number, or a value above 65535. Typical inputs: Settings(db_port=0), the environment variable DB_PORT=70000, or a docker-compose/`.env` file that defines DB_PORT to an out-of-range value.

Common situations: Setting DB_PORT=0 intending to disable the database; pasting a full connection string into the port variable; CI pipelines or container images exporting stale DB_PORT values; mis-typed values like DB_PORT=-5432 after an edit; port values copied from an IANA-registered port above 65535 (e.g. mistaking a container hash for a port).

Related errors


AI-assisted analysis of ruvnet/RuView@4685618388 (2026-08-16). Data as JSON: /api/errors/10a7061cbd25c5ef. Report an issue: GitHub.