4

I use Heroku to host a Django web app with a postgres back-end. I'm now looking to migrate this web app to Azure, taking advantage of a great deal Azure recently offered me.

But I'm confused about one thing: how can I create a Django website on Azure with postgresql as the database? I successfully created a Django website, connected it to git, but I can't seem to change it's DB to postgresql.

Hence, when I do git push azure master, I get the error: Command python setup.py egg_info failed with error code 1 in D:\home\site\wwwroot\env\build\psycopg2 It fails on psycopg2 (postgresql).

Secondly, once I do successfully install postgresql on an Azure Web App, I'd need to tweak its postgresql.conf file to tune the settings (defaults are too low). How would I do that?


The documentation only talks about how to install PG for an Azure VM with Linux, not an Azure Web App: https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-postgresql/

3
  • How did you ended up connecting the django web app with the postgresql VM? Or you just installed the django on a wsgi in the VM? Commented Jan 14, 2016 at 20:37
  • Yes, the set up I have comprises two separate VMs - one hosting the DB, the other the web application. Commented Jan 14, 2016 at 21:43
  • 1
    I finally make it work =D, thanks for your help ! Commented Jan 15, 2016 at 9:59

2 Answers 2

6

Azure Web Apps Service is a PaaS which provides several services to host your web site apps. And we have limit permission to install PostgreSQL on it.

Currently, to host PostgreSQL on Azure, you can leverage virtual machines with linux as @theadriangreen mentioned. And you can use SSH command ssh user@VMhost to remote on your linux VM and set your PG configurations. You may refer to YoLinux Tutorial: The PostgreSQL Database and Linux for more about PG on Linux.

And additionally, you can create a docker contain with PostgreSQL image in Azure add-on market.

Login preview manage portal, click new=>search “postgres” in search bar, you can find the postgres service provided by docker.

enter image description here

You also can remote to this VM via SSH to set your PG configurations.

If you want a direct tcp connection to the PG server, you need to add 5432 port in the Inbound security rules.

In “All settings” of the VM server you created above, click “Network interfaces”=>click the interface in use=>click the network security group name in use, you can find the Inbound security rules in the settings page.

enter image description here

Then you can test the connection of your PG service:

import psycopg2

try:
    conn = psycopg2.connect(database="postgres", user="postgres", password="{password}", host="{host_ip}", port="5432")
    print "Opened database successfully"

    cur = conn.cursor()

    cur.execute("SELECT ARRAY[1.1,2.1,3.1]::int[] = ARRAY[1,2,3]");
    rows = cur.fetchall()
    print(rows)

    conn.commit()
    print "Records created successfully";
    conn.close()
except Exception, e:
    print "I am unable to connect to the database"
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the low down Gary. To connect via ssh, wouldn't I need publicdns? From a previous Azure article, I've been using this command to connect to test Azure VMs I've been creating: ssh <username>@<publicdnsaddress> -p <the ssh port>. On those VMs, if I try ssh username@hostname -p 22, I usually get could not resolve hostname <hostname>. What to do?
if you create a VM via the previous article, you can find the DNS NAME in the DASHBOARD page of your VM portal on Azure mange portal, which should be like {your_vm_name}.cloudapp.net. And check whether the 22 port has been allowed at ENDPOINT page. Then you can directly leverage ssh <username>@<your_vm_name>.cloudapp.net to connect your VM
There's no ENDPOINT tab in a resource manager VM, or am I overlooking it?
I just realized I've been looking at portal.azure.com, whereas you're referring to manage.windowsazure.com. Two different dashboards with very different UI, meant for more or less the same thing. Microsoft.
2

Azure Web Apps only offers web apps, since the database is provided by another service. Typically this is either Azure SQL, which provides a SQL Server database as a service, or ClearDB, which provides MySQL.

Thus to get your Django app up and running with Postgres, you're going to need to deploy the Postgres DB somewhere. Initially you could try with your settings.py pointed at your Heroku Postgres DB, however you might run into some firewall settings on the DB side.

If you were to follow the steps in the link (https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-postgresql/) then you could use that instance as your Postgres DB and adjust your settings.py file to point to that machine. You would also be able to change the postgresql.conf since you own the machine.

4 Comments

You know, prior to delving into an Azure Web App, I was taking a very different approach. I made an Azure VM (w/ Ubuntu), and migrated my DB + app's code (via github) to it. It's still sitting there. But I've put this approach on the back burner after I read Troy Hunt's article on NOT going the VM route: troyhunt.com/2014/01/with-great-azure-vm-comes-great.html But now it seems due to postgresql, I can't avoid the VM route here.
Bear in mind that you can still run the web app on Azure App Service. It's just that you'll need to host the DB somewhere, and the only way to do that in Azure is by provisioning your own VM since Azure doesn't offer Postgres as a service.
{% load rant%}{% rant %}Why don't you guys offer postgres as a service -_- {% endrant %} Have 2 follow-up noob questions for you (apologies in advance): (i) How do I find the database_url for my database on the Azure VM (the one I have to refer to in my settings.py)? (ii) How do I set environment variables for my app (some stuff in my settings.py depends on that)? My guess is, it's Web Apps> Application Name>All Settings>Application Settings>App settings in the management portal?
I think your original question is answered. Can you accept, and ask (i) as a separate question? For (ii) your guess is correct, it is the App settings. You can see the environment settings at the scm site (https://<your-site-name>.scm.azurewebsites.net/Env#envVariables).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.