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:
- In my postgres shell I can see there is a database called
testdb
. - I’ve created a Postgres user
bb
with passwordfoobar
- I ran the following command to grant
bb
access:GRANT ALL PRIVILEGES ON DATABASE testdb TO bb;
- Back in my virtual environment I run
brew services start postgresql
which prints:
==> Successfully started `postgresql@14` (label: homebrew.mxcl.postgresql@14)
- 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",
}
}
- 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
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.
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
ALLOWED_HOSTS
variable."127.0.0.1"
for"localhost"
has any effect.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
You can read more about pg_hba.conf in the docs.