Django/Postgres Setup
Tags: django, postgres, tutorial
Published: 2020-06-07 02:54:54

I noticed that in a lot of the job postings, I noticed that Django and Postgresql topped the list as a framework and database that companies were looking for. This week, I was finally able to figure out how to do all the set up required for a Django and Postgres grocery shopper project, and I want to share the steps that I took in order to get my project up and running. I'll also be sharing some bugs that I ran into and how I went ahead and fixed them. 

Also, as noted in the title, this is going to be for a Windows operating system setup.

##Step 1: Install Python and Pip

Django is a framework that runs on Python, so if you don't have that already installed you'll want to do that first. You can do that [here](https://www.python.org/downloads/)

If you have installed Python, or already had it installed you can check that by running this in your command prompt:

`python -version`

If you get back a response that gives you Python with the version number, you're good to go. If you don't have a version that is beyond 3, this version is recommended, just to be up to date and also because it comes with Pip already installed.

Pip is required, because it is the package manager that manages all of your package installation and configuration. If you're more familiar with the node.js backend frameworks pip is the python equivalent of npm. Without it you won't be able to download any packages, which include Django itself.

In order to check whether you have pip installed you can run this in your command prompt:

`pip -version`

You should get back a similar message which returns pip and the version number you currently have installed. If you somehow installed Python 3+ or have a lower version and don't have pip installed, you can follow the manual instructions [here](https://pip.pypa.io/en/stable/installing/) to do the pip installation yourself. 

#Step 2: Install Postgres

We'll get to the Django portion, but before that let's go ahead and install our necessary database software with Postgresql.

You can go ahead and download at the link [here](https://www.enterprisedb.com/downloads/postgres-postgresql-downloads). 

Once you begin the installation, you will be prompted provide a password and a port number. These are relevant for when you start you run the psql shell, and you'll need to remember the password.

After you've completed the installation, there should be a psql shell (different from your command prompt) now available in your windows start menu when you search for psql in the search bar. This is what you'll use to run the postgres server and interact with your database. There are some commands you'll need to know to display the data, and so if you are interested in using psql to display your data, there is documentation for that [here](http://postgresguide.com/utilities/psql.html).

When start up your psql shell, you should be able the default for most of the required inputs. The default port number you entered during the installation. The last field you'll have to enter will be for the password, and for that you'll need to input the password you entered during installation. 

If you are successful, you should see the `postgres =#` available at the bottom. Something like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/odigny85cs5fpxme8eab.png)

#Step 3: Set up virtualenv and install Django

Now that we have our database, we're now ready to set up our framework. Before we actually start running Django commands though, we need to have set up our virtual environment. 

Essentially, a virtual environment iis what is used to differentiate the dependencies that are required by your specific project. When you create a virtual environment it will create a directory for all the dependencies you install to be put into.

To start, we'll need to first download that virtualenv package which you can do by running:

`pip install virtualenv`

After installing the virtualenv package, you will want to navigate to the folder that you want your django project to be in. You will then create the actual dependency folder and give it a name with this command:

`virtualenv yourenvname`

After creating the folder, you can now activate the environment and utilize all the dependencies you have for your project. To do this run this command with your directory:

`yourenvname\Scripts\activate`

After that you should see your environment name in parentheses behind the command prompts, that looks something like this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i7bh8bn3wcr5td6396sz.png)

*Alternative* - This is apparently supposed to work, but for me I would get the error at the intial creation that virtualenv wasn't a known command. Not sure exactly, but I think it had to do with where i initially downloaded the virtualenv dependency.

Instead, these are the commands that I used to get the virtual env to work in my project directory:

`python -m virtualenv .`

`.\Scripts\Activate`

Now if you have your virtual environment ready, we can finally install our Django dependency into it, with this. 

`pip install Django`

We will also need to download psycopg2, which is a dependency that allows our Python program to talk to our Postgres database, according to Django configurations. To install we can run

`pip install psycopg2`

We're now finally ready to start running actual Django commands. Within your project directory run this with your project's name to intialize:

`django-admin startproject projectname`

You should now notice that you've created the root directory that has the project's name with some python files.

One of the files is a settings file which allows you to configure certain options for your application. We need this file to configure which database our application is talking to, so we'll go into that file and look up a DATABASES object which will intially have engine and name attributes. You'll want to change that file to look like this:

```
DATABASES = {
  ‘default’: {
    ‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’,
    ‘NAME’: ‘postgres’,
    ‘USER’: ‘postgres’,
    ‘PASSWORD’: ‘password’,
    ‘HOST’: ‘localhost’,
    ‘PORT’: ‘5432’,
  }
}
```

The engine we changed to postgres, and all the other options correspond to the options we set in the previous step in the psql shell. The password you'll use is the password that you set during this step. 

*Alternative - I actually preferred using a web application called elephantsql since it gives you a better visual representation of the data although it is a bit slow. 

Here's the [link](https://www.youtube.com/watch?v=t6RbanOhna4&t=44s) to the video that will explain how to use the elephantsql platform and what settings you'll have to use for configuration.

#Step 4: Grand Finale

You should now be able to run your first Django server! (If you are using the elephantsql this command doesn't work)

Type this command from your Django project directory to test your development server:

`python manage.py runserver`

If you get a response that means you're django application is working. Go over to the http address provided in the response and you should see this on the homepage!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q4i9336a808ar71qkmwy.png)

Congratulations! You've created your first Django-Postgresql application!