Having trouble migrating the database from SQLite to PostgreSQL?

:grimacing:Hello everyone, I’m a Django beginner. I’m trying to migrate data from an SQLite database to a PostgreSQL database, but I’m encountering such an error during the process.

django.db.utils.OperationalError: connection failed: password authentication failed for user "blog"
Multiple connection attempts failed. All failures were:
- host: 'localhost', port: '5432', hostaddr: '::1': connection failed: password authentication failed for user "blog"
- host: 'localhost', port: '5432', hostaddr: '127.0.0.1': connection failed: password authentication failed for user "blog"

I created the database using Docker containers. Here are the commands I used:

docker run --name=blog_db  -e  POSTGRES_DB=blog  POSTGRES_USER=blog  -e  POSTGRES_PASSWORD=A0116659  -p  5432:5432  -d postgres:16.2

I also created an environment configuration file in the root directory of my project

In which I stored the database environment configuration parameters.

DB_NAME=blog
DB_USER=blog
DB_PASSWORD=A0116659
DB_HOST=localhost
DB_PORT=5432

Finally, in the main project’s settings.py file, I modified the default SQLite database configuration.

from decouple import config
#配置新型的数据库
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": config("DB_NAME"),
        "USER":config("DB_USER"),
        "PASSWORD":config("DB_PASSWORD"),
        "HOST":config("DB_HOST"),
        "PORT":config("DB_PORT",default=5432)
    }
}

Finally, when I ran python manage.py migrate , an error occurred.

django.db.utils.OperationalError: connection failed: password authentication failed for user "blog"
Multiple connection attempts failed. All failures were:
- host: 'localhost', port: '5432', hostaddr: '::1': connection failed: password authentication failed for user "blog"
- host: 'localhost', port: '5432', hostaddr: '127.0.0.1': connection failed: password authentication failed for user "blog"

I don’t know why the password verification is failing. I’m sure I entered the correct password, but it still gives an error. I hope everyone can help me. Your suggestions will be greatly appreciated!

Hello! From the code snippets you provided, it seems like the db credentials are the same between the docker container and your settings file. Can you double check by inspecting the container with these commands?

# List the containers and find the ID of the postgres container
docker container ls

# Inspect the environment variables
docker container inspect <id-of-postgres-container>

Welcome @songlisp123 !

This can’t be the exact command that you’re running, because it’s invalid as posted. (You’re missing the -e before the POSTGRES_USER variable.)

Check the logs of your docker container - docker logs blog_db

You might want to try to connect directly using the psql command - psql -h localhost -U blog.

You might also want to add a print(DATABASES) statement after its definition in your settings file to verify that the settings are what you expect them to be.

docker container inspect <id-of-postgres-container>

On my computer, this seems to be an incorrect command…

Try docker inspect blog_db

thank you this is my logs

2025-06-05 02:31:27.719 UTC [1] LOG:  starting PostgreSQL 16.2 (Debian 16.2-1.pgdg120+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2025-06-05 02:31:27.719 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2025-06-05 02:31:27.719 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2025-06-05 02:31:27.728 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2025-06-05 02:31:27.739 UTC [64] LOG:  database system was shut down at 2025-06-05 02:31:27 UTC
2025-06-05 02:31:27.747 UTC [1] LOG:  database system is ready to accept connections
2025-06-05 02:36:27.826 UTC [62] LOG:  checkpoint starting: time
2025-06-05 02:36:32.090 UTC [62] LOG:  checkpoint complete: wrote 45 buffers (0.3%); 0 WAL file(s) added, 0 removed, 0 recycled; write=4.234 s, sync=0.014 s, total=4.264 s; sync files=12, longest=0.006 s, average=0.002 s; distance=261 kB, estimate=261 kB; lsn=0/19544D8, redo lsn=0/19544A0

But I don’t know what to focus on