Estas configuraciones pueden ser utilizadas en un Jail o directamente en el servidor.
Asegurarse que los paquetes estén actualizados
root@localhost:~ # pkg update
Proceder con la instalación de PostgreSQL 9.6
root@localhost:~ # pgk install postgresql96-server postgresql96-client
Updating FreeBSD repository catalogue...
.
.
.
Proceed with this action? [y/N]: y
.
.
.
root@localhost:~ #
Habilitar postgresql para su inicio automático con el sistema
root@localhost:~ # sysrc postgresql_enable=yes
postgresql_enable: -> yes
De forma alternativa también se puede agregar la linea postgresql_enable="YES" al final del archivo /etc/rc.conf, usando un editor de texto. Si nano no está instalado se puede instalar con pgk install nano
Inicializar la base de datos.
root@localhost:~ # service postgresql initdb
The fles belonging to this database system will be owned by user "postgres".
.
.
.
Success. You can now start the database server using:
/usr/local/bin/pg_ctl -D /var/db/postgres/data96 -l logfile start
root@localhost:~ #
Iniciar el servidor postgresql
root@localhost:~ # service postgresql start
LOG: ending log output to stderr
HINT: Future log output will go to log destination "syslog".
root@localhost:~ #
Habilitar las conexiones remotas y autenticación por contraseña
Para realizar la configuración utilizaremos nano, si no está instalado, instalarlo con el siguiente comando
root@localhost:~ # pkg install nano
Updating FreeBSD repository catalogue...
.
.
.
[localhost] [1/1] Installing nano-3.2...
[localhost] [1/1] Extracting nano-3.2: 100%
root@localhost:~ #
Abrir el archivo de configuración postgresql.conf
root@localhost:~ # nano /var/db/postgres/data96/postgresql.conf
Descomentariar quitando el símbolo #, de la linea que hace referencia a la escucha de direcciones y cambiar localhost por * (asterísco)
listen_addresses = '*'
Salir y guardar los cambios presionando la combinación de teclas Ctrl + X y aceptando guardar los cambios con “y” y luego la tecla Enter
Abrir el archivo pg_hba.conf
root@localhost:~ # nano /var/db/postgres/data96/pg_hba.conf
Realizar una configuración con respecto a la red local o a las IP de las computadoras que se conectaran a PostgreSQL con método MD5
Por ejemplo:
host all all 192.168.0.0/32 md5
Salir y guardar los cambios presionando la combinación de teclas Ctrl + X y aceptando guardar los cambios con “y” y luego la tecla Enter
Reiniciar el servicio de PostgreSQL
root@localhost:~ # service postgresql restart
LOG: ending log output to stderr
HINT: Future log output will go to log destination "syslog".
root@localhost:~ #
Cambiar la contraseña del usuario postgres en PostgreSQL
Cambiar al usuario postgres
root@localhost:~ # su - postgres
$
Entrar al Shell de PostgreSQL
$ psql
psql (9.6.13)
Type "help" for help.
postgres=#
Cambiar la contraseña del usuario postgres en PostgreSQL
postgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD 'nueva_contraseña';
ALTER ROLE
postgres=#
Salir del Shell
postgres=# \q
$
Salir del usuario postgres
$ exit
root@localhost:~ #
Crear nuevo usuario/rol en PostgreSQL
Por defecto PostgreSQL crea el usuario postgres y el grupo postgres
Cambiar a usuario postgres
root@localhost:~ # su - postgres
$
Crear un nuevo usuario en modo interactivo
$ createuser --interactive
Enter name of role to add: nuevo_usuario
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
Crear un nuevo usuario en modo abreviado
$ createuser -sdrP nuevo_usuario
- s: Este usuario sera superusuario.
- d: Este usuario puede crear bases de datos.
- r: Este usuario puede crear nuevos roles.
- P: Asignar una contraseña a este role. *Al finalizar el comando solicitará una contraseña para
nuevo_usuario
Ingresar al shell de PostgreSQL para agregar la contraseña a nuevo_usuario
$ psql
psql (9.6.13)
Type "help" for help.
postgres=#
Agregar la contraseña para nuevo_usuario
postgres=# ALTER USER nuevo_usuario WITH ENCRYPTED PASSWORD 'contraseña';
ALTER ROLE
postgres=#
Proveer de todos los privilegios a nuevo_usuario
postgres=# GRANT ALL PRIVILEGES ON DATABASE base_de_datos TO nuevo_usuario;
GRANT
postgres=#
Salir del Shel de PostgreSQL
postgres=# \q
$
Salir del usuario postgres
$ exit
root@localhost:~ #
