You are on page 1of 6

School of Science and Engineering Computer Science Department

CSC 3326 Lab Manual


Database Systems Lab
School of Science and Engineering Computer Science Department

Name: Soufiane ELABED


Email: S.elabed@aui.ma
Office Hours: To be communicated later. For now you can book an appointment.

I. Introduction to the CSC 3326 Lab


1. Why is it important?

Databases serve as the foundation for many fields including Data science, Software
development, Decision-making processes. By studying database systems, you gain insights into:

- Data definition language: Create, Change, and Drop databases.


- Data Manipulation language: Insert, update, and delete data from a database.
- Data querying language (SQL): Request for information in a database.

With the use of these abilities, you can store and modify data in a structured manner, extract
important insights from big datasets, run intricate queries, and assist decision-making
process across a variety of industries, including business intelligence, healthcare, finance, and
more.

2. Lab Structure

The lab is 1h 50 min long. It will be structured in the following manner:

 20 - 30 min: Reference to the course


The instructor quickly reviews the subjects covered in the course lecture and does
a practical demonstration utilizing the lab tools..

 30 - 80 min: Practice
During this time, the students practice a sample of projects/exercises. In order to
optimize learning and knowledge of the subject, students are expected to engage
with the instructor and ask questions. The instructor will score the students'
performance during this time period based on the effort they put into their practice
activities, as well as their participation and good behavior in the lab session.

 20 - 40 min: Paper-base Assignments

Students will be assigned to do two or three paper-based assignments throughout


the semester.
School of Science and Engineering Computer Science Department

3. Grading System
 The lab is worth 25% of the course grade.
 The lab grade is scaled as follows:
- 55%: Participation (Attendance, in-class behavior and participation, and
computer-based assignments.)
- 25%: Paper-based Assignments.
- 20%: Web Application Project.

4. Absence Regulation
 Students who miss more than two lab sessions will be given a WF.
 Students who miss lab sessions for a non-valid excuse get a zero in participation
grade for that session.
 Students who miss a lab session for a valid excuse should approach their lab
instructor for a possible chance to do the corresponding in-class exercises.

II. Technology in the Lab: PostgreSQL

PostgreSQL is an open-source object-relational database engine. You can do things like


create, modify, and retrieve data with it. SQL and PLSQL are the two primary programming
languages used by PostgreSQL.

PostgreSQL provides different client applications that you can use to manage PostgreSQL
databases. These client applications enable you to view databases, run SQL queries, and
more. You may access the database from a programming language, through a GUI
application that allows access and administration, or through the command-line interface.

 psql is a text-based command-line application


 pgAdmin is a graphical user interface application, an alternative for pgAdmin for
Mac users is Postico.

1. Windows installation of PostgreSQL:

Visit Postgres main installation website: https://www.postgresql.org/download/


School of Science and Engineering Computer Science Department

From there, select the operating system you are running. Then follow the instructions
listed on the page to download the database management system. These instructions will
vary depending on the operating system you are using.

Install the downloaded file. During installation, you may be asked to provide an admin
user password. Choose a password and take note of it!!.

Please ignore / untick the prompt to run “Stack Builder”. The latter only handles
installation of additional add-ons that are not necessary for us at the moment. The
PostgreSQL installation should handle the installation of PSQL and PgAdmin.

Your terminal will not magically understand what psqlis when typed as a command. You
need to refer to the definition of psql, in other words, you need to set your Environment
Variables For Your Accounts (use your windows search bar to open it). then click on it.

Select System variable Path and Click “Edit...” button. You should get:

Click “New”, and add a path typically like: C:\Program Files\PostgreSQL\


INSERT_POSTGRESQL_VERSION_HERE\bin

Example: C:\Program Files\PostgreSQL\15\bin

If you face issues, double check whether or not the path is correct by looking at your
installation location manually.

2. Mac OS installation of PostgreSQL:


School of Science and Engineering Computer Science Department

Follow the clear steps on this Website: https://www.postgresqltutorial.com/postgresql-


getting-started/install-postgresql-macos/

After finishing the installation steps, you may simply click on the elephant icon in your
menu bar → Open Postgres (on Postgres.App) and Double click on one of the Database
that is created by default (for example the postgres database).

III. Get started with PostgreSQL:


After finishing the installation, you need to open your CMD or terminal in windows.

To log in to the default database for the first time, type in the following command:

psql -U postgres

Then enter the superuser password that you declared during the installation process. After that, you
should be connected to the PostgreSQL database with the postgres superuser. With that user, you can
see all existing databases by typing: \l .To create a new database, use the following command:

CREATE DATABASE test1;

To connect to this new database, type: \c test1

You can also create a new user with the following command:

CREATE ROLE user_name WITH LOGIN PASSWORD 'your_pass';

To change the user's password, use the following command:

ALTER USER user_name WITH PASSWORD 'new_pass';

Then you can create a new database with the new user as the owner:

CREATE DATABASE test2 OWNER user_name;

To connect to the test3 database using the new user instead of the superuser, you need to quit the psql
console using \q. Then reconnect again by typing:

psql -d test2 -U user_name

Note: New users can't create new databases or new users. To give them permission, use the following
command:

ALTER USER user_name CREATEROLE CREATEDB;


School of Science and Engineering Computer Science Department

Finally, it is highly dangerous to drop a database, but you can still do it using one of the following
command lines:

DROP DATABASE test3; Or: DROP DATABASE IF EXISTS test3;

IV. Practice psql command lines:


To get started with using command psql lines, for now we will be using commands above. If you want to
learn other command lines you can refer to the full documentation of PostgreSQL:
https://tomcam.github.io/postgres/

Here's a simple practice exercises to familiarize yourself with PostgreSQL command lines:

Create three databases: "test1", "test2", and "test3" using the following steps:

 Create "test1" with the superuser "postgres".


 Create a two new user called "user1", “user2”, then assign ownership of "test2" to this
“user1”.
 Grant creating databases permission to "user2".
 Connect to "user2" and create the database "test3".
 Print all databases to verify the previous steps.
 Connect to the "test2" database.
 Finally, drop the "test3" database.

You might also like