1

I have installed Mainflux in Cloud Server using Docker. Postgres DB is also running in Docker Container. I have a situation to connect to PostgresDB with Node.Js(programmatically). I found "pg" module to connect to Cloud Postgres DB. But, am unable to connect to Postgres which is running on Docker Container. I pasted my code below, pls let me know the scenario to connect "Docker Postgres".

  const pg = require('pg');
  const conStringPri = postgres://mainflux:mainflux@MYIP/mainflux-things-db;
  const Client = pg.Client;
  const client = new Client({connectionString: conStringPri});
  client.connect();
  client.query(CREATE DATABASE DB_Name)
  .then(() => client.end());

Getting error as below:

node:8084) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED MYIP:5432 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14) (node:8084) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block or by rejecting a promise which was not handled with .catch() (rejection id: 1) (node:8084) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. (node:8084) UnhandledPromiseRejectionWarning: Error: Connection terminated unexpectedly at Connection.con.once (D:\postgres_Nodejs\node_modules\pg\lib\client.js:200:9) at Object.onceWrapper (events.js:313:30) at emitNone (events.js:106:13) at Connection.emit (events.js:208:7) at Socket. (D:\postgres_Nodejs\node_modules\pg\lib\connection.js:76:10) at emitOne (events.js:116:13) at Socket.emit (events.js:211:7) at TCP._handle.close [as _onclose] (net.js:561:12)

1 Answer 1

1

If you started Mainflux by using docker-compose configuration supplied here https://github.com/mainflux/mainflux/blob/master/docker/docker-compose.yml , then your PostgreSQL container doesn't have the port exposed to the host. In order to be able to connect to the database, you need to expose this port.

Here's an example of how the part of the docker-compose would look, with the things-db container having port 5432 (default PostgreSQL port) exposed

   things-db:                                                                                                                                                                
    image: postgres:10.2-alpine                                                                                                                                             
    container_name: mainflux-things-db                                                                                                                                      
    restart: on-failure                                                                                                                                                     
    environment:                                                                                                                                                            
      POSTGRES_USER: mainflux                                                                                                                                               
      POSTGRES_PASSWORD: mainflux                                                                                                                                           
      POSTGRES_DB: things                                                                                                                                                   
    networks:                                                                                                                                                               
      - mainflux-base-net                                                                                                                                                   
    ports:                                                                                                                                                                  
      - 5432:5432  

So you will need to modify your docker-compose.yml. Please note that the Mainflux docker compose has 2 PostgreSQL databases in 2 containers: things-db and users-db.

3
  • Dear Sava, Thank you for an answer. I updated my "docker-compose.yml" file as you suggested above. But, am unable to connect to "PostgresDB" which is running on Docker. If you have any document/code which will connect with Docker Postgres and NodeJs. pls, let me know. Nov 21, 2018 at 8:51
  • You will need to do docker-compose down and then docker-compose up in order to create new containers. Please note that this operation will destroy your current containers and all the data stored in them, so do this only if you don't mind loosing this data.
    – Sava
    Nov 21, 2018 at 11:29
  • yes, my issue solved out. thank you so much Sava. FYI (if we down & up the containers, our data will not be lost. If we kill or remove the containers only our data will be lost.) FYI. Nov 21, 2018 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.