Connection to local database refused (testing Go function)

My lambda function written in Golang is not able to connect to the postgres database that is running locally on my machine ( or localhost:5432). The credentials and the function code are correct! I ensured it by copying the code and running it from a regular Go app.

This is the error message I am getting:

{"errorType":"OpError","errorMessage":"dial tcp connect: connection refused"}


Error --------------------------------------------------
  Error: Failed to run docker for go1.x image (exit code 1})
      at /Users/me/Projects/my-functions/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:536:21
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
  Get Support --------------------------------------------
  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              14.3.0
     Framework Version:         2.15.0 (local)
     Plugin Version:            4.2.0
     SDK Version:               2.3.2
     Components Version:        3.4.3

So somehow when invoking the function with sls, it cannot access the postgres service running on my machine I assume. This is the command I use: sls invoke local -f myFunction

I need help to get this to work :frowning:

I have found an argument docker-arg in the CLI reference. This could be the solution (--docker-arg "-p 5432:5432) but unfortunately this argument seems to have a bug here. I just created an issue

Ok passing this docker-arg did not help in my case. I still need help with this issue.

Error: Failed to run docker for go1.x image (exit code 1})

Currently, invoke local only supports the NodeJs and Python runtimes.

Thank you for your answer! I do not think this is true though. Invoke local in fact does work very well with Go. It is only when I try to connect to my local database, it will error and then crash, which will make the container it is running in crash, thus this error message.

Unless you meant only NodeJS and Python support invoking a function using the local runtime versions of NodeJS/ Python (so without docker). But even when using docker (which I have to apparently), there should be a way to connect to my local db.

Did you do a deploy? Then your codes are run in your provider now (aws?). It makes sense that your localhost is not accessible there

No, I did not deploy. Actually, I load the db host from an environment variable. So only locally, it will be localhost.