SyntaxStudy
Sign Up
Django PostgreSQL, Environment Variables, and CI/CD
Django Beginner 1 min read

PostgreSQL, Environment Variables, and CI/CD

PostgreSQL is the recommended database for Django in production. It offers advanced features like full-text search, JSON fields, window functions, and excellent concurrency handling that SQLite and even MySQL cannot match. Install psycopg2-binary (for quick setup) or psycopg2 (compiled from source for production) to give Django's ORM the PostgreSQL adapter it needs. Running python manage.py migrate on the production database applies all pending schema changes. Environment variables are the standard way to store configuration secrets — database credentials, secret keys, API tokens — outside of version control. The python-dotenv or django-environ library loads a .env file into os.environ during development. In production, environment variables are set directly in the systemd service file, the Docker/Kubernetes configuration, or the hosting platform's environment settings. The .env file must be listed in .gitignore to prevent accidental commits. A basic CI/CD pipeline for a Django project runs linting (flake8 or ruff), tests (pytest-django or Django's test runner), and a security check (python manage.py check --deploy) on every push. If all checks pass, the pipeline deploys the application by SSHing to the server, pulling the latest code, installing dependencies, collecting static files, running migrations, and restarting Gunicorn. Tools like GitHub Actions, GitLab CI, and CircleCI make this straightforward to configure.
Example
# .env (never commit this file)
DJANGO_SECRET_KEY=your-very-long-random-secret-key-here
DJANGO_SETTINGS_MODULE=mysite.settings.production
ALLOWED_HOSTS=example.com,www.example.com
DB_NAME=mysite_db
DB_USER=mysite_user
DB_PASSWORD=strongpassword123
DB_HOST=localhost
DB_PORT=5432
REDIS_URL=redis://127.0.0.1:6379/1

# requirements.txt (production)
# Django==5.0.4
# gunicorn==21.2.0
# psycopg2==2.9.9
# django-environ==0.11.2
# djangorestframework==3.15.1
# Pillow==10.3.0

# .github/workflows/deploy.yml — GitHub Actions CI/CD
# name: Deploy
# on:
#   push:
#     branches: [main]
# jobs:
#   deploy:
#     runs-on: ubuntu-latest
#     steps:
#       - uses: actions/checkout@v4
#       - name: Run tests
#         run: |
#           pip install -r requirements.txt
#           python manage.py test --settings=mysite.settings.test
#       - name: Deploy to server
#         uses: appleboy/ssh-action@v1
#         with:
#           host: ${{ secrets.SERVER_HOST }}
#           username: deploy
#           key: ${{ secrets.SSH_PRIVATE_KEY }}
#           script: |
#             cd /var/www/mysite
#             git pull origin main
#             source venv/bin/activate
#             pip install -r requirements.txt
#             python manage.py collectstatic --noinput
#             python manage.py migrate
#             sudo systemctl restart gunicorn

This is the last lesson in this section.

Create a free account to earn a certificate