title: NodeJS is not able to access postgres database
body: <p>My app.js file is as follows</p>
  <pre><code>var express = require('express');
  var app = express();
  var uuid = require('node-uuid');
  
  var pg = require('pg');
  var conString = process.env.DB; // &quot;postgres://username:password@localhost/database&quot;;
  
  // Routes
  app.get('/api/status', function(req, res) {
    pg.connect(conString, function(err, client, done) {
      if(err) {
        return res.status(500).send('error fetching client from pool');
      }
      client.query('SELECT now() as time', [], function(err, result) {
        //call `done()` to release the client back to the pool
        done();
  
        if(err) {
          return res.status(500).send('error running query');
        }
  
        return res.json({
          request_uuid: uuid.v4(),
          time: result.rows[0].time
        });
      });
    });
  });
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  
  // error handlers
  
  // development error handler
  // will print stacktrace
  if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
      res.status(err.status || 500);
      res.json({
        message: err.message,
        error: err
      });
    });
  }
  
  // production error handler
  // no stacktraces leaked to user
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.json({
      message: err.message,
      error: {}
    });
  });
  
  
  module.exports = app;
  </code></pre>
  <p>and my Dockerfile is as follows</p>
  <pre><code>FROM node:14
  
  WORKDIR /app
  
  COPY package*.json ./
  
  RUN  npm install
  
  COPY . .
  </code></pre>
  <p>my docker-compose file is as follows:</p>
  <pre><code>version: &quot;3.2&quot;
  services:
    db:
      image: postgres
      environment:
        POSTGRES_PASSWORD: test123
        POSTGRES_DB: testing
      volumes:
        - ./pgdata:/var/lib/postgresql/data
    web:
      build: .
      command: npm start app.js
      depends_on:
        - db
      environment:
        DB: 'postgres://postgres:test123@localhost:5432/testing'
      ports:
        - &quot;3000:3000&quot;
  </code></pre>
  <p>When I am checking into the container DB, I can see the user, database but my app.js file cannot fetch a value from the database.
  I am routing the data in this endpoint <strong>/api/status</strong>
  when I am accessing http://ip-address:3000 == then i am getting 404 error which I mentioned in my code.
  without docker, I can easily access my output at the given endpoint.</p>
  <p>Any help will be helpful. Thanksss</p>