Last active
August 11, 2025 03:06
-
-
Save aalfiann/ae06c39036f086903cf32f9296db2784 to your computer and use it in GitHub Desktop.
PostgreSQL Docker Compose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| This is the implementation of PostgreSQL in Docker + Daily Backup Schedule | |
| Make sure structur project is like this | |
| project/ | |
| ├── docker-compose.yml | |
| ├── Dockerfile.pgbackup | |
| ├── backup-scripts/ | |
| │ ├── backup.sh | |
| │ └── crontab.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Config | |
| DB_LIST="postgres dbname_one dbname_two" | |
| DB_USER="postgres" | |
| DB_HOST="postgres" | |
| BACKUP_DIR="/backup" | |
| mkdir -p "$BACKUP_DIR" | |
| for DB_NAME in $DB_LIST; do | |
| FILE_NAME="$BACKUP_DIR/${DB_NAME}_$(date +%F_%H-%M-%S).sql.gz" | |
| pg_dump -h "$DB_HOST" -U "$DB_USER" "$DB_NAME" | gzip > "$FILE_NAME" | |
| echo "[Backup] Backup created for $DB_NAME: $FILE_NAME" | |
| done | |
| # Keep last 7 backups per database | |
| for DB_NAME in $DB_LIST; do | |
| ls -1t $BACKUP_DIR/${DB_NAME}_*.sql.gz | tail -n +8 | xargs -r rm -- | |
| done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 0 2 * * * /scripts/backup.sh >> /backup/cron.log 2>&1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| services: | |
| postgres: | |
| image: postgres:17 | |
| container_name: postgres17 | |
| restart: unless-stopped | |
| ports: | |
| - "6543:5432" | |
| environment: | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres1234 | |
| POSTGRES_DB: postgres | |
| volumes: | |
| - ./postgres-data:/var/lib/postgresql/data | |
| - ./postgres-init:/docker-entrypoint-initdb.d | |
| command: postgres -c listen_addresses='*' | |
| pg_backup: | |
| build: | |
| context: . | |
| dockerfile: Dockerfile.pgbackup | |
| container_name: postgres_backup | |
| depends_on: | |
| - postgres | |
| environment: | |
| PGPASSWORD: postgres1234 | |
| volumes: | |
| - ./postgres-backup:/backup | |
| - ./backup-scripts:/scripts | |
| entrypoint: ["/bin/sh", "-c", "chmod +x /scripts/backup.sh && crontab /scripts/crontab.txt && cron -f -l 2"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FROM postgres:17 | |
| RUN apt-get update && apt-get install -y cron && rm -rf /var/lib/apt/lists/* | |
| CMD ["cron", "-f", "-l", "2"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment