piątek, 13 czerwca 2025

Kubernetes init database in a dirty way

Sometimes you need a dirty trick initialized the kubernetes running database with a set of sql statements (ddls). Here you are a simple example.

init-db:
kubectl exec -i --namespace timescaledb svc/csdb -c timescaledb -- \
        psql -U postgres -d cas --file=- <ddls/00-init-database.sql;

init-db-tables:
@for f in $(shell ls ddls/$(FILE_PREFIX)*.sql | grep -v 00-init); do \
kubectl exec -i --namespace timescaledb svc/csdb -c timescaledb -- \
            psql -U postgres -d cas --file=- <$$f; \
done


But what if you would like to variables in your script.

Here's a solution:


bash -c "cat <<EOF
$(< ddls/00-init-database.sql_tpl)
EOF" | kubectl exec -i --tty --namespace timescaledb ${PGMASTERPOD} -c timescaledb -- \
    psql -U postgres -d ${DBNAME}

Brak komentarzy:

Prześlij komentarz