Skip to content

Commit

Permalink
Feat > Main DB setup (postgresql)
Browse files Browse the repository at this point in the history
    < Result >
        Build postgresql container
        Django can connect to postgresql container
        Initial migration is done
        Verify postgresql DB contains Django tables

    < Make postgres service in docker-compose.yml >
        Add postgres service
            Change exposed port to other port for external access
            Hide DB connection information

        Modify app service
            Set environment variables for Django DB connection

    < Load DB information in settings.py >
        Make env in python container about DB information
        Load env in settings.py from os.environ.get()

        Migrate DB

    ** CAUTION **
        If you struggle to set the DB information in settings.py,
        Try to delete Docker volume and restart docker-compose

        Because the DB information is saved in Docker volume at the first time
        And then it is not changed even if you change the DB information in env data
  • Loading branch information
dpcalfola committed Feb 6, 2023
1 parent 9ad5e09 commit 581abf8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -135,7 +135,7 @@ dmypy.json
# Local tool and os generated files
.idea/
.vscode/
DS_Store
.DS_Store

# Personal use files
personal_files/
20 changes: 19 additions & 1 deletion Documents/project_setup_order.md
Expand Up @@ -47,4 +47,22 @@
> Before make commit, Hide the secret key clearly

### 7
### 7. Main DB(1st DB) setup - postgres
* Make postgres service in docker-compose
* Hide the DB information
* Set the DB information in settings.py
>CAUTION:
>
>If you struggle to set the DB information in settings.py,<br>
>Try to delete Docker volume and restart docker-compose
>
>Because the DB information is saved in Docker volume at the first time<br>
>And then it is not changed even if you change the DB information in env data
* Make migrations
```shell
docker-compose run --rm app sh -c "python manage.py makemigrations"
```
```shell
docker-compose run --rm app sh -c "python manage.py migrate"
```
7 changes: 6 additions & 1 deletion Documents/useful_command.md
Expand Up @@ -20,4 +20,9 @@
```shell
docker-compose run --rm app sh -c "pwd"
```


5. Remove docker volume
```shell
docker volume rm fola-db-manager-for-personal-use_fola-db-postgres-data

```
11 changes: 9 additions & 2 deletions django_app/config/settings.py
Expand Up @@ -78,9 +78,16 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'ENGINE': 'django.db.backends.postgresql',
'HOST': os.environ.get('POSTGRES_DB_HOST'),
'NAME': os.environ.get('POSTGRES_DB_NAME'),
'USER': os.environ.get('POSTGRES_DB_USER'),
'PASSWORD': os.environ.get('POSTGRES_DB_PASSWORD'),
}
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
}

# Password validation
Expand Down
24 changes: 23 additions & 1 deletion docker-compose.yml
Expand Up @@ -11,4 +11,26 @@ services:
volumes:
- ./django_app:/django_app
command: >
sh -c "python manage.py runserver 0.0.0.0:8000"
sh -c "echo ${TEST_VARIABLE} && python manage.py runserver 0.0.0.0:8000"
environment:
- POSTGRES_DB_HOST=db-1-postgres
- POSTGRES_DB_NAME=${POSTGRES_DB_NAME}
- POSTGRES_DB_USER=${POSTGRES_DB_USER}
- POSTGRES_DB_PASSWORD=${POSTGRES_DB_PASSWORD}
depends_on:
- db-1-postgres

db-1-postgres:
image: postgres:14.6-alpine
ports:
- "${POSTGRES_DB_PORT}:5432"
volumes:
- fola-db-postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=${POSTGRES_DB_NAME}
- POSTGRES_USER=${POSTGRES_DB_USER}
- POSTGRES_PASSWORD=${POSTGRES_DB_PASSWORD}


volumes:
fola-db-postgres-data:

0 comments on commit 581abf8

Please sign in to comment.