0% found this document useful (0 votes)
137 views35 pages

Ebook Learn To Use PostgreSQL For Real

The webinar will provide an introduction to using PostgreSQL for real work. It will cover setting up PostgreSQL and installing it, creating databases and users, exploring the database structure in an IDE, using views, materialized views, functions, procedures, triggers, sequences, common table expressions, and other PostgreSQL features. The webinar is aimed at those new to PostgreSQL who want to learn how to use it for their work or projects.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
137 views35 pages

Ebook Learn To Use PostgreSQL For Real

The webinar will provide an introduction to using PostgreSQL for real work. It will cover setting up PostgreSQL and installing it, creating databases and users, exploring the database structure in an IDE, using views, materialized views, functions, procedures, triggers, sequences, common table expressions, and other PostgreSQL features. The webinar is aimed at those new to PostgreSQL who want to learn how to use it for their work or projects.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 35

Learn to Use PostgreSQL, for Real!

‹#›

WWW.PENTALOG.COM
$ whoami;

UserName: Boris POPOVSCHI

Position: GoLang Developer

Achievements: ElasticSearch
contributor, ZF2 contributor.

Nice to know before asking questions:


Karate Practitioner for 14 years;)

‹#›

WWW.PENTALOG.COM
Hey, what about
Postgres?
PostgreSQL user for 5 years.

MySQL hater.

SQL evangelist.

NoSQL…… it’s just fine.

‹#›

WWW.PENTALOG.COM
What this webinar
is NOT about
Database comparison.

SQL VS NoSQL HollyWars.

PostgreSQL queries optimization.

PostgreSQL insides.

‹#›

WWW.PENTALOG.COM
What this webinar
IS about
Postgres Users/Roles model.

PostgreSQL DB structure.

DataGrip IDE.

Postgres Features.

‹#›

WWW.PENTALOG.COM
Ready player user one!

First of all: Let's Install PostgreSQL:

$ apt-get install postgresql postgresql-client postgresql-contrib;


$ psql;

For MacOs: https://postgresapp.com/

$ psql: error?))

‹#›

WWW.PENTALOG.COM
We need a DataBase!

Download the dump:

http://www.postgresqltutorial.com/postgresql-sample-database/

Create the DataBase:

$ CREATEDB p5test;
OR
$ psql;
CREATE DATABASE p5test;

‹#›

WWW.PENTALOG.COM
Users, users, users!

Let’s take a quick look at the user auth:

$ CREATE ROLE p5user WITH LOGIN PASSWORD 'password';


OR
$ CREATE USER p5user WITH PASSWORD 'password';

Change the owner of the DB to a new user:

$ ALTER DATABASE p5test owner to p5user;

‹#›

WWW.PENTALOG.COM
It’s Data time!

Let’s check what we have:

$ psql;
$ \l
$ \c p5test;
$ \dt

Time to fill up the DB:

$ pg_restore -d p5test pathToDownloadedDump/dvdrental


$ psql -d p5test;
$ \dt

‹#›

WWW.PENTALOG.COM
SetUp DataGrip!

Open Database tool in your JetBrains IDE:

Open Database tool in your JetBrains IDE:

‹#›

WWW.PENTALOG.COM
DB structure!

1)All databases on given port and


host

2)Schemas - abstract
containers for tables and other
relations.

3)Roles: default, and those


which we have
just created.

‹#›

WWW.PENTALOG.COM
First steps!

SELECT t.* FROM public.address t LIMIT 5;

[42501] ERROR: permission denied for


relation address

$ psql;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO p5user;

First rule in DB security:


You should revoke all rights from users, and grant them only
when needed.

‹#›

WWW.PENTALOG.COM
Views!
A view is a named query
that provides another way to
present data in the database
tables. A view is defined
based on one or more
tables, which are known as
base tables. When you
create a view, you basically
create a query and assign it
a name, therefore a view is
useful for wrapping a
commonly used complex
query.

‹#›

WWW.PENTALOG.COM
Views, under the hood!

$ psql;
\d+ actor_info;
View definition:
SELECT a.actor_id,
a.first_name,
a.last_name,
group_concat(DISTINCT (c.name::text || ': '::text) || (( SELECT group_concat(f.title::text) AS
group_concat
FROM film f
JOIN film_category fc_1 ON f.film_id = fc_1.film_id
JOIN film_actor fa_1 ON f.film_id = fa_1.film_id
WHERE fc_1.category_id = c.category_id AND fa_1.actor_id = a.actor_id
GROUP BY fa_1.actor_id))) AS film_info
FROM actor a
LEFT JOIN film_actor fa ON a.actor_id = fa.actor_id
LEFT JOIN film_category fc ON fa.film_id = fc.film_id
LEFT JOIN category c ON fc.category_id = c.category_id
GROUP BY a.actor_id, a.first_name, a.last_name;

‹#›

WWW.PENTALOG.COM
MATERIALIZED VIEWS!
CREATE MATERIALIZED VIEW actor_info_mv AS
SELECT a.actor_id,
a.first_name,
a.last_name,
group_concat(DISTINCT (c.name::text || ': '::text) || (( SELECT group_concat(f.title::text) AS
group_concat
FROM film f
JOIN film_category fc_1 ON f.film_id = fc_1.film_id
JOIN film_actor fa_1 ON f.film_id = fa_1.film_id
WHERE fc_1.category_id = c.category_id AND fa_1.actor_id = a.actor_id
GROUP BY fa_1.actor_id))) AS film_info
FROM actor a
LEFT JOIN film_actor fa ON a.actor_id = fa.actor_id
LEFT JOIN film_category fc ON fa.film_id = fc.film_id
LEFT JOIN category c ON fc.category_id = c.category_id
GROUP BY a.actor_id, a.first_name, a.last_name;

‹#›

WWW.PENTALOG.COM
MATERIALIZED VIEWS!

A materialized view is defined as a table which is actually physically


stored on disk, but is really just a view of other database tables

CREATE MATERIALIZED VIEW actor_info_mv AS


SELECT a.actor_id,
a.first_name,
a.last_name,
group_concat(DISTINCT (c.name::text || ': '::text) || (( SELECT group_concat(f.title::text) AS
group_concat
FROM film f
JOIN film_category fc_1 ON f.film_id = fc_1.film_id
JOIN film_actor fa_1 ON f.film_id = fa_1.film_id
WHERE fc_1.category_id = c.category_id AND fa_1.actor_id = a.actor_id
GROUP BY fa_1.actor_id))) AS film_info
FROM actor a
LEFT JOIN film_actor fa ON a.actor_id = fa.actor_id
LEFT JOIN film_category fc ON fa.film_id = fc.film_id
LEFT JOIN category c ON fc.category_id = c.category_id
GROUP BY a.actor_id, a.first_name, a.last_name;

‹#›

WWW.PENTALOG.COM
MATERIALIZED VS SIMPLE VIEWS!

VIEW:

MATERIALIZED VIEW:

‹#›

WWW.PENTALOG.COM
WHY, WHY SIMPLE VIEW EXISTS?

When you need data to be very precise


and fresh, you use VIEW because it
executes a query on every request.

When the speed is more important then data


freshness, use Materialized View.

‹#›

WWW.PENTALOG.COM
FUNCTIONS & PROCEDURES!
PostgreSQL functions, also known as Stored
Procedures, allow you to carry out operations that
would normally take several queries and round trips
in a single function within the database
Functions can be created in any language of your
choice like SQL, PL/pgSQL, C, Python, etc.
CREATE OR REPLACE FUNCTION total_films ()
RETURNS integer AS $total$
declare
total integer;
BEGIN
SELECT count(*) into total FROM film;
RETURN total;
END;
$total$ LANGUAGE plpgsql;
SELECT total_films();
‹#›

WWW.PENTALOG.COM
TRIGGERS!
A PostgreSQL trigger is a function invoked automatically
whenever an event e.g., insert, update, or delete occurred

create function last_updated()


returns trigger
language plpgsql
as $$
BEGIN
NEW.last_update = CURRENT_TIMESTAMP;
RETURN NEW;
END
$$;

alter function last_updated()


owner to postgres;

‹#›

WWW.PENTALOG.COM
TRIGGERS!

‹#›

WWW.PENTALOG.COM
Aggregations!

Aggregations - unique Postgres features with whom we can create


custom aggregations like: sum, max, min.

‹#›

WWW.PENTALOG.COM
Aggregations!

SELECT group_concat(description) AS monolith_text FROM film;

Result:
A Fateful Reflection of a Moose And a Husband who must
Overcome a Monkey in Nigeria, A Epic Drama of a Cat And a
Explorer who must Redeem a Moose……

The concatenated descriptions text from each row.

‹#›

WWW.PENTALOG.COM
SEQUENCE!

Generating unique numeric identifiers,


Sequences are similar, but not identical, to the AUTO_INCREMENT
concept in MySQL.

‹#›

WWW.PENTALOG.COM
CTE!

One of the coolest things in Postgres are CTE, "common table


expressions", also referred to as WITH clauses.

The general idea is that it allows you to create something, somewhat


equivalent to a view that only exists during that transaction. You can
create multiple of these which then allow for clear building blocks and
make it simple to follow what you’re doing.

‹#›

WWW.PENTALOG.COM
CTE in action!

WITH updated_rows AS (
UPDATE film
SET release_year = 2006
WHERE release_year = 2005
RETURNING film_id
),
rated_films AS (
SELECT * FROM film WHERE rental_rate > 4
)
SELECT f.title, f.release_year FROM rated_films f INNER JOIN
updated_rows ur ON f.film_id = ur.film_id;

‹#›

WWW.PENTALOG.COM
RECURSIVE CTE!

A recursive query is a query that refers to a recursive CTE. The


recursive queries are useful in many situations like, for instance,
querying hierarchical data such as an organizational structure, a bill of
materials, etc.
WITH RECURSIVE loop AS (
SELECT 0 AS i

UNION

SELECT i + 1
FROM loop
WHERE i < 10
)
SELECT i FROM loop;

‹#›

WWW.PENTALOG.COM
RECURSIVE FOR REAL!
Unfortunately, in our test DB we don’t have
hierarchical data, therefore we will create a new table:

INSERT INTO geo


CREATE TABLE geo (
(id, parent_id, name)
id int not null primary key,
VALUES
parent_id int references geo(id),
(1, null, 'Earth'),
name varchar(1000)
(2, 1, 'Euroasia'),
);
(3, 1, 'North America'),
(4, 2, 'Europe'),
(5, 4, 'Russia'),
(6, 4, 'Germany'),
(7, 5, 'Moscow'),
(8, 5, 'Sankt-Petersburg'),
(9, 6, 'Berlin');

‹#›

WWW.PENTALOG.COM
RECURSIVE FOR REAL!
Let’s try some real-world examples:

WITH RECURSIVE r AS (
SELECT id, parent_id, name
FROM geo
WHERE parent_id = 4
UNION ALL
SELECT geo.id, geo.parent_id, geo.name
FROM geo
JOIN r
ON geo.parent_id = r.id
)
SELECT * FROM r;

‹#›

WWW.PENTALOG.COM
RECURSIVE FOR REAL!
Here is the output:

‹#›

WWW.PENTALOG.COM
RECURSIVE FOR REAL!
Let’s add some extra information, for example the item level

WITH RECURSIVE r AS (
SELECT id, parent_id, name, 1 AS level
FROM geo
WHERE parent_id = 4
UNION ALL
SELECT geo.id, geo.parent_id, geo.name, level +1 AS level
FROM geo
JOIN r
ON geo.parent_id = r.id
)
SELECT * FROM r;

‹#›

WWW.PENTALOG.COM
RECURSIVE FOR REAL!
And the new output will be:

‹#›

WWW.PENTALOG.COM
What’s next?
- Check the official docs
- Try to use it
- Practice, practice, practice
- Wait for my next webinar(query optimization, monitoring,
logging)

‹#›

WWW.PENTALOG.COM
‹#›

WWW.PENTALOG.COM
THANK YOU!

You might also like