1

Not sure why am I getting the SequelizeConnectionRefusedError. I verified that I am able to run all my docker images locally but when I try to run 'docker-compose up' command, I am running into Error: connect ECONNREFUSED 127.0.0.1:5432. enter image description here

Milan Shah
  • 205
  • 1
  • 3
  • 15
  • 2
    It would be much better if you could copy and paste the content of the docker-compose file, and not the a screen shot. – Vahid Mar 18 '20 at 01:28
  • Do you connect to a postgresql on the code? – Vahid Mar 18 '20 at 01:28
  • Yes, thats right. I am connecting to postressql. – Milan Shah Mar 18 '20 at 01:51
  • Are you sure it's running? If you are, I suggest adding network_mode: host to your services in the docker-compose file (all of them). Check this out as an example https://stackoverflow.com/a/60686968/2596409 let me know how it goes – Vahid Mar 18 '20 at 01:57
  • Just tried with your suggestion and still getting the same error. And yes, my instance is currently running as I am able to connect to my postgressql in Postbird. – Milan Shah Mar 18 '20 at 02:23
  • `docker-compose` doesn't understand image files. Can you replace this with the actual text of your `docker-compose.yml` file? Is there anything else a reader would need to reproduce the issue? Have you looked at other SO questions about getting "connection refused" errors talking to databases in Docker as 127.0.0.1? – David Maze Mar 18 '20 at 11:02

1 Answers1

5

Based on my understanding of your question, here are my assumptions:

  • You are using MacOS
  • Your Postgres server is running in the host OS instead of in another docker container.

With that being said, this is a common problem with MacOS users who want to connect their docker containers to the Postgres server running in the host machine. As they are not in the same network, there is no way for your container to reach the Postgres server and hence, connecting to it via 127.0.0.1:5432 will definitely not reachable.

It will be trivial to solve in a Linux machine by adding network_mode: host so that the containers will be running in the same network as host machine hence is able to reach the Postgres server. However, due to the implementation of Docker on Mac where Docker host is actually being run in a hidden VM on top of your MacOS, this solution will not work here.

Some suggestions:

  1. Migrate your Postgres server to run in a docker container (in the same docker-compose file if you will). You can always do a port mapping in order to access it from your Postbird.
  2. Or if you still insist on running it locally in your MacOS, here is a workaround that involves creating another docker container in the same docker network and perform a revert SSH tunneling.

Here are the steps to migrate the Postgres server to using docker container

  1. Update your docker-compose with a new db service:
db:
    image: postgres:10.5-alpine
    environment:
        POSTGRES_USER: $UDAGRAM_USERNAME
        POSTGRES_PASSWORD: $UDAGRAM_PASSWORD
        POSTGRES_DB: $UDAGRAM_DATABASE
    ports:
        - 35432:5432
    volumes:
        - <path where you want to persist your database data>:/var/lib/postgresql/data
  1. You can now connect to your new postgres using Postbird at localhost:35432

EDIT 1

If you run your Postgres instance in AWS RDS, you will not need to make the changes above but follow other steps:

  • Make sure that your network can reach the RDS endpoint at port 5432. A best practice here is to update the security group inbound rules to allow only port 5432 from only your IP address (how to do that is out of the scope of this answer but can easily be found from AWS documentation)
  • Update the value of UDAGRAM_HOST to be the RDS endpoint which can be found from the AWS RDS console.
Nguyen Lam Phuc
  • 1,411
  • 1
  • 7
  • 12
  • ahh that makes sense! Thank you for your reply. Can you please guide me on how can I migrate my postgres server into docker contrainer? I am still at the learning curve for docker :) – Milan Shah Mar 18 '20 at 03:40
  • My postgres instance is actually running in the RDS. Hence, I am connecting there directly from the container using my env variables. Do I still need to make this change? – Milan Shah Mar 18 '20 at 14:22