Skip to content

Commit 16c5d76

Browse files
committed
completed till Storing Arrays
1 parent 3b38cc8 commit 16c5d76

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

Complete SQL & Databases - ZTM/07_Database_Management.sql

+118
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,121 @@ CREATE DATABASE ZTM;
3939
CREATE SCHEMA Sales;
4040

4141

42+
/*--------------------------------------------------------------------------------------------------------------*/
43+
44+
/*********** 9) Creating Role **************/
45+
CREATE ROLE readonly WITH LOGIN ENCRYPTED PASSWORD 'readonly';
46+
47+
-- to check all avaliable roles
48+
>> \du
49+
50+
/*
51+
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.
52+
*/
53+
54+
/************ 10) Creating users and Configuring Login *************/
55+
56+
-- create role and user
57+
CREATE ROLE test_role_with_login WITH LOGIN ENCRYPTED PASSWORD 'password';
58+
59+
CREATE USER test_user_with_login WITH ENCRYPTED PASSWORD 'password';
60+
61+
\du
62+
63+
/************ Another way of creating user *********/
64+
-- if you are not connected with postgre, there is binary command that we can use, just follow on screen questions.
65+
createuser --interactive
66+
67+
/******* Altering Role *********/
68+
AlTER ROLE test_interactive WITH ENCRYPTED PASSWORD 'password';
69+
70+
/*--------------------------------------------------------------------------------------------------------------*/
71+
72+
/******** Checking hba and config file location *****/
73+
-- login with root user
74+
SHOW hba_file;
75+
SHOW config_file;
76+
77+
/*--------------------------------------------------------------------------------------------------------------*/
78+
79+
/******** 11/12) Privileges *********/
80+
GRANT ALL PRIVILEGES ON <table> TO <user>;
81+
GRANT ALL ON ALL TABLES [IN SCHEMA <schema>] TO <user>;
82+
GRANT [SELECT, UPDATE, INSERT, ...] ON <table> [IN SCHEMA <schema>] TO <user>;
83+
84+
-- grant select to our test user
85+
GRANT SELECT ON titles TO test_user_with_login;
86+
87+
-- revoke privileges from test user
88+
REVOKE SELECT ON titles FROM test_user_with_login;
89+
90+
-- Grant all access
91+
GRANT ALL ON ALL TABLES IN SCHEMA public TO test_user_with_login;
92+
93+
-- Revoke all access
94+
REVOKE ALL ON ALL TABLES IN SCHEMA public TO test_user_with_login;
95+
96+
-- Create a role with privileges and grant that role a specific user
97+
CREATE ROLE employee_read;
98+
GRANT SELECT ON ALL TABLES IN SCHEMA public TO employee_read;
99+
GRANT employee_read TO test_user_with_login;
100+
GRANT employee_read TO test_user_with_login;
101+
REVOKE employee_read FROM test_user_with_login;
102+
103+
104+
/*--------------------------------------------------------------------------------------------------------------*/
105+
106+
/******** 13) Best Practices For Role Management *********/
107+
108+
/*
109+
When managing Roles and Permissions, always go with the "Principle Of Least Privilege".
110+
Give no Privilege at all at first. Then stack on privileges as required.
111+
112+
Don't use Super/Admin by default.
113+
114+
*/
115+
116+
/*--------------------------------------------------------------------------------------------------------------*/
117+
118+
/************ 15) Storing Texts **********/
119+
CREATE TABLE test(
120+
fixed char(4),
121+
variable varchar(20),
122+
unlimited text
123+
);
124+
125+
INSERT INTO test VALUES(
126+
'abcd',
127+
'efghijklm',
128+
'This is super unlimited'
129+
);
130+
131+
132+
/************ 16) Storing Numbers **********/
133+
134+
CREATE TABLE test(
135+
four float4,
136+
eight float8,
137+
big decimal
138+
);
139+
140+
INSERT INTO test VALUES(
141+
1.123456789,
142+
1.123456789123456789,
143+
1.123456789123456789123456789123456789123456789123456789
144+
);
145+
146+
/************ 17) Storing Arrays **********/
147+
CREATE TABLE test(
148+
four char(2)[],
149+
eight text[],
150+
big float64[]
151+
);
152+
153+
INSERT INTO test VALUES(
154+
ARRAY['ab', 'cd', 'ef'],
155+
ARRAY['test', 'sunny', 'goblin'],
156+
ARRAY[1.23, 3.45, 6.78, 9.2345234]
157+
);
158+
159+
/*--------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)