Every student will have an account on one of our Itanium machines for this course (account information will be mailed to each student before assignment0.) The following instructions are based on the configuration of the Itanium machines.

Get Started on PostgreSQL

This document will help you get started on the PostgreSQL, the DBMS that will be used for all the projects. You will find additional information in the PostgreSQL documentation, available online at http://www.postgresql.org/idocs/index.php?index.html You can also find documents on the local machine at /usr/local/pgsql7_3/doc/html/index.html (for v7.3). Another way to get help is using man when you want to learn details about a specific client/server/SQL command. For beginners, we recommend reading the tutorials coming with PostgreSQL, online version is at http://www.postgresql.org/idocs/index.php?tutorial.html. You can also find the tutorials with v7.3 at /usr/local/pgsql7_3/doc/html/tutorial.html.

**Background**

For project0, you will use a shared installation of PostgreSQL (7.3) binaries. The installation is compiled for Itanium architectures and located at /usr/local/pgsql7_3. Related paths have been added to your PATH environment variable.

**Step 1: Create Data Directory**

The first step is to initialize a 'database cluster', which is where PostgreSQL will store all your data files. The environmental variable PGDATA is the directory where the cluster will be initialized. By default, it is $HOME/pgdata (type echo $PGDATA to check it). Alternatively, you can specify the directory of the cluster at the command using the -D path option.

Type the command: initdb

A script will run that creates the directories, installs system tables and then exits. It should take a few seconds to a couple of minutes depending on system load. You should run this command once.

**Step 2: Server Port Number**

Each PostgreSQL server must run a separate port. We have assigned each login account a port which is automatically assigned to the environment variable PGPORT. You can check your assignment by typing echo $PGPORT. Alternatively, you could specify the port when starting the server using the -p port_num option. You do not have to do anything for this step, it is automatically handled.

**Step3: Starting the Server**

PostgreSQL comes with a utility called pg_ctl which allows for the safe starting and stopping of the server.

Type the command: pg_ctl start -o '-i'

The option -o passes the quoted text to the server, which in this case the -i option tells the server to accept connections via TCP/IP. The port number can't be changed after server starts. We recommend using

pg_ctl start -s -l $PGDATA/serverlog -o '-i'

to start the server

**Step 4: Creating a Database**

You can have multiple databases on the same server. This can be useful if you want to have separate projects (or just separate scratch space) on the server. SQL commands can not interact with more than one database at a time.

Type the command: createdb test

You should see a line CREATE DATABASE indicating the request was successful. The one parameter test is the name of the database that should be created. You can issue additional createdb commands. The opposite request:

dropdb

will remove a database.

**Step 5: Connecting to the Server**

PostgreSQL comes with a utility called psql which enables interactive processing of SQL commands.

Type the command: psql test

Notice that the parameter to psql is the name of database on the server. If you do not specify a database, psql will attempt to connect to the database with the same name as your login. Once psql is started you can enter any SQL command. Commands can be multi-line and MUST have a semicolon at the end. psql has its own prompt, which includes the database name and either a equals sign or hyphen followed by a pound sign. The hyphen tells you that a partial SQL statement has been entered. For example: (the following is just to show what psql looks like when you type SQL command, there is no table named student unless you create it)

User=# SELECT firstname, lastname
User-# FROM students;

Notice that after the first line, the prompt changed. This is particularly helpful when you enter a multi-line statement. PostgreSQL will compute the entire answer BEFORE it shows any results. When running long queries, it may appear as though the server has frozen, but it may just be working. psql stores that last query you ran in a buffer. You can access the buffer and modify the query. This is especially helpful if it is a long command and you made a mistake. By default EMACS is used as the editor, however you can change this by setting the EDITOR environmental variable. To access the buffer:

NOTE:In your SQL queries, you need to put table names (or attribute names) in double-quotes if they include capital letters. Otherwise you'll get an error from PostgreSQL that it doesn't recognize the table (attribute).

User=# \e

Once you finish editing the buffer, exit and SAVE. psql will automatically run the query. Typing SQL directly into psql can be error-prone and leaves no log of the commands you ran. You may want to put a number of SQL commands into a file and then have psql run each of those commands. The file can contain multi-lined statements along with as much white space as you want. Once the file is ready, from within psql you can execute the file: for example:

User=# \i schema.sql

When you are finished and want to exit psql:

User=# \q

psql has a number of useful features, read the documentation for psql at: http://www.postgresql.org/idocs/index.php?app-psql.html. We highly recommend at least skimming the online documentation.

**Step 6: Change the DBA Password**(optional)

PostgreSQL maintains its own database of users. By default the only user created is the same as the UNIX login of the person who ran the initdb command (i.e. your account). This account has all permissions. The account is not created with a password, so you should assign one. At the psql prompt type:

ALTER USER userid WITH PASSWORD 'password';

Depending on the server configuration, password will be stored in clear-text or encrypted text. In our experimental environment, it will be stored in encryped text.
Be sure to replace "userid" with your login account, and "password" with the password you want. If the command is successful, psql will display "ALTER USER" After you've changed the password, you can exit psql.

**Step 7: Enable Security**(optional)

Now that you have defined a password for your account, you can enable security. The file $PGDATA/pg_hba.conf contains the configuration options for security. The file contains lots of comments describing the various options for the interested reader. We'll suggest a moderate policy. The end of the file (lines 47-48) contains the actual configuration. Change the method from "trust" to "md5" at line 47 and line 48 Anyone attempting to connect to the database (either by local UNIX or TCP/IP socket) will be required to specify a username and password. By default, psql will use your UNIX login as the username and then prompt for the password.
If you use "ALTER USER username WITH UNENCRYPTED PASSWORD 'password'" to alter your password. The password will be stored in clear-text and you need to use "password" instead of "md5" in the METHOD field.

**Step 8: Stopping the Server**

Stopping the server also uses the pg_ctl utility Type the command:

pg_ctl stop

**Step 9: Using pgaccess**

pgaccess is a free graphical database management tool for PostgreSQL. You can use it to manage your database. pgaccess has been installed at /usr/local/pgaccess The path to the execuble files has been added to your PATH variable. If you access the Itanium machines remotely, you need to set up the environment variable DISPLAY in order to display the interface on your local monitor.
On the Itanium machine, type (suppose machine.andrew.cmu.edu is your local machine)

export DISPLAY=machine.andrew.cmu.edu:0.0

replace "machine.andrew.cmu.edu" with your machine's name.
On your local machine, type

xhost + "Itanium_machine_name"

Addition information about pgaccess can be found at: http://www.flex.ro/pgaccess/

Above are basic steps you usually need to set up your database server.

**Additional References**

For SQL reference: http://www.postgresql.org/idocs/index.php?sql-commands.html
For client utilities: http://www.postgresql.org/idocs/index.php?reference-client.html
For server setup: http://www.postgresql.org/idocs/index.php?runtime.html