Uploaded by cseprojects50

Exp1 G19 Installation & Database Design

advertisement
Experiment 1
Installation &
Database Design
Spatial and Graph Database
Group 19
Rajan Patil - 191080058
Prajwal Kolte - 191080041
Sanket Bhosale - 201080908
T.Y. I.T., Sem V
August 9, 2021
( this page left blank intentionally )
1 | Page
Aim: Installation of Postgres, PostGIS, QGIS & pgAdmin and
Implementation of Database Design.
Theory :
Installation of 1. PostgreSQL
Introduction
PostgreSQL is a powerful, open source object-relational database system that uses and
extends the SQL language combined with many features that safely store and scale the most
complicated data workloads. The origins of PostgreSQL date back to 1986 as part of the
POSTGRES project at the University of California at Berkeley and has more than 30 years of
active development on the core platform.
PostgreSQL has earned a strong reputation for its proven architecture, reliability, data
integrity, robust feature set, extensibility, and the dedication of the open source community
behind the software to consistently deliver performant and innovative solutions. PostgreSQL
runs on all major operating systems, has been ACID-compliant since 2001, and has powerful
add-ons such as the popular PostGIS geospatial database extender. It is no surprise that
PostgreSQL has become the open source relational database of choice for many people and
organisations.
PostgreSQL is used as the primary data store or data warehouse for many web, mobile,
geospatial, and analytics applications.
PostgreSQL comes with many features aimed to help developers build applications,
administrators to protect data integrity and build fault-tolerant environments, and help you
manage your data no matter how big or small the dataset. In addition to being free and open
source, PostgreSQL is highly extensible. For example, you can define your own data types,
build out custom functions, even write code from different programming languages without
recompiling your database!
Installation process
There are three steps to complete the PostgreSQL installation:
1.
Download PostgreSQL installer for Windows
2. Install PostgreSQL
2 | Page
3. Verify the installation
1) Download PostgreSQL Installer for Windows
First, you need to go to the download page of PostgreSQL installers on the EnterpriseDB.
Second, click the download link as shown below:
It will take a few minutes to complete the download.
2) Install PostgreSQL on Windows step by step.
To install PostgreSQL on Windows, you need to have administrator privileges.
Step 1. Double click on the installer file, an installation wizard will appear and guide you
through multiple steps where you can choose different options that you would like to have in
PostgreSQL.
Step 2. Click the Next button
3 | Page
Step 3. Specify installation folder, choose your own or keep the default folder suggested by
PostgreSQL installer and click the Next button
●
4 | Page
Step 4. Select software components to install:
●
The PostgreSQL Server to install the PostgreSQL database server
●
pgAdmin 4 to install the PostgreSQL database GUI management tool.
●
Command Line Tools to install command-line tools such as psql, pg_restore, etc.
These tools allow you to interact with the PostgreSQL database server using the
command-line interface.
●
Stack Builder provides a GUI that allows you to download and install drivers that work
with PostgreSQL.
click the Next button to select the data directory:
Step 5. Select the database directory to store the data or accept the default folder. And click
the Next button to go to the next step:
5 | Page
Step 6. Enter the password for the database superuser (postgres)
PostgreSQL runs as a service in the background under a service account named postgres. If
you already created a service account with the name postgres, you need to provide the
password of that account in the following window.
After entering the password, you need to retype it to confirm and click the Next button:
6 | Page
Step 7. Enter a port number on which the PostgreSQL database server will listen. The default
port of PostgreSQL is 5432. You need to make sure that no other applications are using this
port.
Step 8. Choose the default locale used by the PostgreSQL database. If you leave it as default
locale, PostgreSQL will use the operating system locale. After that click the Next button.
7 | Page
Step 9. The setup wizard will show the summary information of PostgreSQL. You need to
review it and click the Next button if everything is correct. Otherwise, you need to click the
Back button to change the configuration accordingly.
Now, you’re ready to install PostgreSQL on your computer. Click the Next button to begin
installing PostgreSQL.
8 | Page
The installation may take a few minutes to complete.
Step 10. Click the Finish button to complete the PostgreSQL installation.
9 | Page
3) Verify the Installation
There are several ways to verify the PostgreSQL installation. You can try to connect to the
PostgreSQL database server from any client application e.g., psql and pgAdmin.
The quick way to verify the installation is through the psql program.
First, click the psql application to launch it. The psql command-line program will display.
Second, enter all the necessary information such as the server, database, port, username, and
password. To accept the default, you can press Enter. Note that you should provide the
password that you entered during installing the PostgreSQL.
2. PostGIS
Introduction
PostGIS is a spatial database extender for the PostgreSQL Database Management
System that is open source, freely available, and fairly OGC compliant. In a word, it enriches
the database with spatial functions like distance, area, union, intersection, and speciality
geometry data types. In terms of functionality, PostGIS is extremely similar to SQL Server
Spatial support, ESRI ArcSDE, Oracle Spatial, and DB2 spatial extender except it has more
functionality and generally better performance than all of those.
10 | Page
Installation process
1.
After PostgreSQL is installed, open application stack builder which is installed along
with postgreSQL.
2. Select your postgreSQL server from the drop down list. click Next.
3. Select spatial extensions and then select PostGIS option and then click Next.
11 | Page
4. Select the components you are installed on and click Next.
5. Select yes for any prompt. for example
selecting yes automatically adds that particular environment variable to your path for
you.
6. The installer might not respond for some time but do not close it forcefully as this will
cancel your postgis installation.
12 | Page
3. QGIS
Introduction
QGIS is a GNU General Public License-licensed, user-friendly Open Source
Geographic Information System (GIS). The Open Source Geospatial Foundation has
designated QGIS as an approved project (OSGeo). It supports a wide range of vector, raster,
and database formats and functions and runs on Linux, Unix, Mac OSX, Windows, and
Android.
Installation process
1.
First download latest version of qgis (preferably .msi package)
2. After the downloading is completed and open the package and click Next
3. Agree to the license agreement and then select installation directory and click
Next.
13 | Page
4. Wait while the software is being installed and accept any permission the
system asks for.
14 | Page
4. PgAdmin
Introduction
pgAdmin is the leading open source management tool for PostgreSQL, the world's
most advanced open source database. pgAdmin 4 is designed to meet the needs of both
novice and experienced Postgres users alike, providing a powerful graphical interface that
simplifies the creation, maintenance, and use of database objects pgAdmin is derivative of
relational databases such as EnterpriseDB's EDB Advanced Server. It may be run either as a
web or desktop application. pgAdmin has a huge array of features and by far the best way to
see them all is to download a copy and give it a try. Here's a brief list to give you an idea of its
capabilities:
Multiplatform
●
Microsoft Windows ™
●
Linux
●
macOS
Designed for multiple PostgreSQL versions and derivatives
pgAdmin supports the versions of PostgreSQL and EDB Postgres Advanced Server that are in
support at the time of the release of any given version of pgAdmin.
Extensive documentation
●
pgAdmin application documentation
●
PostgreSQL documentation links
Multiple deployment models
●
Desktop mode, using a purpose built runtime
●
Server mode, supporting multiple users over the web
Tools
●
Powerful query tool with colour syntax highlighting
●
Fast datagrid for display/entry of data
●
Graphical query plan display
●
Grant Wizard for rapid updates to ACLs
●
Procedural language debugger (supporting pl/pgsql and edb-spl)
●
Schema diff tool for managing differences between schemas
●
ERD Tool for designing and documenting schemas
Routine maintenance
15 | Page
●
Auto-vacuum management
●
Monitoring dashboard
●
Backup, restore, vacuum and analyze on demand
●
SQL/shell/batch job scheduling agent
Create, view and edit all common PostgreSQL objects
Objects are displayed with their SQL definition, and a user-friendly list of properties.
Dependent, and dependency objects may also be browsed, and object statistics viewed
where appropriate.
Multibyte support
pgAdmin supports most PostgreSQL server-side encodings: SQL_ASCII, EUC_JP, EUC_CN,
EUC_KR, EUC_TW, JOHAB, LATIN1, LATIN2, LATIN3, LATIN4, LATIN5, LATIN6, LATIN7,
LATIN8, LATIN9, LATIN10, UNICODE/UTF-8, etc.
Installation process
1.
Now the pgAdmin 4 installer is up. Click Next:
16 | Page
2. The wizard will then ask you to read over the license agreement. You should agree
and accept. Then click next.
3. It will then ask where you want to save your program files for pgAdmin 4. I keep it in
the default/suggested folder. Once done, select next:
17 | Page
4. Now it will start installing
5. When it’s done installing, you can select Launch pgAdmin 4 and Finish
18 | Page
6. pgAdmin 4 may take some time to launch, and when it does, it will look something
like:
7. There will be a default PostgreSQL server on your system. You can access it on
pgAdmin. On the left hand Browser, click the + next to Servers, and it will show the
server. It will be named something like PostgreSQL 9.6. For PostgreSQL servers, there
is always a default user called “postgres”. For Windows computers, you will need to
create a password for this default user (remember to keep the password).
19 | Page
8. Using pgAdmin 4, you can now explore your PostgreSQL server that hosts your
database. It will be pretty empty.
20 | Page
Database Design
Database design can be generally defined as a collection of tasks or processes that enhance
the designing, development, implementation, and maintenance of enterprise data
management systems. Designing a proper database reduces the maintenance cost thereby
improving data consistency and the cost-effective measures are greatly influenced in terms of
disk storage space. Therefore, there has to be a brilliant concept of designing a database.
The designer should follow the constraints and decide how the elements correlate and what
kind of data must be stored.
●
The main objectives behind database designing are to produce physical and logical
design models of the proposed database system.
●
Database designs provide the blueprints of how the data is going to be stored in a
system. A proper design of a database highly affects the overall performance of any
application.
●
The designing principles defined for a database give a clear idea of the behavior of
any application and how the requests are processed.
●
Another instance to emphasize the database design is that a proper database design
meets all the requirements of users.
●
Lastly, the processing time of an application is greatly reduced if the constraints of
designing a highly efficient database are properly implemented.
When building a relational database from scratch, it is important that we put a good deal of
thought into the process. A poorly designed database can cause a number of headaches for
its users, including:
●
loss of data integrity over time
●
inability to support needed queries
●
slow performance
A well-designed table is one that:
●
seeks to minimize redundant data
●
represents a single subject
●
has a primary key (a field or set of fields whose values will uniquely identify each
record in the table)
●
does not contain multipart fields (e.g., "302 Walker Bldg, University Park, PA 16802")
●
does not contain multi-valued fields (e.g., an Author field shouldn't hold values of the
form "Jones, Martin, Williams")
●
does not contain unnecessary duplicate fields (e.g., avoid using Author1, Author2,
Author3)
●
does not contain fields that rely on other fields (e.g., don't create a Wage field in a
table that has PayRate and HrsWorked fields)
21 | Page
Problem Statement
Ice cream entrepreneurs Jen and Barry have opened their business and now need a
database to track orders. When taking an order, they record the customer's name, the
details of the order such as the flavors and quantities of ice cream needed, the date
the order is needed, and the delivery address. Their database needs to help them
answer two important questions:
1. Which orders are due to be shipped within the next two days?
2. Which flavors must be produced in greater quantities?
Implement a database design for the above scenario.
Solution
Database Design -
Postgresql queries to create the above tables:
CREATE TABLE "public.Customers" (
"customer_id" serial NOT NULL,
"name_first" VARCHAR(255) NOT NULL,
22 | Page
"name_last" VARCHAR(255) NOT NULL,
"delivery_addr" VARCHAR(20) NOT NULL,
CONSTRAINT "Customers_pk" PRIMARY KEY ("customer_id")
) WITH (
OIDS=FALSE
);
CREATE TABLE "public.Flavours" (
"flavour_id" serial NOT NULL,
"name" VARCHAR(255) NOT NULL,
CONSTRAINT "Flavours_pk" PRIMARY KEY ("flavour_id")
) WITH (
OIDS=FALSE
);
CREATE TABLE "public.Orders" (
"order_id" serial NOT NULL,
"customer_id" integer NOT NULL,
"delivery_date" DATE NOT NULL,
CONSTRAINT "Orders_pk" PRIMARY KEY ("order_id")
) WITH (
OIDS=FALSE
);
CREATE TABLE "public.OrderItems" (
"order_item_id" serial NOT NULL,
"flavour_id" integer NOT NULL,
"order_id" integer NOT NULL,
"qty" integer NOT NULL,
CONSTRAINT "OrderItems_pk" PRIMARY KEY ("order_item_id")
) WITH (
OIDS=FALSE
);
ALTER TABLE "Orders" ADD CONSTRAINT "Orders_fk0" FOREIGN KEY
("customer_id") REFERENCES "Customers"("customer_id");
ALTER TABLE "OrderItems" ADD CONSTRAINT "OrderItems_fk0" FOREIGN KEY
("flavour_id") REFERENCES "Flavours"("flavour_id");
ALTER TABLE "OrderItems" ADD CONSTRAINT "OrderItems_fk1" FOREIGN KEY
("order_id") REFERENCES "Orders"("order_id");
23 | Page
1. Which orders are due to be shipped within the next two days?
SQL keyword ORDER BY can be used in the orders table to get the orders to be
shipped within the next 2 days. The following SQL query will produce the desired
result:
SELECT "public.Orders".order_id, "public.Customers".name_first,
"public.Customers".name_last, "public.Flavours".name,
"public.OrderItems".qty, "public.Customers".delivery_addr,
"public.Orders".delivery_date
FROM (("public.Orders" INNER JOIN "public.OrderItems"
ON "public.Orders".order_id = "public.OrderItems".order_id)
INNER JOIN "public.Customers"
ON "public.Orders".customer_id = "public.Customers".customer_id)
INNER JOIN "public.Flavours"
ON "public.OrderItems".flavour_id = "public.Flavours".flavour_id
WHERE ((("public.Orders".delivery_date) <
LOCALTIMESTAMP) + interval '2 day'))
date_trunc('day',
ORDER BY "public.Orders".order_id;
2. Which flavors must be produced in greater quantities?
SQL aggregate function MAX and Group By clause can be used to get flavour which
must be produced in greater quantities. The following SQL query will produce the
desired result:
SELECT "public.Flavours".name as Flavour_name, Max(qty) as Quantity
FROM ("public.OrderItems" INNER JOIN "public.Flavours"
ON "public.OrderItems".flavour_id = "public.Flavours".flavour_id)
GROUP BY "public.Flavours".name
ORDER BY Quantity DESC;
This query lists all the flavours with it’s quantity. quantities are arranged in descending
order. i.e. greater quantity flavour will be displayed at the top. Thus, it is easy to
24 | Page
identify which flavour must be produced in greater quantities.
uwillblic.Orders".order_id, "public.Customers".name_first,
Implementation of Database Design in Postgresql
1.
Creating table Customers with Customer_id, name_first, name_last and delivery_Addr
table attributes in database named postgis_31_sample -
2. Creating table Flavours with flavour_id and name as it’s attribute -
25 | Page
3. Creating table Orders with order_id, customer_id and delivery_date as its attribute -
4. Creating table OrderItems with order_item_id, flavour_id, order_id and qty as it’s
attribute -
26 | Page
27 | Page
28 | Page
29 | Page
30 | Page
1. Which orders are due to be shipped within the next two days?
2. Which flavors must be produced in greater quantities?
31 | Page
In the above output, we can see that the quantity of strawberry flavour is greater, so
strawberry flavors must be produced in greater quantities. This query displays the quantity of
each flavour and quantity is arranged in descending order. Thus, it is easy to get the list of
flavours which must be produced in greater quantities.
Conclusion
Thus, we have installed various tools such as postgres, PostGIS, QGIS, pgAdmin, designed a
database for given scenarios and implemented that Database Design in pgAdmin.
32 | Page
Download