diff --git a/docs/2-local-development/developing-locally.rst b/docs/2-local-development/developing-locally.rst index 11343e1546..8c0ad14803 100644 --- a/docs/2-local-development/developing-locally.rst +++ b/docs/2-local-development/developing-locally.rst @@ -54,7 +54,9 @@ First things first. #. Set the environment variables for your database(s): :: - $ export DATABASE_URL=postgres://postgres:@127.0.0.1:5432/ + $ export POSTGRES_USER=postgres + $ export POSTGRES_PASSWORD= + $ export POSTGRES_DB= # Optional: set broker URL if using Celery $ export CELERY_BROKER_URL=redis://localhost:6379/0 diff --git a/{{cookiecutter.project_slug}}/.drone.yml b/{{cookiecutter.project_slug}}/.drone.yml index d6c13e62bf..aea70aafb4 100644 --- a/{{cookiecutter.project_slug}}/.drone.yml +++ b/{{cookiecutter.project_slug}}/.drone.yml @@ -28,8 +28,6 @@ steps: pull: if-not-exists {%- if cookiecutter.use_docker == 'y' %} image: docker:25.0 - environment: - DATABASE_URL: pgsql://$POSTGRES_USER:$POSTGRES_PASSWORD@postgres/$POSTGRES_DB commands: - docker-compose -f docker-compose.local.yml build - docker-compose -f docker-compose.docs.yml build diff --git a/{{cookiecutter.project_slug}}/.github/workflows/ci.yml b/{{cookiecutter.project_slug}}/.github/workflows/ci.yml index 0d8ed0cada..428798f632 100644 --- a/{{cookiecutter.project_slug}}/.github/workflows/ci.yml +++ b/{{cookiecutter.project_slug}}/.github/workflows/ci.yml @@ -57,10 +57,13 @@ jobs: env: {%- if cookiecutter.use_celery == 'y' %} - CELERY_BROKER_URL: 'redis://localhost:6379/0' + REDIS_URL: 'redis://localhost:6379/0' {%- endif %} - # postgres://user:password@host:port/database - DATABASE_URL: 'postgres://postgres:postgres@localhost:5432/postgres' + POSTGRES_USER: 'postgres' + POSTGRES_PASSWORD: 'postgres' + POSTGRES_DB: 'postgres' + POSTGRES_HOST: 'postgres' + {%- endif %} steps: diff --git a/{{cookiecutter.project_slug}}/.gitlab-ci.yml b/{{cookiecutter.project_slug}}/.gitlab-ci.yml index 71216bc7a2..24d9e8b933 100644 --- a/{{cookiecutter.project_slug}}/.gitlab-ci.yml +++ b/{{cookiecutter.project_slug}}/.gitlab-ci.yml @@ -5,6 +5,7 @@ stages: variables: POSTGRES_USER: '{{ cookiecutter.project_slug }}' POSTGRES_PASSWORD: '' + POSTGRES_HOST: postgres POSTGRES_DB: 'test_{{ cookiecutter.project_slug }}' POSTGRES_HOST_AUTH_METHOD: trust {%- if cookiecutter.use_celery == 'y' %} @@ -42,8 +43,6 @@ pytest: image: python:3.12 services: - postgres:{{ cookiecutter.postgresql_version }} - variables: - DATABASE_URL: pgsql://$POSTGRES_USER:$POSTGRES_PASSWORD@postgres/$POSTGRES_DB before_script: - pip install -r requirements/local.txt script: diff --git a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile index 83d3859494..3ed1d6c3b2 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile @@ -124,8 +124,7 @@ RUN chown -R django:django ${APP_HOME} USER django -RUN DATABASE_URL="" \ - {%- if cookiecutter.use_celery == "y" %} +RUN {%- if cookiecutter.use_celery == "y" %} CELERY_BROKER_URL="" \ {%- endif %} DJANGO_SETTINGS_MODULE="config.settings.test" \ diff --git a/{{cookiecutter.project_slug}}/compose/production/django/entrypoint b/{{cookiecutter.project_slug}}/compose/production/django/entrypoint index fe517f4ce8..07c042f928 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/entrypoint +++ b/{{cookiecutter.project_slug}}/compose/production/django/entrypoint @@ -14,7 +14,6 @@ if [ -z "${POSTGRES_USER}" ]; then base_postgres_image_default_user='postgres' export POSTGRES_USER="${base_postgres_image_default_user}" fi -export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" wait-for-it "${POSTGRES_HOST}:${POSTGRES_PORT}" -t 30 diff --git a/{{cookiecutter.project_slug}}/config/settings/base.py b/{{cookiecutter.project_slug}}/config/settings/base.py index f1039b7484..2c85829105 100644 --- a/{{cookiecutter.project_slug}}/config/settings/base.py +++ b/{{cookiecutter.project_slug}}/config/settings/base.py @@ -45,16 +45,21 @@ # DATABASES # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#databases -{% if cookiecutter.use_docker == "y" -%} -DATABASES = {"default": env.db("DATABASE_URL")} -{%- else %} -DATABASES = { - "default": env.db( - "DATABASE_URL", - default="postgres://{% if cookiecutter.windows == 'y' %}localhost{% endif %}/{{cookiecutter.project_slug}}", - ), -} -{%- endif %} + +if db_url := env.db("DATABASE_URL", default=None): + DATABASES = {"default": db_url} +else: + DATABASES = { + "default": { + "ENGINE": "django.db.backends.postgresql", + "NAME": env.str("POSTGRES_DB"), + "USER": env.str("POSTGRES_USER"), + "PASSWORD": env.str("POSTGRES_PASSWORD"), + "HOST": env.str("POSTGRES_HOST", default="{% if cookiecutter.windows == 'y' and cookiecutter.use_docker == 'n' %}localhost{%else%}postgres{% endif %}"), + "PORT": env.str("POSTGRES_PORT", default="5432"), + }, + } + DATABASES["default"]["ATOMIC_REQUESTS"] = True # https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-DEFAULT_AUTO_FIELD DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"