How do you connect to the postgres from a node server while both is started inside a docker environment









up vote
0
down vote

favorite












I'm currently having some some problems with docker and connecting a node project with postgres when running inside it. I use docker toolbox by the way since I don't have windows pro.



my docker-compose file looks like this



version: '3'
services:
db:
image: postgres:latest
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: 123456
POSTGRES_USER: postgres
POSTGRES_DB: dockerdb
network_mode: bridge
container_name: postgres
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql

web:
build: .
ports:
- "5000:5000"
- "3000:3000"
depends_on:
- db
environment:
DB_HOST: db


and my express server is just this, it's alittle bit of a mess at this time.






const Pool = require('pg')
var express = require('express');
var app = express;

const pool = new Pool(
user: 'postgres',
host: process.env.DB_HOST )

// the pool with emit an error on behalf of any idle clients
// it contains if a backend error or network partition happens
pool.on('error', (err, client) =>
console.error('Unexpected error on idle client', err)
process.exit(-1)
)

// callback - checkout a client
pool.connect((err, client, done) =>
if (err) throw err
client.query('SELECT * FROM users WHERE id = $1', [1], (err, res) =>
done()

if (err)
console.log(err.stack)
else
console.log(res.rows[0])

)
)





When I run it in docker, the containers run, I can access postgress db through the terminal and interact with with like creating and inserting things like that, but my node program doesn't seem to connect with it since I don't get any of the console log, I don't know if that is because of a connection problem or just because of some noobie mistakes, can some please help me. By the way I have created a table called users in my init.sql file and given it data, data I been able to access from the docker terminal using the psql command.



here is a picture of what's happening inside docker.
enter image description here










share|improve this question

























    up vote
    0
    down vote

    favorite












    I'm currently having some some problems with docker and connecting a node project with postgres when running inside it. I use docker toolbox by the way since I don't have windows pro.



    my docker-compose file looks like this



    version: '3'
    services:
    db:
    image: postgres:latest
    ports:
    - "5432:5432"
    environment:
    POSTGRES_PASSWORD: 123456
    POSTGRES_USER: postgres
    POSTGRES_DB: dockerdb
    network_mode: bridge
    container_name: postgres
    volumes:
    - ./init.sql:/docker-entrypoint-initdb.d/init.sql

    web:
    build: .
    ports:
    - "5000:5000"
    - "3000:3000"
    depends_on:
    - db
    environment:
    DB_HOST: db


    and my express server is just this, it's alittle bit of a mess at this time.






    const Pool = require('pg')
    var express = require('express');
    var app = express;

    const pool = new Pool(
    user: 'postgres',
    host: process.env.DB_HOST )

    // the pool with emit an error on behalf of any idle clients
    // it contains if a backend error or network partition happens
    pool.on('error', (err, client) =>
    console.error('Unexpected error on idle client', err)
    process.exit(-1)
    )

    // callback - checkout a client
    pool.connect((err, client, done) =>
    if (err) throw err
    client.query('SELECT * FROM users WHERE id = $1', [1], (err, res) =>
    done()

    if (err)
    console.log(err.stack)
    else
    console.log(res.rows[0])

    )
    )





    When I run it in docker, the containers run, I can access postgress db through the terminal and interact with with like creating and inserting things like that, but my node program doesn't seem to connect with it since I don't get any of the console log, I don't know if that is because of a connection problem or just because of some noobie mistakes, can some please help me. By the way I have created a table called users in my init.sql file and given it data, data I been able to access from the docker terminal using the psql command.



    here is a picture of what's happening inside docker.
    enter image description here










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm currently having some some problems with docker and connecting a node project with postgres when running inside it. I use docker toolbox by the way since I don't have windows pro.



      my docker-compose file looks like this



      version: '3'
      services:
      db:
      image: postgres:latest
      ports:
      - "5432:5432"
      environment:
      POSTGRES_PASSWORD: 123456
      POSTGRES_USER: postgres
      POSTGRES_DB: dockerdb
      network_mode: bridge
      container_name: postgres
      volumes:
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

      web:
      build: .
      ports:
      - "5000:5000"
      - "3000:3000"
      depends_on:
      - db
      environment:
      DB_HOST: db


      and my express server is just this, it's alittle bit of a mess at this time.






      const Pool = require('pg')
      var express = require('express');
      var app = express;

      const pool = new Pool(
      user: 'postgres',
      host: process.env.DB_HOST )

      // the pool with emit an error on behalf of any idle clients
      // it contains if a backend error or network partition happens
      pool.on('error', (err, client) =>
      console.error('Unexpected error on idle client', err)
      process.exit(-1)
      )

      // callback - checkout a client
      pool.connect((err, client, done) =>
      if (err) throw err
      client.query('SELECT * FROM users WHERE id = $1', [1], (err, res) =>
      done()

      if (err)
      console.log(err.stack)
      else
      console.log(res.rows[0])

      )
      )





      When I run it in docker, the containers run, I can access postgress db through the terminal and interact with with like creating and inserting things like that, but my node program doesn't seem to connect with it since I don't get any of the console log, I don't know if that is because of a connection problem or just because of some noobie mistakes, can some please help me. By the way I have created a table called users in my init.sql file and given it data, data I been able to access from the docker terminal using the psql command.



      here is a picture of what's happening inside docker.
      enter image description here










      share|improve this question













      I'm currently having some some problems with docker and connecting a node project with postgres when running inside it. I use docker toolbox by the way since I don't have windows pro.



      my docker-compose file looks like this



      version: '3'
      services:
      db:
      image: postgres:latest
      ports:
      - "5432:5432"
      environment:
      POSTGRES_PASSWORD: 123456
      POSTGRES_USER: postgres
      POSTGRES_DB: dockerdb
      network_mode: bridge
      container_name: postgres
      volumes:
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

      web:
      build: .
      ports:
      - "5000:5000"
      - "3000:3000"
      depends_on:
      - db
      environment:
      DB_HOST: db


      and my express server is just this, it's alittle bit of a mess at this time.






      const Pool = require('pg')
      var express = require('express');
      var app = express;

      const pool = new Pool(
      user: 'postgres',
      host: process.env.DB_HOST )

      // the pool with emit an error on behalf of any idle clients
      // it contains if a backend error or network partition happens
      pool.on('error', (err, client) =>
      console.error('Unexpected error on idle client', err)
      process.exit(-1)
      )

      // callback - checkout a client
      pool.connect((err, client, done) =>
      if (err) throw err
      client.query('SELECT * FROM users WHERE id = $1', [1], (err, res) =>
      done()

      if (err)
      console.log(err.stack)
      else
      console.log(res.rows[0])

      )
      )





      When I run it in docker, the containers run, I can access postgress db through the terminal and interact with with like creating and inserting things like that, but my node program doesn't seem to connect with it since I don't get any of the console log, I don't know if that is because of a connection problem or just because of some noobie mistakes, can some please help me. By the way I have created a table called users in my init.sql file and given it data, data I been able to access from the docker terminal using the psql command.



      here is a picture of what's happening inside docker.
      enter image description here






      const Pool = require('pg')
      var express = require('express');
      var app = express;

      const pool = new Pool(
      user: 'postgres',
      host: process.env.DB_HOST )

      // the pool with emit an error on behalf of any idle clients
      // it contains if a backend error or network partition happens
      pool.on('error', (err, client) =>
      console.error('Unexpected error on idle client', err)
      process.exit(-1)
      )

      // callback - checkout a client
      pool.connect((err, client, done) =>
      if (err) throw err
      client.query('SELECT * FROM users WHERE id = $1', [1], (err, res) =>
      done()

      if (err)
      console.log(err.stack)
      else
      console.log(res.rows[0])

      )
      )





      const Pool = require('pg')
      var express = require('express');
      var app = express;

      const pool = new Pool(
      user: 'postgres',
      host: process.env.DB_HOST )

      // the pool with emit an error on behalf of any idle clients
      // it contains if a backend error or network partition happens
      pool.on('error', (err, client) =>
      console.error('Unexpected error on idle client', err)
      process.exit(-1)
      )

      // callback - checkout a client
      pool.connect((err, client, done) =>
      if (err) throw err
      client.query('SELECT * FROM users WHERE id = $1', [1], (err, res) =>
      done()

      if (err)
      console.log(err.stack)
      else
      console.log(res.rows[0])

      )
      )






      node.js postgresql express docker






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 23:40









      Anh Vũ Mai

      64




      64






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          That happens because the db takes some time to start and with your docker compose your server and db start at the same time. So you need some delay on the server to let the DB starts. I have an example in github that you can download and test https://github.com/PedroS11/node-postgres-redis-docker. Check my docker-compose file where you can see I have a script wait-for-it. This script has a sleep so that the server only starts 15s after the database.






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234643%2fhow-do-you-connect-to-the-postgres-from-a-node-server-while-both-is-started-insi%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            That happens because the db takes some time to start and with your docker compose your server and db start at the same time. So you need some delay on the server to let the DB starts. I have an example in github that you can download and test https://github.com/PedroS11/node-postgres-redis-docker. Check my docker-compose file where you can see I have a script wait-for-it. This script has a sleep so that the server only starts 15s after the database.






            share|improve this answer
























              up vote
              0
              down vote













              That happens because the db takes some time to start and with your docker compose your server and db start at the same time. So you need some delay on the server to let the DB starts. I have an example in github that you can download and test https://github.com/PedroS11/node-postgres-redis-docker. Check my docker-compose file where you can see I have a script wait-for-it. This script has a sleep so that the server only starts 15s after the database.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                That happens because the db takes some time to start and with your docker compose your server and db start at the same time. So you need some delay on the server to let the DB starts. I have an example in github that you can download and test https://github.com/PedroS11/node-postgres-redis-docker. Check my docker-compose file where you can see I have a script wait-for-it. This script has a sleep so that the server only starts 15s after the database.






                share|improve this answer












                That happens because the db takes some time to start and with your docker compose your server and db start at the same time. So you need some delay on the server to let the DB starts. I have an example in github that you can download and test https://github.com/PedroS11/node-postgres-redis-docker. Check my docker-compose file where you can see I have a script wait-for-it. This script has a sleep so that the server only starts 15s after the database.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 14:11









                Pedro Silva

                37311




                37311



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234643%2fhow-do-you-connect-to-the-postgres-from-a-node-server-while-both-is-started-insi%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo