Skip to content

Marina Mele's site

Reflections on family, values, and personal growth

Menu
  • Home
  • About
Menu

How to deploy a Django app on Heroku. Part III

Posted on January 17, 2014September 26, 2014 by Marina Mele

This is the third part of a series on how to deploy a Django app on Heroku.

How to deploy a Django app on Heroku. Part I.

How to deploy a Django app on Heroku. Part II.

You will see that the main idea behind these posts is not to build a functional Django app. Instead, what I want to do is to give you a bunch of good practices on how to build a Django app with different environments for testing, developing and production, useful packages installed, Internationalization and Localization properly configured, PosgreSQL installed, and much more.

PART III

The topics covered here are:

  • configure PostgreSQL in your local machine
  • configure PostgreSQL in Heroku
  • syncdb and Django shell in Heroku

PostgreSQL Database – Local

Let us talk about setting properly the PostgreSQL database on Heroku and on your local computer.

This tutorial assumes you have Postgres installed on your computer. If you have a Mac with OS X Mountain Lion, you might want to see How to set a Django app on Heroku. Part I. I you have Linux or Windows, you might want to see the official Heroku post.

On mac, the command

$ which psql

should point to the Postgres app. And this other command should start the Postgres command line utility:

$ psql -h localhost

If this command rises an error like “psql: FATAL: database <user> does not exist”, it might be because your package manager failed to create the proper database, see this post for more info. To solve this problem, try:

$ createdb

$ psql -h localhost

Now, you should be in the command line of Postgres. You can exit this environment by typing

\q

Similarly, you can type \? for more help. Other useful commands are: \list to list your databases and \du to list your users.

Configuring the local database for your Django app: We need to create a new database for our app and a new user to whom you will give access to the database:

$ createdb mylocaldb

$ psql

# CREATE ROLE myusername WITH LOGIN PASSWORD ‘mypassword’;

# GRANT ALL PRIVILEGES ON DATABASE mylocaldb TO myusername;

Open both your development_settings.py and test_settings.py files and add the following lines:

DATABASES = {
    ‘default’: {
        ‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’, 
        ‘NAME’: ‘taskbusterdb’,  
        ‘USER’: ‘myusername‘,
        ‘PASSWORD’: ‘mypassword’,
        ‘HOST’: ”,  
        ‘PORT’: ”, 
    }
}

After this, you should be able to sync your database with:

$ python manage.py syncdb

remember to have your development environment active.

PostgreSQL Database – Heroku

On Heroku, you can check if your application already has a database provisioned and what plan it is (if it is free or payed) with:

$ heroku addons | grep POSTGRES

If you don’t have any database provisioned, the terminal won’t show any message. Otherwise, you will see something like heroku-postgresql:dev HEROKU_POSTGRESQL_RED. See the official post for more information.

To create a new free postgres database (dev option) attached to your Heroku application:

$ heroku addons:add heroku-postgresql:dev

Once Heroku Postgres has been added, there will be a HEROKU_POSTGRESQL_COLOR_URL setting available in the app configuration (in my case COLOR was YELLOW). This variable will contain the url used to access the new Postgresql service on Heroku. This can be confirmed using

$ heroku config | grep HEROKU_POSTGRESQL

you should see the corresponding url. Heroku recomends using the variable DATABASE_URL to store the location of your primary database. Hence, the config variable created before must be promoted to this new config variable:

$ heroku pg:promote HEROKU_POSTGRESQL_YELLOW_URL

To see all PostgreSQL databases provisioned by your application and its characteristics, use

$ heroku pg:info

To establish a psql session, the native PostgreSQL interactive terminal, with your remote database use

$ heroku pg:psql

More useful commands for your database

There are some useful commands that can be used to control your Postgres database.

Pull can be used to pull data from your remote Heroku Postgres database to your local machine database.

$ heroku pg:pull HEROKU_POSTGRESQL_YELLOW mylocaldb –app myapponheroku

This command takes the Heroku database of the app myapponheroku and saves its data to a local database in your machine, named mylocaldb. If the local database already exists, you will be asked to remove it before proceeding. The previous command is equivalent to:

$ heroku pg:pull myapponheroku::YELLOW mylocaldb

Push is the inverse of pull, and takes data from your local database and push it to the database in Heroku:

$ heroku pg:push mylocaldb HEROKU_POSTGRESQL_YELLOW –app myapponheroku

If the remote database is not empty, you will be prompted to reset it with

$ heroku pg:reset HEROKU_POSTGRESQL_YELLOW

Syncdb and Django shell

Before proceeding, I recommend to upload your changes on Heroku. Recall that you can do this with:

$ git add .

$ git commit -m “PostgreSQL set”

$ git push heroku master

Make sure your app is still working on your local machine and on Heroku.

To sync the Django models, you can use the heroku run command, which lets you run one-off admin dynos.

$ heroku run python manage.py syncdb

In a similar way, you can use the Django shell:

$ heroku run python manage.py shell

 

This is the end of this post.

The next part is available here: How to deploy a Django app on Heroku. Part IV.

Click on the g+1 if you found this post useful! 🙂

Marina Melé
Marina Mele

Marina Mele has experience in artificial intelligence implementation and has led tech teams for over a decade. On her personal blog (marinamele.com), she writes about personal growth, family values, AI, and other topics she’s passionate about. Marina also publishes a weekly AI newsletter featuring the latest advancements and innovations in the field (marinamele.substack.com)

Leave a Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Personal Growth and Development
  • Artificial Intelligence
  • Mindful Parenting and Family Life
  • Productivity and Time Management
  • Mindfulness and Wellness
  • Values and Life Lessons
  • Posts en català
  • Other things to learn

Recent Posts

  • BlueSky Social – A Sneak Peek at the Future of Social Media
  • The Incredible Journey of AI Image Generation
  • AI and Fundamental Rights: How the AI Act Aims to Protect Individuals
  • Overcoming Regrets: Finding the Strength to Move Forward
  • Thinking Outside the Box: Creative Problem-Solving with Critical Thinking

RSS

  • Entries RSS
Follow @marina_mele
  • Cookie Policy
  • Privacy Policy
©2023 Marina Mele's site | Built using WordPress and Responsive Blogily theme by Superb
This website uses cookies to improve your experience. If you keep navigating through this website, we'll assume you're ok with this, but you can opt-out if you wish.Accept Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT