Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Provision a Free PostgreSQL Server on Heroku

For more information on concepts covered in this lesson, you can check out:

Here are the command-line snippets used in this lesson:

Shell
$ heroku addons
$ psql `heroku config:get DATABASE_URL`
$ heroku pg:psql
<app-name>::DATABASE-> \l
<app-name>::DATABASE-> \d
<app-name>::DATABASE-> \q
$ heroku pg
$ heroku run python manage.py migrate
$ vim Procfile
$ git commit -am "Automate remote migrations"
$ git push heroku

Configuration File
web: gunicorn portfolio.wsgi
release: python manage.py migrate

00:00 In this lesson, you’ll connect your Django project to a standalone relational database server. Thankfully, Heroku automatically provisions a managed database service in the cloud at no charge through an add-on.

00:13 It takes the burden of installing, configuring, securing, and maintaining a relational database off your shoulders. When you first deploy an app, Heroku spins up a PostgreSQL database, which is coincidentally one of the most popular choices for Django projects.

00:30 Nevertheless, Heroku has special support for this particular database management system, regardless of whether you use Python or not.

00:40 You may recall a DATABASE_URL environment variable in your app’s configuration, which Heroku defined for you. As you can see, it’s an address to an EC2 virtual machine on Amazon’s AWS infrastructure, which hosts a PostgreSQL database on the default port.

00:57 So, if you happen to have PostgreSQL installed locally, then you can use this public address to drop into the interactive terminal and start typing SQL queries against your remote database.

01:09 On the other hand, if you don’t have the PostgreSQL tools, then you can rely on the Heroku CLI, which provides a convenient pg:psql wrapper command that achieves the same effect.

01:21 That’s even quicker than manually copying and pasting an address buried somewhere in remote configuration. Interestingly, once you’re connected, you can list all the available databases owned by other Heroku apps that don’t belong to you. While you can’t open them, it shows that data between the individual apps shares the same infrastructure and may not be isolated enough for sensitive information storage.

01:47 Some countries have strict laws that require additional separation of medical records of patients, for example. Keep in mind, that’s one of the limitations of the free Heroku account, but there are more. When you type heroku pg, it will show your database’s status.

02:06 Among the displayed information, you’ll see that you have a pool of at most twenty connections, only one gigabyte of storage, and just 10,000 rows. Other than that, replication and rollbacks are unsupported.

02:22 That said, these constraints shouldn’t really be a big deal in a hobby project. Back to your provisioned database, when you list your tables, then you’ll find out there are none, even though you might have run the Django migrations before. That’s because you ran them locally against a file-based SQLite database.

02:44 Normally, you’d have to tell Django how to connect to the database provisioned by Heroku by installing the PostgreSQL driver and by updating your Django project settings accordingly. However, if you were following along, then there are no extra steps to take.

02:59 The django-heroku library comes with the right driver out of the box and picks up the database URL variable to configure your Django settings for you.

03:08 By the way, if you run into issues with django-heroku, which isn’t maintained anymore, then you can try the slightly newer django-on-heroku fork, which uses a binary driver for a PostgreSQL database.

03:23 To apply the migrations remotely against your PostgreSQL instance, you must start a temporary Heroku container and manually run the migration command in it.

03:34 This will populate the provisioned PostgreSQL instance by creating the default app’s tables. Unfortunately, it’s easy to forget about applying pending migrations when you make new deployments. Therefore, Heroku lets you automate this task by specifying a special process in the Procfile. Go ahead and edit the Procfile.

03:55 Add a release process, and type the migration command that will run after each deployment.

04:05 Now, commit your automated migrations and push the code to Heroku to trigger a build.

04:22 Once finished, you should see the familiar output in the terminal. In this case, there were no migrations applied because your database schema hasn’t changed.

04:32 You can try modifying your data model and make another deployment to see if there’s any difference as an exercise. When you push the code to Heroku or change your app’s configuration, it creates a new release, hence the release process in your Procfile.

04:47 You’re going to learn more about releases, what they are, and how to manage them in the next video.

Denilson Noa on Nov. 3, 2022

I cannot find the DATABASE_URL.

Bartosz Zaczyński RP Team on Nov. 4, 2022

@Denilson Noa How do you check your environment variables? I just created a test Heroku app with Django, and it provisioned a new PostgreSQL instance with the corresponding DATABASE_URL variable in my app’s config. Perhaps you haven’t pushed your code to Heroku, which didn’t trigger the initial deployment?

Become a Member to join the conversation.