Skip to content
This repository was archived by the owner on Oct 11, 2024. It is now read-only.

Commit f047948

Browse files
committed
Added rollout.sh + made all script executable on their own
1 parent ed370bb commit f047948

27 files changed

+82
-12
lines changed

00_create_database.sql

-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,3 @@ In case of multiple environments (enterprize scenario) it is highly recommended
1515
minimize chances of executing things on the wrong DB - e.g. dev_app1_db, prod_app1_db.
1616
1717
*/
18-
19-
-- assuming "psql" as execution environment here
20-
\c pg_features_demo

01_create_role.sql

+4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
-- assuming "psql" as execution environment here
2+
\c pg_features_demo
3+
14
-- Create a role (a.k.a: user, login, group) that is allowed to log in.
25
-- NB! By default roles are allowed to connect to all databases of a cluster. This does not mean that they can automatically access tables though.
36
-- But if this is not wanted, per DB connections can be set up with "REVOKE/GRANT CONNECT ON DATABASE"
47

58
CREATE ROLE demorole WITH LOGIN; -- create a normal(unprivileged) user
9+
-- NB! when seeing ERRORs during re-rollout it's normal here as role are "global" objects
610

711
-- Will later be set up to only allow selecting data
812
CREATE ROLE demorole_ro WITH LOGIN;

02_create_schema.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
RESET ROLE;
1+
\c pg_features_demo
2+
23
/*
34
Schemas in Postgres are basically "namespaces", allowing tables with same names within one DB, if schema names differ.
45
By default there is always one "public" schema pre-created with every database, but for bigger applications it is usually

02_search_path.sql

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
\c pg_features_demo
2+
13
/*
24
35
Important concept tied to schemas is "search_path". Basically it's a priority list of schemas,

03_make_public_schema_secure.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
\c pg_features_demo
2+
13
/*
24
35
For sensitive environments it is recommended to avoid creating object in the "public" schema or even better to secure
@@ -17,4 +19,3 @@ GRANT USAGE ON SCHEMA public TO demorole_ro;
1719
ALTER DEFAULT PRIVILEGES
1820
IN SCHEMA public
1921
GRANT SELECT ON TABLES TO demorole_ro;
20-

04_alter_role.sql

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1+
\c pg_features_demo
2+
13
-- It's possible to set global, or per user or per user/db settings. Most used such settings are search_path and statement_timeout.
24
-- Changes are effective with next login.
35

4-
RESET ROLE;
5-
66
ALTER ROLE demorole IN DATABASE pg_features_demo SET search_path TO public, banking;
77
ALTER ROLE demorole_ro IN DATABASE pg_features_demo SET search_path TO public, banking;
88
ALTER ROLE demorole_ro IN DATABASE pg_features_demo SET statement_timeout TO '5s';
9-
10-
SET ROLE TO demorole;
11-

05_create_table.sql

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
\c pg_features_demo
2+
13
-- assume "application" role
24
-- it's a good practice to own all objects by one "application" role, so that changes could be done using the same role,
35
-- not requiring the samewhat dangerous "superuser".

06_create_table_options.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
Other ways of creating tables are:
36
1) using LIKE to use existing tables as a templates and selecting (or leaving out) some constraints/checks/indexes

07_alter_table.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
ALTER TABLE is mostly commonly used to:
36
1) add/drop/rename columns

09_data_type_showcase.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
CREATE TABLE public.main_datatypes (
25
/* serials aka sequences */
36
id bigserial PRIMARY KEY, -- serial/bigserial corresponds to int4/int8 and will just auto-attach a DEFAULT sequence

10_views.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
Views allow application layering and can be also used for securing data. They behave the same as tables for GRANT privileges and
36
don't normally incur any performance hit when selecting.

15_materialized_views.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
-- Materialized view is a "point in time" copy of the select statement and needs explicit refreshing
25
-- In other aspects the mat.view acts like a normal table - one can create indexes on it
36

20_stored_procedures.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
PL/pgSQL is a specialized programming language that allows to query data and provides common features like variables, comparisons,
36
branching (if/else), error handling, calling other functions.

25_table_using_sproc.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
-- setting a function as default value
25
-- dropping the function with "cascade" will also remove the "default" declaration
36
CREATE TABLE func_as_def_param(

30_simple_trigger.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
Triggers function similar to other RDBMSs - first you declare a "trigger function" and then the trigger itself on a specific table. Thus
36
one fuction can be used for X tables. Trigger functions are almost like normal PL/pgSQL function but they can only return "trigger"

32_event_trigger.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
CREATE OR REPLACE FUNCTION rewrite_date_check() RETURNS event_trigger AS
25
$$
36
BEGIN

40_custom_types.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
Postgres is a the most extensible general purpose RDBMS out there and one of the most used extensibility feature is creating custom
36
types that can be thought of as "classes" in the object-oriented programming world. Created types can then be used as built-in

41_enum_types.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
Special subtype of custom types are ENUMs. Concept is the same as in other programming languages, enabling to assing a nice label
36
to some internally stored code value, helping with clarity and also performance for bigger data amounts.

45_domains.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
Domains allow extending data types with checks so that you don't have to change multiple single checks when validation requirements change.
36
*/

50_row_level_security.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
Row Level Security (RLS) is quite a new feature (9.5+) and allows having tables where it is guaranteed that all unprivileged users
36
will see only rows that they're allowed to see based on some filter condition. Needs enabling per table.

60_transaction_management.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
Transaction management is a complex topic but most importantly one should know that Postgres has 3 different isolation levels:
36

70_basic_analytics.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
Postgres has rich analytics support. Analytics can mean a lot of things but here it means there are functions that in addition to
36
normal aggregates (AVG, SUM, etc) can also compare individual rows and row groupings! Thus enabling answering questions like -

80_advanced_indexing.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
36
For good performance it's essential that frequent queries take advantage of indexes.

81_string_processing.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
Postgres has excellent text/string processing capabilities, including support for regular expressions.
36
Below some of the most useful functions and text handling techniques.

82_arrays.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
\c pg_features_demo
2+
SET ROLE TO demorole;
3+
14
/*
25
36
Arrays can be composed from all data types and are first class citizens in Postgres, being used a lot for internal catalogs.

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
Sample object creation and query scripts to illustrate different Postgres object types and data quering possibilities.
44
Targeted for beginners.
55

6-
Rollout with psql:
6+
### Rollout
7+
8+
For running all the scripts on a local Postgres DB (superuser assumed) use the provided rollout.sh script which consist of:
79

810
```
9-
ls -1 *.sql | sort -V | xargs cat | psql
11+
ls *.sql | sort -n | xargs -n 1 psql -f
1012
```
1113

1214
All feedback welcome!

rollout.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
# Will create a new database "pg_features_demo". NB! Superuser rights are assumed.
4+
#
5+
# If not running a local Postgres server use libpq standard environment variables to specify connection string - e.g.:
6+
# export PGHOST=some.ip.com
7+
# export PGPORT=5433
8+
# export PGUSER=myuser
9+
# export PGDATABASE=postgres
10+
ls *.sql | sort -n | xargs -n 1 psql -f

0 commit comments

Comments
 (0)