Ch14. App deployment in Heroku

In this chapter, we deploy our app in Heroku. First, we modify some files in GitHub. Second, we deploy and create a postgres database in Heroku. Third, in our local computer, we create the databases by connecting remotely to the postgres database in Heroku. Fourth, we populate the databases and make some postgres queries.


JSON dumps

Some useful links

The links to the YouTube video, and the GitHub account for this section are below:

In this section, we learn how to deploy our app in Heroku, and we learn how to create a postgres database in Heroku. We proceed in four different steps:

  1. In GitHub. We need to upload our app in GitHub.

    As when we deploy our app by using AWS, we need to upload the requirementx.txt file. On top of that we need to upload the Procfile file. Please, copy and paste that file from the GitHub repository associated to this chapter.

    As when we deploy our app by using AWS, we need to introduce some changes in the __init__.py file.

  2. In Heroku. We proceed in three different steps.

    1. We deploy the app by connecting it to GitHub.

    2. We create a postgres database.

    3. We create some environmental variables to protect the secret key, and the database.

    Some useful links about Heroku postgres databases and Heroku environmental variables:

  3. In GitHub. We need to introduce the information associated with environmental variables.

    1. For the secret key: application.config['SECRET_KEY'] = os.environ.get("SECRET_KEY").

    2. For the database: SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL']. The problem with this line of code is that when we deploy the app, Heroku does not recognize that environmental variable. Therefore, we cannot use that environmental variable, and it is necessary to introduce the information related to our database without protecting it: DBVAR="postgresql://username:password@host:port/database". It seems that to have access to the environmental variables in Heroku, it is necessary to change some information in AWS. I am not going to explain in more detail about that process, since that is out of the scope of this chapter. However, I leave a link below with more information.

  4. In your local computer. In our local computer, we connect remotely to Heroku, and we create the databases there. We proceed in two steps:

    1. In the __init__.py file, we connect remotely to the postgres database in Heroku, by typing DBVAR="postgresql://username:password@host:port/database".

    2. In the terminal, we must type python. Once that we are in the Python terminal, we must type from capp import db, and then db.create_all(). By following these steps, we create the databases in Heroku.

Once we have created the databases, we can start to use the app hosted in Heroku and populate the database. After registering some users and introducing some means of entries in the database, we can create some Heroku clips to access the data by using some basic postsgresSQL commands.

Weekly challenge

In this weekly challenge, you must deploy your app in Heroku. Follow the four steps explained in the chapter and watch the video to complete the process.