Introduction to PostgreSQL -...

25
In the name of Allah Islamic University of Gaza Faculty of Engineering Computer Engineering Department ECOM 4113 DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry 2013

Transcript of Introduction to PostgreSQL -...

Page 1: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

In the name of Allah

Islamic University of Gaza

Faculty of Engineering

Computer Engineering Department

ECOM 4113

DataBase Lab

Lab # 1

Introduction to PostgreSQL

Eng. Haneen El-masry

2013

Page 2: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 2

Objective

To be familia with PostgreSQL DBMS.

BACKGROUND

What is a DataBase?

A database is a collection of related data that is known facts that can be recorded and that have

implicit meaning.

Examples: University, Library, ect.

A database may be generated and maintained manually or it may be computerized. A

computerized database may be created and maintained by a database management system

(DBMS).

What is the DBMS ?

DBMS is a collection of programs that enables users to create and maintain a database.

DBMS is a general-purpose software system that facilitates the processes of defining,

constructing, manipulating, and sharing databases among various users and

applications.

DBMS includes protecting the database and maintaining it over a long period of time.

DBMS Responsibilities

Creating the database.

Providing query and update facilities.

Multitasking.

Managing the security of the database.

Maintaining referential integrity.

Examples:

Oracle, SQL Server, MySQL, PostgreSQL.

For DataBase Lab, we choose PostgreSQL 9.2.

Page 3: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 3

DataBase System

The database and DBMS software together are called a database system.

Figure 1: DataBase System

PostgreSQL is a full-featured open-source DBMS.

It provides a solid relational engine with:

Efficient implementation of relational operations.

Very good transaction processing (concurrent access).

Good backup/recovery (from application/system failure).

Already supports several non-standard data types.

Allows users to define their own data types.

Page 4: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 4

PostgreSQL Installation on Windows

1- Set the installation location on your system >> Next.

2- Set the location of data directory that contains all of the data files for PostgreSQL.

3- Enter the password for the database superuser “postgres”, that has a full access to all of

the system tables and features in PostgreSQL.

Note: Save this password carefully.

Page 5: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 5

4- Select the port number that assigns a specific TCP port to the PostgreSQL server so that

applications can connect to send queries. The default value for the PostgreSQL port is 5432. You can elect to change this value, but it is important that you remember the new value you assign, as it must be used for all communications with the PostgreSQL system.

5- The Locale parameter is where you configure the language used on the PostgreSQL

system, leave it a default value >> click Next.

Page 6: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 6

6- you’ve finished providing information for the PostgreSQL installer, Click Next.

7- Click Finish.

Page 7: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 7

PostgreSQL Applications

There are two applications that are included in the PostgreSQL installation. These applications

help you interface with the PostgreSQL server and provide an easy way to administrating and

using of databases.

psql

The psql application provides a command-line interface to the PostgreSQL system. psql

commands are of two different types:

SQL commands: We can issue any SQL statement that PostgreSQL supports to psql, and

it will execute it.

Internal commands: These are psql commands used to perform operations not directly

supported in SQL. All internal commands begin with a backslash (\).

To start psql:

Start >> programs >> postgreSQL 9.2 >> sql shell (psql).

pgAdmin III

The pgAdmin III application is a program that provides a graphical interface for administering a

PostgreSQL system. It allows you to perform any database function from a graphical front end.

The pgAdmin III program also includes a SQL command interface.

To start pgAdmin III:

Start >> programs >> postgreSQL 9.2 >> pgAdmin III.

Page 8: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 8

Connection to postgreSQL server

You need to specify the following five parameters to connect to PostgreSQL:

host or host address.

port.

database name.

user.

password.

Connection using pgAdmin III

1- Start pgAdmin III.

2- By default, pgAdmin III is configured to connect to a PostgreSQL server running on the local

system , and use the default PostgreSQL TCP port of 5432.

3- To connect to the server, double click on the server or right click >> Connect.

Page 9: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 9

4- By default, pgAdmin III will attempt to connect to the server using the standard postgres

superuser account. Enter the password of postgres.

5- Now, you are connected to the server .

Page 10: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 10

6- If you need to change the default login account or the default TCP port number for a

server, right-click the server entry in the main window and select Properties from the

menu. In the Properties window you can set the IP address, TCP port, the default

database name, and the user account to log into the PostgreSQL server with.

Page 11: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 11

7- If you want to use pgAdmin III to connect to remote PostgreSQL servers: File menu >>

Add Server or click on icon.

Page 12: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 12

Connection using psql

Enter all the required information such as server, database, port, username and password.

If you press Enter, psql will use default values inside the square brackets [].

After connection, psql will prompt for commands with a prompt that consists of the name of

the database we are connected to, followed by =# for administrative users, but for other users

the prompt is replaced with =>.

POSTGRESQL SYSTEM

When you are connected to a server, the pgAdmin III window splits into three frames.

Detailed configuration

values of the object

currently selected in the left

frame.

The SQL code used to create the object currently selected in the left frame.

A graphical representation of all the objects contained on the PostgreSQL server.

Page 13: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 13

There are five basic components that make up the PostgreSQL server:

Databases.

Tablespaces.

Schemas (listed under each individual database).

Group Roles.

Login Roles.

Databases

Databases are the core objects in PostgreSQL. They hold all of the data objects used in

PostgreSQL.

The default database created during the PostgreSQL installation is called postgres. It contains

the default system tables for handling the internal PostgreSQL Data Dictionary. These tables are

not shown in pgAdmin III, but can be accessed via SQL queries.

There are two additional databases that are configured by default in PostgreSQL, but not shown

in pgAdmin III:

template0.

template1.

These are generic templates that are used to create new databases.

Tablespaces

Tablespaces are the physical locations where objects are stored. Objects can be anything from

database tables, indexes, functions, and triggers.

By default two tablespaces are created:

pg_default

It is the default location for all database objects created on the PostgreSQL system.

pg_global

It is used to hold internal Data Dictionary information for the PostgreSQL system to operate.

Schemas

Schemas are the most important objects within the database. A schema contains the tables,

triggers, functions, views, and other objects for handling data in the database.

Page 14: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 14

Schemas have two purposes:

To help manage the access of many different users to a single database.

To allow extra tables to be associated with a standard database, but kept separate.

By default, the template0 and template1 templates contain a schema called public.

Group Roles

Group Roles are used to create access permissions for groups of users.

By default, there is one Group Role configured in PostgreSQL. The public group role applies to

all users on the PostgreSQL system. You are not able to remove any user account from the

public Group Role. Because of this, the public Group Role does not appear in the pgAdmin III

Group Roles listing.

Login Roles

Login Roles are roles that are allowed to log into the PostgreSQL server. They are also known as

user accounts.

Create User Account

Using pgAdmin III

1- Right click on Login Roles >> New Login Role.

Page 15: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 15

2- Enter Role name, password and privileges.

Page 16: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 16

Using psql

Using sql command:

Modifying User

We can modify user properties as name, password and privileges.

Using pgAdmin III Right click on the username >> Enter new properties.

CREATE USER username [WITH PASSWORD 'password' CREATEDB | NOCREATEDB CREATEUSER | NOCREATEUSER IN GROUP groupname VALID UNTIL 'abstime' ];

Note: By default, only postgres user is allowed to create new Login roles and DataBase.

Page 17: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 17

Using psql

Using sql command:

There is also a special variant for renaming a user:

Deleting User

Using psql

Using sql command:

Using pgAdmin III

Right click on the user name >> Delete/Drop.

Creating New DataBase

Using psql

Using SQL command:

CREATE DATABASE dbname [WITH OWNER [=]owner ] [ TEMPLATE [=] template ] [ ENCODING [=] encoding ] [ TABLESPACE [=] tablespace ] ]

ALTER USER username [WITH PASSWORD 'password' CREATEDB | NOCREATEDB CREATEUSER | NOCREATEUSER IN GROUP groupname VALID UNTIL 'abstime' ];

ALTER USER username RENAME TO new-username;

Drop User username;

Page 18: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 18

Using pgAdmin III

1- Right click on Databases >> New Database.

2- Enter Database name and its owner.

Note: The default owner for the new Database is the user who logged into the server.

Page 19: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 19

Altering DataBase

We can change the name and owner of a database.

Using psql

Using pgAdmin III

Right click on the database >> properties >> Enter the new properties.

Deleting DataBase

Using psql

Using pgAdmin III

Right click on the DataBase >> Delete/Drop.

SQL Query Tool on pgAdmin III

To open SQL query tool for database: click on DataBase >> click on icon.

You can quickly change your database connection from one database to another, without

launching another instance of the query tool, following the next steps:

1- <new connection> from the combobox.

ALTER DATABASE dbname RENAME TO newname;

ALTER DATABASE dbname OWNER TO newowner;

Drop Database DBName;

Page 20: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 20

2- Enter Database name and Username.

Changing the database connection Using psql

Use the internal command command:

\c dbname username

Page 21: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 21

DataBase Backup

1- Right click on Database name >> choose Backup.

2- Browse for backup file location.

3- Enter the format of the file, encoding and role.

Page 22: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 22

4- Click Done .

DataBase Restore

1- Create a new DataBase.

Page 23: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 23

2- Right click on the new database >> Restore.

3- Select backup file.

4- Enter the format and the role >> click OK.

Page 24: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 24

5- Click Done .

Disconnecting from the server

Using psql

Using the internal command \q.

Using pgAdmin III

Right click on the server >> Disconnect server.

Page 25: Introduction to PostgreSQL - site.iugaza.edu.pssite.iugaza.edu.ps/hmasry/files/Lab1-IntroductionToPostGreSQL.pdf · DataBase Lab Lab # 1 Introduction to PostgreSQL Eng. Haneen El-masry

DataBase Lab

Eng. Haneen 25

Exercises

Using psql, Do the following:

1- Create a new user (U1) with your name and give him a password.

2- Create a new database (DB1) and make it is owned by the new user (U1).

3- Give the new user permissions to create database and user.

4- Create another new user (U2).

5- Change the owner of the database (DB1) to the new user (U2).

6- Connect to the database (DB1) as the first user.

7- Create a new database (DB2).

8- Connect to DB2 as the second user.

Using pgAdmin III, Do the following:

1- Create two new users.

2- Give the second user permission to create database.

3- Create two databases.

4- Make the first user to be the owner of the two databases.

5- Open SQL query tool of the first database as the first user.

6- Open SQL query tool of the second database as the second user.