Jekson
Jekson

Reputation: 3272

Connect postgresql and using virtual environment and docker on the same project

I customize the project locally to work with docker and pipenv. In settings.py, the database has changed

DATABASES = {
        'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('POSTGRES_DB', default='postgres'),
        'USER': os.environ.get('POSTGRES_USER', default='postgres'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD', default='postgres'),
        'HOST': os.environ.get('POSTGRES_HOST', default='localhost'),
        'PORT': "5432"
        }
}

and added in to docker-compose.yml file

version: '3.7'
services:
  web:
    build: .
    command: python /profi/manage.py runserver 0.0.0.0:8000
    environment:
      - SECRET_KEY=dy)zvq+sf07^^456t$$6+mv*tj6#5iwyo896-z!v=h^njl9^&@q
      - DEBUG=1
    volumes:
      - .:/profi
    ports:
      - 8000:8000
    depends_on:
      - db
  db:
    image: postgres:11
    volumes:
      - postgres_data:/var/lib/posgresql/data/
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
      POSTGRES_HOST: db
    ports:
      - "5432:5432"


volumes:
  postgres_data:

I want to be able to run the project locally in two ways: via virtual environment and via docker. But now I have error in both case

using docker

db_1   | 
db_1   | 2020-01-28 09:03:18.408 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2020-01-28 09:03:18.408 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2020-01-28 09:03:18.425 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-01-28 09:03:18.454 UTC [64] LOG:  database system was shut down at 2020-01-28 09:03:18 UTC
db_1   | 2020-01-28 09:03:18.462 UTC [1] LOG:  database system is ready to accept connections
....
web_1  |        Is the server running on host "localhost" (127.0.0.1) and accepting
web_1  |        TCP/IP connections on port 5432?
web_1  | could not connect to server: Cannot assign requested address
web_1  |        Is the server running on host "localhost" (::1) and accepting
web_1  |        TCP/IP connections on port 5432?

and using ENV

django.db.utils.OperationalError: 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?

Could you help me to make a right configuration?

Upvotes: 0

Views: 2146

Answers (2)

jozy kial
jozy kial

Reputation: 11

he first starts by installing python-decoupe with the command pip install python-decouple. then create an environment file .env at the root of your application add the following lines in your .env file

.env file :

DB_NAME=<your dbname>
DB_USER=postgres
DB_USER_PASSWORD=<your password>
DB_HOSt=localhost

settings.py file :

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config('DB_NAME'),
'USER': config('DB_USER'),
'PASSWORD': config('DB_USER_PASSWORD'),
'HOST': config('DB_HOSt'),
'PORT':5432
}

}

Upvotes: 0

markwalker_
markwalker_

Reputation: 12869

You need to add the environment variables to your webapp. So your docker-compose file becomes;

version: '3.7'
services:
  web:
    build: .
    command: python /profi/manage.py runserver 0.0.0.0:8000
    environment:
      DJANGO_SETTINGS_MODULE: "project.settings.development"
      DEBUG: 1
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
      POSTGRES_HOST: db

    volumes:
      - .:/profi
    ports:
      - "${HTTP_PORT:-8000}:8000"
    depends_on:
      - db
  db:
    image: postgres:11
    volumes:
      - postgres_data:/var/lib/posgresql/data/
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
      POSTGRES_HOST: db
    ports:
      - "${DB_PORT:-5432}:5432"


volumes:
  postgres_data:

By doing this, your database will be setup with the environment variables you need, your web app will have those environment variables and you've already got django listening for them. Therefore django won't be listening on localhost for a database connection anymore, it'll use db as the docker hostname for the postgres instance.

Oh, and don't pass your secret key as an environment variable, that just adds potential for security issues. Set DJANGO_SETTINGS_MODULE as an environment variable and then django will know where to look for your secret key & other settings.

In order to run the project in docker and in a venv, or even multiple projects at the same time you can use environment variables for HTTP_PORT and DB_PORT so that the ports on your machine can map into the container.

For example you might be running the postgres sever on your machine on port 5432 and the port in the db container will be port 5432. By setting your compose file like above, you can set DB_PORT environment variable to 54321 and it'll be able to run alongside your localhost.

Upvotes: 1

Related Questions