SlideShare a Scribd company logo
4
Most read
6
Most read
20
Most read
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

PPTX
PostgreSQL Database Slides
metsarin
 
PDF
Get to know PostgreSQL!
OddbjĂžrn Steffensen
 
PPTX
Getting started with postgresql
botsplash.com
 
PDF
Postgresql tutorial
Ashoka Vanjare
 
PPTX
PostgreSQL- An Introduction
Smita Prasad
 
PDF
Postgresql database administration volume 1
Federico Campoli
 
PDF
PostgreSQL Tutorial For Beginners | Edureka
Edureka!
 
PPTX
PostGreSQL Performance Tuning
Maven Logix
 
PostgreSQL Database Slides
metsarin
 
Get to know PostgreSQL!
OddbjĂžrn Steffensen
 
Getting started with postgresql
botsplash.com
 
Postgresql tutorial
Ashoka Vanjare
 
PostgreSQL- An Introduction
Smita Prasad
 
Postgresql database administration volume 1
Federico Campoli
 
PostgreSQL Tutorial For Beginners | Edureka
Edureka!
 
PostGreSQL Performance Tuning
Maven Logix
 

What's hot (20)

ODP
Introduction to PostgreSQL
Jim Mlodgenski
 
PPT
Sql ppt
Anuja Lad
 
PPTX
Introduction to PostgreSQL
Joel Brewer
 
PDF
Cassandra Database
YounesCharfaoui
 
PPT
MYSQL.ppt
webhostingguy
 
PDF
MongoDB Fundamentals
MongoDB
 
ZIP
NoSQL databases
Harri Kauhanen
 
PPTX
SUBQUERIES.pptx
RenugadeviR5
 
PDF
MySQL for beginners
Saeid Zebardast
 
ODP
OpenGurukul : Database : PostgreSQL
Open Gurukul
 
PPTX
Introduction to SQL
Ehsan Hamzei
 
PPTX
MongoDB - Aggregation Pipeline
Jason Terpko
 
PPTX
SQL Basics
Hammad Rasheed
 
PPTX
Sql and Sql commands
Knowledge Center Computer
 
PDF
Mastering PostgreSQL Administration
EDB
 
PPTX
Introduction to Redis
Maarten Smeets
 
PDF
Looking ahead at PostgreSQL 15
Jonathan Katz
 
PPTX
MongoDB presentation
Hyphen Call
 
PDF
PostgreSQL Deep Internal
EXEM
 
PDF
MySQL Tutorial For Beginners | Relational Database Management System | MySQL ...
Edureka!
 
Introduction to PostgreSQL
Jim Mlodgenski
 
Sql ppt
Anuja Lad
 
Introduction to PostgreSQL
Joel Brewer
 
Cassandra Database
YounesCharfaoui
 
MYSQL.ppt
webhostingguy
 
MongoDB Fundamentals
MongoDB
 
NoSQL databases
Harri Kauhanen
 
SUBQUERIES.pptx
RenugadeviR5
 
MySQL for beginners
Saeid Zebardast
 
OpenGurukul : Database : PostgreSQL
Open Gurukul
 
Introduction to SQL
Ehsan Hamzei
 
MongoDB - Aggregation Pipeline
Jason Terpko
 
SQL Basics
Hammad Rasheed
 
Sql and Sql commands
Knowledge Center Computer
 
Mastering PostgreSQL Administration
EDB
 
Introduction to Redis
Maarten Smeets
 
Looking ahead at PostgreSQL 15
Jonathan Katz
 
MongoDB presentation
Hyphen Call
 
PostgreSQL Deep Internal
EXEM
 
MySQL Tutorial For Beginners | Relational Database Management System | MySQL ...
Edureka!
 
Ad

Similar to Postgresql (20)

PDF
PostgreSQL - Case Study
S.Shayan Daneshvar
 
PPT
Object Relational Database Management System
Amar Myana
 
PDF
An evening with Postgresql
Joshua Drake
 
PPTX
Chjkkkkkkkkkkkkkkkkkjjjjjjjjjjjjjjjjjjjjjjjjjj01_The Basics.pptx
MhmdMk10
 
PPTX
PostgreSQL - Object Relational Database
Mubashar Iqbal
 
PDF
0292-introduction-postgresql.pdf
Mustafa Keskin
 
KEY
PostgreSQL
Reuven Lerner
 
PPT
A brief introduction to PostgreSQL
Vu Hung Nguyen
 
PDF
Don't panic! - Postgres introduction
Federico Campoli
 
PPTX
PostgreSQL Terminology
Showmax Engineering
 
PDF
Migrating to postgresql
botsplash.com
 
PDF
PostgreSQL Prologue
Md. Golam Hossain
 
PPTX
Module 5 Web Programing Setting Up Postgres.pptx
earningmoney9595
 
PDF
Beyond Postgres: Interesting Projects, Tools and forks
Sameer Kumar
 
PDF
PostgreSQL : Introduction
Open Source School
 
PDF
PostgreSQL versus MySQL - What Are The Real Differences
All Things Open
 
PPTX
Postgres level up
Fabio Telles Rodriguez
 
PDF
Postgresql quick guide
Ashoka Vanjare
 
PDF
postgresql 16.3(latest version) 2024-25.pdf
ayaankim007
 
PDF
Pg big fast ugly acid
Federico Campoli
 
PostgreSQL - Case Study
S.Shayan Daneshvar
 
Object Relational Database Management System
Amar Myana
 
An evening with Postgresql
Joshua Drake
 
Chjkkkkkkkkkkkkkkkkkjjjjjjjjjjjjjjjjjjjjjjjjjj01_The Basics.pptx
MhmdMk10
 
PostgreSQL - Object Relational Database
Mubashar Iqbal
 
0292-introduction-postgresql.pdf
Mustafa Keskin
 
PostgreSQL
Reuven Lerner
 
A brief introduction to PostgreSQL
Vu Hung Nguyen
 
Don't panic! - Postgres introduction
Federico Campoli
 
PostgreSQL Terminology
Showmax Engineering
 
Migrating to postgresql
botsplash.com
 
PostgreSQL Prologue
Md. Golam Hossain
 
Module 5 Web Programing Setting Up Postgres.pptx
earningmoney9595
 
Beyond Postgres: Interesting Projects, Tools and forks
Sameer Kumar
 
PostgreSQL : Introduction
Open Source School
 
PostgreSQL versus MySQL - What Are The Real Differences
All Things Open
 
Postgres level up
Fabio Telles Rodriguez
 
Postgresql quick guide
Ashoka Vanjare
 
postgresql 16.3(latest version) 2024-25.pdf
ayaankim007
 
Pg big fast ugly acid
Federico Campoli
 
Ad

More from NexThoughts Technologies (20)

PDF
Alexa skill
NexThoughts Technologies
 
PDF
Docker & kubernetes
NexThoughts Technologies
 
PDF
Apache commons
NexThoughts Technologies
 
PDF
HazelCast
NexThoughts Technologies
 
PPTX
MySQL Pro
NexThoughts Technologies
 
PDF
Microservice Architecture using Spring Boot with React & Redux
NexThoughts Technologies
 
PDF
Solid Principles
NexThoughts Technologies
 
PDF
Arango DB
NexThoughts Technologies
 
PDF
Introduction to TypeScript
NexThoughts Technologies
 
PDF
Smart Contract samples
NexThoughts Technologies
 
PDF
My Doc of geth
NexThoughts Technologies
 
PDF
Geth important commands
NexThoughts Technologies
 
PDF
Ethereum genesis
NexThoughts Technologies
 
PPTX
Springboot Microservices
NexThoughts Technologies
 
PDF
An Introduction to Redux
NexThoughts Technologies
 
PPTX
Google authentication
NexThoughts Technologies
 
Docker & kubernetes
NexThoughts Technologies
 
Apache commons
NexThoughts Technologies
 
Microservice Architecture using Spring Boot with React & Redux
NexThoughts Technologies
 
Solid Principles
NexThoughts Technologies
 
Introduction to TypeScript
NexThoughts Technologies
 
Smart Contract samples
NexThoughts Technologies
 
My Doc of geth
NexThoughts Technologies
 
Geth important commands
NexThoughts Technologies
 
Ethereum genesis
NexThoughts Technologies
 
Springboot Microservices
NexThoughts Technologies
 
An Introduction to Redux
NexThoughts Technologies
 
Google authentication
NexThoughts Technologies
 

Recently uploaded (20)

PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
PPTX
Comunidade Salesforce SĂŁo Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira JĂșnior
 
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Captain IT
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PDF
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Orbitly Pitch DeckA Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
GYTPOL If You Give a Hacker a Host
linda296484
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Doc9.....................................
SofiaCollazos
 
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
Comunidade Salesforce SĂŁo Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira JĂșnior
 
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Captain IT
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Software Development Methodologies in 2025
KodekX
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Orbitly Pitch DeckA Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
GYTPOL If You Give a Hacker a Host
linda296484
 

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.