diff --git a/Containers/nextcloud/start.sh b/Containers/nextcloud/start.sh index 10ac59c4..9c0fe07a 100644 --- a/Containers/nextcloud/start.sh +++ b/Containers/nextcloud/start.sh @@ -6,6 +6,16 @@ while ! nc -z "$POSTGRES_HOST" 5432; do sleep 5 done +# Use the correct Postgres username +POSTGRES_USER="oc_$POSTGRES_USER" +export POSTGRES_USER + +# Fix false database connection on old instances +if [ -f "/var/www/html/config/config.php" ] && sleep 2 && psql -d "postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:5432/$POSTGRES_DB" -c "select now()"; then + sed -i "s|'dbuser'.*=>.*$|'dbuser' => '$POSTGRES_USER',|" /var/www/html/config/config.php + sed -i "s|'dbpassword'.*=>.*$|'dbpassword' => '$POSTGRES_PASSWORD',|" /var/www/html/config/config.php +fi + # Run original entrypoint if ! bash /entrypoint.sh; then exit 1 diff --git a/Containers/postgresql/Dockerfile b/Containers/postgresql/Dockerfile index cc97ddaa..3a560c90 100644 --- a/Containers/postgresql/Dockerfile +++ b/Containers/postgresql/Dockerfile @@ -17,7 +17,9 @@ RUN set -ex; \ chown -R postgres:postgres "$PGDATA" COPY start.sh /usr/bin/ -RUN chmod +x /usr/bin/start.sh +COPY init-user-db.sh /docker-entrypoint-initdb.d/ +RUN chmod +x /usr/bin/start.sh; \ + chmod +xr /docker-entrypoint-initdb.d/init-user-db.sh RUN mkdir /mnt/data; \ chown postgres:postgres /mnt/data; diff --git a/Containers/postgresql/init-user-db.sh b/Containers/postgresql/init-user-db.sh new file mode 100644 index 00000000..b3c8f47f --- /dev/null +++ b/Containers/postgresql/init-user-db.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -ex + +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + CREATE USER "oc_$POSTGRES_USER" WITH PASSWORD "$POSTGRES_PASSWORD"; + GRANT ALL PRIVILEGES ON DATABASE "$POSTGRES_DB" TO "oc_$POSTGRES_USER"; +EOSQL + +set +ex diff --git a/Containers/postgresql/start.sh b/Containers/postgresql/start.sh index aad235f5..e4ac9827 100644 --- a/Containers/postgresql/start.sh +++ b/Containers/postgresql/start.sh @@ -18,11 +18,13 @@ if ! [ -w "$DUMP_DIR" ]; then exit 1 fi -# # Delete the datadir once (needed for the migration from debian to alpine) -# if ! [ -f "$DUMP_DIR/initial-cleanup-done" ]; then -# rm -rf "${DATADIR:?}/"* -# touch "$DUMP_DIR/initial-cleanup-done" -# fi +# Delete the datadir once (needed for setting the correct credentials on old instances once) +if ! [ -f "$DUMP_DIR/export.failed" ] && ! [ -f "$DUMP_DIR/initial-cleanup-done" ]; then + set -ex + rm -rf "${DATADIR:?}/"* + touch "$DUMP_DIR/initial-cleanup-done" + set +ex +fi # Test if some things match # shellcheck disable=SC2235