2

I'm trying to execute a django project on my local machine, the project requires Postgres.

I know close to nothing of docker. I pulled postgres image from docker hub and executed the followin command, as suggested by the instructions in postgres docker hub page.

$ docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

The docker container is up:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
402180487f68        postgres            "docker-entrypoint.s…"   2 hours ago         Up 2 hours          5432/tcp            some-postgres

But I can't make Django to connect to it. (Django is running on my local machine, not on docker) Django settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'mysecretpassword',
        'HOST': 'localhost',
        'PORT': 5432,
    }
}

If I execute migrations with the settings above the error is:

django.db.utils.OperationalError: could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

I assume the connection is not beign refused, because if I stop the container the error is the same.

Some tutorial/answer suggested HOST should be the container name. To me it doesn't make much sense, as I don't know how Django is supposed to resolve that, but I tried nonetheless:

        'HOST': 'some-postgres',

The error raised is:

django.db.utils.OperationalError: could not translate host name "some-postgres" to address: nodename nor servname provided, or not known

I have checked several questions and tutorials, but they all seem to use docker-composer and/or have the django project also inside docker. Still haven't been able to make the project connect to postgres.

2
  • 1
    I recommend docker-compose this will make your life much easier. This project of mine has a docker-compese.yml connecting django and postgres. github.com/DiegoMagg/caddy-django Commented Aug 12, 2020 at 20:42
  • I've avoided having to learn another thing on top of something I already know nothing, however if I were to use docker-compose, does that mean that both the django proj and postgres must run on a container? Or I can still have the django on local machine? (By local machine I mean not on a container) Commented Aug 12, 2020 at 20:52

1 Answer 1

1

I believe you have to forward port 5432 from the docker: https://docs.docker.com/config/containers/container-networking/#published-ports

Good analogy is a webserver - imagine you would start a django application in a container on port 8000. You couldn't just simply open firefox and navigate to localhost:8000 from within the host as the application is running in an isolated environment.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Couldn't find a way to do it with an already created instance, but $ docker run --name some-postgres -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres worked like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.