# Haproxy with Postgresql

**URL:** <https://discourse.haproxy.org/t/haproxy-with-postgresql/7124>\
**Category:** Help!\
**Created:** [November 23, 2021, 2:45pm UTC](https://discourse.haproxy.org/t/haproxy-with-postgresql/7124 "2021-11-23T14:45:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![buddicom](https://avatars.discourse-cdn.com/v4/letter/b/b9bd4f/32.png) [@buddicom](https://discourse.haproxy.org/u/buddicom)\
**Post date:** [November 23, 2021, 2:45pm UTC](https://discourse.haproxy.org/t/haproxy-with-postgresql/7124/1 "2021-11-23T14:45:55Z")

</div>

Hi,

For a POC, I am testing a haproxy config to allow access to different Postgresql instances on the same server, based on a hostname.  
Serveur RHEL8 / HAProxy 1.8 / Postgresql 14  
IE :  
iaspX.domain.tld → postgresql1:54321  
iaspY.domain.tld → postgresql2:54322  
etc.

Here is the tested config.

```auto
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    log /dev/log local0 debug

    chroot /var/lib/haproxy
    pidfile /var/run/haproxy.pid
    maxconn 4000
    user haproxy
    group haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode tcp
    log global
    option tcplog
    retries 3
    timeout connect 10s
    timeout client 1m
    timeout server 1m
    timeout check 10s
    maxconn 3000

#---------------------------------------------------------------------
# statistics
#---------------------------------------------------------------------
listen stats
    mode http
    bind *:7000
    stats enable
    stats uri /
    stats refresh 20s
    stats realm PSQL Haproxy\ Statistics # Title text for popup window
    stats show-node
    stats show-legends
    stats show-desc PSQL load balancer stats (master)

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend pgsql_in
    bind *:5432
    mode tcp
    log global
    tcp-request inspect-delay 10s

    use_backend bk_iasp1 if { hdr(host) -m beg pgsql-lat-iasp1. }
    use_backend bk_iasp2 if { hdr(host) -m beg pgsql-lat-iasp2. }

    #default_backend iasp1

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend bk_iasp1
    mode tcp
    option tcp-check
    tcp-check connect
    log global
    server psql1 127.0.0.1:54321 check

backend bk_iasp2
    mode tcp
    log global
    option tcp-check
    tcp-check connect
    server psql2 127.0.0.1:54322 check

```

It seems that the use\_backend directives, depending on the host, do not work. If I add the default\_backend directive, on one of the two postgres instances, the connection works.

Thanks for your help

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [November 23, 2021, 6:29pm UTC](https://discourse.haproxy.org/t/haproxy-with-postgresql/7124/2 "2021-11-23T18:29:15Z")

</div>

You are trying use HTTP Header (“Host”) for a protocol that is not HTTP (but postqresql). What you are trying to do will not work.

---

<div class="post-metadata">

**Author:** ![buddicom](https://avatars.discourse-cdn.com/v4/letter/b/b9bd4f/32.png) [@buddicom](https://discourse.haproxy.org/u/buddicom)\
**Post date:** [November 24, 2021, 9:30am UTC](https://discourse.haproxy.org/t/haproxy-with-postgresql/7124/3 "2021-11-24T09:30:31Z")

</div>

Hi,

I’m a stupid guy !  
I should have thought of that, it is obvious that there are no http headers in tcp mode.

Thnaks
