Deployed Django app successfully on Azure, but not able to run management commands via SSH on Postgres database

Daniel Vannucci 1 Reputation point
2022-06-29T03:33:27.607+00:00

Hey there,

I'm having some trouble with my Django app on Azure with Postgres database. The app deploys successfully, and I am able to use it. However, I am unable to run management commands via SSH from the web app to update/migrate/change the database.

Specifically, I followed the instructions in the Django w/ Postgres to deploy my web application and everything went smoothly. On step 7 of the tutorial, there is a point where it asks you to log into the app via SSH and run 'python manage.py migrate' to create all the tables in the database. I did not have to do this step, as I already had a created database. For my instance, I used psql command line tool to access the azure database, and I restored a local backup onto azure.

That went fine, and the whole database is up and running. So I did not have to do that step.

The web app is deployed and has full access to the database, so things are running. Later I went into the SSH for the webapp, and it did not allow me to run any management commands saying:

django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?

I find this strange though as I've been googling around for answers and in most cases, it just means that the web app can't find the database. As in, it doesn't know where it is. But the web app deploys and accesses the database with no problem.

Any help would be awesome as I need to be able to run management commands from there to update my database.

Thanks,
Daniel

Azure Database for PostgreSQL
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,287 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Vannucci 1 Reputation point
    2022-06-29T11:41:20.763+00:00

    I think I have found the solution to this particular issue.

    What seems to have been the problem is the code within the manage.py file itself.

    Referring to the test app that Azure provides, there is a statement in the manage.py file which uses either production or development settings.

    I had never thought to look in the manage.py file as I had never opened it, but there it was.

    What solved this for me was adding the line checking if WEBSITE_HOSTNAME was in os.environ, and if it is, use production settings. This allows migrate, makemikgrations etc to connect to the correct database.

    Hope this helps someone someday!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.