django can't connect to postgresql database on Mac

I am having similar problem but on Mac -Sonoma version 14.2 -Django 5… I can connect to my database via terminal but not from my Vscode (installed extensions: psycopg2, python-decouple)

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”),
}
}

–getting this error:
…env/lib/python3.11/site-packages/psycopg2/init.py", line 142, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: FATAL: password authentication failed for user “postgres,”

Your help is needed… I reinstalled postgress and pgadmin and my environment and the problem is still there… Thanks in advance for your help!

—> everything runs smoothly with SQLITE

Please show the command line you are using for psql to connect. Also post the values that you are using for NAME, USER, HOST, and PORT for your settings.

this is what I used to connect to db via terminal:

psql -d foodOnline_db -U postgres --and it connects pretty well

from settings.py:

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”),
}
}


from my .env:

#main secrete key and debug mode

SECRET_KEY=django-insecure-pgqt(123123…etcetc

DEBUG=True

#database configuration

DB_NAME=foodOnline_db,

DB_USER=postgres,

DB_PASSWORD=123456,

DB_HOST=localhost,

DB_PORT=5432,

Ok, you’re actually using two different methods for connecting. Your command line doesn’t match how you’re trying to connect with Django.

Try psql -d foodOnline_db -U postgres -h localhost -p 5432 - I’m expecting that it’s not going to work.

You can also try removing the HOST and PORT settings from your database configuration to have it connect to postgres using the domain socket.

I just used your last command → psql -d foodOnline_db -U postgres -h localhost -p 5432and I was able to login

Do those commas exist on those lines in your .env file? If so, they should be removed.

1 Like

You are the man! Holy molly! After almost a week it is working!!! Those freaking commas… is it too late to buy you a cup of coffee :slight_smile: