olá estou tomando o erro abaixo quando tento rodar a api
[error] failed to initialize database, got error failed to connect to `host=postgres user=root database=root`: hostname resolving error (lookup postgres on 127.0.0.53:53: server misbehaving)
2025/03/17 23:42:59 Erro ao conectar com banco de dados
panic: Erro ao conectar com banco de dados
goroutine 1 [running]:
log.Panic({0xc0000e7ef8?, 0xc00007cd70?, 0xc00024a070?})
/home/cynthia.tong/.asdf/installs/golang/1.23.2/go/src/log/log.go:432 +0x5a
github.com/tsaiyuen/curso-alura-api/database.ConectaComBancoDeDados()
/home/cynthia.tong/golang/alura/api-rest/database/db.go:20 +0x13e
main.main()
/home/cynthia.tong/golang/alura/api-rest/main.go:10 +0x17
exit status 2
Já troquei o localhost por posgres, ja reiniciei a maquina e tudo, e já alterei o codigo conforme ultimas orientações.
db.go
package database
import (
"log"
"github.com/tsaiyuen/curso-alura-api/models"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var (
DB *gorm.DB
err error
)
func ConectaComBancoDeDados() {
stringDeConexao := "host=postgres user=root password=root dbname=root port=5432 sslmode=disable"
DB, err = gorm.Open(postgres.Open(stringDeConexao), &gorm.Config{})
if err != nil {
log.Panic("Erro ao conectar com banco de dados")
}
DB.AutoMigrate(&models.Aluno{})
}
docker-compose.yml
version: '3'
services:
postgres:
image: "postgres"
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
- POSTGRES_DB=root
ports:
- "5432:5432"
volumes:
- ./migration/docker-database-initial.sql:/docker-entrypoint-initdb.d/docker-database-initial.sql
pgadmin-compose:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: "cyn@alura.com"
PGADMIN_DEFAULT_PASSWORD: "123456"
ports:
- "54321:80"
depends_on:
- postgres