postgres
postgres copied to clipboard
Unable to run flyway migration
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.
... 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
-
in the end of the "Initialization scripts" parts :
- "... Also, as of docker-library/postgres#440, the temporary daemon started for these initialization scripts listens only on the Unix socket, so any psql usage should drop the hostname portion (see docker-library/postgres#474 (comment) for example)."
so try without the "localhost".
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?
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?
I sanitized them. I found a work around from other posts and trying that. Will keep you posted.