From d678f9b3a2dd17f97721f4122aba6610db37d890 Mon Sep 17 00:00:00 2001 From: Rostislav Dugin Date: Mon, 21 Jul 2025 19:36:42 +0300 Subject: [PATCH] FEATURE (container): Move PostgreSQL into container --- Dockerfile | 77 ++++++++++++++++++- README.md | 61 ++++++++------- backend/.env.production.example | 6 +- docker-compose.yml.example | 25 +----- .../backups/ui/EditBackupConfigComponent.tsx | 2 +- install-postgresus.sh | 23 ------ 6 files changed, 110 insertions(+), 84 deletions(-) diff --git a/Dockerfile b/Dockerfile index 350c7da..8f33f66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -53,18 +53,23 @@ RUN CGO_ENABLED=0 \ # ========= RUNTIME ========= FROM --platform=$TARGETPLATFORM debian:bookworm-slim -# Install PostgreSQL client tools (versions 13-17) +# Install PostgreSQL server and client tools (versions 13-17) RUN apt-get update && apt-get install -y --no-install-recommends \ - wget ca-certificates gnupg lsb-release && \ + wget ca-certificates gnupg lsb-release sudo gosu && \ wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \ echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \ > /etc/apt/sources.list.d/pgdg.list && \ apt-get update && \ apt-get install -y --no-install-recommends \ - postgresql-client-13 postgresql-client-14 postgresql-client-15 \ + postgresql-17 postgresql-client-13 postgresql-client-14 postgresql-client-15 \ postgresql-client-16 postgresql-client-17 && \ rm -rf /var/lib/apt/lists/* +# Create postgres user and set up directories +RUN useradd -m -s /bin/bash postgres || true && \ + mkdir -p /postgresus-data/pgdata && \ + chown -R postgres:postgres /postgresus-data/pgdata + WORKDIR /app # Copy Goose from build stage @@ -87,7 +92,71 @@ RUN if [ ! -f /app/.env ]; then \ fi; \ fi +# Create startup script +COPY <> /postgresus-data/pgdata/pg_hba.conf + echo "local all all trust" >> /postgresus-data/pgdata/pg_hba.conf + echo "port = 5437" >> /postgresus-data/pgdata/postgresql.conf + echo "listen_addresses = 'localhost'" >> /postgresus-data/pgdata/postgresql.conf + echo "shared_buffers = 256MB" >> /postgresus-data/pgdata/postgresql.conf + echo "max_connections = 100" >> /postgresus-data/pgdata/postgresql.conf +fi + +# Start PostgreSQL in background +echo "Starting PostgreSQL..." +gosu postgres \$PG_BIN/postgres -D /postgresus-data/pgdata -p 5437 & +POSTGRES_PID=\$! + +# Wait for PostgreSQL to be ready +echo "Waiting for PostgreSQL to be ready..." +for i in {1..30}; do + if gosu postgres \$PG_BIN/pg_isready -p 5437 -h localhost >/dev/null 2>&1; then + echo "PostgreSQL is ready!" + break + fi + if [ \$i -eq 30 ]; then + echo "PostgreSQL failed to start" + exit 1 + fi + sleep 1 +done + +# Create database and set password for postgres user +echo "Setting up database and user..." +gosu postgres \$PG_BIN/psql -p 5437 -h localhost -d postgres << 'SQL' +ALTER USER postgres WITH PASSWORD 'Q1234567'; +CREATE DATABASE "postgresus" OWNER postgres; +\q +SQL + +# Start the main application +echo "Starting Postgresus application..." +exec ./main +EOF + +RUN chmod +x /app/start.sh + EXPOSE 4005 -ENTRYPOINT ["./main"] +# Volume for PostgreSQL data +VOLUME ["/postgresus-data"] + +ENTRYPOINT ["/app/start.sh"] CMD [] diff --git a/README.md b/README.md index 8a50b85..d377345 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@
Postgresus Logo -

PostgreSQL monitoring and backup

Free, open source and self-hosted solution for automated PostgreSQL monitoring and backups. With multiple storage options and notifications

@@ -65,21 +64,29 @@ - **Historical data**: View trends and patterns over time - **Alert system**: Get notified when issues are detected +### 📦 Installation + +You have three ways to install Postgresus: + +- Script (recommended) +- Simple Docker run +- Docker Compose setup + Postgresus Dashboard --- ## 📦 Installation -You have two ways to install Postgresus: via automated script (recommended) or manual Docker Compose setup. +You have three ways to install Postgresus: automated script (recommended), simple Docker run, or Docker Compose setup. ### Option 1: Automated Installation Script (Recommended, Linux only) The installation script will: -- ✅ Install Docker with Docker Compose (if not already installed) -- ✅ Create optimized `docker-compose.yml` configuration -- ✅ Set up automatic startup on system reboot via cron +- ✅ Install Docker with Docker Compose(if not already installed) +- ✅ Set up Postgresus +- ✅ Configure automatic startup on system reboot ```bash sudo apt-get install -y curl && \ @@ -87,7 +94,26 @@ sudo curl -sSL https://raw.githubusercontent.com/RostislavDugin/postgresus/refs/ | sudo bash ``` -### Option 2: Manual Docker Compose Setup +### Option 2: Simple Docker Run + +The easiest way to run Postgresus with embedded PostgreSQL: + +```bash +docker run -d \ + --name postgresus \ + -p 4005:4005 \ + -v ./postgresus-data:/postgresus-data \ + --restart unless-stopped \ + rostislavdugin/postgresus:latest +``` + +This single command will: + +- ✅ Start Postgresus +- ✅ Store all data in `./postgresus-data` directory +- ✅ Automatically restart on system reboot + +### Option 3: Docker Compose Setup Create a `docker-compose.yml` file with the following configuration: @@ -102,29 +128,6 @@ services: - "4005:4005" volumes: - ./postgresus-data:/postgresus-data - depends_on: - postgresus-db: - condition: service_healthy - restart: unless-stopped - - postgresus-db: - container_name: postgresus-db - image: postgres:17 - # we use default values, but do not expose - # PostgreSQL ports so it is safe - environment: - - POSTGRES_DB=postgresus - - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=Q1234567 - volumes: - - ./pgdata:/var/lib/postgresql/data - command: -p 5437 - shm_size: 10gb - healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres -d postgresus -p 5437"] - interval: 5s - timeout: 5s - retries: 5 restart: unless-stopped ``` diff --git a/backend/.env.production.example b/backend/.env.production.example index 8b3601b..c41c7dc 100644 --- a/backend/.env.production.example +++ b/backend/.env.production.example @@ -5,9 +5,9 @@ DEV_DB_PASSWORD=Q1234567 #app ENV_MODE=production # db -DATABASE_DSN=host=postgresus-db user=postgres password=Q1234567 dbname=postgresus port=5437 sslmode=disable -DATABASE_URL=postgres://postgres:Q1234567@postgresus-db:5437/postgresus?sslmode=disable +DATABASE_DSN=host=localhost user=postgres password=Q1234567 dbname=postgresus port=5437 sslmode=disable +DATABASE_URL=postgres://postgres:Q1234567@localhost:5437/postgresus?sslmode=disable # migrations GOOSE_DRIVER=postgres -GOOSE_DBSTRING=postgres://postgres:Q1234567@postgresus-db:5437/postgresus?sslmode=disable +GOOSE_DBSTRING=postgres://postgres:Q1234567@localhost:5437/postgresus?sslmode=disable GOOSE_MIGRATION_DIR=./migrations \ No newline at end of file diff --git a/docker-compose.yml.example b/docker-compose.yml.example index 87d7784..fe31174 100644 --- a/docker-compose.yml.example +++ b/docker-compose.yml.example @@ -16,27 +16,4 @@ services: volumes: - ./postgresus-data:/postgresus-data container_name: postgresus-local - depends_on: - postgresus-db: - condition: service_healthy - restart: unless-stopped - - postgresus-db: - image: postgres:17 - # we use default values, but do not expose - # PostgreSQL ports so it is safe - environment: - - POSTGRES_DB=postgresus - - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=Q1234567 - volumes: - - ./pgdata:/var/lib/postgresql/data - container_name: postgresus-db - command: -p 5437 - shm_size: 10gb - healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres -d postgresus -p 5437"] - interval: 5s - timeout: 5s - retries: 5 - restart: unless-stopped + restart: unless-stopped \ No newline at end of file diff --git a/frontend/src/features/backups/ui/EditBackupConfigComponent.tsx b/frontend/src/features/backups/ui/EditBackupConfigComponent.tsx index 0ee0d35..9eb6629 100644 --- a/frontend/src/features/backups/ui/EditBackupConfigComponent.tsx +++ b/frontend/src/features/backups/ui/EditBackupConfigComponent.tsx @@ -341,7 +341,7 @@ export const EditBackupConfigComponent = ({ diff --git a/install-postgresus.sh b/install-postgresus.sh index 796fdec..877857d 100644 --- a/install-postgresus.sh +++ b/install-postgresus.sh @@ -68,29 +68,6 @@ services: - "4005:4005" volumes: - ./postgresus-data:/postgresus-data - depends_on: - postgresus-db: - condition: service_healthy - restart: unless-stopped - - postgresus-db: - container_name: postgresus-db - image: postgres:17 - # we use default values, but do not expose - # PostgreSQL ports so it is safe - environment: - - POSTGRES_DB=postgresus - - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=Q1234567 - volumes: - - ./pgdata:/var/lib/postgresql/data - command: -p 5437 - shm_size: 10gb - healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres -d postgresus -p 5437"] - interval: 5s - timeout: 5s - retries: 5 restart: unless-stopped EOF log "docker-compose.yml created successfully"