PostgreSQL installation

The easiest way to install Postgres on Windows is using a program you can find here:

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 ).

Mac OS X

The easiest way is to download the free 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.

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

Run the following command:

Fedora

Run the following command:

  1. sudo yum install postgresql93-server

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

  1. 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 \bin added at the end. You should see the message SUCCESS: Specified value was saved..
  2. Close and then reopen the Command Prompt.

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 <username> -W first and enter the password later.

  1. $ psql
  2. psql (9.3.4)
  3. Type "help" for help.
  4. #

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):

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.

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

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!

Updating settings

Find this part in your mysite/settings.py file:

  1. DATABASES = {
  2. 'default': {
  3. 'ENGINE': 'django.db.backends.sqlite3',
  4. 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  5. }
  6. }

And replace it with this:

  1. DATABASES = {
  2. 'ENGINE': 'django.db.backends.postgresql',
  3. 'NAME': 'djangogirls',
  4. 'PASSWORD': '',
  5. 'HOST': 'localhost',
  6. 'PORT': '',
  7. }
  8. }

Remember to change name to the user name that you created earlier in this chapter.

Installing PostgreSQL package for Python

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.

Windows

For Windows, download the pre-built file from http://www.stickpeople.com/projects/python/win-psycopg/

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):

  1. easy_install C:\psycopg2.exe

Run the following in your console:

If that goes well, you’ll see something like this

  1. Downloading/unpacking psycopg2
  2. Installing collected packages: psycopg2
  3. Successfully installed psycopg2
  4. Cleaning up...

Once that’s completed, run python -c "import psycopg2". If you get no errors, everything’s installed successfully.

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:

  1. (myvenv) ~/djangogirls$ python manage.py migrate

To add new posts to your blog, you also need to create a superuser by running the code:

    Now you can run the server, log into your application with the superuser account and start adding posts to your new database.