SlideShare a Scribd company logo
1 of 20
Postgresql
The world's most advanced
open source database
Agenda
History of Postgresql
Why Postgresql?
Basic operations in Postgresql
Advanced features of Postgresql
Postgresql vs Mysql
Code Snippets
Questions
History
1. PostgreSQL (or Postgres) began its life in 1986 as POSTGRES, a research project of the
University of California at Berkeley
2. Improved upon INGRES, an earlier prototype project also led by Stonebraker, principally
through the support of user defined types (or “domains”) with arbitrarily complex business
rules and other object-relational concepts
3. PostgreSQL started at version 6, for consistency with the Berkeley versioning
4. 7.* line was marked by enhancements and advanced developer-focused features.It was
the 8.* line, lasting from 2004 - 2009, brought features like Windowing functions and
Common Table Expressions
5. 9.* line represents a turning point for the PostgreSQL community, for a number of
reasons; principally, in the mind of most users, for 9.0’s introduction of simple, out of the
box streaming binary replication.
6. Stable release: 10.3 / 1 March 2018
Why Postgresql ?
1. PostgreSQL isn't just relational, it's object-relational.it's object-relational. This gives it
some advantages over other open source SQL databases like MySQL, MariaDB and
Firebird.
2. A fundamental characteristic of an object-relational database is support for user-
defined objects and their behaviors including data types, functions, operators,
domains and indexes.Complex data structures can be created, stored and retrieved
3. PostgreSQL provides for storing different network address types. The CIDR
(Classless Internet Domain Routing) data type follows the convention for IPv4 and
IPv6 network addresses
4. Because PostgreSQL is an object-relational database, arrays of values can be stored
for most of the existing data types. Do this by appending square brackets to the data
type specification for the column or by using the ARRAY expressionMySQL, MariaDB,
and Firebird do not have this capability
Why Postgresql ? (..continued)
5. PostgreSQL has long supported a variety of geometric data types such as points, lines,
circles, and polygons. The PATH data type is one of these. A path consists of multiple points
in a sequence and can be open .
6. PostgreSQL's JSON support lets you go schema-less in a SQL database.
7. The JSON data type enforces valid JSON which allows you to then make use of the
specialized JSON operators and functions built into PostgreSQL for querying and
manipulating the data. Also available is the JSONB type - a binary form of JSON
8. If PostgreSQL's extensive list of existing data types wasn't enough, you can use the
CREATE TYPE command to create new data types as composite, enumerated, range and
base
ROLES , PREVILIGES AND SCHEMA
PostgreSQL uses the roles concept to manage database access permissions. A role can be a
user or a group, depending on how you setup the role. A role that has login right is called user. A
role may be a member of other roles, which are known as groups.
CREATE ROLE role_name;
Role attributes The attributes of a database role define role’s privileges including login,
superuser, database creation, role creation, password, etc.
CREATE ROLE doe WITH PASSWORD 'pgSecpas1970' VALID UNTIL '2020-01-01';
Role membership
It is easier to manage roles as a group so that you can grant or revoke privileges from a group
as a whole. In PostgreSQL, you create a role that represents a group, and then grant
membership in the group role to individual user roles.
ROLES , PREVILIGES AND SCHEMA
Notice that PostgreSQL does not allow you to have circular membership loops, in which a role
is the member of another role and vice versa.
To restore the original privilege, you can use the following statement:
RESET ROLE;
Removing roles
You can use the DROP ROLE statement to remove a group role or user role.
DROP ROLE role_name;
Create Database
To create a new PostgreSQL database, you use CREATE DATABASE statement as shown
below:
CREATE DATABASE db_name
OWNER = role_name
TEMPLATE = template
ENCODING = encoding
LC_COLLATE = collate
LC_CTYPE = ctype
TABLESPACE = tablespace_name
CONNECTION LIMIT = max_concurrent_connection
Drop database
Once a database is no longer needed, you can delete it by using the DROP DATABASE
statement. The following illustrates the syntax of the DROP DATABASE statement:
DROP DATABASE [IF EXISTS] name;
The DROP DATABASE statement deletes catalog entries and data directory permanently. This
action cannot be undone so you have to use it with caution.
Only the database owner can execute the DROP DATABASE statement. In addition, you
cannot execute the DROP DATABASE statement if there is any active connection to the
database. You have to connect to another database e.g., postgresqlto execute the DROP
DATABASE statement.
PostgreSQL also provides a utility program named dropdbthat allows you to remove a
database. The dropdbprogram executes the DROP DATABASE statement behind the scenes
Create Table
To create a new PostgreSQL database, you use CREATE DATABASE statement as shown
below:
CREATE TABLE table_name (
column_name TYPE column_constraint,
table_constraint table_constraint
) INHERITS existing_table_name;
PostgreSQL table constraints
1. UNIQUE (column_list)– to force the value stored in the columns listed inside the parentheses to be unique.
2. PRIMARY KEY(column_list) – to define the primary key that consists of multiple columns.
3. CHECK (condition) – to check a condition when inserting or updating data.
4. REFERENCES– to constrain the value stored in the column that must exist in a column in another table.
Update table
To change the values of the columns in a table, you use the UPDATE statement. The following
illustrates the syntax of the UPDATE statement:
UPDATE table
SET column1 = value1,
column2 = value2 ,...
WHERE
condition;
You can also update data of a column from another column within the same table UPDATE
tableName SET column2 = column1;
Sometimes, you need to update data of a table based on values in another table. In this case,
you can use the PostgreSQL UPDATE join syntax as follows:
UPDATE A
SET A.c1 = expresion
FROM B
WHERE A.c2 = B.c2;
Delete table
To delete data from a table, you use PostgreSQL DELETE statement as shown below:
DELETE FROM table WHERE condition;
PostgreSQL - Schema
A schema is a named collection of tables. A schema can also contain views,
indexes, sequences, data types, operators, and functions. Schemas are analogous
to directories at the operating system level, except that schemas cannot be nested
CREATE SCHEMA name;
The basic syntax to create table in schema is as follows −
CREATE TABLE myschema.mytable (
...
);
To drop a schema use the command
DROP SCHEMA myschema CASCADE;
Important Queries
INSERT QUERY :
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
SELECT QUERY : SELECT column1, column2, columnN FROM table_name;
UPDATE QUERY :
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
DELETE QUERY:
DELETE FROM table_name
MVCC
1. One of the big selling points of Postgres is how it handles concurrency. The promise is
simple: reads never block writes and vice versa. Postgres achieves this via a mechanism
called Multi Version Concurrency Control. This technique is not unique to Postgres: there
are several databases that implement some form of MVCC including Oracle, Berkeley
DB, CouchDB and many more. Understanding how MVCC is implemented in Postgres is
important when designing highly concurrent apps on PostgreSQL
2. Every transaction in postgres gets a transaction ID called XID. This includes single one
statement transactions such as an insert, update or delete, as well as explicitely wrapping
a group of statements together via BEGIN - COMMIT. When a transaction starts, Postgres
increments an XID and assigns it to the current transaction. Postgres also stores
transaction information on every row in the system, which is used to determine whether a
row is visible to the transaction
PostgreSQL vs MySQL
PostgreSQL is ACID compliant from ground up and
ensures that all requirements are met.
MySQL is only ACID compliant when using InnoDB
and NDB Cluster Storage engines.
PostgreSQL is largely SQL compliant. The level of
conformance for each feature is clearly laid out in
Appendix D of the manual, and any deviations are
clearly documented in the “Reference” section of the
PostgreSQL manual.
MySQL is partially compliant on some of the versions
(e.g does not support CHECK constraints).
Overall, PostgreSQL performance is utilized best in
systems requiring execution of complex queries.
MySQL performs well in OLAP/OLTP systems when
only read speeds are required.
PostgreSQL has ROLES and inherited roles to set and
maintain permissions. PostgreSQL has native SSL
support for connections to encrypt client/server
communications. It also has Row Level Security.
MySQL implements security based on Access Control
Lists (ACLs) for all connections, queries, and other
operations that a user may attempt to perform
PostgreSQL tackles concurrency efficiently with its
MVCC implementation, which achieves very high
levels of concurrency.
MySQL only has MVCC support in InnoDB.
PostgreSQL vs MySQL
PostgreSQL tackles concurrency efficiently with its
MVCC implementation, which achieves very high levels
of concurrency.
MySQL only has MVCC support in InnoDB.
PostgreSQL supports JSON and other NoSQL features
like native XML support and key-value pairs with
HSTORE. It also supports indexing JSON data for
faster access.
MySQL has JSON data type support but no other
NoSQL feature. It does not support indexing for JSON.
Supports materialized views and temporary tables. Supports temporary tables but does not support
materialized views.
PostgreSQL supports Geospatial data via the PostGIS
extension. There are dedicated types and functions for
geospatial data
Geospatial data support is built in.
PostgreSQL has several features dedicated to
extensibility. It is possible to add new types, new
functions, new index types, etc.
No support for extensibility.
 https://github.com/NexThoughts/Postgress-Sample
References
● http://www.postgresqltutorial.com/
● https://devcenter.heroku.com/articles/postgresql-concurrency
● https://uk.dice.com/technews/advanced-features-postgresql/
Thank you

More Related Content

What's hot

Advanced Postgres Monitoring
Advanced Postgres MonitoringAdvanced Postgres Monitoring
Advanced Postgres Monitoring
Denish Patel
 

What's hot (20)

Introduction to PostgreSQL
Introduction to PostgreSQLIntroduction to PostgreSQL
Introduction to PostgreSQL
 
Getting started with postgresql
Getting started with postgresqlGetting started with postgresql
Getting started with postgresql
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
 
Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB
 
Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017
Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017
Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017
 
Linux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performanceLinux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performance
 
Advanced Postgres Monitoring
Advanced Postgres MonitoringAdvanced Postgres Monitoring
Advanced Postgres Monitoring
 
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQLTop 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
 
Elastic stack Presentation
Elastic stack PresentationElastic stack Presentation
Elastic stack Presentation
 
What is new in PostgreSQL 14?
What is new in PostgreSQL 14?What is new in PostgreSQL 14?
What is new in PostgreSQL 14?
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
PostgreSQL HA
PostgreSQL   HAPostgreSQL   HA
PostgreSQL HA
 
PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System Administrators
 
PostgreSQL and JDBC: striving for high performance
PostgreSQL and JDBC: striving for high performancePostgreSQL and JDBC: striving for high performance
PostgreSQL and JDBC: striving for high performance
 
[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
[pgday.Seoul 2022] PostgreSQL with Google Cloud
[pgday.Seoul 2022] PostgreSQL with Google Cloud[pgday.Seoul 2022] PostgreSQL with Google Cloud
[pgday.Seoul 2022] PostgreSQL with Google Cloud
 
Indexing with MongoDB
Indexing with MongoDBIndexing with MongoDB
Indexing with MongoDB
 
Mongodb basics and architecture
Mongodb basics and architectureMongodb basics and architecture
Mongodb basics and architecture
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
 

Similar to Postgresql

My sql with querys
My sql with querysMy sql with querys
My sql with querys
NIRMAL FELIX
 
PostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, StructuredPostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, Structured
priya951125
 

Similar to Postgresql (20)

Sq lite
Sq liteSq lite
Sq lite
 
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
 
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdf
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdfDBMS LAB FILE1 task 1 , task 2, task3 and many more.pdf
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdf
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
 
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...
 
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...
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
Android sq lite-chapter 22
Android sq lite-chapter 22Android sq lite-chapter 22
Android sq lite-chapter 22
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
PostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, StructuredPostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, Structured
 
7. SQL.pptx
7. SQL.pptx7. SQL.pptx
7. SQL.pptx
 
Big data technology unit 3
Big data technology unit 3Big data technology unit 3
Big data technology unit 3
 
MongoDB Knowledge share
MongoDB Knowledge shareMongoDB Knowledge share
MongoDB Knowledge share
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
My sql.ppt
My sql.pptMy sql.ppt
My sql.ppt
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
PostgreSQL Terminology
PostgreSQL TerminologyPostgreSQL Terminology
PostgreSQL Terminology
 
Database programming
Database programmingDatabase programming
Database programming
 
Introduction to Oracle Database.pptx
Introduction to Oracle Database.pptxIntroduction to Oracle Database.pptx
Introduction to Oracle Database.pptx
 
Jdbc 4.0 New Features And Enhancements
Jdbc 4.0 New Features And EnhancementsJdbc 4.0 New Features And Enhancements
Jdbc 4.0 New Features And Enhancements
 

More from NexThoughts Technologies

More from NexThoughts Technologies (20)

Alexa skill
Alexa skillAlexa skill
Alexa skill
 
GraalVM
GraalVMGraalVM
GraalVM
 
Docker & kubernetes
Docker & kubernetesDocker & kubernetes
Docker & kubernetes
 
Apache commons
Apache commonsApache commons
Apache commons
 
HazelCast
HazelCastHazelCast
HazelCast
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
Microservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & ReduxMicroservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & Redux
 
Swagger
SwaggerSwagger
Swagger
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Arango DB
Arango DBArango DB
Arango DB
 
Jython
JythonJython
Jython
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
 
Smart Contract samples
Smart Contract samplesSmart Contract samples
Smart Contract samples
 
My Doc of geth
My Doc of gethMy Doc of geth
My Doc of geth
 
Geth important commands
Geth important commandsGeth important commands
Geth important commands
 
Ethereum genesis
Ethereum genesisEthereum genesis
Ethereum genesis
 
Ethereum
EthereumEthereum
Ethereum
 
Springboot Microservices
Springboot MicroservicesSpringboot Microservices
Springboot Microservices
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 
Google authentication
Google authenticationGoogle authentication
Google authentication
 

Recently uploaded

Recently uploaded (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

Postgresql

  • 1. Postgresql The world's most advanced open source database
  • 2. Agenda History of Postgresql Why Postgresql? Basic operations in Postgresql Advanced features of Postgresql Postgresql vs Mysql Code Snippets Questions
  • 3. History 1. PostgreSQL (or Postgres) began its life in 1986 as POSTGRES, a research project of the University of California at Berkeley 2. Improved upon INGRES, an earlier prototype project also led by Stonebraker, principally through the support of user defined types (or “domains”) with arbitrarily complex business rules and other object-relational concepts 3. PostgreSQL started at version 6, for consistency with the Berkeley versioning 4. 7.* line was marked by enhancements and advanced developer-focused features.It was the 8.* line, lasting from 2004 - 2009, brought features like Windowing functions and Common Table Expressions 5. 9.* line represents a turning point for the PostgreSQL community, for a number of reasons; principally, in the mind of most users, for 9.0’s introduction of simple, out of the box streaming binary replication. 6. Stable release: 10.3 / 1 March 2018
  • 4. Why Postgresql ? 1. PostgreSQL isn't just relational, it's object-relational.it's object-relational. This gives it some advantages over other open source SQL databases like MySQL, MariaDB and Firebird. 2. A fundamental characteristic of an object-relational database is support for user- defined objects and their behaviors including data types, functions, operators, domains and indexes.Complex data structures can be created, stored and retrieved 3. PostgreSQL provides for storing different network address types. The CIDR (Classless Internet Domain Routing) data type follows the convention for IPv4 and IPv6 network addresses 4. Because PostgreSQL is an object-relational database, arrays of values can be stored for most of the existing data types. Do this by appending square brackets to the data type specification for the column or by using the ARRAY expressionMySQL, MariaDB, and Firebird do not have this capability
  • 5. Why Postgresql ? (..continued) 5. PostgreSQL has long supported a variety of geometric data types such as points, lines, circles, and polygons. The PATH data type is one of these. A path consists of multiple points in a sequence and can be open . 6. PostgreSQL's JSON support lets you go schema-less in a SQL database. 7. The JSON data type enforces valid JSON which allows you to then make use of the specialized JSON operators and functions built into PostgreSQL for querying and manipulating the data. Also available is the JSONB type - a binary form of JSON 8. If PostgreSQL's extensive list of existing data types wasn't enough, you can use the CREATE TYPE command to create new data types as composite, enumerated, range and base
  • 6. ROLES , PREVILIGES AND SCHEMA PostgreSQL uses the roles concept to manage database access permissions. A role can be a user or a group, depending on how you setup the role. A role that has login right is called user. A role may be a member of other roles, which are known as groups. CREATE ROLE role_name; Role attributes The attributes of a database role define role’s privileges including login, superuser, database creation, role creation, password, etc. CREATE ROLE doe WITH PASSWORD 'pgSecpas1970' VALID UNTIL '2020-01-01'; Role membership It is easier to manage roles as a group so that you can grant or revoke privileges from a group as a whole. In PostgreSQL, you create a role that represents a group, and then grant membership in the group role to individual user roles.
  • 7. ROLES , PREVILIGES AND SCHEMA Notice that PostgreSQL does not allow you to have circular membership loops, in which a role is the member of another role and vice versa. To restore the original privilege, you can use the following statement: RESET ROLE; Removing roles You can use the DROP ROLE statement to remove a group role or user role. DROP ROLE role_name;
  • 8. Create Database To create a new PostgreSQL database, you use CREATE DATABASE statement as shown below: CREATE DATABASE db_name OWNER = role_name TEMPLATE = template ENCODING = encoding LC_COLLATE = collate LC_CTYPE = ctype TABLESPACE = tablespace_name CONNECTION LIMIT = max_concurrent_connection
  • 9. Drop database Once a database is no longer needed, you can delete it by using the DROP DATABASE statement. The following illustrates the syntax of the DROP DATABASE statement: DROP DATABASE [IF EXISTS] name; The DROP DATABASE statement deletes catalog entries and data directory permanently. This action cannot be undone so you have to use it with caution. Only the database owner can execute the DROP DATABASE statement. In addition, you cannot execute the DROP DATABASE statement if there is any active connection to the database. You have to connect to another database e.g., postgresqlto execute the DROP DATABASE statement. PostgreSQL also provides a utility program named dropdbthat allows you to remove a database. The dropdbprogram executes the DROP DATABASE statement behind the scenes
  • 10. Create Table To create a new PostgreSQL database, you use CREATE DATABASE statement as shown below: CREATE TABLE table_name ( column_name TYPE column_constraint, table_constraint table_constraint ) INHERITS existing_table_name; PostgreSQL table constraints 1. UNIQUE (column_list)– to force the value stored in the columns listed inside the parentheses to be unique. 2. PRIMARY KEY(column_list) – to define the primary key that consists of multiple columns. 3. CHECK (condition) – to check a condition when inserting or updating data. 4. REFERENCES– to constrain the value stored in the column that must exist in a column in another table.
  • 11. Update table To change the values of the columns in a table, you use the UPDATE statement. The following illustrates the syntax of the UPDATE statement: UPDATE table SET column1 = value1, column2 = value2 ,... WHERE condition; You can also update data of a column from another column within the same table UPDATE tableName SET column2 = column1; Sometimes, you need to update data of a table based on values in another table. In this case, you can use the PostgreSQL UPDATE join syntax as follows: UPDATE A SET A.c1 = expresion FROM B WHERE A.c2 = B.c2;
  • 12. Delete table To delete data from a table, you use PostgreSQL DELETE statement as shown below: DELETE FROM table WHERE condition;
  • 13. PostgreSQL - Schema A schema is a named collection of tables. A schema can also contain views, indexes, sequences, data types, operators, and functions. Schemas are analogous to directories at the operating system level, except that schemas cannot be nested CREATE SCHEMA name; The basic syntax to create table in schema is as follows − CREATE TABLE myschema.mytable ( ... ); To drop a schema use the command DROP SCHEMA myschema CASCADE;
  • 14. Important Queries INSERT QUERY : INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1, value2, value3,...valueN); INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN); SELECT QUERY : SELECT column1, column2, columnN FROM table_name; UPDATE QUERY : UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition]; DELETE QUERY: DELETE FROM table_name
  • 15. MVCC 1. One of the big selling points of Postgres is how it handles concurrency. The promise is simple: reads never block writes and vice versa. Postgres achieves this via a mechanism called Multi Version Concurrency Control. This technique is not unique to Postgres: there are several databases that implement some form of MVCC including Oracle, Berkeley DB, CouchDB and many more. Understanding how MVCC is implemented in Postgres is important when designing highly concurrent apps on PostgreSQL 2. Every transaction in postgres gets a transaction ID called XID. This includes single one statement transactions such as an insert, update or delete, as well as explicitely wrapping a group of statements together via BEGIN - COMMIT. When a transaction starts, Postgres increments an XID and assigns it to the current transaction. Postgres also stores transaction information on every row in the system, which is used to determine whether a row is visible to the transaction
  • 16. PostgreSQL vs MySQL PostgreSQL is ACID compliant from ground up and ensures that all requirements are met. MySQL is only ACID compliant when using InnoDB and NDB Cluster Storage engines. PostgreSQL is largely SQL compliant. The level of conformance for each feature is clearly laid out in Appendix D of the manual, and any deviations are clearly documented in the “Reference” section of the PostgreSQL manual. MySQL is partially compliant on some of the versions (e.g does not support CHECK constraints). Overall, PostgreSQL performance is utilized best in systems requiring execution of complex queries. MySQL performs well in OLAP/OLTP systems when only read speeds are required. PostgreSQL has ROLES and inherited roles to set and maintain permissions. PostgreSQL has native SSL support for connections to encrypt client/server communications. It also has Row Level Security. MySQL implements security based on Access Control Lists (ACLs) for all connections, queries, and other operations that a user may attempt to perform PostgreSQL tackles concurrency efficiently with its MVCC implementation, which achieves very high levels of concurrency. MySQL only has MVCC support in InnoDB.
  • 17. PostgreSQL vs MySQL PostgreSQL tackles concurrency efficiently with its MVCC implementation, which achieves very high levels of concurrency. MySQL only has MVCC support in InnoDB. PostgreSQL supports JSON and other NoSQL features like native XML support and key-value pairs with HSTORE. It also supports indexing JSON data for faster access. MySQL has JSON data type support but no other NoSQL feature. It does not support indexing for JSON. Supports materialized views and temporary tables. Supports temporary tables but does not support materialized views. PostgreSQL supports Geospatial data via the PostGIS extension. There are dedicated types and functions for geospatial data Geospatial data support is built in. PostgreSQL has several features dedicated to extensibility. It is possible to add new types, new functions, new index types, etc. No support for extensibility.