1

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.

BBrooklyn
  • 350
  • 1
  • 3
  • 15
  • 1) This is why you should develop on the database you will run in production. 2) `GRANT ALL PRIVILEGES ON DATABASE testdb TO bb` is not doing what you think it is. Rather then going through this one oops at a time I suggest you read: [Grant privileges](https://www.postgresql.org/docs/current/ddl-priv.html). – Adrian Klaver Aug 07 '23 at 23:09
  • 1) That's what I'm trying to do 2) I read through the page you linked and nothing jumps out as being relevant to the fact that connection to the server failed – BBrooklyn Aug 07 '23 at 23:24
  • 1) I did not scroll all the way to the end of the error message. Still the `GRANT ALL PRIVILEGES ON DATABASE testdb TO bb` once you get connected is not going to get you much further and the migration will fail, so read the link I posted. 2) a) There is no such thing as a `psql` user there is a `Postgres` user. b) What Postgres version are connecting to?(**ADD info as update to question**). c) Add the `CREATE ROLE` statement to your question. – Adrian Klaver Aug 07 '23 at 23:42
  • This error you have doesn't tell you that you haven't connect to database, it says that in your models python code there were no changes so no migrations needs to be created. Add new fields/new model then try to use `python manage.py makemigrations` becuase this error is saying that all model code is included in actual migrations. – dawid.pawlowski Aug 08 '23 at 09:21
  • @dawid.pawlowski, actually it does: *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"* – Adrian Klaver Aug 08 '23 at 14:45
  • @BBrooklyn `password authentication failed for user "bb"` is pretty clear.. – aaron Aug 12 '23 at 17:14
  • @aaron I set the password to "foobar" in psql then provide the credentials in the Django settings. What's going wrong? – BBrooklyn Aug 14 '23 at 00:02
  • @BBrooklyn are you able to login to the database manually using created user and password? – Ankush Chavan Aug 15 '23 at 12:05

2 Answers2

1

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.

stiffi
  • 106
  • 6
0

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.
Martin
  • 105
  • 10
  • Thanks for the writeup...there should be a way to connect Django to its postgres db without additional dependencies though – BBrooklyn Aug 08 '23 at 13:48