-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy path07_Database_Management.sql
196 lines (144 loc) · 5.55 KB
/
07_Database_Management.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/************************ 5) Template Databases ****************/
/*
In General, there are 2 templates; Template0 and Template1.
Template0 is kinda like safty net and used it to create Template1.
You don't want to mess up with Template0.
*/
/******** Create Own Templates **********/
CREATE DATABASE mysuperdupertemplate;
--connect to that db and run
CREATE TABLE superdupertable();
--after creating the database and connect to it, you will see that table.
CREATE DATABASE mysuperduperdatabase WITH mysuperdupertemplate;
/*--------------------------------------------------------------------------------------------------------------*/
/*********** 6) Creating a Database **************/
/*
TEMPLATE : template01
ENCODING : UTF8
CONNECTION_LIMIT : 100
OWNER : Current User
*/
CREATE DATABASE db_name
[[WITH] [OWNER [=] user_name]
[TEMPLATE [=] template]
[ENCODING [=] encoding]
[LC_COLLATE [=] lc_collate]
[LC_CTYPE [=] lc_ctype]
[TABLESPACE [=] tablespace]
[CONNECTION LIMIT [=] connlimit]]
-- to store ZTM db to store courses
CREATE DATABASE ZTM;
-- Create Schema
CREATE SCHEMA Sales;
/*--------------------------------------------------------------------------------------------------------------*/
/*********** 9) Creating Role **************/
CREATE ROLE readonly WITH LOGIN ENCRYPTED PASSWORD 'readonly';
-- to check all avaliable roles
>> \du
/*
By default, when you create a new database - only Superuser and creater of the db can access to it. Other users must be given access to it, if required.
*/
/************ 10) Creating users and Configuring Login *************/
-- create role and user
CREATE ROLE test_role_with_login WITH LOGIN ENCRYPTED PASSWORD 'password';
CREATE USER test_user_with_login WITH ENCRYPTED PASSWORD 'password';
\du
/************ Another way of creating user *********/
-- if you are not connected with postgre, there is binary command that we can use, just follow on screen questions.
createuser --interactive
/******* Altering Role *********/
AlTER ROLE test_interactive WITH ENCRYPTED PASSWORD 'password';
/*--------------------------------------------------------------------------------------------------------------*/
/******** Checking hba and config file location *****/
-- login with root user
SHOW hba_file;
SHOW config_file;
/*--------------------------------------------------------------------------------------------------------------*/
/******** 11/12) Privileges *********/
GRANT ALL PRIVILEGES ON <table> TO <user>;
GRANT ALL ON ALL TABLES [IN SCHEMA <schema>] TO <user>;
GRANT [SELECT, UPDATE, INSERT, ...] ON <table> [IN SCHEMA <schema>] TO <user>;
-- grant select to our test user
GRANT SELECT ON titles TO test_user_with_login;
-- revoke privileges from test user
REVOKE SELECT ON titles FROM test_user_with_login;
-- Grant all access
GRANT ALL ON ALL TABLES IN SCHEMA public TO test_user_with_login;
-- Revoke all access
REVOKE ALL ON ALL TABLES IN SCHEMA public TO test_user_with_login;
-- Create a role with privileges and grant that role a specific user
CREATE ROLE employee_read;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO employee_read;
GRANT employee_read TO test_user_with_login;
GRANT employee_read TO test_user_with_login;
REVOKE employee_read FROM test_user_with_login;
/*--------------------------------------------------------------------------------------------------------------*/
/******** 13) Best Practices For Role Management *********/
/*
When managing Roles and Permissions, always go with the "Principle Of Least Privilege".
Give no Privilege at all at first. Then stack on privileges as required.
Don't use Super/Admin by default.
*/
/*--------------------------------------------------------------------------------------------------------------*/
/************ 15) Storing Texts **********/
CREATE TABLE test(
fixed char(4),
variable varchar(20),
unlimited text
);
INSERT INTO test VALUES(
'abcd',
'efghijklm',
'This is super unlimited'
);
/************ 16) Storing Numbers **********/
CREATE TABLE test(
four float4,
eight float8,
big decimal
);
INSERT INTO test VALUES(
1.123456789,
1.123456789123456789,
1.123456789123456789123456789123456789123456789123456789
);
/************ 17) Storing Arrays **********/
CREATE TABLE test(
four char(2)[],
eight text[],
big float64[]
);
INSERT INTO test VALUES(
ARRAY['ab', 'cd', 'ef'],
ARRAY['test', 'sunny', 'goblin'],
ARRAY[1.23, 3.45, 6.78, 9.2345234]
);
/*--------------------------------------------------------------------------------------------------------------*/
/************ 19) Create Tables **************/
CREATE TABLE <name> (
<col1> TYPE [Constraint],
table_constraint [Constraint]
)[INHERITS <existing_table>];
-- create table for ztm db
-- first need to create extension, otherwise we can't use uuid_generate_v4() function.
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE student(
student_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
date_of_birth DATE NOT NULL
);
-- check the existing tables
>> \dt
-- check the newly created student table
>> \d student
/*--------------------------------------------------------------------------------------------------------------*/
/************ 21) Column Constraints **************/
/*
NOT NULL : cannot be Null
PRIMARY KEY : column will be primary key
UNIQUE : can only contain unique values (Null is Unique)
CHECK : apply a special condition check against the values in the column
REFERENCES : used in Foreign Key referencing
*/