Forums

Con not connect to postgres sql on Django app

with following setting i am not able to connect database . since i overwrite my previous settings that was working fine . now missing something that cause the error any help ?

DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.postgresql_psycopg2',
        'NAME':'postgres', 
        'HOST':'AFGHANINVEST-XXX.postgres.pythonanywhere-services.com',
        'PASSWORD':'Pasword',
        'USER':'AFGHANINVEST',
        'Port':'10340',

    }
}

django.db.utils.OperationalError: could not connect to server: Connection refused Is the server running on host "AFGHANINVEST-XXX.postgres.pythonanywhere-services.com" (10.0.0.45) and accepting TCP/IP connections on port 5432?

Hi there, is the username correct? By default we set up a user called "super", with full superuser privileges. We do recommend that people set up other users for their apps (with more secure privileges), but maybe you haven't done that yet?

thanks but still have the same error . and now my app is completely down any help will be highly appreciated .

you mentioned that you overwrote your previous settings which were working before? Do you know what you changed or is there a way to revert back to the old settings?

another thing to check- can you use those settings to connect to the postgres database from the bash commandline?

yes bfg i can connect to sql in bash with the same settings .

psql -h AFGHANINVEST-340.postgres.pythonanywhere-services.com -d postgres -U super --port=10340

there is no problem in postgres sql itself , but i just can't connect to it from django . previous i manually changed the setting file in pythonanywhere i need to stash it before pulling my commit from git hub

when i re pull it i lost track of stash so no way to go back , or do not know how to go back

thanks for help but problem solved unexpectedly like this

DATABASES = {
    'default': {
        'PORT':'10340',
        'PASSWORD':'Password',
        'USER':'super',
        'ENGINE':'django.db.backends.postgresql',
        'NAME':'postgres',
        'HOST':'AFGHANINVEST-340.postgres.pythonanywhere-services.com'
    }
}

Ah, I think I see the problem. In your original settings you had

'Port':'10340',

The settings dictionary is case-sensitive, so that was being ignored because it only looks for "PORT", all capitals. That's why it was trying to connect on the default Postgres post of 5432, as it said in the error message.

The code that you have now, which works, uses the correct capitalisation for the port number:

'PORT':'10340',

The extra clarification just saved me so much confusion @giles. Thank you, you beautiful person.

:-D

FYI - I was just setting up postgres for the first time. I ran into an error when I used the example config below from PA. When I added single quotes around the port number it appears to work fine. Partly mentioning in case PA wants to update the example in the link below.

https://help.pythonanywhere.com/pages/PostgresGettingStarted/

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'myappdb', 'USER': 'myappuser', 'PASSWORD': 'a-nice-random-password', 'HOST': 'myusername-667.postgres.pythonanywhere-services.com', 'PORT': 10667, } }

A quick experiment indicates that the PORT setting for postgres works if the PORT is a string or an int.

Apologies. I was wrong as glenn confirms.