Depending on HOST NAME value for PostgreSQL database in my .env file I get different result - node.js

I have created a PostgreSQL with a web server DOCKER CONTAINER and my app running with an API to manage all the endpoints.
The link of my git repo is: https://github.com/JulioValderrama/store-front-project.git
But I am facing problems with the connections with the Database, as depending on the Postgres HOST name value in my .env file one of the two servers (one local running in PORT 4000 and the web server running in the docker container in PORT 3000) will work or will fail.
I have tried the next:
Local server running in PORT 4000
Docker web server running in PORT 3000
HOST "127.0.0.1"
Going to¨:
http://localhost:3000/
Works fine and get response, now when trying to connect it to any of my database API:
http://localhost:3000/products
I get the error:
"Could not get PRODUCTS. Error: Error: connect ECONNREFUSED 127.0.0.1:5432"
Going to:
http://localhost:4000
Works fine and get response, now when trying to connect it to any of my database API:
http://localhost:4000/products
It works! I get the list of all my products!!
HOST "postgres"
I put "postgres" because I read online that you have to name HOST as the postgres docker image created, which is my case. And it works for the remote server.
Going to:
http://localhost:3000
Works fine and get response, then when trying to connect it to database API:
http://localhost:3000/products
It works!! It gives me the list of my products !!
Going to:
http://localhost:4000
Works fine and get response, then when trying to connect it to database API:
http://localhost:4000/products
It gives me the error:
"Could not get PRODUCTS. Error: Error: getaddrinfo ENOTFOUND postgres"
So it seems like there is an error when trying to connect to the database due to the server or the HOST name, I have no idea....

In docker-compose.yaml you have linked your machine 5432 to containers 5432 port.
If you are using docker container and want to reach postgres, use postgres as POSTGRES_HOST value.
If you are running application outside the docker environment use 0.0.0.0, 127.0.0.1.
Make sure you haven't installed postgres locally
brew uninstall postgres

Related

Nodejs app in Docker not accessible on localhost:3000

I am trying to run a Node application in a Docker container. The installation instructions specified that after adding host: '0.0.0.0' to config/local.js and running docker-compose up the app should be accessible at localhost:3000, but I get an error message in the browser saying "The connection was reset - The connection to the server was reset while the page was loading."
I have tried to add the host: '0.0.0.0' in different places, or remove it entirely, access https://localhost:3000 as opposed to at http, connecting to localhost:3000/webpack-dev-server/index.html, etc.
What could be going wrong?

Why connection to postgres from nodejs is not working?

I have back-end created in nodejs hosted on Debian server using postgres 14.
the Nodejs listenning on port 5000, and when I try to connect to postgres using port 5432, I am getting the following reponse from Postman:
"Password is invalid".
I was trying the postgres role, and to create a new role, and problem is the same.
I was installing postgres on my Windows 10 computer, and all works fine in my localhost, but on the remote server not.
My Nodejs back-end, and postgres are on the same computer, but each role I've tryed won't connect to the database.
There is pg_hba.conf in /etc/postgres/14 on your server you have to add:
host username database 0.0.0.0/0 md5
This would allow connection to database from any host to username using password authentication.
Also there is a pg.conf or postgres conf
There is line allow_connections the value should be '*'
allow_connections='*'
This would generally allow connections to postgres running on that server
Guide https://coderwall.com/p/cr2a1a/allowing-remote-connections-to-your-postgresql-vps-installation
don’t forget you have to reload postgres after config change:
systemctl reload postgres

Connect local postgres database from PGAdmin Container

I am trying to connect to my local postgres database from PGadmin in container, but it is through following error:
Unable to connect to Server: connection to server at "localhost" (127.0.0.1), port 5432 failed. connection refused is the server running on that host and accepting TCP/IP connections?
configurations i am trying for creating server are as follows:
hostname/address = localhost
port = 5432
username = postgres
my OS configurations
OS = Linux- Ubuntu
Version = 18.04
PS: there are some existing questions regarding this on Stackoverflow, but no solution helped me.
While it runs locally, the docker container is like a different machine. You need to put the IP address of the local host machine instead of localhost.

Error: connect ECONNREFUSED 127.0.0.1:5432 docker-compose up

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.
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:
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.
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
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
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.

Error connecting to my redis server from my code

io.adapter(redis({ host: config.redisHost, port: config.redisPort }));
Both the confif.redisHost and config.redisPort are the correct values, but when I try to connect from my code I get the error -
'Error: Redis connection to 104.xxx.xx.xxx:6379 failed - connect ECONNREFUSED 104.xxx.xx.xxx:6379'
In redis.conf I've changed the bind to 0.0.0.0 as well as removing it completely and I've also tried setting protected mode to off just to try and get the connection working.
The IP and port are definitely, correct. Does anyone know why I might not be able to access the server from my code? My code is just being ran on my local machine and the redis server is being ran on a digital ocean ubuntu 14.04 machine. The status of the server is definitely running and I can access the redis-cli from the machine itself.
For some reason, it wasn't using redis.conf when I tried starting the server, I had to manually tell it to use it when starting up, now it's working.

Resources