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
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
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
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
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
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
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
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
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
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
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
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 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
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
– 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
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
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
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
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
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