
Espero conectar mi servicio Docker de Postgres desde un cliente remoto. Así es como creo el contenedor:
sudo docker run --name pg -p 5432:5432 -v pg_data:/var/lib/postgres/data -e POSTGRES_DB=mydb -e POSTGRES_USER=dbowner -e POSTGRES_PASSWORD=MySecretPassword -d postgres -c "listen_addresses='*'"
Investigué y alguien dijo que el archivo pg_hba.conf
debería editarse. ¿Cómo debo editar pg_hba.conf? No hay parámetros de configuración para configurar pg_hba.conf
. Además, ¿hay alguna diferencia entre host all all all md5
y host all all 0.0.0.0/0 md5
? ¿Cuál debo usar y cómo debo colocarlo pg_hba.conf
dentro del contenedor acoplable?
Aquí está el resultado netstat
después de crear el contenedor:
$ netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::5432 :::* LISTEN
tcp6 0 0 :::3000 :::* LISTEN
tcp6 0 0 :::4000 :::* LISTEN
tcp6 0 0 :::4001 :::* LISTEN
tcp6 0 0 :::6379 :::* LISTEN
¿Eso significa que el servicio PostgreSQL solo escucha IPv6?
Respuesta1
Hay errores en -v option
:
- Tu escribes
/var/lib/postgres/data
debería ser/var/lib/postgresql/data
- La ruta del host debe estar llena:
/root/pgdata
- Si el host usa selinux, entonces úselo
-v /root/pgdata:/var/lib/postgresql/data:Z
o si no, úselo:rw
en su lugar
Esto funciona perfectamente:
root@testubt:~# docker run --name pg -p 5432:5432 -v /root/pgdata:/var/lib/postgresql/data:Z -e POSTGRES_DB=mydb -e POSTGRES_USER=dbowner -e POSTGRES_PASSWORD=MySecretPassword -d postgres:alpine
root@testubt:~# ss -tnl | grep 5432
LISTEN 0 128 :::5432 :::*
Pruebe el servidor local:
root@testubt:~# PGPASSWORD=MySecretPassword psql -U dbowner mydb -h localhost
psql (9.5.14, server 10.5)
Type "help" for help.
mydb=# \q
Prueba desde otra máquina:
alp01:~# PGPASSWORD=MySecretPassword psql -U dbowner mydb -h testubt
psql (10.5)
Type "help" for help.
mydb=#
Y si quieres editarlo pg_hba.conf
está en el /root/pgdata
directorio. que creí que no necesitarías.
Salud :)