mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-06-02 05:44:03 +02:00
Create run
This commit is contained in:
35
ente/rootfs/etc/services.d/01-postgres-init/run
Normal file
35
ente/rootfs/etc/services.d/01-postgres-init/run
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/with-contenv bashio
|
||||||
|
# shellcheck shell=bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
if bashio::config.true 'USE_EXTERNAL_DB'; then
|
||||||
|
# External DB; nothing to create here.
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
DB_USER="$(bashio::config 'DB_USERNAME')"
|
||||||
|
DB_PASS="$(bashio::config 'DB_PASSWORD')"
|
||||||
|
DB_NAME="$(bashio::config 'DB_DATABASE_NAME')"
|
||||||
|
|
||||||
|
bashio::log.info "Postgres-init: waiting for local Postgres..."
|
||||||
|
until pg_isready -q -h 127.0.0.1 -p 5432 -U postgres; do
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
bashio::log.info "Postgres-init: creating role/database if missing..."
|
||||||
|
# psql search_path safe quoting using dollar-quoted strings for password
|
||||||
|
psql -v ON_ERROR_STOP=1 -h 127.0.0.1 -p 5432 -U postgres <<SQL
|
||||||
|
DO \$\$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = '${DB_USER}') THEN
|
||||||
|
EXECUTE 'CREATE ROLE ${DB_USER} LOGIN PASSWORD ''' || replace('${DB_PASS}','''','''''') || '''';
|
||||||
|
END IF;
|
||||||
|
IF NOT EXISTS (SELECT 1 FROM pg_database WHERE datname = '${DB_NAME}') THEN
|
||||||
|
EXECUTE 'CREATE DATABASE ${DB_NAME} OWNER ${DB_USER}';
|
||||||
|
END IF;
|
||||||
|
END
|
||||||
|
\$\$;
|
||||||
|
SQL
|
||||||
|
|
||||||
|
bashio::log.info "Postgres-init: done."
|
||||||
|
exit 0
|
||||||
Reference in New Issue
Block a user