I'm running Django rest api with the django 3.2 on GCPUbuntu 22.04 LTS and docker. I switched my database engine to postgresql from mysql (Read that for large scale operations, PostgreSQL is most suitable). I'm getting the following password authentication error when I run my containers.
django.db.utils.OperationalError: connection to server at "db.earthling" (172.16.16.4), port 5432 failed: FATAL: password authentication failed for user "root"
which comes from :
psycopg2.OperationalError:onnection to server at "db.earthling" (172.16.16.4), port 5432 failed: FATAL: password authentication failed for user "root" DETAIL: Role "root" does not exist DETAIL: Role "root" does not exist
There is also a similar error :
/usr/local/lib/python3.8/site-packages/django/core/management/commands/makemigrations.py:105: RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': connection to server at "db.earthling" (172.16.16.4), port 5432 failed: FATAL: password authentication failed for user "root"
Howvever, I have placed the POSTGRE_PASSWORD in environment files. Here are the details:
.docker-compose.env which points to the relevant variables
# [GROUP] POSTGRESQL Database Configuration
# [DESCRIPTION] The SQL database configuration
# [REQUIRED][ALWAYS]
RDS_DB_NAME=earthling
RDS_USERNAME=root
RDS_HOSTNAME=db.earthling
RDS_PORT=5432
RDS_HOSTNAME_READER=db.earthling
RDS_HOSTNAME_WRITER=db.earthling
#[NAME] RDS_PASSWORD
# [DESCRIPTION] The password for the PostgreSQL Database. If using earthling-cli, obtained from .earthling-cli.json
RDS_PASSWORD=Long password here
POSTGRES_PASSWORD=Same as RDS_PASSWORD
.env file which points to the relevant variables
# [GROUP] POSTGRESQL Database Configuration
# [DESCRIPTION] The SQL database configuration
# [REQUIRED][ALWAYS]
RDS_DB_NAME=earthling
RDS_USERNAME=root
RDS_HOSTNAME=172.16.16.4
RDS_PORT=5433
RDS_HOSTNAME_READER=172.16.16.4
RDS_HOSTNAME_WRITER=172.16.16.4
# [NAME] RDS_PASSWORD
# [DESCRIPTION] The password for the SQL Database.
RDS_PASSWORD=Long password here
POSTGRES_PASSWORD=Same as RDS_PASSWORD
Here is my settings.py that points to the database engine change:
if IS_BUILD or TESTING:
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.mysql',
'ENGINE' :'django.db.backends.postgresql',
'NAME': 'earthling-api'
}
}
else:
RDS_DB_NAME = os.environ.get('RDS_DB_NAME')
RDS_USERNAME = os.environ.get('RDS_USERNAME')
RDS_PASSWORD = os.environ.get('RDS_PASSWORD')
RDS_PORT = os.environ.get('RDS_PORT')
RDS_HOSTNAME = os.environ.get('RDS_HOSTNAME')
RDS_HOSTNAME_WRITER = os.environ.get('RDS_HOSTNAME_WRITER', RDS_HOSTNAME)
RDS_HOSTNAME_READER = os.environ.get('RDS_HOSTNAME_READER', RDS_HOSTNAME_WRITER)
# db_options = {
# 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
# 'charset': 'utf8mb4'
# }
writer_db_config = {
# 'ENGINE': 'django.db.backends.mysql',
'ENGINE' :'django.db.backends.postgresql',
'NAME': RDS_DB_NAME,
'USER': RDS_USERNAME,
'PASSWORD': RDS_PASSWORD,
'HOST': RDS_HOSTNAME_WRITER,
'PORT': RDS_PORT,
# 'OPTIONS': db_options,
}
DATABASES = {
'default': writer_db_config,
'Reader': {
# 'ENGINE': 'django.db.backends.mysql',
'ENGINE' :'django.db.backends.postgresql',
'NAME': RDS_DB_NAME,
'USER': RDS_USERNAME,
'PASSWORD': RDS_PASSWORD,
'HOST': RDS_HOSTNAME_READER,
'PORT': RDS_PORT,
# 'OPTIONS': db_options,
'ATOMIC_REQUESTS': True,
}
}
DATABASE_ROUTERS = ['django_replicated.router.ReplicationRouter']
My docker-compose.yml :
version: '3'
services:
webserver:
container_name: my-api-webserver
build:
dockerfile: Dockerfile
context: ./.docker/api
privileged: true
extra_hosts:
- db.earthling:172.16.16.4
- redis.earthling:172.16.16.5
volumes:
- ./:/opt/earthling-api
- ./.docker-cache/pip:/root/.cache/pip
ports:
- 80:80
working_dir: /opt/earthling-api
networks:
earthling:
ipv4_address: 172.16.16.1
depends_on:
- db
- redis
env_file:
- .docker-compose.env
db:
container_name: earthling-postgresql-database
image: postgres:latest
hostname: db.earthling
volumes:
- postgresdb:/var/lib/postgresql
ports:
- 5432
privileged: false
networks:
earthling:
ipv4_address: 172.16.16.4
# command: --character-set-server=utf8 --collation-server=utf8_unicode_ci
env_file:
- .docker-compose.env
volumes:
postgresdb:
redisdb:
networks:
earthling:
ipam:
driver: default
config:
- subnet: "172.16.16.0/16"
My webserver docker file :
FROM python:3.8.14
RUN apt-get -y update && apt-get -y upgrade && apt-get install -y ffmpeg
COPY wait-for-it.sh /wait-for-it.sh
# Copy any files over
COPY entrypoint.sh /entrypoint.sh
# Copy any files over
COPY bootstrap_development_data.sh /bootstrap_development_data.sh
# Change permissions
RUN chmod +x /entrypoint.sh
RUN chmod +x /bootstrap_development_data.sh
RUN chmod +x /wait-for-it.sh
RUN groupadd -r docker && useradd -r -g docker earthling
ENTRYPOINT ["/entrypoint.sh"]
COPY requirements.txt /requirements.txt
RUN pip3 install --upgrade pip setuptools wheel
RUN pip3 install -r /requirements.txt
RUN yes | pip uninstall django-rq-scheduler
RUN yes | pip install -U django-rq-scheduler
VOLUME ["/opt/earthling-api"]
EXPOSE 80
CMD ["python", "manage.py", "runserver", "0.0.0.0:80"]
# Change permissions
RUN chown -R earthling:docker /root/.cache/pip
I'm definitely missing something OR there is something wrong in the manner in which I am setting up. Help appreciated.