Tanveer Hasan
Tanveer Hasan

Reputation: 343

Django app is not connecting with remote database

I have a Django app running on hosting server. I am using postgres database which is running on different server. The app is working properly in my local machine but in hosting server, the app is running but any functionality is not working which is associated with database. For example, user can not authenticated while login even the username and password is correct. I am receiving a error saying "could not connect to server: Connection refused Is the server running on host xxx.xxx.xxx.xxx and accepting TCP/IP connections on port 5432?" My settings for database

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'DB_NAME',
        'USER': 'USER',
        'PASSWORD': '*****',
        'HOST': 'IP_ADDRESS',
        'PORT': 'PORT',
    }
}

N:B I have given permission to the postgres database. I can access the db from local machine


Django version 2.2.5

Upvotes: 2

Views: 2384

Answers (1)

Umaprasad Bisen
Umaprasad Bisen

Reputation: 123

Try with following database configuration snippet in settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '******',
        'USER': '****',
        'PASSWORD': '****',
        'HOST': '***.***.***.***',
        'PORT': '5432'
    },
}

Replace *** with your database credentials Make sure you have already installed psycopg2 through package manager like pip

  • Also ensure that your local postgrSQL db server's settings were made to access it from outside your localhost if not then try with below settings.
    • In postgresql.conf file Change listen_addresses = “localhost” to “ * ”
    • In pg_hba.conf file add this 2 lines at the end of file
# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    all             all              0.0.0.0/0                  md5
host    all             all              ::/0                       md5

# Allow replication connections from localhost, by a user with the
host    replication     all             0.0.0.0/0            md5
host    replication     all             ::/0                 md5
  • Finally restart the db service of postgresql

Upvotes: 2

Related Questions