A production-ready Flask application template with PostgreSQL, pgAdmin, Alembic migrations, and automated backup/restore functionality.
- 🐍 Flask web application
- 🐘 PostgreSQL database with automatic migrations
- 🔧 pgAdmin for database management
- 💾 Automated backup and restore system
- 🐳 Docker containerization
- 🔄 Alembic database migrations
- 🛠️ Comprehensive Makefile with all commands
- 🔒 Proper permission management
- Docker and Docker Compose installed
- Make utility
- Clone the repository
- Initialize the project:
make init
- Edit
.env
file with your configurations - Start the application:
make up
The application will be available at:
- Flask App: http://localhost:5000
- pgAdmin: http://localhost:5050
make init # Initialize the entire project
make init-env # Create .env from template
make init-migrations # Initialize migrations structure
make fix-permissions # Fix directory permissions
make build # Build Docker images
make up # Start all containers
make down # Stop all containers
make restart # Restart all containers
make ps # Show container status
make status # Show complete system status
make dev # Start development environment with logs
make logs # View application logs
make logs-db # View database logs
make logs-pgadmin # View pgAdmin logs
make shell # Open bash in web container
make shell-db # Open PostgreSQL shell
make migrate MSG="description" # Create new migration
make upgrade # Apply all pending migrations
make downgrade # Rollback last migration
make migrate-status # Check current migration
make migrate-history # Show migration history
make backup # Create database backup
make backup-list # List all backups
make backup-clean # Remove old backups
make restore FILE=backup_file.sql.gz # Restore from backup
Backups are stored in ./backups/
directory with format: backup_YYYYMMDD_HHMMSS.sql.gz
make db-reset # Complete database reset (creates backup first)
make pgadmin-open # Show pgAdmin connection info
make prod # Build and start in production mode
make clean # Remove containers, images, volumes
make clean-all # Complete cleanup including data
Default credentials (change in .env
):
- Email: admin@admin.com
- Password: admin
- URL: http://localhost:5050
- Open pgAdmin at http://localhost:5050
- Right-click "Servers" → "Register" → "Server"
- General tab:
- Name:
MyApp Database
(or any name)
- Name:
- Connection tab:
- Host:
db
- Port:
5432
- Database:
myapp_db
(from .env) - Username:
postgres
(from .env) - Password:
postgres
(from .env)
- Host:
Key variables in .env
:
# Automatic user detection (usually correct)
UID=1000
GID=1000
# Flask
FLASK_ENV=development
SECRET_KEY=your-secret-key
APP_PORT=5000
# PostgreSQL
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=myapp_db
POSTGRES_PORT=5432
# pgAdmin
PGADMIN_EMAIL=admin@admin.com
PGADMIN_PASSWORD=admin
PGADMIN_EXTERNAL_PORT=5050
# Backup
BACKUP_RETENTION_DAYS=7
.
├── app/
│ ├── __init__.py # Flask app factory
│ ├── models.py # Database models
│ ├── routes.py # Application routes
│ └── migrations/ # Alembic migrations
├── backups/ # Database backups
├── docker-compose.yml # Docker services configuration
├── Dockerfile # Application container
├── entrypoint.sh # Container startup script
├── requirements.txt # Python dependencies
├── Makefile # All commands
├── .env # Environment variables (not in git)
└── .env.example # Environment template
## Troubleshooting
### Permission Issues
If you encounter permission errors:
```bash
make fix-permissions
Check if database is ready:
make logs-db
For a fresh start:
make clean-all
make init
make up
The system includes automatic backup capabilities:
- Manual Backup:
make backup
- Before Reset: Automatic backup before
make db-reset
- Retention: Old backups are cleaned based on
BACKUP_RETENTION_DAYS
- Change
SECRET_KEY
in.env
- Use strong passwords for database and pgAdmin
- Don't commit
.env
to version control - Use environment-specific
.env
files - Consider using Docker secrets for sensitive data
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
MIT License - Feel free to use this template for your projects.