PostgreSQL (or Postgres) began its life in 1986 as POSTGRES, a research project of the University of California at Berkeley.
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. 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.