psql: error: could not connect to server: received invalid response to SSL negotiation: H
we also get above error if we are trying to access postgres container service exposed through some proxy container service like haproxy/nginx using http mode/protocol.
we need to use tcp mode/protocol.
Try it yourself with below details.
nginx.conf
events {}
stream {
upstream postgres {
server pdb:5432;
}
server {
listen 5432;
proxy_pass postgres;
}
}
haproxy.cfg
global
log 127.0.0.1 local1
maxconn 4096
defaults
mode http
maxconn 2048
frontend postgresDB
bind *:5000
mode tcp
timeout connect 5s
timeout client 5s
timeout server 5s
default_backend postgresDB
backend postgresDB
mode tcp
server pdb pdb:5432 check inter 5s rise 2 fall 3
Containers deployment
sudo docker run -d --name testdb 'POSTGRES_PASSWORD=123456' postgres
sudo docker run -d --name pdb -e 'POSTGRES_USER=admin' -e 'POSTGRES_DB=testdb' -e 'POSTGRES_PASSWORD=admin123456' postgres
sudo docker run -d --name nginx-TCPreverseProxy -p 5432:5432 --link pdb:pdb -v /home/admin/nginx.conf:/etc/nginx/nginx.conf nginx
sudo docker run -d -p 5000:5000 --name haproxy --link pdb:pdb -v /home/admin/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg haproxy
#Testing of connection
sudo docker exec -it -u 0 testdb bash
#Inside test postgres container
#checking connection through nginx-proxy
root@34211600c3f7:/# psql -h 192.168.0.2 -p 5432 -d testdb -U admin -W
#checking connection through haproxy-proxy
root@34211600c3f7:/# psql -h 192.168.0.2 -p 5000 -d testdb -U admin -W