Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Preguna 1
- su postgres
- createdb RURAL
- psql RURAL
- CREATE ROLE RECURSOS WITH CONNECTION LIMIT 1 PASSWORD 'PRUEBA' LOGIN;
- \du
- List of roles
- Role name | Attributes | Member of
- -----------+--------------+-----------
- juan | Superuser | {}
- : Create role
- : Create DB
- postgres | Superuser | {}
- : Create role
- : Create DB
- recursos | 1 connection | {}
- Pregunta 2
- A-) Existen por lo menos dos formas de realizar esta operación:
- 1.- Invocar el script createuser :
- // Cambiamos al usuario postgres
- su postgres
- // Invocamos el script createuser con la opción --password para que nos obligue a introducir uno para el nuevo usuario
- postgres@Server:/root$ createuser SUPER --password
- Shall the new role be a superuser? (y/n) y
- Password:
- // Conectamos con nuestra base de datos
- psql -d RURAL -U SUPER -W
- // Vemos el listado de usuarios de la BDD y comprobamos que se ha añadido correctamente.
- postgres@Server:~/8.4/main/global$ psql -d RURAL -U SUPER -W
- Password for user SUPER:
- psql (8.4.11)
- Type "help" for help.
- // Para ver el listado de usuarios podemos usar:
- // 1
- select * from pg_user;
- usename | usesysid | usecreatedb | usesuper | usecatupd | passwd | valuntil | useconfig
- ----------+----------+-------------+----------+-----------+----------+----------+-----------
- postgres | 10 | t | t | t | ******** | |
- recursos | 16388 | f | f | f | ******** | |
- SUPER | 16392 | t | t | t | ******** | |
- // 2
- List of roles
- Role name | Attributes | Member of
- -----------+--------------+-----------
- SUPER | Superuser | {}
- : Create role
- : Create DB
- postgres | Superuser | {}
- : Create role
- : Create DB
- recursos | 1 connection | {}
- 2.- Utilizar la instrucción CREATE USER :
- // Cambiamos al usuario postgres
- su postgres
- //Conectamos con la base de datos RURAL
- postgres@Server:/$ psql RURAL
- psql (8.4.11)
- Type "help" for help.
- RURAL=#
- // Utilizamos la instrucción CREATE USER
- RURAL=# CREATE USER SUPER WITH PASSWORD 'xxxxx';
- CREATE ROLE
- // Asignamos todos los privilegios al usuario recien creado
- RURAL=# grant all privileges on database RURAL to super;
- GRANT
- // Salimos de la BDD RURAL con \d y reconectamos como el usuario recien creado para probarlo
- postgres@Server:/$ psql -d RURAL -U SUPER -W
- Password for user SUPER:
- psql (8.4.11)
- Type "help" for help.
- RURAL=#
- B-)
- // Estando conectados como el usuario SUPER (final del anterior apartado)
- CREATE TABLE HABITACIONS (
- id_hab CHAR(4) primary key,
- num_hab NUMERIC(3) ,
- planta NUMERIC(2) ,
- capacitat NUMERIC(2)
- );
- CREATE TABLE CLIENTS (
- id_cli CHAR(8) primary key,
- id_hab CHAR(4) references HABITACIONS(id_hab),
- dni CHAR(9) UNIQUE,
- NOM VARCHAR (20),
- COGNOMS VARCHAR (30),
- TELEFONO VARCHAR (11),
- EMAIL VARCHAR (20)
- );
- CREATE TABLE RESERVES (
- id_reserv CHARACTER(10) primary key,
- id_cli CHARACTER(8) references CLIENTS(id_cli),
- data_entrada DATE,
- id_hab CHARACTER(4) references HABITACIONS(id_hab),
- data_sortida DATE,
- numpersones NUMERIC(3),
- emorzar smallint default 0,
- dinar smallint default 0,
- SOPAR smallint default 0
- );
- //Insertamos algunos registros
- INSERT INTO HABITACIONS VALUES( '0001', '001', '01', '2');
- INSERT INTO HABITACIONS VALUES( '0002', '002', '01', '1');
- INSERT INTO HABITACIONS VALUES( '0003', '003', '01', '2');
- INSERT INTO HABITACIONS VALUES( '0004', '004', '01', '2');
- INSERT INTO HABITACIONS VALUES( '0005', '005', '01', '1');
- INSERT INTO HABITACIONS VALUES( '0006', '006', '01', '2');
- INSERT INTO CLIENTS VALUES( '00000001', '0001', '12312312A', 'Antonio', 'Martinez Vera','34624125668', 'Avera@gmail.com');
- INSERT INTO CLIENTS VALUES ( '00000002', '0002', '62564525A', 'John', 'Nash R.', '34254325668','Avera@pengaton.gov');
- INSERT INTO CLIENTS VALUES ( '00000003', '0003', '52455896A', 'Niels Bohr', 'Henrik David','3462865586', 'Niels@atom.com');
- INSERT INTO CLIENTS VALUES ( '00000004', '0004', '89564825A', 'Heisenberg', 'Werner Karl', '34628452568','Hewk@uncertainty.com');
- INSERT INTO RESERVES VALUES( '0000000001', '00000001', '03/16/2012', '0001', null, '1','1','1','1');
- INSERT INTO RESERVES VALUES( '0000000002', '00000002', '01/01/2012', '0002', '02/10/2012','1', '1','1','0');
- INSERT INTO RESERVES VALUES( '0000000003', '00000003', '01/01/2012', '0003', '03/01/2012', '1','1','0','1');
- INSERT INTO RESERVES VALUES( '0000000004', '00000004', '02/15/2012', '0004', '03/05/2012', '1', '0','1','1');
- // Generamos una vista de cada tabla para comprobar que se han insertado los datos correctamente
- SELECT * FROM HABITACIONS;
- Habitacions
- id_hab | num_hab | planta | capacitat
- --------+---------+--------+-----------
- 0001 | 1 | 1 | 2
- 0002 | 2 | 1 | 1
- 0003 | 3 | 1 | 2
- 0004 | 4 | 1 | 2
- 0005 | 5 | 1 | 1
- 0006 | 6 | 1 | 2
- SELECT * FROM CLIENTS;
- id_cli | id_hab | dni | nom | cognoms | telefono | EMAIL
- ----------+--------+-----------+------------+---------------+-------------+----------------------
- 00000001 | 0001 | 12312312A | Antonio | Martinez Vera | 34624125668 | Avera@gmail.com
- 00000002 | 0002 | 62564525A | John | Nash R. | 34254325668 | Avera@pengaton.gov
- 00000003 | 0003 | 52455896A | Niels Bohr | Henrik David | 3462865586 | Niels@atom.com
- 00000004 | 0004 | 89564825A | Heisenberg | Werner Karl | 34628452568 | Hewk@uncertainty.com
- SELECT * FROM RESERVES;
- id_reserv | id_cli | data_entrada | id_hab | data_sortida | numpersones | emorzar | dinar | sopar
- ------------+----------+--------------+--------+--------------+-------------+---------+-------+-------
- 0000000001 | 00000001 | 2012-03-16 | 0001 | | 1 | 1 | 1 | 1
- 0000000002 | 00000002 | 2012-01-01 | 0002 | 2012-02-10 | 1 | 1 | 1 | 0
- 0000000003 | 00000003 | 2012-01-01 | 0003 | 2012-03-01 | 1 | 1 | 0 | 1
- 0000000004 | 00000004 | 2012-02-15 | 0004 | 2012-03-05 | 1 | 0 | 1 | 1
- Pregunta 3
- A-)
- // Utilizamos create user para generar los usuarios
- create user masovers;
- create user cuiner;
- create user clientweb;
- B-)
- // creamos los roles correspondientes
- create role masover;
- create role Cuina;
- create role Web;
- // asignamos los privilegios correspondientes
- grant SELECT, INSERT, UPDATE, DELETE ON TABLE CLIENTS, RESERVES, HABITACIONS TO masover;
- grant SELECT ON TABLE RESERVES TO Cuina;
- Grant INSERT, SELECT, UPDATE ON TABLE RESERVES, HABITACIONS, CLIENTS TO Web;
- // asignamos cada rol al usuario correspondiente
- GRANT masover TO masovers;
- GRANT Cuina to Cuiner;
- Grant Web to clientweb;
- Registro de postgres:
- /var/log/postgresql
- // Ejemplo de output
- 2012-03-11 20:56:08 MSK STATEMENT: revoke SELECT ON TABLE RESERVES TO CUINA;
- 2012-03-11 20:56:15 MSK ERROR: syntax error at or near "to" at character 15
- 2012-03-11 20:56:15 MSK STATEMENT: revoke select to cuina;
- 2012-03-11 20:56:29 MSK ERROR: syntax error at or near "to" at character 33
- 2012-03-11 20:57:29 MSK STATEMENT: revoke select on clients to cuina;
- 2012-03-11 20:57:53 MSK ERROR: role "cuina" cannot be dropped because some objects depend on it
- 2012-03-11 20:57:53 MSK DETAIL: access to table clients
- 2012-03-11 20:57:53 MSK STATEMENT: drop role cuina;
- Pregunta 4
- a-)
- 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