Skip to content

Commit 43000b2

Browse files
authored
Merge pull request #19 from tsotetsi/tf-18-setup-postgreSQL
TF 18 Setup postgre sql
2 parents 21bf114 + 8bd803e commit 43000b2

File tree

12 files changed

+132
-28
lines changed

12 files changed

+132
-28
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# POSTGRESQL DEV DATABASE
22
# ---------------------------------------------------------
3-
POSTGRES_HOST=postgres
3+
POSTGRES_HOST=localhost
44
POSTGRES_PORT=5432
5-
POSTGRES_DB=user_service
6-
POSTGRES_USER=dev
5+
POSTGRES_DB=user_service_db
6+
POSTGRES_USER=user_dev_local
77
POSTGRES_PASSWORD=@changeMe1234
88

99
# POSTGRESQL TEST DATABASE
1010
# ---------------------------------------------------------
1111
POSTGRES_DB_TEST=user_service_test
1212
POSTGRES_USER_TEST=postgres
13-
POSTGRES_PASSWORD_TEST=@changeMe1234
13+
POSTGRES_PASSWORD_TEST=@changeMe123

backend/user_service/.envs/.local/.userservice

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ IPYTHONDIR="/app/.ipython"
55

66
DEBUG=1
77
SECRET_KEY="django-insecure-abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*"
8-
DJANGO_ALLOWED_HOSTS=["*"]
98
DJANGO_SETTINGS_MODULE=config.settings.local

backend/user_service/README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@
2929
- Install VS Code or PyCharm
3030
- Install Python extension
3131

32+
4. **PostgreSQL**
33+
- Install PostgreSQL from official site
34+
- Create user and set the password
35+
```bash
36+
# Use postgres user to create new user, depending on your setup.
37+
psql -U postgres -h localhost
38+
```
39+
```sql
40+
CREATE USER user_dev_local WITH PASSWORD 'your_password';
41+
ALTER ROLE user_dev_local CREATEDB; -- Optional: Use can create databases.
42+
CREATE DATABASE user_dev_local;
43+
GRANT ALL PRIVILEGES ON DATABASE user_dev_local TO user_dev_local;
44+
psql -U user_dev_local -d your_password -h localhost
45+
```
46+
3247
### Local Development
3348

3449
1. Clone Repository
@@ -96,7 +111,21 @@
96111
```bash
97112
kubectl rollout restart
98113
```
99-
114+
5. Connect to PostGreSQL inside the Pod
115+
```bash
116+
kubectl exec -it postgres-c8c4f79bf-jkgbz -- /bin/bash
117+
root@postgres-c8c4f79bf-jkgbz:/# # No you are inside postgresql.
118+
psql -U user_dev_local -d user_service_db
119+
psql (14.15 (Debian 14.15-1.pgdg120+1)) # This means the instance is working.
120+
...
121+
```
122+
6. Other ways to check for services.
123+
```bash
124+
apt update && apt install -y iputils-ping
125+
ping postgres
126+
telnet postgres 5432
127+
nc -vz postgres 5432
128+
```
100129
## Architecture
101130
- Event-Driven Architecture
102131
- Microservices Architecture

backend/user_service/compose/local/user-service/start

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ echo "Python version: $(python --version)"
1212
# NB: You can use a while loop here
1313
# to wait for certain services
1414
# be available.
15+
16+
# Check for any django related issues.
17+
echo "Checking django related issues..."
18+
python manage.py check || {
19+
echo "Checking django related issues failed..."
20+
exit 1
21+
}
1522
# Database migrations with error checking
1623
echo "Checking database migrations..."
1724
python manage.py makemigrations || {
18-
echo "Migration preparation failed"
25+
echo "Migration preparation failed..."
1926
exit 1
2027
}
2128

@@ -37,5 +44,4 @@ exec gunicorn config.wsgi:application --bind 0.0.0.0:8000 \
3744
--workers 3 \
3845
--log-level=info \
3946
--log-file=- \
40-
--capture-output
41-
#python manage.py runserver 0.0.0.0:8000
47+
--capture-output
-144 KB
Binary file not shown.

backend/user_service/config/settings/base.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
import os
12
from pathlib import Path
23

4+
from dotenv import load_dotenv
5+
6+
37
# Build paths inside the project like this: BASE_DIR / 'subdir'.
48
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
59
# Quick-start development settings - unsuitable for production
610
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
711

8-
# SECURITY WARNING: keep the secret key used in production secret!
9-
SECRET_KEY = 'django-insecure-thdv&w5!x!cx0kb+iu3yym_(u4_vqb+jzrd5i5r#%2x19q63nh'
10-
11-
# SECURITY WARNING: don't run with debug turned on in production!
12-
DEBUG = True
12+
USER_SERVICE_ENV_PATH = os.path.join(BASE_DIR, '.envs', '.local', '.userservice')
13+
POSTGRES_ENV_PATH = os.path.join(BASE_DIR, '.envs', '.local', '.postgres')
1314

14-
ALLOWED_HOSTS = []
15+
load_dotenv(USER_SERVICE_ENV_PATH)
16+
load_dotenv(POSTGRES_ENV_PATH)
1517

1618
# TicketFlow Application definition.
1719

@@ -70,7 +72,7 @@
7072
# ------------------------------------------------------------------------------
7173
# https://docs.djangoproject.com/en/dev/ref/settings/#authentication-backends
7274
AUTHENTICATION_BACKENDS = [
73-
# Needed to login by username in Django admin, regardless of `allauth`
75+
# Needed to log in by username in Django admin, regardless of `allauth`
7476
"django.contrib.auth.backends.ModelBackend",
7577
]
7678
# https://docs.djangoproject.com/en/dev/ref/settings/#auth-user-model
@@ -82,13 +84,17 @@
8284

8385
WSGI_APPLICATION = 'config.wsgi.application'
8486

85-
# Database
86-
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
87-
87+
# DATABASES
88+
# ------------------------------------------------------------------------------
89+
# https://docs.djangoproject.com/en/dev/ref/settings/#databases
8890
DATABASES = {
89-
'default': {
90-
'ENGINE': 'django.db.backends.sqlite3',
91-
'NAME': BASE_DIR / 'db.sqlite3',
91+
"default": {
92+
"ENGINE": "django.db.backends.postgresql",
93+
"NAME": os.getenv("POSTGRES_DB", "user_service_db"),
94+
"USER": os.getenv("POSTGRES_USER", "user_dev_local"),
95+
"PASSWORD": os.getenv("POSTGRES_PASSWORD", "@changeMe1234"),
96+
"HOST": os.getenv("POSTGRES_HOST", "localhost"),
97+
"PORT": os.getenv("POSTGRES_PORT", "5432"),
9298
}
9399
}
94100

backend/user_service/config/settings/local.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
# GENERAL
44
# ------------------------------------------------------------------------------
55
# https://docs.djangoproject.com/en/dev/ref/settings/#debug
6-
DEBUG = True
6+
DEBUG = os.getenv('DEBUG', 1)
77
# https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
8-
SECRET_KEY = "nJF7857vESYkIayySmQySmvPmumtfsttzIRhgy1bOvf3gNaYDeqW2G7Hvv7qAxxa"
8+
SECRET_KEY = os.getenv('SECRET_KEY', 'changeMeIfYouSeeThis')
99

1010
# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
1111
# TODO: Set static IP address for minikube, minikube start --static=192.168.49.2
12-
# ALLOWED_HOSTS = ["localhost", "0.0.0.0", "127.0.0.1"] # noqa: S104
13-
ALLOWED_HOSTS = ["*"]
12+
ALLOWED_HOSTS = ["localhost", "0.0.0.0", "127.0.0.1"] # noqa: S104

backend/user_service/docker-compose.local.yml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1+
volumes:
2+
userservice_local_postgres_data: {}
3+
userservice_local_postgres_data_backups: {}
4+
15
services:
6+
postgres-service:
7+
image: postgres:latest
8+
container_name: user_service_postgres_db
9+
env_file:
10+
- ./.envs/.local/.postgres
11+
volumes:
12+
- userservice_local_postgres_data:/var/lib/postgresql/data
13+
ports:
14+
- "5431:5431" # I have local instance postgres running.
15+
healthcheck:
16+
test: ["CMD-SHELL", "pg_isready -U user_dev_local -d user_service_db"]
17+
interval: 5s
18+
timeout: 5s
19+
retries: 5
20+
221
user-service:
22+
depends_on:
23+
- postgres-service
324
build:
425
context: .
526
dockerfile: ./compose/local/user-service/Dockerfile
@@ -9,4 +30,4 @@ services:
930
- ./.envs/.local/.userservice
1031
ports:
1132
- '8000:8000'
12-
command: /start
33+
command: /start
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: postgres
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
app: postgres
10+
template:
11+
metadata:
12+
labels:
13+
app: postgres
14+
spec:
15+
containers:
16+
- name: postgres
17+
image: postgres:14 # Use a stable PostgreSQL version
18+
ports:
19+
- containerPort: 5432
20+
env:
21+
- name: POSTGRES_USER
22+
value: "user_dev_local"
23+
- name: POSTGRES_PASSWORD
24+
value: "@changeMe1234"
25+
- name: POSTGRES_DB
26+
value: "user_service_db"
27+
volumeMounts:
28+
- mountPath: "/var/lib/postgresql/data"
29+
name: postgres-storage
30+
volumes:
31+
- name: postgres-storage
32+
emptyDir: {} # Replace with PersistentVolumeClaim for production
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: postgres
5+
spec:
6+
ports:
7+
- port: 5432
8+
selector:
9+
app: postgres
10+
type: ClusterIP

0 commit comments

Comments
 (0)