Hassan Baig
Hassan Baig

Reputation: 15804

Django app with postgres backend throwing error in new set up (Azure VM)

I have a Django app (with a postgresql backend) which I used to test on a local development server, and then simply push to Heroku (my service of choice). I had a Procfile that told Heroku dynos what processes to spin up, and then never worried about anything else.

I'm now migrating to Azure, where I'm setting up my own VM (v1) to host my app + postgres db. Now I need to set up my own webserver as well, which I unfortunately have thin experience of. So can someone guide me how to set up my own webserver? My ultimate goal is to set up Gunicorn with NginX as a reverse proxy. The first step, though, is to set up just Gunicorn for starters, and start seeing some HTTP traffic. How do I do that?

Here's my directory structure:

app/
    __init__.py
    models.py
    tests.py
    views.py
project/
    __init__.py
    wsgi.py
    urls.py
    settings.py
    celery.py
    static/
    templates/
    middleware/

The contents of my wsgi.py file are:

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

I tried the following:

gunicorn -b 0.0.0.0:8080 project.wsgi:application

It fires up; I see the following: enter image description here

I likewise added endpoints in my Azure VM like so: enter image description here

I likewise added endpoints for port 5432. Finally, when I run this I get the error: Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?. Can you point out what I'm doing wrong?

Upvotes: 0

Views: 249

Answers (1)

Gary Liu
Gary Liu

Reputation: 13918

According your description, it seems a PostgreSQL configuration issue. As I have a quick test on ubuntu + PostgreSQL + python27 + psycopg2 to test the connection to local PostgreSQL on Azure VM. However, I cannot reproduce your issue, it works fine on my side. Here are my steps of installing PostgreSQL service and test on python, for your information:

1, remote to Azure VM, and install PostgreSQL via sudo apt-get update, sudo apt-get install PostgreSQL postgresql-contrib

2, create a new PostgreSQL user: psql , ` create user someuser password 'somepassword';

3, install PostgreSQL service which is required by psycopg2 : sudo apt-get install postgresql-server-dev-9.4

4, install python package psycopg2: sudo apt-get install python-pip and sudo apt-get install python-psycopg2

5, And test in python code:

import psycopg2

try:
    conn = psycopg2.connect("dbname='postgres' user=' someuser' host='localhost' password='somepassword'")
    cur = conn.cursor()
    cur.execute("""SELECT 1=1""")
    rows = cur.fetchall()
    for row in rows:
        print "   ", row[0]
except:
    print "I am unable to connect to the database"

And specific to your issue, I think you can quick check the following points for troubleshooting:

1, make sure your PostgreSQL service is running, because of here is an issue with the same error message with you Posgresql connection through port 5432.

2, make sure the host setting of PostgreSQL in your Django app is “localhost”, and you can easy test in psql –h localhost -Usomeuser to login on PostgreSQL. If you occur the issue, you can try to configure your PostgreSQL server shown in Postgresql and TCP/IP connections on port 5432.

Upvotes: 1

Related Questions