Trying to run docker-compose up in a project for the very first time and you ran into a problem? Don’t be afraid. It’s normal for errors to occur when running new technologies on the first try.

For my case, I have a Rails app which was Dockerized a few months ago. I had no experience with Docker, and I had no idea what to expect when running all those fancy docker-compose commands. I did run into an error after running docker-compose up, and this error is something some of you may encounter. 🤓

api_1      | /usr/local/bundle/gems/pg-1.0.0/lib/pg.rb:56:in `initialize': could not connect to server: Connection refused (PG::ConnectionBad)
api_1      |     Is the server running on host "db" (172.20.0.2) and accepting
api_1      |     TCP/IP connections on port 5432?

WAS DER FUCHS! 😱 I checked to make sure that the database exists, and I also ran docker ps to see the port that’s being used for postgres, and it’s indeed 5432. Then, I found this post.

In the app’s database.yml file, the host is set to db. Previously, host is set to 127.0.0.1, which is localhost! Based on what was written in the Stack Exchange post linked above, I opened my postgresql.conf file, scrolled down to listen_addresses, and set it to the following.

listen_addresses = 'localhost,db'

Afterwards, restart Postgres, and run docker-compose up for the relevant app once again.
The database should be running! Hope the post has helped you. ✌️

Pardon my Postgres. Here’s a link on what is listen_addresses.