Description
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.