admin管理员组

文章数量:1122832

I'm working in a set of lambda funcitions that connects to a Postgres DB. To do so, I'm using SAM. My issue comes when I'm trying to connect to my DB. I can connect from my local, but I cannot connect from the local lambda. The reason that I think is not connecting is due some docker network issue. I tried to do:

docker network create my-sam-network

docker volume create postgres_data

docker run -d --name postgres_db_new --restart always --network my-sam-network -e POSTGRES_USER=local_db -e POSTGRES_PASSWORD=local_db -e POSTGRES_DB=vigitrack -p 5432:5432 -v postgres_data:/var/lib/postgresql/data postgres:15

But when I run my lambda using:

sam local start-api --docker-network 72a7d526bfc1ea9f0556b8349539d256c7519d05143920087dec0e05f2d73e45

I get:

2024/11/22 18:29:18 Failed to ping the database: dial tcp [::1]:5432: connect: connection refused

My code is pretty simple:

func (p *PostgresDB) Connect(ctx context.Context) error {

    db, err := sql.Open("postgres", p.databaseUrl)

    if err != nil {
        return err
    }

    if err := db.Ping(); err != nil {
        log.Fatalf("Failed to ping the database: %v", err)
        return err
    }

    p.db = db
    return nil
}

And my connection string:

connStr := "host=localhost port=5432 user=local_db password=local_db dbname=vigitrack sslmode=disable"

I'm not sure what I'm doing wrong. Any ideas what might be happening?

Thanks for your help!

本文标签: goSAM Local Api Docker Network IssueStack Overflow