# Potgres 10 Docker for Trade software A robust, containerized Postgres 10 setup designed for production environments. It includes automated hourly/daily backups, custom configuration management, and a comprehensive management script. ## Quick Start Configure Environment: Create a .env file in the root directory (see Environment Variables). Run Setup: Initialize directories and permissions. This will create and set the right permissions for `config`, `pgdata` and `backups` folders: ```cmd ./manage_db.sh setup ``` Make sure that the right folders are created. Inside `config/` there must be `pg_hba.conf` and `postgresql.conf`. The files are created based on [config_template](postgres_10_docker/config_template/) folder or from docker template files. Start Services: Launch the database and backup containers: ```cmd ./manage_db.sh run ``` ## Features * **Database:** `PostgreSQL 10` with persisted data in `./pgdata`. * **Custom Config:** Uses external `postgresql.conf` and `pg_hba.conf` for fine-tuning. * **Automatic Backups:** Scheduled backups using `postgres-backup-local` with configurable retention. * **Timezone Sync:** Containers automatically sync with the host system time. * **Health Checks:** Backup service waits for the database to be fully "Ready" before starting. * **Security:** Configuration files are mounted as Read-Only inside the container. ## Project Structure * [manage_db.sh](manage_db.sh): The main entry point for managing the stack. * [config/](config/): Contains PostgreSQL configuration files (generated during setup). * [pgdata/](pgdata/): Persistent storage for the database (Postgres UID 999). * [backups/](backups/): Location for compressed .sql.gz backup files. ## Environment Variables (.env) Ensure the following variables are defined in your `.env` file: * **POSTGRES_USER**: Database administrator username - `admin` * **POSTGRES_PASSWORD**: Database administrator password - `securepassword` * **POSTGRES_DB**: Name of the primary database - `db` * **TZ**: System timezone `UTC` * **BACKUP_SCHEDULE**: Cron schedule for backups - `0 0 * * * (Daily)` * **BACKUP_RETENTION_DAYS**: How many days to keep backups - `7` ## Management Script Usage The `manage_db.sh` script simplifies common Docker commands: Command Description: * Creates folders, sets permissions, and extracts config templates. ```cmd ./manage_db.sh setup ``` * Starts all services in detached mode. ```cmd ./manage_db.sh run ``` * Stops the containers. ```cmd ./manage_db.sh stop ``` * Shows running status and resource usage (CPU/RAM). ```cmd ./manage_db.sh status ``` * Tails logs for all services. ```cmd ./manage_db.sh logs ``` * Jumps directly into the Postgres SQL prompt. ```cmd ./manage_db.sh psql ``` * Triggers a manual backup immediately. ```cmd ./manage_db.sh backup ``` * Restores a database from a .gz or .dump file. ```cmd ./manage_db.sh restore ``` ## Security & Permissions During the setup phase, the script applies `chown -R 999:999` to the data and config folders. This matches the internal postgres user ID used by the official Docker image, ensuring the container has the necessary rights to write data and read configurations.