connorjcantrell / syndicate

Learning Go and Docker by building a web application. Application stores HVAC equipment specifications and relevant distributors into a postgres database container. Front end is built using Tailwind CSS.
0 stars 2 forks source link

Error connecting to postgres database running on Docker #5

Open connorjcantrell opened 3 years ago

connorjcantrell commented 3 years ago

Problem

Unable to connect to database when attempting to run main.go:

$ go run cmd/syndicate/main.go
2021/01/25 16:37:25 error connecting to database: dial tcp: lookup db: no such host

The only suspicion that comes to mind is perhaps my database URL is incorrect. postgres://postgres:secret@db:5432/syndicate?sslmode=disable

I have verified my postgres container is running:

$ docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                    NAMES
ab5c8cbe9702   adminer    "entrypoint.sh docke…"   2 minutes ago   Up 2 minutes   0.0.0.0:8080->8080/tcp   syndicate_adminer_1
7a7fd4890e4a   postgres   "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   0.0.0.0:5432->5432/tcp   syndicate_db_1
$ docker-compose ps
       Name                      Command               State           Ports         
-------------------------------------------------------------------------------------
syndicate_adminer_1   entrypoint.sh docker-php-e ...   Up      0.0.0.0:8080->8080/tcp
syndicate_db_1        docker-entrypoint.sh postgres    Up      0.0.0.0:5432->5432/tcp
connorjcantrell commented 3 years ago

For what its worth, I received a new error after trying a different database url:

postgres://postgres:secret@5432:5432/syndicate?sslmode=disable

After attempting:

syndicate $go run cmd/syndicate/main.go
2021/01/25 18:34:08 error connecting to database: dial tcp 0.0.21.56:5432: connect: no route to host
exit status 1

The error has changed at least? Maybe a good sign? 🤔

connorjcantrell commented 3 years ago

Aram Fingal from 'dockercommunity.slack.com' writes:

"

since there is no indication you're running the go code inside a container, try it with

postgres://postgres:secret@localhost:5432/syndicate?sslmode=disable

"

After attempting:

syndicate $go run cmd/syndicate/main.go
2021/01/25 19:24:30 error connecting to database: pq: role "postgres" does not exist
exit status 1

New error, but I think I'm getting close to solving