43
My Django app cannot connect to PostgreSQL. I get "connection refused" error. PostgreSQL is installed and running.
Loading...
Q&A · Answered
My Django app cannot connect to PostgreSQL. I get "connection refused" error. PostgreSQL is installed and running.
Accepted answer
This is usually a configuration issue. Here's how to fix it:
# Check PostgreSQL status
sudo systemctl status postgresql
# Check listening ports
sudo netstat -tlnp | grep postgres
# Edit pg_hba.conf
sudo nano /etc/postgresql/14/main/pg_hba.conf
# Add or modify:
local all all md5
host all all 127.0.0.1/32 md5
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'your_db',
'USER': 'your_user',
'PASSWORD': 'your_password',
'HOST': 'localhost', # or '127.0.0.1'
'PORT': '5432',
}
}Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.