migrate icon indicating copy to clipboard operation
migrate copied to clipboard

PostgreSQL: connection refused for golang-migrate

Open dominikbraun opened this issue 3 years ago • 3 comments

Describe the Bug

When connecting to a PostgreSQL server using golang-migrate, I run into a connection refused error. Everything works using another database client (I'm using sqlx) though.

Steps to Reproduce Steps to reproduce the behavior:

This is my code:

m, err := migrate.New(
	"file:///migrations",
	"postgres://productservice:productservice@postgresql:5432/productservice?sslmode=disable")
if err != nil {
	log.Printf("%s\n", err.Error())
}

m.Steps(2)

Expected Behavior

The database connection should be established. Instead, I run into this error:

dial tcp 10.96.190.196:5432: connect: connection refused

When using my own sqlx client, the connection is established without any problems.

Migrate Version

migrate/v4

Loaded Source Drivers

file

Loaded Database Drivers postgres

Go Version

go1.17.5 darwin/amd64

Additional context PostgreSQL is running inside a minikube cluster.

dominikbraun avatar Dec 29 '21 14:12 dominikbraun

Have you resolved this problem? I had the same problem.

lilely avatar Jan 23 '22 16:01 lilely

@lilely Not quite, I'm now using the golang-migrate Docker image and running that, which is sufficient for my case.

This didn't work either at the first try, but that was due to my Kubernetes Job not being within the service mesh and trying to connect to a meshed database. My issue here doesn't seem to be related to this though.

dominikbraun avatar Jan 23 '22 16:01 dominikbraun

For people getting here: the issue for me was that my application was not waiting for the postgres container to be ready before trying to migrate. Adding the following to my docker-compose in the application solved it:

depends_on:
  postgres:
    condition: service_healthy

giovannizotta avatar Mar 29 '24 16:03 giovannizotta