skip to Main Content

I am trying to switch a local Django project from the default sqlite3 database to a postgres v.14 database. I’ve been through several tutorials but nothing seems to work and I’m not sure what I’m doing wrong.

Steps I’ve performed:

  1. In my postgres shell I can see there is a database called testdb.
  2. I’ve created a Postgres user bb with password foobar
  3. I ran the following command to grant bb access: GRANT ALL PRIVILEGES ON DATABASE testdb TO bb;
  4. Back in my virtual environment I run brew services start postgresql which prints:
==> Successfully started `postgresql@14` (label: homebrew.mxcl.postgresql@14)
  1. In my Django project’s settings.py I have the following database configuration:
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": "testdb",
        "USER": "bb",
        "PASSWORD": "foobar",
        "HOST": "127.0.0.1",
        "PORT": "5432",
    }
}
  1. Running python manage.py makemigrations produces the following error:
/opt/miniconda3/envs/django-db/lib/python3.9/site-packages/django/core/management/commands/makemigrations.py:158: RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': connection to server at "127.0.0.1", port 5432 failed: FATAL:  password authentication failed for user "bb"

  warnings.warn(
No changes detected

Is there a step I’ve missed somewhere? No idea why there’s no connection to the server available.

2

Answers


  1. I am using postgresql in development. I am going to copy my setup below, and also suggest what I would try. Hopefully some of this helps you.

    My setup

    I am using dj-database-url to simplify the database dictionary.

    # settings.py
    import dj_database_url
    
    ALLOWED_HOSTS=['127.0.0.1', 'localhost']
    # (I use python-decouple, so the line above is a bit different
    #  in my code, but this is equivalent and simpler to read)
    
    DATABASES = {
        "default": dj_database_url.config(
            default="postgres://bb:foobar@localhost:5432/testdb" # (same here)
            conn_max_age=600,
        )
    }
    

    I adjusted the database url to the information you provided in the question. The format is like postgres://{user}:{password}@{hostname}:{port}/{database-name}this post has more information about it.

    What I would try

    • From what it says in this link, I would check the ALLOWED_HOSTS variable.
    • I would also check if changing "127.0.0.1" for "localhost" has any effect.
    Login or Signup to reply.
  2. I have two suggestions as to what you can try.

    You could try to access the database using a service file and password file as explained in the Django docs here

    Secondly, the problem could be that your pg_hba.conf file do not allow the testdb user to access the database by using a password. Here is an example of how you could configure your database to allow access by using a password for the user bb

    # for user bb connected via local IPv4 or IPv6 connections, always require 
    # md5 when trying to access database with name testdb
    host    testdb   bb        127.0.0.1/32          md5
    host    testdb   bb        ::1/128               md5
    

    You can read more about pg_hba.conf in the docs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search