# Hands-on Assignment: Deploying to Production Part 1 (PostgreSQL Installation)

May 17, 2018

In the last project, we got our Reddit clone running locally on our computers using a sqlite database. However the real power of a web application is to have it be accessible by anyone on the web, so we’re going to take our Reddit clone that we built in the last assignment and deploy it to Heroku. We’ll talk more about Heroku in Part 2 of this assignment.

SQLite works well when you want to build a quick prototype but if you’d like to deploy a production application then you want all the guarantees that come with battle-tested database systems such as PostgreSQL, MySQL, or MongoDB. We’re going to be using PostgreSQL (or simply Postgres) for the purposes of this tutorial, so let’s dive right into installing it on your local machine! Once we have Postgres all set up, we’ll deploy to Heroku in the next assignment!

# Project Goals

• Set up a local Postgres database (Part 1).
• Set up Git (Part 2).
• Set up Heroku and deploy our Reddit application to it (Part 2).

# Step 0: Setup

Go ahead and setup your project. Download the proj3-starter code here. (Use this for Django 1.11). It’s basically the same as our previous Reddit final code with the database migrations, python packages, etc. removed.

Let’s activate our environment and install our python packages locally.

python3 -m venv myvenv
source myvenv/bin/activate
pip install -r requirements.txt

Now, we’re ready to get cracking!

The easiest way to install Postgres on Windows is using a program you can find here: http://www.enterprisedb.com/products-services-training/pgdownload#windows

Choose the newest version available for your operating system. Download the installer, run it and then follow the instructions available here: http://www.postgresqltutorial.com/install-postgresql/. Take note of the installation directory as you will need it in the next step (typically, it’s C:\Program Files\PostgreSQL\9.3).

# Step 1: Install Postgres

## Windows

The easiest way to install Postgres on Windows is using a program you can find here: http://www.enterprisedb.com/products-services-training/pgdownload#windows

Choose the newest version available for your operating system. Download the installer, run it and then follow the instructions available here: http://www.postgresqltutorial.com/install-postgresql/. Take note of the installation directory as you will need it in the next step (typically, it’s C:\Program Files\PostgreSQL\9.3).

## Mac OS X

The easiest way is to download the free Postgres.app and install it like any other application on your operating system.

Download it, drag to the Applications directory and run by double clicking. That’s it!

You’ll also have to add the Postgres command line tools to your PATH variable, what is described here.

## Linux

Installation steps vary from distribution to distribution. Below are the commands for Ubuntu and Fedora, but if you’re using a different distro take a look at the PostgreSQL documentation.

## Ubuntu

Run the following command:

sudo apt-get install postgresql postgresql-contrib

## Fedora

Run the following command:

sudo yum install postgresql93-server

# Step 2: Create Database

Next up, we need to create our first database, and a user that can access that database. PostgreSQL lets you create as many databases and users as you like, so if you’re running more than one site you should create a database for each one.

## Windows

If you’re using Windows, there’s a couple more steps we need to complete. For now it’s not important for you to understand the configuration we’re doing here, but feel free to ask in the discussion below if you’re curious as to what’s going on.

1. Open the Command Prompt (Start menu → All Programs → Accessories → Command Prompt)
2. Run the following by typing it in and hitting return: setx PATH "%PATH%;C:\Program Files\PostgreSQL\9.3\bin". You can paste things into the Command Prompt by right clicking and selecting Paste. Make sure that the path is the same one you noted during installation with \binadded at the end. You should see the message SUCCESS: Specified value was saved..
3. Close and then reopen the Command Prompt.

## Create the Database

First, let’s launch the Postgres console by running psql. Remember how to launch the console?

On Mac OS X you can do this by launching the Terminal application (it’s in Applications → Utilities). On Linux, it’s probably under Applications → Accessories → Terminal. On Windows you need to go to Start menu → All Programs → Accessories → Command Prompt. Furthermore, on Windows, psql might require logging in using the username and password you chose during installation. If psql is asking you for a password and doesn’t seem to work, try psql -U -W first and enter the password later.

If this still doesn’t seem to work you can try psql -d <<your account name>> -w -h localhost. Replace <<your account name>> with whatever account username postgres was installed on. -d is your database name, -w implies no password is needed, and -h is your hostname.

$psql psql (9.3.4) Type "help" for help. # Our $ now changed into #, which means that we’re now sending commands to PostgreSQL. Let’s create a user with CREATE USER name; (remember to use the semicolon):

# CREATE USER name;

Replace name with your own name. You shouldn’t use accented letters or whitespace (e.g. bożena maria is invalid - you need to convert it into bozena_maria). If it goes well, you should get CREATE ROLE response from the console. Remember this name as we’ll use it throughout this assignment.

Now it’s time to create a database for your Django project:

# CREATE DATABASE cl OWNER name;

Remember to replace name with the name you’ve chosen (e.g. bozena_maria). This creates an empty database that you can now use in your project. If it goes well, you should get CREATE DATABASE response from the console.

Great — that’s databases all sorted!

# Step 3: Updating settings

Let’s go back to our reddit project. Modify the settings.py file located at mysite/settings.py file. Go to the DATABASES section:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

And replace it with this:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'cl',
'USER': 'name',
'HOST': 'localhost',
'PORT': '5432',
}
}

We’ve updated the ENGINE to point to the postgresql database backend. Remember to change name to the user name that you created earlier in this chapter.

# Step 4: Installing PostgreSQL package for Python

First, install Heroku Toolbelt from https://toolbelt.heroku.com/ While we will need this mostly for deploying your site later on, it also includes Git, which might come in handy already.

Next up, we need to install a package which lets Python talk to PostgreSQL - this is called psycopg2. The installation instructions differ slightly between Windows and Linux/OS X.

First go to reddit/requirements.txt, and add psycopg2==2.7.4 at the bottom. Your requirements.txt should now look like:

django==1.11.0
psycopg2==2.7.4

## Windows

Make sure you get the one corresponding to your Python version (3.4 should be the last line) and to the correct architecture (32 bit in the left column or 64 bit in the right column).

Rename the downloaded file and move it so that it’s now available at C:\psycopg2.exe.

Once that’s done, enter the following command in the terminal (make sure your virtualenv is activated):

easy_install C:\psycopg2.exe

## Linux and OS X

Run the following in your console:

(myvenv) ~/proj3-starter$pip install -r requirements.txt If that goes well, you’ll see something like this: Requirement already satisfied: django==1.11.0 in ./myvenv/lib/python3.6/site-packages (from -r requirements.txt (line 1)) Collecting psycopg2==2.7.4 (from -r requirements.txt (line 2)) Using cached https://files.pythonhosted.org/packages/8c/a5/0e61d6f4a140a6e06a9ba40266c4b49123d834f1f97fe9a5ae0b6e45112b/psycopg2-2.7.4-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl Requirement already satisfied: pytz in ./myvenv/lib/python3.6/site-packages (from django==1.11.0->-r requirements.txt (line 1)) Installing collected packages: psycopg2 Successfully installed psycopg2-2.7.4 django and pytz have already been installed so they won’t need to be reinstalled. Once that’s completed, run python -c "import psycopg2". If you get no errors, everything’s installed successfully. # Step 5: Applying Migrations and Creating a Superuser In order to use the newly created database in your website project, you need to apply all the migrations. In your virtual environment run the following code: (myvenv) ~/proj3-starter$ python manage.py makemigrations
(myvenv) ~/proj3-starter$python manage.py migrate Make sure your application loads locally by first creating a superuser. (myvenv) ~/proj3-starter$ python manage.py createsuperuser