SlideShare a Scribd company logo
Rapid POSTGRESQL
learning, PART-1
BY: ALI MASUDIANPOUR MASUD.AMP@GMAIL.COM
RAPID POSTGRESQL LEARNING. 1
Introduction
In this manual, my assumption is that reader knows basic concepts of database topic.
RAPID POSTGRESQL LEARNING. 2
Introduction
Postgresql
Powerfull OOP relational DBMS
Large community
Open Source
All major Operating Systems such as Linux/ Windows/ Solaris/ Mac and so on…
Started in 1995
Latest version at this moment (June 2013) is 9.2
RAPID POSTGRESQL LEARNING. 3
Features
Supports Procedure languages [pl, pgsql, pljava, plphp, plperl]
Supports multi-version concurrency control (MCC or MVCC)
◦ More details on: https://en.wikipedia.org/wiki/Multiversion_concurrency_control
Supports variety of DATA TYPES including INTEGER, CHARARCTER, BOOLEAN, DATE, INTERVAL, TIMESTAMP
Allows users to define their own objects such as:
◦ DATA TYPES
◦ FUNCTIONS
Supports data integrity such as PK, FK, Restrictions, Cascading and Constraint
Supports binary objects such as pictures, sounds, videos
Open source and free
Main users:
◦ Yahoo, Skype, imtv.com and so on…
RAPID POSTGRESQL LEARNING. 4
Installation
Postgresql is available to download on http://postgresql.org . So you can download it from the
mentioned internet address. (the latest one at writing this document is 9.2)
On my assumption you have an Ubuntu distribution installed on your computer, so to install
Postgresql we can use the following statement:
◦ Sudo apt-get install postgres-9.2
After all processes you can enter to postrgresql shell using the following command:
◦ Sudo –u postgres psql postgres
◦ Note that postgres user is the super user of our installed postgresql
◦ After above command you are logged in to postgresql command line.
RAPID POSTGRESQL LEARNING. 5
Start Up Commands
Basic settings and commands would be like the following lines:
CREATE DATABASE
◦ To create Database in postgresql we use the following command
◦ CREATE DATABASE [database name];
◦ Example: CREATE DATABASE testName;
◦ Or you may be not in postgresql command line, so creating database will be accomplished with the following
statement:
◦ Sudo –u postgres created [table name]
◦ For example: sudo –u postgres created testName
CREATE USER
◦ To create user in postgres, we use the following command:
◦ CREATE USER [username] WITH PASSWORD [password];
◦ For instance: CREATE USER masud WITH PASSWORD ‘masudpassword’;
◦ Other way is outside of postgres command line:
◦ Sudo –u postgres createuser ‘masud’
RAPID POSTGRESQL LEARNING. 6
Start Up Commands
DROP USER
◦ TO drop user the first way would be like below in postgresql command line:
◦ DROP USER [username];
◦ For example: DROP USER masud;
◦ The second way is outside of postgres command line:
◦ Sudo –u postgres dropuser ‘username’
◦ For example: sudo –u postgres dropuser ‘masud’
DROP DATABASE
◦ To drop a database first way would be like bellow in postgresql command line:
◦ DROP DATABASE [database name]
◦ for example: DROP DATABASE ‘testName’;
◦ The second way is outside of postgres command line:
◦ Sudo –u postgres dropdb [db name]
◦ For example: sudo –u postgres dropdb ‘testName’
RAPID POSTGRESQL LEARNING. 7
Start Up Commands
CHANGE PASSWORD OF USER
◦ We can do it by:
◦ Way1: In psql shell type : password username
◦ Example: password masud
◦ Way2: in psql shell type: ALTER USER [username] WITH PASSWORD [new password]
◦ Example: ALTER USER masud WITH PASSWORD ‘masudsnewPassword’
RAPID POSTGRESQL LEARNING. 8
Manage POSTGRESQL Service
In order to handle postgresql services we can use the following commands to [start/ stop/
restart] postgresql service:
◦ Sudo service postgresql start
◦ Sudo service postgresql stop
◦ Sudo service postgresql restart
RAPID POSTGRESQL LEARNING. 9
Important Commands in
POSTGRESQL
There are some short commands in postgresql that each one performs an specific task. In continue I
will introduce you some of them:
◦ i
◦ Imports a dump into database
◦ l
◦ List of databases
◦ d
◦ List of tables
◦ dt
◦ List of tables and relations
◦ h
◦ help
◦ ?
◦ help
◦ df
◦ List of functions
RAPID POSTGRESQL LEARNING. 10
Important Commands in
POSTGRESQL
◦ df+
◦ Same df but with source
◦ timing
◦ Turn on/off timing
◦ Timing shows execution time of a query
◦ password
◦ Change password
◦ q
◦ Quite
◦ c
◦ Change database connection [switch between databases]
◦ dp
◦ Access privileges
◦ conninfo
◦ Current Connection Info
◦ e
◦ Execute command
RAPID POSTGRESQL LEARNING. 11
Important Commands in
POSTGRESQL
◦ pset format [html/ aligned/ unaligned/ wrapped/ html/ latex]
◦ Changes the output format
◦ For instance if we change output format to HTML, any result after that time will be shown in HTML format
◦ pset border [0/1/2]
◦ Set border of result [ for instance border around result table]
◦ 0: none border
◦ 1: normal border
◦ 2: whole table has a border
◦ pset null null
◦ It will write null instead of nothing when found a null value in a query
◦ In order to find out which version of postgres we are using we can use the following command:
◦ SELECT VERSION();
RAPID POSTGRESQL LEARNING. 12
Keywords and Identifiers
◦ Keywords are words that are available in SQL, for instance:
◦ CREATE
◦ DATABASE
◦ ORDER
◦ Identifiers are used to identify objects such as:
◦ TABLE
◦ COLUMN
◦ Note that keywords can not be used as identifiers
◦ Identifiers can be up to 63 characters
◦ Identifiers and Keywords are case insensitive
RAPID POSTGRESQL LEARNING. 13
Comments
◦ In order to comment one line in postgresql we can use -- (two-dash signs)
◦ --this is a comment in psql
◦ For multiline commands we use /* */
◦ /* This is a multiline command in postgresql
postgresql or postgres or psql are same
*/
RAPID POSTGRESQL LEARNING. 14
Data Types
Data types in postgresql
◦ INTEGER
◦ Including 3 size variations:
◦ 2bytes (smallint)
◦ 4bytes (integer)
◦ 8bytes(bigint)
◦ SERIAL
◦ it is used to create unique identifier columns
◦ It will generate a sequence automatically
◦ NUMERIC
◦ Stores large numbers that need exact calculations
RAPID POSTGRESQL LEARNING. 15
Data Types
◦ FLOATING POINT
◦ REAL
◦ DOUBLE
This data types accepts some special non-numeric values such as:
Infinity
-Infinity
NaN(Not a Number)
◦ CHAR
◦ VARCHAR
◦ Varying character
◦ TEXT
◦ Stores strings of any length
RAPID POSTGRESQL LEARNING. 16
Data Types
◦ DATE
◦ Dates must be between single quotes
◦ TIME
◦ TIMESTAMP
◦ Without time zone
◦ With time zone
◦ INTERVAL
◦ POSTGRES SUPPORTS THE FOLLOWING SPECIAL VALUES:
◦ Today
◦ Tomorrow
◦ Yesterday
◦ Infinity
◦ -infinity
RAPID POSTGRESQL LEARNING. 17
Date Style in Postgresql
Postgresql supports 3 different DATESTYLE containing:
◦ ISO
◦ SQL
◦ POSTGRES
We can change DATESTYLE with the following statements:
◦ SET DATESTYLE TO ISO,MDY
◦ Which means ISO and MONTH - DAY – YEAR
◦ SET DATESTYLE TO SQL, MDY
◦ SET DATESTYLE TO POSTGRES,MDY
RAPID POSTGRESQL LEARNING. 18
INTERVALS
Intervals represent a duration of a time
Unites in interval include:
◦ DAY
◦ HOUR
◦ MINUTE
◦ SECOND
◦ YEAR TO MONTH
◦ DAY TO HOUR
◦ DAY TO MINUTE
◦ DAY TO SECOND
◦ HOUR TO MINUTE
◦ HOUR TO SECOND
◦ MINUTE TO SECOND
◦ …
RAPID POSTGRESQL LEARNING. 19
INTERVALS
◦ Example:
◦ SET INTERVALSTYLE TO sql_standard
◦ Now try to type: SELECT INTERVAL ‘1 14:13:15’;
◦ RESULT: 1 14:13:15
◦ SET INTERVALSTYLE TO postgres;
◦ Now try to type: SELECT INTERVAL ‘1 14:13:15’;
◦ RESULT: 1 day 14:13:15
◦ SET INTERVALSTYLE TO iso_8601
◦ Now try to type: SELECT INTERVAL ‘1 14:13:15’;
◦ RESULT: P1 DT 14H13M15S
RAPID POSTGRESQL LEARNING. 20
Boolean Data Type
It holds three values
◦ True
◦ False
◦ Unknown
◦ TRUE [can be one of the following values]
◦ T
◦ True
◦ Yes
◦ Y
◦ On
◦ 1
◦ False [can be one of the following values]
◦ False
◦ F
◦ NO
◦ Off
◦ 0
All Values except true and false should be between single quotes.
RAPID POSTGRESQL LEARNING. 21
Enumerated Data Types
Enums are not predefined in postgresql, this means that Postgresql does not officially supports
mysql’s ENUM data type. In order to achieve an Enumeration data type we must create our own.
CREATE ENUM
◦ CREATE TYPE [type name] AS ENUM([data array])
◦ For example: CREATE TYPE colors AS ENUM(‘red’,’blue’,’green’);
HOW TO USE
◦ Just like another data types
◦ If an integer is: INTEGER variable name -> it would be: colors color
◦ [ENUM NAME] [variable name]
Notes:
◦ Enums are case sensitive
◦ Sort is important in enums, for instance we must start with smallest values to the largest. For instance it is
better to use ENUM(‘red’,’blue’,’green’) instead of ENUM(‘green’,’red’,’blue’)
RAPID POSTGRESQL LEARNING. 22
GEOMETRIC Data Types
These data types are used to represent two dimensional objects
◦ Objects can be point, box, line, polygon, circle, path
◦ POINT
◦ To define a point (x,y)
◦ Example: (‘2,3’) or (‘(2,3)’)
◦ LINE
◦ To define a line using 2 points
◦ Example: ((x1,y1),(x2,y2))
◦ PATH
◦ Represented by lists of connected points
◦ Example: (‘(2,3,4,5)’) or (‘*2,3,4,5+’)
◦ CIRCLE
◦ For a circle
◦ Example: (‘2,3,4’) -> in this example 4 is radius
◦ BOX
◦ To define a rectangle for instance : ((x1,y1),(x2,y2))
RAPID POSTGRESQL LEARNING. 23
Arrays
To define an array we add brackets after data type or we can use from array keyword.
◦ Test INTEGER[]
◦ Test INTEGER[][]
◦ Test INTEGER ARRAY
◦ In insert mode:
◦ In one-dimension: (‘,1,2,3,4,5,6,7-’)
◦ In two-dimension: (‘,1,2,3,4-,,5,6,7,8-’)
RAPID POSTGRESQL LEARNING. 24
End of part 1
This is the end of part 1
◦ In the next part following topics will be covered
◦ Creating tables
◦ PRIMARY and FORIGN Keys
◦ Check Constraint
◦ NOT NULL Constraint
◦ UNIQUE Constraint
◦ DEFAULT values
◦ CASCADE
◦ CRUD
◦ INSERT
◦ UPDATE
◦ DELETE
◦ DELETE CASCADE
◦ ON DELETE CASCADE
◦ ON UPDATE CASCADE
◦ TRUNCATE
◦ QUERIES
◦ …
RAPID POSTGRESQL LEARNING. 25
Ad

More Related Content

What's hot (20)

My sql.ppt
My sql.pptMy sql.ppt
My sql.ppt
MAGNA COLLEGE OF ENGINEERING
 
BITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQLBITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQL
BITS
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
togather111
 
Scaling Databases with DBIx::Router
Scaling Databases with DBIx::RouterScaling Databases with DBIx::Router
Scaling Databases with DBIx::Router
Perrin Harkins
 
MySql:Basics
MySql:BasicsMySql:Basics
MySql:Basics
DataminingTools Inc
 
2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung Mosbach2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung Mosbach
Johannes Hoppe
 
MYSQL
MYSQLMYSQL
MYSQL
Ankush Jain
 
Django - sql alchemy - jquery
Django - sql alchemy - jqueryDjango - sql alchemy - jquery
Django - sql alchemy - jquery
Mohammed El Rafie Tarabay
 
MongoDB-SESSION03
MongoDB-SESSION03MongoDB-SESSION03
MongoDB-SESSION03
Jainul Musani
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
Smita Prasad
 
Store and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and CassandraStore and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and Cassandra
Deependra Ariyadewa
 
Data Processing Inside PostgreSQL
Data Processing Inside PostgreSQLData Processing Inside PostgreSQL
Data Processing Inside PostgreSQL
EDB
 
Html web sql database
Html web sql databaseHtml web sql database
Html web sql database
AbhishekMondal42
 
Cassandra 2.2 & 3.0
Cassandra 2.2 & 3.0Cassandra 2.2 & 3.0
Cassandra 2.2 & 3.0
Victor Coustenoble
 
Msql
Msql Msql
Msql
ksujitha
 
Lab1-DB-Cassandra
Lab1-DB-CassandraLab1-DB-Cassandra
Lab1-DB-Cassandra
Lilia Sfaxi
 
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
Johannes Hoppe
 
Oracle PL-SQL
Oracle PL-SQLOracle PL-SQL
Oracle PL-SQL
Saurav Sony
 
Mysql Ppt
Mysql PptMysql Ppt
Mysql Ppt
Hema Prasanth
 
Lobos Introduction
Lobos IntroductionLobos Introduction
Lobos Introduction
Nicolas Buduroi
 
BITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQLBITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQL
BITS
 
Scaling Databases with DBIx::Router
Scaling Databases with DBIx::RouterScaling Databases with DBIx::Router
Scaling Databases with DBIx::Router
Perrin Harkins
 
2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung Mosbach2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung Mosbach
Johannes Hoppe
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
Smita Prasad
 
Store and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and CassandraStore and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and Cassandra
Deependra Ariyadewa
 
Data Processing Inside PostgreSQL
Data Processing Inside PostgreSQLData Processing Inside PostgreSQL
Data Processing Inside PostgreSQL
EDB
 
Lab1-DB-Cassandra
Lab1-DB-CassandraLab1-DB-Cassandra
Lab1-DB-Cassandra
Lilia Sfaxi
 
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
Johannes Hoppe
 

Similar to Rapid postgresql learning, part 1 (20)

Postgresql
PostgresqlPostgresql
Postgresql
NexThoughts Technologies
 
DataBase Management System Lab File
DataBase Management System Lab FileDataBase Management System Lab File
DataBase Management System Lab File
Uttam Singh Chaudhary
 
What’s New In PostgreSQL 9.3
What’s New In PostgreSQL 9.3What’s New In PostgreSQL 9.3
What’s New In PostgreSQL 9.3
Pavan Deolasee
 
Postgresql quick guide
Postgresql quick guidePostgresql quick guide
Postgresql quick guide
Ashoka Vanjare
 
Beyond Wordcount with spark datasets (and scalaing) - Nide PDX Jan 2018
Beyond Wordcount  with spark datasets (and scalaing) - Nide PDX Jan 2018Beyond Wordcount  with spark datasets (and scalaing) - Nide PDX Jan 2018
Beyond Wordcount with spark datasets (and scalaing) - Nide PDX Jan 2018
Holden Karau
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
metsarin
 
MySQL up and running 30 minutes.pdf
MySQL up and running 30 minutes.pdfMySQL up and running 30 minutes.pdf
MySQL up and running 30 minutes.pdf
Vinicius M Grippa
 
A brief introduction to PostgreSQL
A brief introduction to PostgreSQLA brief introduction to PostgreSQL
A brief introduction to PostgreSQL
Vu Hung Nguyen
 
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
Dinesh Neupane
 
PLSQL
PLSQLPLSQL
PLSQL
Shubham Bammi
 
SQL - RDBMS Concepts
SQL - RDBMS ConceptsSQL - RDBMS Concepts
SQL - RDBMS Concepts
WebStackAcademy
 
Introducing Apache Spark's Data Frames and Dataset APIs workshop series
Introducing Apache Spark's Data Frames and Dataset APIs workshop seriesIntroducing Apache Spark's Data Frames and Dataset APIs workshop series
Introducing Apache Spark's Data Frames and Dataset APIs workshop series
Holden Karau
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3
Belal Arfa
 
PostgreSQL 9.5 - Major Features
PostgreSQL 9.5 - Major FeaturesPostgreSQL 9.5 - Major Features
PostgreSQL 9.5 - Major Features
InMobi Technology
 
OpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQLOpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQL
Open Gurukul
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the Server
devObjective
 
Sql killedserver
Sql killedserverSql killedserver
Sql killedserver
ColdFusionConference
 
Cassandra
CassandraCassandra
Cassandra
Robert Koletka
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
Alex Zaballa
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
Alex Zaballa
 
What’s New In PostgreSQL 9.3
What’s New In PostgreSQL 9.3What’s New In PostgreSQL 9.3
What’s New In PostgreSQL 9.3
Pavan Deolasee
 
Postgresql quick guide
Postgresql quick guidePostgresql quick guide
Postgresql quick guide
Ashoka Vanjare
 
Beyond Wordcount with spark datasets (and scalaing) - Nide PDX Jan 2018
Beyond Wordcount  with spark datasets (and scalaing) - Nide PDX Jan 2018Beyond Wordcount  with spark datasets (and scalaing) - Nide PDX Jan 2018
Beyond Wordcount with spark datasets (and scalaing) - Nide PDX Jan 2018
Holden Karau
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
metsarin
 
MySQL up and running 30 minutes.pdf
MySQL up and running 30 minutes.pdfMySQL up and running 30 minutes.pdf
MySQL up and running 30 minutes.pdf
Vinicius M Grippa
 
A brief introduction to PostgreSQL
A brief introduction to PostgreSQLA brief introduction to PostgreSQL
A brief introduction to PostgreSQL
Vu Hung Nguyen
 
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
Dinesh Neupane
 
Introducing Apache Spark's Data Frames and Dataset APIs workshop series
Introducing Apache Spark's Data Frames and Dataset APIs workshop seriesIntroducing Apache Spark's Data Frames and Dataset APIs workshop series
Introducing Apache Spark's Data Frames and Dataset APIs workshop series
Holden Karau
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3
Belal Arfa
 
PostgreSQL 9.5 - Major Features
PostgreSQL 9.5 - Major FeaturesPostgreSQL 9.5 - Major Features
PostgreSQL 9.5 - Major Features
InMobi Technology
 
OpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQLOpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQL
Open Gurukul
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the Server
devObjective
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
Alex Zaballa
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
Alex Zaballa
 
Ad

More from Ali MasudianPour (6)

Start using less css
Start using less cssStart using less css
Start using less css
Ali MasudianPour
 
An Introduction to REDIS NoSQL database
An Introduction to REDIS NoSQL databaseAn Introduction to REDIS NoSQL database
An Introduction to REDIS NoSQL database
Ali MasudianPour
 
Rapid postgresql learning, part 4
Rapid postgresql learning, part 4Rapid postgresql learning, part 4
Rapid postgresql learning, part 4
Ali MasudianPour
 
Rapid postgresql learning, part 3
Rapid postgresql learning, part 3Rapid postgresql learning, part 3
Rapid postgresql learning, part 3
Ali MasudianPour
 
A comparison between C# and Java
A comparison between C# and JavaA comparison between C# and Java
A comparison between C# and Java
Ali MasudianPour
 
Xp exterme-programming-model
Xp exterme-programming-modelXp exterme-programming-model
Xp exterme-programming-model
Ali MasudianPour
 
An Introduction to REDIS NoSQL database
An Introduction to REDIS NoSQL databaseAn Introduction to REDIS NoSQL database
An Introduction to REDIS NoSQL database
Ali MasudianPour
 
Rapid postgresql learning, part 4
Rapid postgresql learning, part 4Rapid postgresql learning, part 4
Rapid postgresql learning, part 4
Ali MasudianPour
 
Rapid postgresql learning, part 3
Rapid postgresql learning, part 3Rapid postgresql learning, part 3
Rapid postgresql learning, part 3
Ali MasudianPour
 
A comparison between C# and Java
A comparison between C# and JavaA comparison between C# and Java
A comparison between C# and Java
Ali MasudianPour
 
Xp exterme-programming-model
Xp exterme-programming-modelXp exterme-programming-model
Xp exterme-programming-model
Ali MasudianPour
 
Ad

Recently uploaded (20)

Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Mastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdfMastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdf
Spiral Mantra
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Vaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without HallucinationsVaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without Hallucinations
john409870
 
Top 10 IT Help Desk Outsourcing Services
Top 10 IT Help Desk Outsourcing ServicesTop 10 IT Help Desk Outsourcing Services
Top 10 IT Help Desk Outsourcing Services
Infrassist Technologies Pvt. Ltd.
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Mastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdfMastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdf
Spiral Mantra
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Vaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without HallucinationsVaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without Hallucinations
john409870
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 

Rapid postgresql learning, part 1

  • 1. Rapid POSTGRESQL learning, PART-1 BY: ALI MASUDIANPOUR [email protected] RAPID POSTGRESQL LEARNING. 1
  • 2. Introduction In this manual, my assumption is that reader knows basic concepts of database topic. RAPID POSTGRESQL LEARNING. 2
  • 3. Introduction Postgresql Powerfull OOP relational DBMS Large community Open Source All major Operating Systems such as Linux/ Windows/ Solaris/ Mac and so on… Started in 1995 Latest version at this moment (June 2013) is 9.2 RAPID POSTGRESQL LEARNING. 3
  • 4. Features Supports Procedure languages [pl, pgsql, pljava, plphp, plperl] Supports multi-version concurrency control (MCC or MVCC) ◦ More details on: https://en.wikipedia.org/wiki/Multiversion_concurrency_control Supports variety of DATA TYPES including INTEGER, CHARARCTER, BOOLEAN, DATE, INTERVAL, TIMESTAMP Allows users to define their own objects such as: ◦ DATA TYPES ◦ FUNCTIONS Supports data integrity such as PK, FK, Restrictions, Cascading and Constraint Supports binary objects such as pictures, sounds, videos Open source and free Main users: ◦ Yahoo, Skype, imtv.com and so on… RAPID POSTGRESQL LEARNING. 4
  • 5. Installation Postgresql is available to download on http://postgresql.org . So you can download it from the mentioned internet address. (the latest one at writing this document is 9.2) On my assumption you have an Ubuntu distribution installed on your computer, so to install Postgresql we can use the following statement: ◦ Sudo apt-get install postgres-9.2 After all processes you can enter to postrgresql shell using the following command: ◦ Sudo –u postgres psql postgres ◦ Note that postgres user is the super user of our installed postgresql ◦ After above command you are logged in to postgresql command line. RAPID POSTGRESQL LEARNING. 5
  • 6. Start Up Commands Basic settings and commands would be like the following lines: CREATE DATABASE ◦ To create Database in postgresql we use the following command ◦ CREATE DATABASE [database name]; ◦ Example: CREATE DATABASE testName; ◦ Or you may be not in postgresql command line, so creating database will be accomplished with the following statement: ◦ Sudo –u postgres created [table name] ◦ For example: sudo –u postgres created testName CREATE USER ◦ To create user in postgres, we use the following command: ◦ CREATE USER [username] WITH PASSWORD [password]; ◦ For instance: CREATE USER masud WITH PASSWORD ‘masudpassword’; ◦ Other way is outside of postgres command line: ◦ Sudo –u postgres createuser ‘masud’ RAPID POSTGRESQL LEARNING. 6
  • 7. Start Up Commands DROP USER ◦ TO drop user the first way would be like below in postgresql command line: ◦ DROP USER [username]; ◦ For example: DROP USER masud; ◦ The second way is outside of postgres command line: ◦ Sudo –u postgres dropuser ‘username’ ◦ For example: sudo –u postgres dropuser ‘masud’ DROP DATABASE ◦ To drop a database first way would be like bellow in postgresql command line: ◦ DROP DATABASE [database name] ◦ for example: DROP DATABASE ‘testName’; ◦ The second way is outside of postgres command line: ◦ Sudo –u postgres dropdb [db name] ◦ For example: sudo –u postgres dropdb ‘testName’ RAPID POSTGRESQL LEARNING. 7
  • 8. Start Up Commands CHANGE PASSWORD OF USER ◦ We can do it by: ◦ Way1: In psql shell type : password username ◦ Example: password masud ◦ Way2: in psql shell type: ALTER USER [username] WITH PASSWORD [new password] ◦ Example: ALTER USER masud WITH PASSWORD ‘masudsnewPassword’ RAPID POSTGRESQL LEARNING. 8
  • 9. Manage POSTGRESQL Service In order to handle postgresql services we can use the following commands to [start/ stop/ restart] postgresql service: ◦ Sudo service postgresql start ◦ Sudo service postgresql stop ◦ Sudo service postgresql restart RAPID POSTGRESQL LEARNING. 9
  • 10. Important Commands in POSTGRESQL There are some short commands in postgresql that each one performs an specific task. In continue I will introduce you some of them: ◦ i ◦ Imports a dump into database ◦ l ◦ List of databases ◦ d ◦ List of tables ◦ dt ◦ List of tables and relations ◦ h ◦ help ◦ ? ◦ help ◦ df ◦ List of functions RAPID POSTGRESQL LEARNING. 10
  • 11. Important Commands in POSTGRESQL ◦ df+ ◦ Same df but with source ◦ timing ◦ Turn on/off timing ◦ Timing shows execution time of a query ◦ password ◦ Change password ◦ q ◦ Quite ◦ c ◦ Change database connection [switch between databases] ◦ dp ◦ Access privileges ◦ conninfo ◦ Current Connection Info ◦ e ◦ Execute command RAPID POSTGRESQL LEARNING. 11
  • 12. Important Commands in POSTGRESQL ◦ pset format [html/ aligned/ unaligned/ wrapped/ html/ latex] ◦ Changes the output format ◦ For instance if we change output format to HTML, any result after that time will be shown in HTML format ◦ pset border [0/1/2] ◦ Set border of result [ for instance border around result table] ◦ 0: none border ◦ 1: normal border ◦ 2: whole table has a border ◦ pset null null ◦ It will write null instead of nothing when found a null value in a query ◦ In order to find out which version of postgres we are using we can use the following command: ◦ SELECT VERSION(); RAPID POSTGRESQL LEARNING. 12
  • 13. Keywords and Identifiers ◦ Keywords are words that are available in SQL, for instance: ◦ CREATE ◦ DATABASE ◦ ORDER ◦ Identifiers are used to identify objects such as: ◦ TABLE ◦ COLUMN ◦ Note that keywords can not be used as identifiers ◦ Identifiers can be up to 63 characters ◦ Identifiers and Keywords are case insensitive RAPID POSTGRESQL LEARNING. 13
  • 14. Comments ◦ In order to comment one line in postgresql we can use -- (two-dash signs) ◦ --this is a comment in psql ◦ For multiline commands we use /* */ ◦ /* This is a multiline command in postgresql postgresql or postgres or psql are same */ RAPID POSTGRESQL LEARNING. 14
  • 15. Data Types Data types in postgresql ◦ INTEGER ◦ Including 3 size variations: ◦ 2bytes (smallint) ◦ 4bytes (integer) ◦ 8bytes(bigint) ◦ SERIAL ◦ it is used to create unique identifier columns ◦ It will generate a sequence automatically ◦ NUMERIC ◦ Stores large numbers that need exact calculations RAPID POSTGRESQL LEARNING. 15
  • 16. Data Types ◦ FLOATING POINT ◦ REAL ◦ DOUBLE This data types accepts some special non-numeric values such as: Infinity -Infinity NaN(Not a Number) ◦ CHAR ◦ VARCHAR ◦ Varying character ◦ TEXT ◦ Stores strings of any length RAPID POSTGRESQL LEARNING. 16
  • 17. Data Types ◦ DATE ◦ Dates must be between single quotes ◦ TIME ◦ TIMESTAMP ◦ Without time zone ◦ With time zone ◦ INTERVAL ◦ POSTGRES SUPPORTS THE FOLLOWING SPECIAL VALUES: ◦ Today ◦ Tomorrow ◦ Yesterday ◦ Infinity ◦ -infinity RAPID POSTGRESQL LEARNING. 17
  • 18. Date Style in Postgresql Postgresql supports 3 different DATESTYLE containing: ◦ ISO ◦ SQL ◦ POSTGRES We can change DATESTYLE with the following statements: ◦ SET DATESTYLE TO ISO,MDY ◦ Which means ISO and MONTH - DAY – YEAR ◦ SET DATESTYLE TO SQL, MDY ◦ SET DATESTYLE TO POSTGRES,MDY RAPID POSTGRESQL LEARNING. 18
  • 19. INTERVALS Intervals represent a duration of a time Unites in interval include: ◦ DAY ◦ HOUR ◦ MINUTE ◦ SECOND ◦ YEAR TO MONTH ◦ DAY TO HOUR ◦ DAY TO MINUTE ◦ DAY TO SECOND ◦ HOUR TO MINUTE ◦ HOUR TO SECOND ◦ MINUTE TO SECOND ◦ … RAPID POSTGRESQL LEARNING. 19
  • 20. INTERVALS ◦ Example: ◦ SET INTERVALSTYLE TO sql_standard ◦ Now try to type: SELECT INTERVAL ‘1 14:13:15’; ◦ RESULT: 1 14:13:15 ◦ SET INTERVALSTYLE TO postgres; ◦ Now try to type: SELECT INTERVAL ‘1 14:13:15’; ◦ RESULT: 1 day 14:13:15 ◦ SET INTERVALSTYLE TO iso_8601 ◦ Now try to type: SELECT INTERVAL ‘1 14:13:15’; ◦ RESULT: P1 DT 14H13M15S RAPID POSTGRESQL LEARNING. 20
  • 21. Boolean Data Type It holds three values ◦ True ◦ False ◦ Unknown ◦ TRUE [can be one of the following values] ◦ T ◦ True ◦ Yes ◦ Y ◦ On ◦ 1 ◦ False [can be one of the following values] ◦ False ◦ F ◦ NO ◦ Off ◦ 0 All Values except true and false should be between single quotes. RAPID POSTGRESQL LEARNING. 21
  • 22. Enumerated Data Types Enums are not predefined in postgresql, this means that Postgresql does not officially supports mysql’s ENUM data type. In order to achieve an Enumeration data type we must create our own. CREATE ENUM ◦ CREATE TYPE [type name] AS ENUM([data array]) ◦ For example: CREATE TYPE colors AS ENUM(‘red’,’blue’,’green’); HOW TO USE ◦ Just like another data types ◦ If an integer is: INTEGER variable name -> it would be: colors color ◦ [ENUM NAME] [variable name] Notes: ◦ Enums are case sensitive ◦ Sort is important in enums, for instance we must start with smallest values to the largest. For instance it is better to use ENUM(‘red’,’blue’,’green’) instead of ENUM(‘green’,’red’,’blue’) RAPID POSTGRESQL LEARNING. 22
  • 23. GEOMETRIC Data Types These data types are used to represent two dimensional objects ◦ Objects can be point, box, line, polygon, circle, path ◦ POINT ◦ To define a point (x,y) ◦ Example: (‘2,3’) or (‘(2,3)’) ◦ LINE ◦ To define a line using 2 points ◦ Example: ((x1,y1),(x2,y2)) ◦ PATH ◦ Represented by lists of connected points ◦ Example: (‘(2,3,4,5)’) or (‘*2,3,4,5+’) ◦ CIRCLE ◦ For a circle ◦ Example: (‘2,3,4’) -> in this example 4 is radius ◦ BOX ◦ To define a rectangle for instance : ((x1,y1),(x2,y2)) RAPID POSTGRESQL LEARNING. 23
  • 24. Arrays To define an array we add brackets after data type or we can use from array keyword. ◦ Test INTEGER[] ◦ Test INTEGER[][] ◦ Test INTEGER ARRAY ◦ In insert mode: ◦ In one-dimension: (‘,1,2,3,4,5,6,7-’) ◦ In two-dimension: (‘,1,2,3,4-,,5,6,7,8-’) RAPID POSTGRESQL LEARNING. 24
  • 25. End of part 1 This is the end of part 1 ◦ In the next part following topics will be covered ◦ Creating tables ◦ PRIMARY and FORIGN Keys ◦ Check Constraint ◦ NOT NULL Constraint ◦ UNIQUE Constraint ◦ DEFAULT values ◦ CASCADE ◦ CRUD ◦ INSERT ◦ UPDATE ◦ DELETE ◦ DELETE CASCADE ◦ ON DELETE CASCADE ◦ ON UPDATE CASCADE ◦ TRUNCATE ◦ QUERIES ◦ … RAPID POSTGRESQL LEARNING. 25