I’m fairly new to Docker and I am trying to create a project with it and Django. My version of Django is 4.2 and I use the latest versions of PostgreSQL and Docker. So I created a docker-compose.yml
file with Postgres-15 installed in it. I also have the database created on the PgAdmin application. I ran migrations and migrated with no issues. But when I opened the PgAdmin application, I could not see the newly created tables. They are absent as if they were not created. I did not encounter any error or anything when I ran migrations.
This is my docker-compose.yml
file:
version: "3.9"
services:
db:
image: postgres:15
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=2030
- POSTGRES_DB=ikenga_db
ports:
- "5432:5432"
app:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/usr/src/app
ports:
- "8000:8000"
env_file:
- .env
depends_on:
db:
condition: service_healthy
and the command I ran was:
docker-compose run app python manage.py makemigrations
and docker-compose run app python manage.py migrate
This is my settings.py
file:
from pathlib import Path
from typing import List
import dj_database_url
from pydantic import PostgresDsn
from pydantic_settings import BaseSettings
class GeneralSettings(BaseSettings):
DEBUG: bool = False
SECRET_KEY: str
ALLOWED_HOSTS: List[str]
DATABASE_URL: PostgresDsn
GENERAL_SETTINGS = GeneralSettings()
SECRET_KEY = GENERAL_SETTINGS.SECRET_KEY
DEBUG = GENERAL_SETTINGS.DEBUG
ALLOWED_HOSTS = GENERAL_SETTINGS.ALLOWED_HOSTS
And my .env
file is this:
SECRET_KEY="+SV8S2ga3SgYMdJN1AOwwdZZoV5v0aM1eJh39yDxEzY="
ALLOWED_HOSTS='["localhost", "127.0.0.1", "0.0.0.0"]'
DEBUG=True
# database settings
DATABASE_URL=postgres://postgres:2030@db:5432/ikenga_db
I tried to use ChatGPT to understand the situation, but I checked my docker-compose file and did not find any issues, I also checked my code and docker connection to the database and I think everything is on the right track.
I tried to shut down the containers and restart them, but that did not work.
Please if you can help me understand this, let me know what it is I’m doing wrong, I’d really like to understand this problem.
2
Answers
After doing some research, I found out that the problem was I was trying to run Postgres on my local machine while in my docker container. Which is crazy! So all I needed to do to view PgAdmin in docker was to put a service for it in the
dockercompose.yml
file like so:then open the server via
localhost:5050
in the browser, log in using the email and password I specified in thedockercompose.yml
file, then viola, I could see my PgAdmin interface and the database I created. This interface is the one that is running for the docker container. So next you gotta create your own server, using the procedures left from this great video, Connecting postgres in docker to pgadmin serverI hope this helps someone too! Also the email and password are default for
dpage/admin
, though I did not try, but I think you can create your own specific ones.Try this docker-compose. I just fixed the indentation and added volumes at the end so that the db won’t get flushed right away when you kill, or stop the container
Furthermore, you need to check your settings.py if the backend is being able to establish the connection to Postgres or not.