Need some clarity on shell-script for Docker setup on Phoeinx project

I found below shell-script(entrypoint.sh) from some GitHub Phoenix repo and I saw the same code in multiple Phoenix projects

#!/bin/bash

# Wait until postgres is ready

while ! pg_isready -q -h $PGHOST -p $PGPORT -U $PGUSER

do

echo "$(date) - waiting for database to start"

sleep 2

done


# Create migrate and seed database if it does't exist.

if [[ -z `psql -Atqc "\\list $PGDATABASE"` ]]; then

echo "Database $PGDATABASE does not exist. Creating..."

createdb -E UTF8 $PGDATABASE -l en_US.UTF-8 -T template0

mix ecto.migrate

mix run priv/repo/seeds.exs

echo "Database $PGDATABASE created."

fi

exec mix phx.server

the 1st block of code is clear it checks for Postgres readiness
but 2nd part I could not understand the if condition line
if [[ -z `psql -Atqc "\\list $PGDATABASE"` ]]; and I searched postgres document some online resources but I couldn’t find any reasonable explanation
and in my project I think that if fi block of code not excuted, then I added 3 lines of code to make it work

echo "$(date) - PostgreSQL is ready"
mix ecto.create
mix ecto.migrate

#!/bin/bash
# Docker entry point script.
# Wait until postgres is ready
while ! pg_isready -q -h $PGHOST -p $PGPORT -U $PGUSER
do
  echo "$(date) - waiting for database to start"
  sleep 2
done

# I added
echo "$(date) - PostgreSQL is ready"
mix ecto.create
mix ecto.migrate

# Create migrate and seed database if it does't exist.
if [[ -z `psql -Atqc "\\list $PGDATABASE"` ]]; then
  echo "Database $PGDATABASE does not exist. Creating..."
  createdb -E UTF8 $PGDATABASE -l en_US.UTF-8 -T template0
  mix ecto.migrate
  mix run priv/repo/seeds.exs
  echo "Database $PGDATABASE created."
fi
exec mix phx.server

What is the explanation for this if [[ -z `psql -Atqc "\\list $PGDATABASE"` ]]; then ?

thank you.

Like described in the comment above the if:
# Create migrate and seed database if it does't exist.

psql -Atqc "\\list $PGDATABASE" returns infos about the specified database (name, owner, …) and [[ -z ... ]] checks for an empty string.
Which means if the psql command doesn’t return infos, the commands inside the if block are executed.

1 Like

thank you
and I find more details here postgres