# Connection to local database refused (testing Go function)

**URL:** <https://forum.serverless.com/t/connection-to-local-database-refused-testing-go-function/13320>\
**Category:** Serverless Framework\
**Tags:** aws, lambda\
**Created:** [December 6, 2020, 5:37pm UTC](https://forum.serverless.com/t/connection-to-local-database-refused-testing-go-function/13320 "2020-12-06T17:37:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mg98](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/mg98/32/4835_2.png) [@mg98](https://forum.serverless.com/u/mg98)\
**Post date:** [December 6, 2020, 5:37pm UTC](https://forum.serverless.com/t/connection-to-local-database-refused-testing-go-function/13320/1 "2020-12-06T17:37:49Z")

</div>

My lambda function written in Golang is not able to connect to the postgres database that is running locally on my machine (`127.0.0.1:5432` 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:

```auto
{"errorType":"OpError","errorMessage":"dial tcp 127.0.0.1:5432: connect: connection refused"}

```

Further…

```auto
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 --------------------------------------------
     Docs: docs.serverless.com
     Bugs: github.com/serverless/serverless/issues
     Issues: forum.serverless.com
 
  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 ☹

---

<div class="post-metadata">

**Author:** ![mg98](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/mg98/32/4835_2.png) [@mg98](https://forum.serverless.com/u/mg98)\
**Post date:** [December 8, 2020, 9:26am UTC](https://forum.serverless.com/t/connection-to-local-database-refused-testing-go-function/13320/2 "2020-12-08T09:26:12Z")

</div>

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 [https://github.com/serverless/serverless/issues/8587](https://github.com/serverless/serverless/issues/8587)

---

<div class="post-metadata">

**Author:** ![mg98](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/mg98/32/4835_2.png) [@mg98](https://forum.serverless.com/u/mg98)\
**Post date:** [December 9, 2020, 12:49pm UTC](https://forum.serverless.com/t/connection-to-local-database-refused-testing-go-function/13320/3 "2020-12-09T12:49:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DmitryNefedov](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@DmitryNefedov](https://forum.serverless.com/u/DmitryNefedov)\
**Post date:** [December 11, 2020, 3:55am UTC](https://forum.serverless.com/t/connection-to-local-database-refused-testing-go-function/13320/4 "2020-12-11T03:55:56Z")

</div>

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

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

> **[Serverless Framework Commands - AWS Lambda - Invoke](https://www.serverless.com/framework/docs/providers/aws/cli-reference/invoke/)**
>
> Invoke an AWS Lambda Function using the Serverless Framework

---

<div class="post-metadata">

**Author:** ![mg98](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/mg98/32/4835_2.png) [@mg98](https://forum.serverless.com/u/mg98)\
**Post date:** [December 11, 2020, 1:24pm UTC](https://forum.serverless.com/t/connection-to-local-database-refused-testing-go-function/13320/5 "2020-12-11T13:24:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jonee](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jonee](https://forum.serverless.com/u/jonee)\
**Post date:** [December 13, 2020, 3:31pm UTC](https://forum.serverless.com/t/connection-to-local-database-refused-testing-go-function/13320/6 "2020-12-13T15:31:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mg98](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/mg98/32/4835_2.png) [@mg98](https://forum.serverless.com/u/mg98)\
**Post date:** [December 14, 2020, 8:34am UTC](https://forum.serverless.com/t/connection-to-local-database-refused-testing-go-function/13320/7 "2020-12-14T08:34:50Z")

</div>

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