Reputation: 334
i have problem with connect to my postgresql database with node.js running dockerized postgresql server like that (https://docs.docker.com/engine/examples/postgresql_service/). can connect with pgadmin, but cant connect with node. didnt see any process on this port. what error i see:
MacBook-Pro-Maxim:nodejs lucio$ docker logs 5cc44d01bf4ada729e5f1c71157e4eba5cb6fa9fd40122f384b423fbd1ae13a7
2019-12-28T12:38:53: PM2 log: Launching in no daemon mode
2019-12-28T12:38:53: PM2 log: App [index:0] starting in -fork mode-
2019-12-28T12:38:53: PM2 log: App [index:0] online
{ Error: connect ECONNREFUSED 127.0.0.1:32775
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 32775 }
database.js here is simple example of my DataBase class. just constuctor of params for connecting and simpe query request method.
const {
Pool,
Client
} = require('pg')
const sqlQueryGenerator = require('sql-query-generator')
const sqlGenerator = require('sql')
sqlGenerator.setDialect('postgres')
sqlQueryGenerator.use('postgres')
class DataBase {
constructor(params) {
this.base = new Pool(params)
}
query(text) {
return new Promise((resolve, reject) => {
this.base.query(text, (err, res) => {
if (err) reject(err)
resolve(res)
})
})
}
}
module.exports = DataBase
using this module in my api.js file
require('dotenv').config({ path: './.env' })
const DataBase = require('./database')
const db = new DataBase({
user : process.env.USER,
host : process.env.HOST,
database : process.env.TABLE,
password : process.env.PASSWORD,
port : process.env.PORT,
})
here is my .env file
HOST = localhost
USER = docker
TABLE = docker
PASSWORD = docker
PORT = 32775
before run container with node.js api i checking actual port of my postgresql docker container. how to solve my problem? thx for feedback
Upvotes: 4
Views: 2266
Reputation: 59946
You can not connect to DB container using localhost
, here localhost
mean the Nodejs container not the DB container.
{ Error: connect ECONNREFUSED 127.0.0.1:32775
The connection looking for DB in the nodejs container.
To connect with DB container you have two option.
HOST = db_container_name
USER = docker
TABLE = docker
PASSWORD = docker
PORT = 5432
HOST = HOST_IP
USER = docker
TABLE = docker
PASSWORD = docker
PORT = 32775
Or better to use docker-compose that handle networking for you and you can refer any container to connect using the name of the container.
Upvotes: 2
Reputation: 181
your node js application is running with docker right ? If this is the case you have to put the two containers on the same network ( the simplest solution ) , you can do that with docker compose that automatically put all containers to the same network and then you can contact another container by its name. Or you can inspect the DB container with :
docker container inspect DB_CONTAINER
Find the NetworksSettings -> Networks --> Here you will find the network name
Then you can connect your node js container to the same network
docker network connect NETWORK_NAME NODE_CONTAINER_NAME
Now you should be able to connect node container to the DB by the DB container host name ( by default is the id of the container , but you can specify an hostname with --hostname argument during docker run command execution )
Hope this help you!!!
Upvotes: 0