alvarovaca

BBDD Departamentos PostgreSQL

Feb 5th, 2021 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.02 KB | None | 0 0
  1. root@servidor2:~# apt update && apt upgrade && apt install postgresql
  2.  
  3. root@servidor2:~# netstat -tln
  4. Active Internet connections (only servers)
  5. Proto Recv-Q Send-Q Local Address           Foreign Address         State      
  6. tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN    
  7. tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN    
  8. tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN    
  9. tcp6       0      0 :::22                   :::*                    LISTEN    
  10. tcp6       0      0 ::1:631                 :::*                    LISTEN    
  11. tcp6       0      0 ::1:5432                :::*                    LISTEN
  12.  
  13. root@servidor2:~# su - postgres
  14.  
  15. postgres@servidor2:~$ psql
  16. psql (11.9 (Debian 11.9-0+deb10u1))
  17. Type "help" for help.
  18.  
  19. postgres=# CREATE DATABASE prueba2;
  20. CREATE DATABASE
  21.  
  22. postgres=# \l
  23.                                   List of databases
  24.    Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges  
  25. -----------+----------+----------+-------------+-------------+-----------------------
  26.  postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
  27.  prueba2   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
  28.  template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
  29.            |          |          |             |             | postgres=CTc/postgres
  30.  template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
  31.            |          |          |             |             | postgres=CTc/postgres
  32. (4 rows)
  33.  
  34. postgres=# CREATE USER alvaro2 WITH PASSWORD 'alvaro2';
  35. CREATE ROLE
  36.  
  37. postgres=# GRANT ALL PRIVILEGES ON DATABASE prueba2 TO alvaro2;
  38. GRANT
  39.  
  40. postgres=# exit
  41.  
  42. postgres@servidor2:~$ psql -h localhost -U alvaro2 -d prueba2
  43. Password for user alvaro2:
  44. psql (11.9 (Debian 11.9-0+deb10u1))
  45. SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
  46. Type "help" for help.
  47.  
  48. ---------------------------------------------------------------------------
  49.  
  50. -- La base de datos es bastante simple, pues se almacena información sobre los departamentos de una empresa, algo similar a la tabla DEPT del esquema SCOTT de Oracle. Como anteriormente hemos mencionado, en la tabla "Empleados" ubicada en el otro gestor, existe una columna que sería el resultado de una relación 1:N con la tabla "Departamentos", pero en esta ocasión, ambas tablas se encuentran ubicadas en gestores separados.
  51.  
  52. prueba2=> CREATE TABLE Departamentos
  53. prueba2-> (
  54. prueba2(> Identificador   NUMERIC(2),
  55. prueba2(> Nombre          VARCHAR(20),
  56. prueba2(> Localizacion    VARCHAR(15),
  57. prueba2(> CONSTRAINT pk_departamentos PRIMARY KEY (Identificador),
  58. prueba2(> CONSTRAINT locrellena CHECK (Localizacion IS NOT NULL)
  59. prueba2(> );
  60. CREATE TABLE
  61.  
  62. prueba2=> INSERT INTO Departamentos
  63. prueba2-> VALUES (10, 'Administración', 'Sevilla');
  64. INSERT 0 1
  65.  
  66. prueba2=> INSERT INTO Departamentos
  67. prueba2-> VALUES (20, 'Recursos Humanos', 'Barcelona');
  68. INSERT 0 1
  69.  
  70. prueba2=> INSERT INTO Departamentos
  71. prueba2-> VALUES (30, 'Seguridad', 'Madrid');
  72. INSERT 0 1
  73.  
  74. prueba2=> INSERT INTO Departamentos
  75. prueba2-> VALUES (40, 'Informática', 'Valencia');
  76. INSERT 0 1
  77.  
  78. ---------------------------------------------------------------------------
  79.  
  80. root@servidor2:~# ip a
  81. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  82.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  83.     inet 127.0.0.1/8 scope host lo
  84.        valid_lft forever preferred_lft forever
  85.     inet6 ::1/128 scope host
  86.        valid_lft forever preferred_lft forever
  87. 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  88.     link/ether 08:00:27:15:e3:4b brd ff:ff:ff:ff:ff:ff
  89.     inet 192.168.1.161/24 brd 192.168.1.255 scope global dynamic noprefixroute enp0s3
  90.        valid_lft 86329sec preferred_lft 86329sec
  91.     inet6 fe80::a00:27ff:fe15:e34b/64 scope link noprefixroute
  92.        valid_lft forever preferred_lft forever
  93.  
  94. root@servidor2:~# nano /etc/postgresql/11/main/postgresql.conf
  95.  
  96. #listen_addresses = 'localhost'
  97.  
  98. listen_addresses = '*'
  99.  
  100. root@servidor2:~# nano /etc/postgresql/11/main/pg_hba.conf
  101.  
  102. host    all             all             127.0.0.1/32            md5
  103.  
  104. host    all             all             all            md5
  105.  
  106. root@servidor2:~# systemctl restart postgresql
  107.  
  108. root@servidor2:~# netstat -tln
  109. Active Internet connections (only servers)
  110. Proto Recv-Q Send-Q Local Address           Foreign Address         State      
  111. tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN    
  112. tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN    
  113. tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN    
  114. tcp6       0      0 :::22                   :::*                    LISTEN    
  115. tcp6       0      0 ::1:631                 :::*                    LISTEN    
  116. tcp6       0      0 :::5432                 :::*                    LISTEN
Add Comment
Please, Sign In to add comment