Skip to content

Commit b9c6bd3

Browse files
fix: Wait for DB to detect psql version
1 parent ea4ff7f commit b9c6bd3

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

postgres/bin/entrypoint.sh

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,39 @@ set -euf -o pipefail
44

55
export PGSSLMODE=require
66

7+
wait_for_db() {
8+
local retries=30
9+
local sleep_time=2
10+
11+
echo "Waiting for PostgreSQL server to be ready..."
12+
until psql "$DATABASE_URL" -c '\q' 2>/dev/null || [ "$retries" -eq 0 ]; do
13+
echo "PostgreSQL is unavailable - sleeping ($((retries--)) retries left)..."
14+
sleep "$sleep_time"
15+
done
16+
17+
if [ "$retries" -eq 0 ]; then
18+
echo "ERROR: PostgreSQL server did not become ready in time."
19+
exit 1
20+
fi
21+
22+
echo "PostgreSQL server is ready!"
23+
}
24+
725
if [ -z "${DATABASE_URL:-""}" ]; then
826
echo "WARNING: DATABASE_URL not found in environment."
927
else
10-
echo "DEBUG: DATABASE_URL=$DATABASE_URL"
1128

1229
# Extract connection details from DATABASE_URL
1330
# shellcheck disable=SC2046
1431
export $(parse_database_url.py | xargs)
1532

16-
# Log the parsed variables for debugging
17-
echo "DEBUG: Parsed variables:"
18-
echo " HOST=$HOST"
19-
echo " PORT=$PORT"
20-
echo " NAME=$NAME"
21-
echo " USER=$USER"
22-
2333
# Setup PGSERVICE so `psql` just does the right thing
2434
/bin/echo -e "[$NAME]\nhost=$HOST\nport=$PORT\ndbname=$NAME\nuser=$USER" > ~/.pg_service.conf
2535
export PGSERVICE="$NAME"
2636

37+
# Wait for PostgreSQL to be ready
38+
wait_for_db
39+
2740
# Detect PostgreSQL server version
2841
SERVER_VERSION=$(psql "$DATABASE_URL" -tAc "SHOW server_version;" | cut -d '.' -f 1 || true)
2942
if [ -z "$SERVER_VERSION" ]; then

0 commit comments

Comments
 (0)