Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Azure Web App with Postgresql database (is that possible?)

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/

like image 855
Hassan Baig Avatar asked Feb 09 '23 11:02

Hassan Baig


2 Answers

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"
like image 136
Gary Liu Avatar answered Feb 10 '23 23:02

Gary Liu


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.

like image 44
theadriangreen Avatar answered Feb 11 '23 00:02

theadriangreen