add checking of ready for PostgreSQL container
This commit is contained in:
parent
53cfd84364
commit
1d146dd0d4
28
init-db.sh
28
init-db.sh
@ -15,13 +15,39 @@ set -e
|
|||||||
|
|
||||||
export DEBIAN_FRONTEND=noninteractive
|
export DEBIAN_FRONTEND=noninteractive
|
||||||
|
|
||||||
|
PG_CONTAINER=postgres
|
||||||
CREATE_DB_NAME=$1
|
CREATE_DB_NAME=$1
|
||||||
CREATE_DB_PASSWORD=$2
|
CREATE_DB_PASSWORD=$2
|
||||||
|
|
||||||
: "${POSTGRES_USER:?}"
|
: "${POSTGRES_USER:?}"
|
||||||
: "${POSTGRES_DB:?}"
|
: "${POSTGRES_DB:?}"
|
||||||
|
|
||||||
sudo docker exec -i postgres psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOSQL
|
# Function to check container status:
|
||||||
|
check_container_state() {
|
||||||
|
state=$(sudo docker inspect -f '{{.State.Health.Status}}' "$PG_CONTAINER" 2>/dev/null)
|
||||||
|
|
||||||
|
if [ "$state" = "healthy" ]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
state=$(sudo docker inspect -f '{{.State.Status}}' "$PG_CONTAINER" 2>/dev/null)
|
||||||
|
|
||||||
|
if [ "$state" = "running" ]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Waiting for PostgreSQL container to start..."
|
||||||
|
while ! check_container_state; do
|
||||||
|
echo "Waiting for PostgreSQL container to be ready..."
|
||||||
|
sleep 5
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "PostgreSQL container is ready. Init DB..."
|
||||||
|
|
||||||
|
sudo docker exec -i $PG_CONTAINER psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOSQL
|
||||||
CREATE USER "$CREATE_DB_NAME" WITH ENCRYPTED PASSWORD '$CREATE_DB_PASSWORD';
|
CREATE USER "$CREATE_DB_NAME" WITH ENCRYPTED PASSWORD '$CREATE_DB_PASSWORD';
|
||||||
CREATE DATABASE "$CREATE_DB_NAME";
|
CREATE DATABASE "$CREATE_DB_NAME";
|
||||||
GRANT ALL PRIVILEGES ON DATABASE "$CREATE_DB_NAME" TO "$CREATE_DB_NAME";
|
GRANT ALL PRIVILEGES ON DATABASE "$CREATE_DB_NAME" TO "$CREATE_DB_NAME";
|
||||||
|
Loading…
Reference in New Issue
Block a user