Home > Net >  Flyway connection failing, connecting using psql command line works fine
Flyway connection failing, connecting using psql command line works fine

Time:10-07

I have a docker service for postgres that is running fine:

postgres:
    restart: always
    image: postgres:13.0-alpine
    ports:
      - "5432:5432"
    environment:
      - DEBUG=false
      - POSTGRES_DB=yola
      - POSTGRES_PASSWORD=password123

I can connect fine using terminal like this:

export PGPASSWORD=password123; psql -h localhost -p 5432 -d yola -U postgres

Now when I try and run flyway I get a connection problem. I tried doing both 0.0.0.0 and localhost and the docker image name yola_postgres_1.

I still get this error running the following command:

docker run --rm -v /path/to/folder/migrations:/flyway/sql flyway/flyway -url=jdbc:postgresql://localhost:5432/yola -user=postgres -password=password123 info

ERROR: Unable to obtain connection from database (jdbc:postgresql://localhost:5432/yola) for user 'postgres': Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- SQL State : 08001 Error Code : 0 Message : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. Caused by: java.net.ConnectException: Connection refused (Connection refused)

CodePudding user response:

The Problem

You have PostgreSQL running in one container and Flyway in another, and localhost is per container, so Flyway isn't communicating with PostgreSQL

A Solution

You already map 5432 in PostgreSQL to 5432 on the host, so you can add --network=host to the docker run command for the Flyway container which makes localhost match that of the host

This will make your current set up work, and you could then look into container networking if you want your containers to communicate with each other directly, not through the host

  • Related