W
W
Wayne12121212021-05-25 11:01:19
PostgreSQL
Wayne1212121, 2021-05-25 11:01:19

How to access from the container to the database, which works locally on localhost:5432?

Hello!
I'm trying to connect from a container to a database that works locally at localhost:5432, but the database is not visible from the container.

//docker-compose
version: "3"
services:
    server:
      image: node:latest
      command: sh -c "npm install && npm run dev"
      volumes:
        - ./backend:/usr/src/client
      working_dir: /usr/src/client
      ports:
        - "9001:9001"
      restart: always
      tty: true

//пытаюсь подключиться к локальной базе
const { Pool } = require('pg');

const pool = new Pool({
    host: '127.0.0.1',
    user: 'andrejkolosov',
    password: 'ipadib339',
    port: 5432,
    database: 'docker'
});

module.exports = pool

//в итоге такая ошибка
node:internal/process/promises:246
server_1  |           triggerUncaughtException(err, true /* fromPromise */);
server_1  |           ^
server_1  | 
server_1  | Error: connect ECONNREFUSED 127.0.0.1:5432
server_1  |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1133:16) {
server_1  |   errno: -111,
server_1  |   code: 'ECONNREFUSED',
server_1  |   syscall: 'connect',
server_1  |   address: '127.0.0.1',
server_1  |   port: 5432
server_1  | }


As I understand it, the container looks inside itself at the address 127.0.0.1:5432 and does not find it from there. How to make it look where it needs to?
I would be very grateful for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-05-25
@q2digger

Instead of 127.0.0.1 , use the address of the host system, for example 192.168.1.1 (replace with your own).
You can first check that postgress is "listening" on this port of this interface (netstat -tlnp | grep 5432).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question