[Python] How to migrate Django from local sqlite3 to postgresql Docker container?

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to learn Django with some demo projects and want to migrate from sqlite3 to a local postgresql container for dev work. When I try to run uv run python manage.py migrate, I get the following exception:

    django.db.utils.OperationalError: connection failed: connection to server at "127.0.0.1", port 5432 failed: FATAL: password authentication failed for user "myuser"


    While the container is running, the database is accessible via psql, and my pg_hba.conf looks like it should be allowing authentication:

    # TYPE DATABASE USER ADDRESS METHOD

    # "local" is for Unix domain socket connections only
    local all all trust
    # IPv4 local connections:
    host all all 127.0.0.1/32 trust
    # IPv6 local connections:
    host all all ::1/128 trust
    # Allow replication connections from localhost, by a user with the
    # replication privilege.
    local replication all trust
    host replication all 127.0.0.1/32 trust
    host replication all ::1/128 trust

    host all all all scram-sha-256


    I'm creating and running my postgresql container with:

    docker run --name=db_container -d -e POSTGRES_DB=mydatabase -e POSTGRES_USER=myuser -e POSTGRES_PASSWORD=mypassword -p 5432:5432 postgres:16.2


    My .env file contains:

    POSTGRES_DB=mydatabase
    POSTGRES_USER=myuser
    POSTGRES_PASSWORD=mypassword
    HOST=localhost
    PORT=5432


    My settings.py file contains:

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': config('POSTGRES_DB'),
    'USER': config('POSTGRES_USER'),
    'PASSWORD': config('POSTGRES_PASSWORD'),
    'HOST': config('HOST'),
    'PORT': config('PORT'),
    }
    }


    For additional reference, I'm using Python 3.12 and the following dependencies:

    "asgiref>=3.8.1",
    "django>=5.1.6",
    "django-taggit>=6.1.0",
    "markdown>=3.7",
    "psycopg>=3.2.5",
    "python-decouple>=3.8",
    "sqlparse>=0.5.3"


    How do I use a postgres container as a local dev database in my Django app?

    Continue reading...

Compartilhe esta Página