Skip to content

Commit

Permalink
feat(build): add postgres as a compose service
Browse files Browse the repository at this point in the history
  • Loading branch information
yld-weng committed Jul 20, 2023
1 parent ca1492d commit ae16a44
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 8 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ secrets.json
!.gitignore
!.github

dist/
dist/

# Docker container volumes
volumes
35 changes: 29 additions & 6 deletions RSEAdmin/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,36 @@

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
DEV_CONTAINER = os.getenv('DEV_CONTAINER')

# Use Postgres with containers
if DEV_CONTAINER is not None:
DATABASE_NAME = os.getenv('DATABASE_NAME', 'django')

DATABASES = dict(
default={
'ENGINE': 'django.db.backends.postgresql',
'NAME': DATABASE_NAME,
'USER': os.getenv('DATABASE_USER', 'django'),
'PASSWORD': os.getenv('DATABASE_PASSWORD', 'django_postgres'),
'HOST': os.getenv('DATABASE_HOST', 'localhost'),
'PORT': int(os.getenv('DATABASE_PORT', '5432')),
# Test database
# https://docs.djangoproject.com/en/4.0/topics/testing/overview/#the-test-database
# https://docs.djangoproject.com/en/4.0/ref/settings/#test
'TEST': {
'NAME': f"test_{DATABASE_NAME}",
}
}
)
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
}

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
Expand Down
7 changes: 7 additions & 0 deletions dev.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DATABASE_NAME=django
DATABASE_USER=django
DATABASE_PASSWORD=django_postgres
DJANGO_SUPERUSER_PASSWORD=rseadmindjango
DATABASE_HOST=db
DATABASE_PORT=5432
DEV_CONTAINER=True
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,23 @@ services:
app:
build: .
command: python manage.py runserver 0.0.0.0:8080
env_file:
- ./dev.env
volumes:
- .:/var/www
ports:
- "8080:8080"


# PostgreSQL docs https://www.postgresql.org/docs/14/index.html
# https://hub.docker.com/_/postgres
db:
image: postgres:14-bullseye
environment:
# postgres superuser
POSTGRES_PASSWORD: django_postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
volumes:
- ./scripts/db_init.sh:/tmp/db_init.sh
- "./volumes/postgresql/data:/var/lib/postgresql/data"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Django = "^3.2"
django-adminlte2 = "~0.4.1"
django-polymorphic = "^3.1"
gunicorn = {version = "^20.1", optional = true}
psycopg2 = {version = "^2.9", optional = true}
psycopg2 = "^2.9"
mysqlclient = {version = "^2", optional = true}
python-dateutil = "~2.8.2"
cryptography = "^39.0"
Expand Down
24 changes: 24 additions & 0 deletions scripts/db_init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# Configure postgres database
# This should probably be run as the postgres user

# Options
user=django
database_name=django
password=django_postgres

# Create the role
psql -c "create role $user with login password '$password';"

# Create the database
createdb --owner=django $database_name

# Give the user permission to create databases.
# This is required for Django testing:
# https://docs.djangoproject.com/en/4.0/topics/testing/overview/#the-test-database
echo "Granting $user user permission to create databases..."
psql -c "ALTER ROLE $user CREATEDB"
echo "You might also need to apply migrations."


0 comments on commit ae16a44

Please sign in to comment.