Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to run flyway migration #1003

Closed
prashanthparsi opened this issue Oct 14, 2022 · 4 comments
Closed

Unable to run flyway migration #1003

prashanthparsi opened this issue Oct 14, 2022 · 4 comments

Comments

@prashanthparsi
Copy link

prashanthparsi commented Oct 14, 2022

I have a requirement to run the flyway migrations during docker run as soon as the postgres docker container is run.

So, based on my analysis on the docker-entrypoint.sh, it sounds like the file names should be in a certain order as they follow sequence to run. I created a_initial_setup.sql and init_db.sh so that I can create a database and the users and do the grants. So, the database is created correctly and the logs shows that server is running, but when I am trying to connect using localhost on port 5432 it doesn't work. I get below errors -

waiting for server to start....2022-10-14 01:59:58.400 UTC [123] LOG: starting PostgreSQL 12.12 (Debian 12.12-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-10-14 01:59:58.403 UTC [123] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-10-14 01:59:58.492 UTC [127] LOG: database system was shut down at 2022-10-14 01:59:57 UTC
2022-10-14 01:59:58.513 UTC [123] LOG: database system is ready to accept connections
done
server started
CREATE DATABASE

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/a_initial_setup.sql
CREATE ROLE
GRANT
CREATE SCHEMA
GRANT
CREATE EXTENSION

/usr/local/bin/docker-entrypoint.sh: sourcing /docker-entrypoint-initdb.d/init_db.sh
HOSTNAME...localhost
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
Is the server running on that host and accepting TCP/IP connections?

Sample flyway migration code -

RUN_MIGRATIONS="${RUN_MIGRATIONS:-true}"
  DB_URL="jdbc:postgresql://localhost:5432/<<database_name>>"

if [ "$RUN_MIGRATIONS" == "true" ]; then
        echo "running migrations ..."
        ${FLYWAY_EXE} -user=<<username>> -password=<<password>> -url=$DB_URL -locations="filesystem:$MIGRATIONS_LOCATION" migrate
fi

Does the server not running on port 5432 or it runs on random port? How do I over come this error? Critical for the work I am doing now. Any thoughts are appreciated.

@ImreSamu
Copy link
Contributor

... but when I am trying to connect using localhost on port 5432 it doesn't work.
DB_URL="jdbc:postgresql://localhost:5432/<<database_name>>"

imho: see the doc in https://hub.docker.com/_/postgres

so try without the "localhost".

@prashanthparsi
Copy link
Author

prashanthparsi commented Oct 14, 2022

Thanks @ImreSamu for the pointers. I followed the exact pattern from the #474 issue and still getting unknown host exception.

RUN_MIGRATIONS="${RUN_MIGRATIONS:-true}"
  DB_URL="jdbc:postgresql://@:5432/<<database>>"

#psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
#    CREATE DATABASE $DB_NAME;
#EOSQL
echo "coming..."
if [ "$RUN_MIGRATIONS" == "true" ]; then
        echo "running migrations ..."
        ${FLYWAY_EXE} -user=<<user>> -password=<<password>> -url=$DB_URL -locations="filesystem:$MIGRATIONS_LOCATION" migrate
fi

Errors ->

Flyway Community Edition 9.4.0 by Redgate
See what's new here: https://flywaydb.org/documentation/learnmore/releaseNotes#9.4.0

ERROR: Unable to obtain connection from database (jdbc:postgresql://@:5432/<<db>>) for user '<<user>>': The connection attempt failed.
----------------------------------------------------------------------------------------------------------------------------------
SQL State  : 08001
Error Code : 0
Message    : The connection attempt failed.

Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
Caused by: java.net.UnknownHostException: @

Any thoughts? May be this doesn't work for jdbc drivers? Is this a bug in the image?

@wglambert
Copy link

Unable to obtain connection from database (jdbc:postgresql://@:5432/<<db>>) for user '<<user>>'

Are the <<user>> and <<db>> verbatim or are they sanitized like that?

@prashanthparsi
Copy link
Author

I sanitized them. I found a work around from other posts and trying that. Will keep you posted.

@docker-library docker-library locked and limited conversation to collaborators Dec 19, 2023
@tianon tianon converted this issue into discussion #1171 Dec 19, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants