forked from bento-platform/katsu
-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_db.sh
40 lines (32 loc) · 1.12 KB
/
create_db.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env bash
# This script checks if a database exists, and creates it if necessary.
# It relies on environment variables from docker-compose, default are:
# - POSTGRES_HOST: postgres-db
# - POSTGRES_USER: admin
# - POSTGRES_DATABASE: clinical
# - PGPASSWORD: password stored at /run/secrets/postgres_db_secret.
set -Euo pipefail
export PGPASSWORD=$(< /run/secrets/postgres_db_secret)
# check if the database exists
check_db_exist() {
psql --quiet -h "$POSTGRES_HOST" -U "$POSTGRES_USER" -lqt | cut -d \| -f 1 | grep -qw "$POSTGRES_DATABASE"
}
# create database
create_db() {
echo "Initializing database..."
createdb -h "$POSTGRES_HOST" -U "$POSTGRES_USER" "$POSTGRES_DATABASE"
echo "Database created successfully."
}
# Wait for postgres container to be ready
until pg_isready -h "$POSTGRES_HOST" -p 5432 -U "$POSTGRES_USER"; do
echo "Waiting for the database to be ready..."
sleep 1
done
# Postgres container connected, check to create or skip katsu db
if check_db_exist; then
echo "Database already exists. Skipping the database creation."
else
create_db
fi
# Run migrations
python manage.py migrate