0

I apologize for the trouble, but I have searched similar issues but to no avail.

I have a Postgres Server (PgServer) running inside Docker (attached is an image). It works fine with no issues. I ran this PgServer using the following command

docker run --name PgServer --network=brapi_network -e POSTGRES_PASSWORD=****-d postgres

enter image description here

The application properties of this docker container, or jdbc database (sorry still a beginner at these technical terms) is as follows

server.port = 8080
server.servlet.context-path=/brapi/v2

spring.datasource.url=jdbc:postgresql://PgServer:5432/brapi_local
spring.datasource.username=postgres
spring.datasource.password=******

I have a very simple node client that tries to connect to this database and does a simple select * from table query. Code is below:

const {Client} = require('pg')

const client = new Client({
    host: "PgServer",
    user: "postgres",
    port: 8080,
    password: "*****",
    database: "brapi_local"

})

Whenever I try to run this node client app, I always get the following error

(node:4448) UnhandledPromiseRejectionWarning: Error: getaddrinfo ENOTFOUND PgServer
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26)

What could I be doing wrong?

jpineds
  • 119
  • 4
  • 15
  • Is the application also running in a container? How did you start the container(s)? – David Maze Jul 12 '21 at 11:28
  • the node client is not running inside docker. I started the PgServer docker using docker run --name PgServer --network=brapi_network -e POSTGRES_PASSWORD=*** -d postgres – jpineds Jul 12 '21 at 11:40
  • 1
    If the database is in a container, and the client isn't, then you need to start the container with a `docker run -p` option to publish ports out of Docker, and then you need to connect to `127.0.0.1` or the host's IP address or DNS name. – David Maze Jul 12 '21 at 13:18
  • In this case, the host is PgServer (the docker container), right? – jpineds Jul 12 '21 at 13:25
  • If you can connect to your system using the host name `pgserver.example.com` because your network's DNS server provides that, then it will work, but that's probably not the case. The name of the container is irrelevant and can't be used here. – David Maze Jul 12 '21 at 13:26
  • Thanks @DavidMaze. Exposing the database during docker run was indeed the solution. Thanks for your help! – jpineds Jul 12 '21 at 13:41

0 Answers0