Score:0

Connecting to hosts postgresql from django app running in a docker container

in flag

I am trying to connect to my hosts postgresql server (version 14) from my django app running in a docker container.

here are my settings

docker-compose.yml:

version: '3'
services: 
   web:
     build: .
       container_name: dm-api
     volumes: 
       - .:/code
       - static:/code/dojo_manager/static
       - media:/code/dojo_manager/media
     expose: 
       - 8080
     extra_hosts:
       - "database:172.17.0.1"
     command: bash -c "systemctl restart cron && python manage.py collectstatic --no input && python manage.py migrate && gunicorn --workers=3 dojo_manager.wsgi -b 0.0.0.0:8080"
   nginx:
     restart: always
     build: ./nginx/
     volumes: 
        - ./nginx/:/etc/nginx/conf.d
        - ./logs/:/code/logs
        - static:/code/static
        - media:/code/media
     ports: 
        - "1221:80"
     links:
        - web  
 volumes: 
   media:
   static:  

pg_hba.conf:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             172.17.0.0/16           md5
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer

ifconfig:

 docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
    inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
    inet6 fe80::42:41ff:fe7c:b9e9  prefixlen 64  scopeid 0x20<link>
    ether 02:42:41:7c:b9:e9  txqueuelen 0  (Ethernet)
    RX packets 40651  bytes 6850716 (6.8 MB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 51087  bytes 570351402 (570.3 MB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

postgresql.conf:

listen_addresses = '*'

however when I start the container I get the error:

django.db.utils.OperationalError: connection to server at "database" (172.17.0.1), port 5432 failed: FATAL:  no pg_hba.conf entry for host "172.19.0.2", user "dtuser", database "dtdb", SSL encryption
connection to server at "database" (172.17.0.1), port 5432 failed: FATAL:  no pg_hba.conf entry for host "172.19.0.2", user "dtuser", database "dtdb", no encryption

I have tried all sorts of online resources and documentations. However, I cant seem to get the connection running.

Any tips/help is much appreciated. Best Benedict

Score:0
us flag

The problem is quite litereally in that error message. There is no hba entry for the host 172.19.0.2. You only allow connections from 172.16.0.0/16. What you Probably want is 172.16.0.0/12

bwright avatar
in flag
thanks i just added ```host all all 172.19.0.0/16 md5``` and it worked . no idea how i missed this.. been working on it for hours
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.