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
版权声明:本文标题:go - SAM Local Api Docker Network Issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301557a1931221.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论