'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?



Solution 1:[1]

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
}

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jozy kial