简体   繁体   中英

Connecting Django to Postgres: django.db.utils.OperationalError: FATAL: database "DATABASENAME" does not exist

I have just started learning Django after I took some tutorials for Python.

I am trying to connect my POSTGRES database to the Django project I have just created.

However, I am experiencing this problem:

django.db.utils.OperationalError: FATAL:  database "producthuntdb" does not exist

I followed these steps:

1) Opened postgress by clicking its icon

2) Clicked on the database "postgress". The terminal opened and I wrote: CREATE DATABASE producthuntdb ; The database has been created because I see it if I open postgress via its icon.

3) Went to my Django project in "settings" and change the SQLITE database to the following:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'producthuntdb',
        'USER': 'mymac2017',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

4) Run the code python3 manage.py migrate

However, I am getting the error:

django.db.utils.OperationalError: FATAL: database "producthuntdb" does not exist

So I did the following:

  • Cliking on postgress icon and opening the the database producthuntdb

  • Once the terminal is open, I wrote: \\du

  • There are two users with the attributes:

    1) mymac2017 | Superuser, Create role, Create DB | {}

    2) postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

What am I doing wrong?

I tried to look other answers to this problem and most of the issues are from misspelling the database name OR not creating it. However, the name of my database is correct and I can see the database producthuntdb if I open postgres.

Many thanks for your help.

I set the wrong port of the Database in the Django "settings.py".

I was using 'PORT': '5432' instead of 'PORT': '5433'

SOLUTION:

  • Go to your Postgres app/icon/program
  • Click on "Server setting" and see which PORT the database POSTGRES is using
  • Be sure that the PORT the database is using is the same as the one you write in the "settings.py" of your Django project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM