Unable to connect to External PostgreSql Database from a Application Docker Container

Hi,

I have installed PostgreSql on my machine which I want to use a external DB for the Dockerized Laravel Web app.

I donot want to use the postgre image, need to use the local DB, it can be mysql as well.


docker-compose.yml

version: ‘3’
services:
app:
build:
context: .
dockerfile: .docker/Dockerfile
image: docker-app
ports:
- 8004:80

.env

DB_CONNECTION=pgsql
DB_HOST= 127.0.0.1 or localhost
DB_PORT=5432
DB_DATABASE=testdb
DB_USERNAME=postgres
DB_PASSWORD=password

Error is given below:

SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host “127.0.0.1” and accepting TCP/IP connections on port 5432?**


The reason of the issue I know the application container is not connecting to the external DB as it is outside of its filesystem.

If I change the DB_HOST to the local IP Address, there comes up the below error:
SQLSTATE[08006] [7] FATAL: no pg_hba.conf entry for host “10.0.9.57”,

Urgent Help would be highly appreciated.

regards

Hi babars,

This is not a docker but a Postgres configuration issue. You just need to add your server ip adress to pg_hba.conf.

Please visit the followin link to get more information:

https://www.postgresql.org/docs/9.2/auth-pg-hba-conf.html

Have a nice day.