Skip to content

Q&A · Answered

PostgreSQL connection refused in Django - solutions

asked 9 months, 1 week ago 1992 views 1 answer
43

My Django app cannot connect to PostgreSQL. I get "connection refused" error. PostgreSQL is installed and running.

1 Answer

One accepted
9

Accepted answer

Not confirmed since it was written, 9 months, 1 week ago

This is usually a configuration issue. Here's how to fix it:

Check PostgreSQL is listening

# Check PostgreSQL status
sudo systemctl status postgresql
# Check listening ports
sudo netstat -tlnp | grep postgres

Configure pg_hba.conf

# 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

Django settings

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',
    }
}

Your answer

Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.

Shown beside your answer and never checked, so it cannot be a name someone already has here.

No account needed to answer. Sign in instead and it carries your name and earns reputation.