Integrate · 02
Database and storage
PostgreSQL is the production system of record. Static, media and documentation can stay on local disk behind Nginx or move to S3 with CloudFront delivery. Keeping those responsibilities separate makes backups and recovery predictable.
Database modes
| Mode | When | Settings |
|---|---|---|
| SQLite | Local development and production-equivalence checks | No POSTGRES_DB in config.settings; DB_ENGINE=sqlite in production settings. |
| PostgreSQL | Production and production-like testing | DB_ENGINE=postgresql plus DB_NAME, user, password, host and port. |
Production settings require PostgreSQL unless DB_ENGINE=sqlite is explicitly set. SQLite is intentionally a local/staging escape hatch, not the VPS target.
PostgreSQL settings
DB_ENGINE=postgresql
DB_NAME=perfumz
DB_USER=perfumz
DB_PASSWORD=change-me
DB_HOST=127.0.0.1
DB_PORT=5432
DB_CONN_MAX_AGE=600
DB_CONNECT_TIMEOUT=10Gunicorn keeps connections alive for 600 seconds and checks their health. The readiness endpoint runs SELECT 1 on every probe; a failed check returns 503 with checks.database=error.
Migrations
Migrations are forward-only in normal operation. Run them during a release before restarting the service. The release script refuses to deploy when makemigrations --check reports drift; this catches model changes that were not committed.
python manage.py makemigrations --check --dry-run
python manage.py migrate --settings config.settings_production
python manage.py showmigrations --settings config.settings_production | tail -20Take a database backup before a release that changes schema or data. A failed migration can leave the database in a partially applied state.
Static and media
| Asset | Local root | Local serving | S3/CloudFront |
|---|---|---|---|
| Collected static | staticfiles/ | Nginx /static/ alias | static/ prefix, CloudFront |
| Public media | media/ | Nginx /media/ alias | media/ prefix, CloudFront |
| Private media | media/private/ | Authenticated Django view | Local only; never uploaded to S3/CloudFront |
| Documentation | documentations/ | Nginx /documentations/ alias | Package or static host |
Nginx runs as www-data. The web service uses UMask=0022 so runtime uploads are readable; the release scripts also apply chmod -R a+rX to static and media.
S3 and CloudFront
Set USE_S3=1 to switch the default and static storage backends to S3. The storage adapter keeps a local-disk fallback when the flag is off. The bucket needs the media/ and static/ prefixes. Private media stays on local disk behind the authenticated route and is never uploaded to the public bucket.
USE_S3=1
AWS_USE_IAM_ROLE=1
AWS_STORAGE_BUCKET_NAME=perfumz-media
AWS_S3_REGION_NAME=me-central-1
AWS_S3_CUSTOM_DOMAIN=
CLOUDFRONT_DOMAIN=cdn.perfumz.com
CLOUDFRONT_DISTRIBUTION_ID=E123EXAMPLE
CLOUDFRONT_KEY_ID=K123EXAMPLE
CLOUDFRONT_PRIVATE_KEY_PATH=/etc/perfumz/cloudfront-private.pem
CLOUDFRONT_SIGNED_URLS=1
CLOUDFRONT_EXPIRATION=3600manage.py check --deploy validates the bucket, region, credentials/IAM role, storage backends, CloudFront domain, distribution ID and signing key. The media dashboard exposes Purge CDN and private/public asset controls.
Storage boundary
Local disk and S3 are both supported. Do not mix local and remote URLs for existing assets without a migration plan. The media library records a storage backend and checksum for every asset. Manual backups include public S3 media and local private media; bucket versioning remains useful for object-level recovery.
A backup on the same disk is not disaster recovery. Copy local archives off the VPS and replicate the S3 bucket.
Readiness checks
curl -fsS http://127.0.0.1:8007/health/live/
curl -fsS http://127.0.0.1:8007/health/ready/
curl -fsS https://perfumz.com/health/ready//health/live/ only proves a worker answered. /health/ready/ proves the database and cache are usable. Use readiness for the load balancer and release gate; use liveness only for process supervision.