mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-06-11 18:11:29 +02:00
Update 99-run.sh
This commit is contained in:
@@ -107,24 +107,27 @@ bootstrap_internal_db() {
|
|||||||
if $USE_EXTERNAL_DB; then
|
if $USE_EXTERNAL_DB; then
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
bashio::log.info "Creating role/database if needed..."
|
bashio::log.info "Creating role/database if needed..."
|
||||||
|
|
||||||
# Create role if it doesn't exist
|
# Create role if it doesn't exist, else update password to match config
|
||||||
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres <<SQL
|
if ! psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname = '${DB_USER}'" | grep -q 1; then
|
||||||
DO \$\$
|
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \
|
||||||
BEGIN
|
-c "CREATE ROLE \"${DB_USER}\" LOGIN PASSWORD '${DB_PASS//\'/\'\'}';"
|
||||||
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = '${DB_USER}') THEN
|
else
|
||||||
EXECUTE 'CREATE ROLE ${DB_USER} LOGIN PASSWORD ''' || replace('${DB_PASS}','''','''''') || '''';
|
# update password in case it changed
|
||||||
END IF;
|
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \
|
||||||
END
|
-c "ALTER ROLE \"${DB_USER}\" PASSWORD '${DB_PASS//\'/\'\'}';"
|
||||||
\$\$;
|
fi
|
||||||
SQL
|
|
||||||
|
|
||||||
# Check and create database if it doesn't exist
|
# Check and create database if it doesn't exist
|
||||||
if ! psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '${DB_NAME}'"; then
|
if ! psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '${DB_NAME}'" | grep -q 1; then
|
||||||
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres <<SQL
|
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \
|
||||||
CREATE DATABASE ${DB_NAME} OWNER ${DB_USER};
|
-c "CREATE DATABASE \"${DB_NAME}\" OWNER \"${DB_USER}\";"
|
||||||
SQL
|
else
|
||||||
|
# Optional: ensure DB ownership
|
||||||
|
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \
|
||||||
|
-c "ALTER DATABASE \"${DB_NAME}\" OWNER TO \"${DB_USER}\";"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
bashio::log.info "Internal Postgres ready."
|
bashio::log.info "Internal Postgres ready."
|
||||||
|
|||||||
Reference in New Issue
Block a user