Habilitar el acceso remoto de un Servidor PostgreSQL

Pasos para habilitar el acceso remoto de un Servidor PostgreSQL.

Requerimientos

  • Gestor de base de datos PostgreSQL versión 12.
  • Sistema Operativo Debian versión 10 (buster).

Procedimiento

En el siguiente ejemplo se habilitará el acceso remoto para la IP 192.168.32.1.

1. Habilita la opción para escuchar IP's remotas

Encuentra el archivo postgresql.conf.

sudo find / -name "postgresql.conf"
/etc/postgresql/12/main/postgresql.conf
/usr/lib/tmpfiles.d/postgresql.conf
...

Abre el archivo que aparece en la carpeta /etc/postgresql.

sudo nano /etc/postgresql/12/main/postgresql.conf

y adiciona la siguiente línea al final del archivo.

listen_addresses = '*'

Por ejemplo:

postgresql.conf

# -----------------------------
# PostgreSQL configuration file
# ----------------------------

...

#------------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#------------------------------------------------------------------------------

# Add settings for extensions here

listen_addresses = '*'

2. Configura las IP's que pueden conectarse

Encuentra el archivo pg_hba.conf.

sudo find / -name "pg_hba.conf"
/etc/postgresql/12/main/pg_hba.conf
...

Abre el archivo que aparece en la carpeta /etc/postgresql.

sudo nano /etc/postgresql/12/main/pg_hba.conf

y adiciona la siguiente línea al final del archivo.

host    all             all             192.168.32.1/32         md5

Por ejemplo:

pg_hba.conf

# PostgreSQL Client Authentication Configuration File
# ===================================================

...

# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5

host    all             all             192.168.32.1/32         md5

Nota:

  • Para habilitar una sola IP utilice la máscara /32. Por ejemplo: 192.168.32.1/32.

  • Para habilitar un rango de IP's utilice otra máscara. Por ejemplo: 192.168.32.1/24 habilita las ip's desde la 192.168.32.1 hasta la 192.168.32.254.

3. Reinicia el servicio

Reinicia el servidor de PostgreSQL para aplicar los cambios.

sudo service postgresql restart

Referencias

Publicado: 2 de junio de 2020