From dbee621d902405e310611a08aff8512f3cf2b323 Mon Sep 17 00:00:00 2001 From: dekzter Date: Thu, 27 Mar 2025 20:15:09 -0400 Subject: [PATCH] moving postgres database --- docker/docker-compose.aio.yml | 6 +++--- docker/entrypoint.sh | 7 +++++-- docker/init/02-postgres.sh | 26 +++++++++++++++++--------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/docker/docker-compose.aio.yml b/docker/docker-compose.aio.yml index 6f67bded..34d098da 100644 --- a/docker/docker-compose.aio.yml +++ b/docker/docker-compose.aio.yml @@ -3,16 +3,16 @@ services: # build: # context: . # dockerfile: Dockerfile - image: dispatcharr/dispatcharr:alpha-v1 + image: dispatcharr/dispatcharr:latest container_name: dispatcharr ports: - 9191:9191 volumes: - - dispatcharr_db:/app/data/db + - dispatcharr_data:/data environment: - DISPATCHARR_ENV=aio - REDIS_HOST=localhost - CELERY_BROKER_URL=redis://localhost:6379/0 volumes: - dispatcharr_db: + dispatcharr_data: diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index d04edcb0..4fe82ae0 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -37,6 +37,9 @@ export POSTGRES_PORT=${POSTGRES_PORT:-5432} export REDIS_HOST=${REDIS_HOST:-localhost} export REDIS_DB=${REDIS_DB:-0} +# READ-ONLY - don't let users change these +export POSTGRES_DIR=/data/db + # Global variables, stored so other users inherit them if [[ ! -f /etc/profile.d/dispatcharr.sh ]]; then echo "export PATH=$PATH" >> /etc/profile.d/dispatcharr.sh @@ -65,13 +68,13 @@ echo "Starting init process..." # Start PostgreSQL echo "Starting Postgres..." -su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D /data start -w -t 300 -o '-c port=${POSTGRES_PORT}'" +su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D ${POSTGRES_DIR} start -w -t 300 -o '-c port=${POSTGRES_PORT}'" # Wait for PostgreSQL to be ready until su - postgres -c "/usr/lib/postgresql/14/bin/pg_isready -h ${POSTGRES_HOST} -p ${POSTGRES_PORT}" >/dev/null 2>&1; do echo_with_timestamp "Waiting for PostgreSQL to be ready..." sleep 1 done -postgres_pid=$(su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D /data status" | sed -n 's/.*PID: \([0-9]\+\).*/\1/p') +postgres_pid=$(su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D ${POSTGRES_DIR} status" | sed -n 's/.*PID: \([0-9]\+\).*/\1/p') echo "✅ Postgres started with PID $postgres_pid" pids+=("$postgres_pid") diff --git a/docker/init/02-postgres.sh b/docker/init/02-postgres.sh index ddaa15b7..64662865 100644 --- a/docker/init/02-postgres.sh +++ b/docker/init/02-postgres.sh @@ -1,28 +1,36 @@ #!/bin/bash +# Temporary migration from postgres in /data to /data/db. Can likely remove +# some time in the future. +if [ -e "/data/postgresql.conf" ]; then + mv /data /db + mkdir /data + mv /db /data/ +fi + # Inwitialize PostgreSQL database -if [ -z "$(ls -A "/data")" ]; then +if [ -z "$(ls -A $POSTGRES_DIR)" ]; then echo_with_timestamp "Initializing PostgreSQL database..." - mkdir -p "/data" - chown -R postgres:postgres "/data" - chmod 700 "/data" + mkdir -p $POSTGRES_DIR + chown -R postgres:postgres $POSTGRES_DIR + chmod 700 $POSTGRES_DIR # Initialize PostgreSQL - su - postgres -c "/usr/lib/postgresql/14/bin/initdb -D /data" + su - postgres -c "/usr/lib/postgresql/14/bin/initdb -D ${POSTGRES_DIR}" # Configure PostgreSQL - echo "host all all 0.0.0.0/0 md5" >> "/data/pg_hba.conf" - echo "listen_addresses='*'" >> "/data/postgresql.conf" + echo "host all all 0.0.0.0/0 md5" >> "${POSTGRES_DIR}/pg_hba.conf" + echo "listen_addresses='*'" >> "${POSTGRES_DIR}/postgresql.conf" # Start PostgreSQL echo "Starting Postgres..." - su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D /data start -w -t 300 -o '-c port=${POSTGRES_PORT}'" + su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D ${POSTGRES_DIR} start -w -t 300 -o '-c port=${POSTGRES_PORT}'" # Wait for PostgreSQL to be ready until su - postgres -c "/usr/lib/postgresql/14/bin/pg_isready -h ${POSTGRES_HOST} -p ${POSTGRES_PORT}" >/dev/null 2>&1; do echo_with_timestamp "Waiting for PostgreSQL to be ready..." sleep 1 done - postgres_pid=$(su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D /data status" | sed -n 's/.*PID: \([0-9]\+\).*/\1/p') + postgres_pid=$(su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D ${POSTGRES_DIR} status" | sed -n 's/.*PID: \([0-9]\+\).*/\1/p') # Setup database if needed if ! su - postgres -c "psql -p ${POSTGRES_PORT} -tAc \"SELECT 1 FROM pg_database WHERE datname = '$POSTGRES_DB';\"" | grep -q 1; then