Vous êtes sur la page 1sur 7

Install PostgreSQL 10.

5, Fall 2018

Objectives

1. Install PostgreSQL on your personal machine


2. Create a role (user) called isdb which we will use as our default role
3. Interact with Postgres via the command line client psql

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:

From a terminal window type:

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

# Setting PATH for Postgres


export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin/:$PATH

The path /Applications/Postgres.app/Contents/Versions/latest/bin/ should be the


same/similar path that appears when you open a psql terminal window via the elephant
icon on your MacOS menu bar (at the very top of your screen). Save the file
.bash_profile and exit the editor. Close any open terminal windows and open a new
terminal window. If you now do:

% which psql

you should see the absolute path to the executable


/Applications/Postgres.app/Contents/Versions/latest/bin/psql

Windows

PostgreSQL installation in Windows is equally straightforward.

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.

2. Detailed instructions for installation are available at


http://www.postgresqltutorial.com/install-postgresql/ which walks you through
downloading EnterpriseDB distribution of PG. (The given instructions are for PG 9.5, but
they are the same for PG 10.5 too.)

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.

Uncheck "Launch Stack Builder on Exit" and finish the installation.

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

5. Passwordless access. When postgres is installed under MacOS using postgres.app it is


configured for password-less access. While you don't want to have this in a production
environment, it is useful in a learning environment. When postgres is installed under
windows the default configuration requires a password to be specified. In these set of
instructions you will turn that off.

a. Via the Windows explorer, navigate to the folder


C:/Program Files/PostgreSQL/10.5/data

b. Make a copy of the file pg_hba.conf and call it pg_hba-orig.conf


Open pg_hba.conf in your favorite editor and scroll to the very bottom. You will
see the following lines

# TYPE DATABASE USER ADDRESS METHOD

# IPv4 local connections:


host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#host replication postgres 127.0.0.1/32 md5
#host replication postgres ::1/128 md5

Edit the lines in bold as show below:

# TYPE DATABASE USER ADDRESS METHOD

# IPv4 local connections:


host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#host replication postgres 127.0.0.1/32 md5
#host replication postgres ::1/128 md5

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

II. Test your Installation

A postgres installation is known as a cluster (of databases), even though it is on a single


machine. Once you have successfully installed postgres, you may want to peruse
https://wiki.postgresql.org — a nice, community curated, collection of Postgres resources.

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.

% createuser -U postgres --superuser isdb

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.

On Windows, postgres should have automatically started. To stop / start, go to ‘Services’.


Easiest way would be to just search for services in the Windows/Search box, and select
postgres from the list of services. Once selected you will see options on the left to Stop /
Start postgres.

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

Check that you see the message:


Psql (PostgreSQL) 10.5

Then execute:

% psql -U isdb -d postgres

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

III Try a Couple of Simple SQL scripts.

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 IF EXISTS Greetings;


CREATE TABLE Greetings (a text, b text);
INSERT INTO Greetings
VALUES ('Hello,', 'world!');
SELECT * FROM Greetings;

Run the SQL code with


% psql –U isdb -d postgres –f hello_world.sql

You should see the following output:

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.

2. Create and Populate a table. Log on to postgres with:

% psql -U isdb -d postgres

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.

A SQL statement, similar to a Java statement, is terminated with a semi-colon. SQL


commands are free form: white space doesn't matter.

CREATE TABLE Reviews (


id serial,
movie text,
rating integer
);

INSERT INTO Reviews (movie, rating) VALUES ('The Godfather', 5);


INSERT INTO Reviews (movie, rating) VALUES ('Sahara', 0);
INSERT INTO Reviews (movie, rating) VALUES ('Inside Out', 4);

3. View the populated table.

postgres=# SELECT * FROM Reviews;

4. Dump the database. Exit psql (\q) and from the command line prompt type

% pg_dump -U isdb -d postgres -f reviews.sql

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.

Vous aimerez peut-être aussi