- создайте новый кластер PostgresSQL 15 (использовал ранеесозданны 15 версии)
done
-
зайдите в созданный кластер под пользователем postgres
postgres=# postgres=#
-
создайте новую базу данных testdb
CREATE DATABASE testdb;
postgres=# CREATE DATABASE testdb;
CREATE DATABASE
postgres=#
-
зайдите в созданную базу данных под пользователем postgres
postgres=# \c testdb; You are now connected to database "testdb" as user "postgres". testdb=#
-
создайте новую схему create schema testnm;
testdb=# create schema testnm; CREATE SCHEMA testdb=#
-
создайте новую таблицу t1 с одной колонкой c1 типа integer create table t1(c1 int);
testdb=# create table t1(c1 int); CREATE TABLE testdb=#
-
вставьте строку со значением c1=1 insert into t1 values (1);
testdb=# insert into t1 values (1); INSERT 0 1 testdb=# select * from t1; c1 ---- 1 (1 row) testdb=#
-
создайте новую роль readonly
testdb=# CREATE role readonly; CREATE ROLE testdb=#
-
дайте новой роли право на подключение к базе данных testdb
testdb=# grant connect on DATABASE testdb TO readonly; GRANT testdb=#
-
дайте новой роли право на использование схемы testnm
testdb=# grant usage on SCHEMA testnm to readonly; GRANT
-
дайте новой роли право на select для всех таблиц схемы testnm
testdb=# grant SELECT on all TABLEs in SCHEMA testnm TO readonly; GRANT testdb=#
-
создайте пользователя testread с паролем test123
testdb=# CREATE USER testread with password 'test123'; CREATE ROLE testdb=#
-
дайте роль readonly пользователю testread
testdb=# grant readonly TO testread; GRANT ROLE testdb=#
-
зайдите под пользователем testread в базу данных testdb
testdb=# \c testdb testread Password for user testread: You are now connected to database "testdb" as user "testread". testdb=>
-
сделайте select * from t1;
testdb=> SELECT * FROM t1; ERROR: permission denied for table t1 testdb=>
-
получилось? (могло если вы делали сами не по шпаргалке и не упустили один существенный момент про который позже) напишите что именно произошло в тексте домашнего задания у вас есть идеи почему? ведь права то дали? посмотрите на список таблиц подсказка в шпаргалке под пунктом 20 а почему так получилось с таблицей (если делали сами и без шпаргалки то может у вас все нормально)
testdb=> \dt List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | t1 | table | postgres (1 row) testdb=>
Проблема с селектом, так как таблица создана в схеме public, а права мы дали для схемы testnm
-
вернитесь в базу данных testdb под пользователем postgres
testdb=> \c testdb postgres Password for user postgres: You are now connected to database "testdb" as user "postgres". testdb=#
-
удалите таблицу t1
testdb=# DROP TABLE t1; DROP TABLE testdb=#
-
создайте ее заново но уже с явным указанием имени схемы testnm
testdb=# CREATE TABLE testnm.t1(c1 integer); CREATE TABLE testdb=#
-
вставьте строку со значением c1=1
testdb=# INSERT INTO testnm.t1 values(1); INSERT 0 1 testdb=# select * from testnm.t1; c1 ---- 1 (1 row) testdb=#
-
зайдите под пользователем testread в базу данных testdb
testdb=# ^C testdb=# \c testdb testread Password for user testread: You are now connected to database "testdb" as user "testread". testdb=>
-
сделайте select * from testnm.t1; получилось? есть идеи почему?
testdb=> select * from testnm.t1; ERROR: permission denied for table t1 testdb=>
та же самая ситуация, что была на вебинаре, мы сначала дали права, а потом создали таблицу
- как сделать так чтобы такое больше не повторялось
надо опять зайти пользователем postgres и изменить права grant usage on SCHEMA testnm to readonly; grant SELECT on all TABLEs in SCHEMA testnm TO readonly; после чего переподключился testread
- сделайте select * from testnm.t1;
получилось? да, теперь получилось
testdb=# \c testdb testread
Password for user testread:
You are now connected to database "testdb" as user "testread".
testdb=>
testdb=>
testdb=> SELECT * FROM testnm.t1;
c1
----
1
(1 row)
testdb=>
-
теперь попробуйте выполнить команду create table t2(c1 integer); insert into t2 values (2);
testdb=> create table t2 (c1 integer); ERROR: permission denied for schema public LINE 1: create table t2 (c1 integer); ^
ситуация соответствует, прав на public не предоставлялась роли readonly Проверил по шпаргалке к ДЗ, там есть ** P.S.S. - у кого не получается создать табличку в public - в 15 версии права на CREATE TABLE по умолчанию отозваны у схемы PUBLIC, только USAGE
у меня как раз 15 версия