nickjj / docker-django-example

A production ready example Django app that's using Docker and Docker Compose.
MIT License
1.17k stars 249 forks source link

`postgres` setup #44

Closed khaledJabr closed 5 months ago

khaledJabr commented 5 months ago

Hello,

Thanks again for the great repo. I have a question about how you setup postgres for this project. I don't have a fullstack/djano background and I was not able to find the answer anywhere I looked.

When doing the very first migration, the current setup does not recognize my local postgres database so it throws an error saying that my target db name, lets call it testdb, does not exist. Even if I create this database in my local postgres server, is still not recognized.

It only works when I docker exec into the postgres docker image and create the database in the running psql instance.

I also tried updated the docker-compose file to point from "postgres:/var/lib/postgresql/data" to my locatl data directory (/var/lib/postgresql/16/main) but still the same problem.

do you have an idea why is this happening? is it possible to post it at my local postgres ?

edit:

im using ubuntu 22, postgres 16.2

nickjj commented 5 months ago

Hi,

Postgres is running in a container in Docker Compose. It wouldn't use your local postgres DB, at least not by default.

If you want to use your local Postgres you can change your .env file from #export POSTGRES_HOST=postgres to export POSTGRES_HOST=host.docker.internal:5432 and then update the other env vars near there to use your local DB's credentials. Then you can remove Postgres from the Docker Compose file if you want but that's technically optional.

The above is optional though, this project includes everything you need to run Postgres through Docker.

khaledJabr commented 5 months ago

Thank you for your timely response. I went over your docker best practices article and I think the setup makes a lot more sense now.