databasepostgresqldocker

Error Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections


$ docker run --rm -v /var/lib/jenkins/workspace/flyway@2/jenkins_flyway/sql:/flyway/sql -v /var/lib/jenkins/workspace/flyway@2/jenkins_flyway/conf:/flyway/conf flyway/flyway:10.13.0 -user=postgres -password=postgres migrate
WARNING: Storing migrations in 'sql' is not recommended and default scanning of this location may be deprecated in a future release
ERROR: No database found to handle "jdbc:postgresql://localhost:5432/mydb"

So Docker is able to read the conf file but unable to handle the Postgres database.

I have tried changing the database to MySQL, but I'm still getting the same error.

This was caused because I was using quotes for property values in my Flyway config.

Now I have another error:

Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

    docker run --rm -v /var/lib/jenkins/workspace/flyway/jenkins_flyway/sql:/flyway/sql -v /var/lib/jenkins/workspace/flyway/jenkins_flyway/conf:/flyway/conf flyway/flyway:8.5.1 migrate

ERROR: Unable to obtain connection from database (jdbc:postgresql://localhost:5432/flyway) 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)

Solution

  • Ok so I found solution and here they are

    To rectify this make sure you have written the configuration file exactly as above. I was using values in quotes.

    To rectify this I modified following properties in my postgresql.conf file

    listen_addresses = '*'
    

    And added following line in my postgres pg_hba.conf file

    host    all             all             192.168.1.0/24          md5