aws-sam-cli: Can not connect to local database on Ubuntu

I get timeout error or connection to server at "one of the host names that i use" port 5432 failed: Connection refused when trying to connect to a postgresql that I have locally on my OS, Ubuntu 18.04.6 LTS, from a lambda that is running in sam.

In pg_hba.conf I set 0.0.0.0/0 for all IPv4 connection just to ensure that it is not an issue. Database is working fine as I can use it from other applications that I am running on my local machine, but they are not using sam.

I used all possible combinations for the database host: 172.17.0.1 which is address of docker0, localhost, 127.0.0.1, host.docker.internal And commands to run aws sam: sam local start-api --docker-network host, sam local start-api, sam local start-api --container-host localhost --container-host-interface 0.0.0.0 --docker-network host, sam local start-api --container-host localhost --container-host-interface 0.0.0.0 and some others.

Result is the same, either timeout error or connection to server at {one of the host addresses} port 5432 failed

SAM CLI, version 1.56.1

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 20 (4 by maintainers)

Commits related to this issue

Most upvoted comments

I haven’t tried it since I need to connect from SAM to local PG instance which is not in docker, but running locally. I and my teammate tried to use SAM on his machine, with Ubuntu 22.04 LTS , same problem. I am pretty sure it is universal problem.

Thanks @GuillaumeDesforges for providing the solution. In short, create a docker network and connect your local database through the network.

Closing as a solution is provided. Feel free to open a new issue if you have any further question.

Also I was able to run dockerized (non SAM) backend server and connect to it from my machine.