Guest User

Untitled

a guest
Jul 6th, 2018
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. Preguna 1
  2. su postgres
  3. createdb RURAL
  4. psql RURAL
  5. CREATE ROLE RECURSOS WITH CONNECTION LIMIT 1 PASSWORD 'PRUEBA' LOGIN;
  6. \du
  7. List of roles
  8. Role name | Attributes | Member of
  9. -----------+--------------+-----------
  10. juan | Superuser | {}
  11. : Create role
  12. : Create DB
  13. postgres | Superuser | {}
  14. : Create role
  15. : Create DB
  16. recursos | 1 connection | {}
  17.  
  18.  
  19. Pregunta 2
  20.  
  21. A-) Existen por lo menos dos formas de realizar esta operación:
  22.  
  23. 1.- Invocar el script createuser :
  24.  
  25. // Cambiamos al usuario postgres
  26. su postgres
  27. // Invocamos el script createuser con la opción --password para que nos obligue a introducir uno para el nuevo usuario
  28. postgres@Server:/root$ createuser SUPER --password
  29. Shall the new role be a superuser? (y/n) y
  30. Password:
  31. // Conectamos con nuestra base de datos
  32.  
  33. psql -d RURAL -U SUPER -W
  34.  
  35. // Vemos el listado de usuarios de la BDD y comprobamos que se ha añadido correctamente.
  36. postgres@Server:~/8.4/main/global$ psql -d RURAL -U SUPER -W
  37. Password for user SUPER:
  38. psql (8.4.11)
  39. Type "help" for help.
  40.  
  41. // Para ver el listado de usuarios podemos usar:
  42. // 1
  43. select * from pg_user;
  44.  
  45. usename | usesysid | usecreatedb | usesuper | usecatupd | passwd | valuntil | useconfig
  46. ----------+----------+-------------+----------+-----------+----------+----------+-----------
  47. postgres | 10 | t | t | t | ******** | |
  48. recursos | 16388 | f | f | f | ******** | |
  49. SUPER | 16392 | t | t | t | ******** | |
  50.  
  51.  
  52. // 2
  53. List of roles
  54. Role name | Attributes | Member of
  55. -----------+--------------+-----------
  56. SUPER | Superuser | {}
  57. : Create role
  58. : Create DB
  59.  
  60. postgres | Superuser | {}
  61. : Create role
  62. : Create DB
  63. recursos | 1 connection | {}
  64.  
  65.  
  66. 2.- Utilizar la instrucción CREATE USER :
  67. // Cambiamos al usuario postgres
  68. su postgres
  69. //Conectamos con la base de datos RURAL
  70.  
  71. postgres@Server:/$ psql RURAL
  72. psql (8.4.11)
  73. Type "help" for help.
  74.  
  75. RURAL=#
  76. // Utilizamos la instrucción CREATE USER
  77. RURAL=# CREATE USER SUPER WITH PASSWORD 'xxxxx';
  78. CREATE ROLE
  79.  
  80. // Asignamos todos los privilegios al usuario recien creado
  81.  
  82. RURAL=# grant all privileges on database RURAL to super;
  83. GRANT
  84.  
  85. // Salimos de la BDD RURAL con \d y reconectamos como el usuario recien creado para probarlo
  86.  
  87. postgres@Server:/$ psql -d RURAL -U SUPER -W
  88. Password for user SUPER:
  89. psql (8.4.11)
  90. Type "help" for help.
  91.  
  92. RURAL=#
  93.  
  94. B-)
  95.  
  96. // Estando conectados como el usuario SUPER (final del anterior apartado)
  97.  
  98.  
  99. CREATE TABLE HABITACIONS (
  100. id_hab CHAR(4) primary key,
  101. num_hab NUMERIC(3) ,
  102. planta NUMERIC(2) ,
  103. capacitat NUMERIC(2)
  104. );
  105.  
  106. CREATE TABLE CLIENTS (
  107. id_cli CHAR(8) primary key,
  108. id_hab CHAR(4) references HABITACIONS(id_hab),
  109. dni CHAR(9) UNIQUE,
  110. NOM VARCHAR (20),
  111. COGNOMS VARCHAR (30),
  112. TELEFONO VARCHAR (11),
  113. EMAIL VARCHAR (20)
  114. );
  115. CREATE TABLE RESERVES (
  116. id_reserv CHARACTER(10) primary key,
  117. id_cli CHARACTER(8) references CLIENTS(id_cli),
  118. data_entrada DATE,
  119. id_hab CHARACTER(4) references HABITACIONS(id_hab),
  120. data_sortida DATE,
  121. numpersones NUMERIC(3),
  122. emorzar smallint default 0,
  123. dinar smallint default 0,
  124. SOPAR smallint default 0
  125. );
  126. //Insertamos algunos registros
  127.  
  128. INSERT INTO HABITACIONS VALUES( '0001', '001', '01', '2');
  129. INSERT INTO HABITACIONS VALUES( '0002', '002', '01', '1');
  130. INSERT INTO HABITACIONS VALUES( '0003', '003', '01', '2');
  131. INSERT INTO HABITACIONS VALUES( '0004', '004', '01', '2');
  132. INSERT INTO HABITACIONS VALUES( '0005', '005', '01', '1');
  133. INSERT INTO HABITACIONS VALUES( '0006', '006', '01', '2');
  134. INSERT INTO CLIENTS VALUES( '00000001', '0001', '12312312A', 'Antonio', 'Martinez Vera','34624125668', 'Avera@gmail.com');
  135. INSERT INTO CLIENTS VALUES ( '00000002', '0002', '62564525A', 'John', 'Nash R.', '34254325668','Avera@pengaton.gov');
  136. INSERT INTO CLIENTS VALUES ( '00000003', '0003', '52455896A', 'Niels Bohr', 'Henrik David','3462865586', 'Niels@atom.com');
  137. INSERT INTO CLIENTS VALUES ( '00000004', '0004', '89564825A', 'Heisenberg', 'Werner Karl', '34628452568','Hewk@uncertainty.com');
  138.  
  139. INSERT INTO RESERVES VALUES( '0000000001', '00000001', '03/16/2012', '0001', null, '1','1','1','1');
  140. INSERT INTO RESERVES VALUES( '0000000002', '00000002', '01/01/2012', '0002', '02/10/2012','1', '1','1','0');
  141. INSERT INTO RESERVES VALUES( '0000000003', '00000003', '01/01/2012', '0003', '03/01/2012', '1','1','0','1');
  142. INSERT INTO RESERVES VALUES( '0000000004', '00000004', '02/15/2012', '0004', '03/05/2012', '1', '0','1','1');
  143.  
  144. // Generamos una vista de cada tabla para comprobar que se han insertado los datos correctamente
  145.  
  146. SELECT * FROM HABITACIONS;
  147. Habitacions
  148. id_hab | num_hab | planta | capacitat
  149. --------+---------+--------+-----------
  150. 0001 | 1 | 1 | 2
  151. 0002 | 2 | 1 | 1
  152. 0003 | 3 | 1 | 2
  153. 0004 | 4 | 1 | 2
  154. 0005 | 5 | 1 | 1
  155. 0006 | 6 | 1 | 2
  156. SELECT * FROM CLIENTS;
  157. id_cli | id_hab | dni | nom | cognoms | telefono | EMAIL
  158. ----------+--------+-----------+------------+---------------+-------------+----------------------
  159. 00000001 | 0001 | 12312312A | Antonio | Martinez Vera | 34624125668 | Avera@gmail.com
  160. 00000002 | 0002 | 62564525A | John | Nash R. | 34254325668 | Avera@pengaton.gov
  161. 00000003 | 0003 | 52455896A | Niels Bohr | Henrik David | 3462865586 | Niels@atom.com
  162. 00000004 | 0004 | 89564825A | Heisenberg | Werner Karl | 34628452568 | Hewk@uncertainty.com
  163. SELECT * FROM RESERVES;
  164.  
  165. id_reserv | id_cli | data_entrada | id_hab | data_sortida | numpersones | emorzar | dinar | sopar
  166. ------------+----------+--------------+--------+--------------+-------------+---------+-------+-------
  167. 0000000001 | 00000001 | 2012-03-16 | 0001 | | 1 | 1 | 1 | 1
  168. 0000000002 | 00000002 | 2012-01-01 | 0002 | 2012-02-10 | 1 | 1 | 1 | 0
  169. 0000000003 | 00000003 | 2012-01-01 | 0003 | 2012-03-01 | 1 | 1 | 0 | 1
  170. 0000000004 | 00000004 | 2012-02-15 | 0004 | 2012-03-05 | 1 | 0 | 1 | 1
  171.  
  172.  
  173. Pregunta 3
  174. A-)
  175. // Utilizamos create user para generar los usuarios
  176. create user masovers;
  177. create user cuiner;
  178. create user clientweb;
  179. B-)
  180. // creamos los roles correspondientes
  181. create role masover;
  182. create role Cuina;
  183. create role Web;
  184. // asignamos los privilegios correspondientes
  185. grant SELECT, INSERT, UPDATE, DELETE ON TABLE CLIENTS, RESERVES, HABITACIONS TO masover;
  186. grant SELECT ON TABLE RESERVES TO Cuina;
  187. Grant INSERT, SELECT, UPDATE ON TABLE RESERVES, HABITACIONS, CLIENTS TO Web;
  188. // asignamos cada rol al usuario correspondiente
  189. GRANT masover TO masovers;
  190. GRANT Cuina to Cuiner;
  191. Grant Web to clientweb;
  192.  
  193. Registro de postgres:
  194.  
  195. /var/log/postgresql
  196.  
  197. // Ejemplo de output
  198.  
  199. 2012-03-11 20:56:08 MSK STATEMENT: revoke SELECT ON TABLE RESERVES TO CUINA;
  200. 2012-03-11 20:56:15 MSK ERROR: syntax error at or near "to" at character 15
  201. 2012-03-11 20:56:15 MSK STATEMENT: revoke select to cuina;
  202. 2012-03-11 20:56:29 MSK ERROR: syntax error at or near "to" at character 33
  203. 2012-03-11 20:57:29 MSK STATEMENT: revoke select on clients to cuina;
  204. 2012-03-11 20:57:53 MSK ERROR: role "cuina" cannot be dropped because some objects depend on it
  205. 2012-03-11 20:57:53 MSK DETAIL: access to table clients
  206. 2012-03-11 20:57:53 MSK STATEMENT: drop role cuina;
  207.  
  208.  
  209. Pregunta 4
  210.  
  211. a-)
  212.  
  213. CREATE VIEW serveis_per_dia AS SELECT now(), emorzar, dinar, sopar FROM RESERVES group by emorzar, dinar sopar;
Add Comment
Please, Sign In to add comment