Académique Documents
Professionnel Documents
Culture Documents
5, Fall 2018
Objectives
I. Install Postgres
There are a number of ways of installing PostgreSQL (pronounced post-gres-kyoo-el but usually
called Postgres) on your machine starting from compiling the source to using platform specific
pre-compiled binaries. Below, for each of the prevalent operating systems, we provide
recommended ways of doing a Postgres installation. For consistency and ease of
troubleshooting we suggest you follow the recommended way of installing Postgres for your OS.
If you decide to choose an alternate way of installation, do not perform multiple installations
(e.g., under MacOS install via Postgres.app and also via homebrew). Multiple installations done
in different ways can interfere with each other. We will use Postgres version 10.5 in this course.
In the directions below, the percent sign, %, stands for a generic command line prompt and the
hash sign,#, stands for the postgres psql prompt. Do not type the % or # sign when
entering the commands.
Note: Most software systems require 8 bit ASCII characters whereas Google docs uses Unicode
characters. Hence, to be on the cautious side, type the following commands --- do not cut
& paste.
MacOS
1. Postgres.app. The recommended (and simplest) option is to use the Postgres.app for
MacOS (http://postgresapp.com). Downloading and installing is easy. Double clicking the
app will start Postgres. You will see an icon for an elephant (Postgres’ emblem) on your
menu bar. Right clicking the icon gives various options including quitting the app.
2. Edit PATH. To run postgres from a terminal window you need to add the installation path
to your PATH environment variable. Step-3 of the set of installation instructions at
https://postgresapp.com/ suggests changing /etc/paths.d . Do not follow this step.
You will find it easier to adjust the PATH environment variable as recommended below:
% touch ~/.bash_profile
% open ~/.bash_profile
this will open the file .bash_profile in your default editor. Add the following lines to the
very end of the .bash_profile file:
% which psql
Windows
1. You will need to find whether your machine is 32 bit or 64 bit, which you can do by
opening your system settings e.g., Window > PC settings > PC and devices > PC info . If
uncertain, 32-bit is a safe choice.
During the installation you may be asked to also install Microsoft C++. Keep all of the
default settings. Select a password for the database superuser (postgres) account.
3. After the installation is over, the above write-up has a section called "Verify the
installation" which you can skip. We will verify the correctness of the installation in an
alternate way.
4. Edit PATH. Observe where the postgres executables were installed. Most likely they will
be installed in a folder with a name similar to C:\Program Files\PostgreSQL\10.5\bin .
Update the environment variable PATH to include this path. Over the years, the way to
access the panel to set environment variables in Windows has changed. Following is the
current sequence of clicks: Control Panel > System and Security > System >
Advanced System Settings > Environment Variables > Path > Edit > New
You can also search for ‘environment variable’ in the search box (which pops up after
clicking the windows icon in the lower left of your screen). Search for the variable PATH
and append the path the PG binaries
(most likely C:\Program Files\PostgreSQL\10.5\bin)
For additional guidance, the following page describes how to edit the PATH variable for
various flavors of windows https://www.computerhope.com/issues/ch000549.htm
Linux
Postgres can be installed via the standard way of installing a package. For example, for Debian
or Ubuntu distros:
% sudo apt-get install postgresql
When connecting with the postgres server we need to specify a role1 with which we are
connecting and the database to which we want to connect. Depending on the way and platform
you installed postgres there may be different default roles and databases. For example if you
installed on MacOS using Postgres.app you will have a predefined user with your username on
your machine (in my case raja). You will also have three predefined databases: postgres,
template0, and template1.
A windows installation will have the same three default databases (postgres, template0,
template1) but the default user is ‘postgres’.
To ensure commonality across all class members, you will enhance your installation so that all
class members can logon to their postgres cluster with the role isdb
1. Create a work folder. Be free to organize your work as you prefer. I’ll assume you have
created a folder for keeping all our course related content. From the command line,
change directories to this work folder.
2. Open a Command Line Interface (CLI). Under MacOS start a new Terminal window and
under Windows start a new Command Shell (or Power Shell) window.
3. Create a new role isdb. Recall that the percent sign % is the generic CLI prompt.
If the command was properly executed, you will not see any message and will see the
next prompt. The new user isdb would have been created; we will test this in the next
section. If you get an error message do check with us.
4. Start and Stop Postgres. Under MacOS, just start and stop the PostgresApp. Once
started you will see an elephant icon on the menu bar which provides an option to exit.
1 Previously in postgres there were two concepts: a user and a group. Both of these have been subsumed into the
term role. Reflecting this history, some postgres commands have the suffix ‘…user’, where we are actually creating a
special type of role (a role with login privileges).
3. With postgres running, navigate to your course folder. From the command prompt
execute the following (for all OSs):
% psql --version
Then execute:
[[If by chance the user isdb doesn't work corectly, you can use the postgres role and
change the above line to: % psql -U postgres -d postgres ]]
If the change to pg_hba.conf took effect, Windows users should not be prompted for the
password for isdb. You should then see the postgres prompt:
postgres=#
The default psql prompt shows the database you are connected to --- not the role you
are connected as. You can check your current database and role with the psql command
\c
postgres=# \c
You are now connected to database "postgres" as user "isdb".
Exit postgres by typing \q at the postgres prompt; you will return to the command line
prompt.
1. Hello world, SQL style. Type the following SQL code into a file hello_world.sql. Type
the code as opposed to cut/paste as sometimes you may get Unicode characters as
opposed to the ASCII characters SQL expects. Even if you are not familiar with SQL, you
may be able to guess what is going on:
DROP TABLE
CREATE TABLE
INSERT 0 1
a | b
--------+--------
Hello, | world!
(1 row)
Show your output to your lab TA and have them check it off.
At the psql prompt type the following. You may want to type the SQL code into a file of
your own and then cut/paste from your editor to the psql prompt.
4. Dump the database. Exit psql (\q) and from the command line prompt type
5. Show reviews.sql
Open the file in an editor (or do a ‘more’ from the command line if on MacOS or Linux)
and examine the contents of reviews.sql to see reverse engineered SQL code. You are
not expected to fully understand the contents of this file at this point, but you will get a
flavor for how SQL code looks.
Show the contents of reviews.sql to your lab TA and have it checked.