1

I'm learning Docker and after few time, I'm able to run a postgres database and a django app in two different container. The problem is that with docker, I can't use Pycharm's debugging tools.

So I would like to run my code without docker but keep the database in its container.

But I can't connect Dockerized postgres dabatase and locally hosted Django App. I always have this error :

psycopg2.OperationalError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Program Files (x86)\Python37-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
    self.check_migrations()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\core\management\base.py", line 486, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
    self.build_graph()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\migrations\loader.py", line 220, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\migrations\recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\backends\base\base.py", line 259, in cursor
    return self._cursor()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\backends\base\base.py", line 235, in _cursor
    self.ensure_connection()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
    self.connect()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
    self.connect()
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\backends\base\base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\Nicolas Borowicz\Desktop\ProjetSolSol\PlateformeClient\env\lib\site-packages\psycopg2\__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError

I know that professional version of Pycharm allow to connect debugging tool to a docker container but I don't have it.

I saw this question which does the opposite, but I found nothing for my problem.

Here is how my database container is created in docker-compose.yml (authentication data are used only for local dev):

version: "3.7"
   
services:
  db:
    container_name: customer_platform_database_local
    image: postgres
    volumes:
      - postgresPFC:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=pfcdb
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    hostname: db
    expose:
      - "5433" # Publishes 5433 to other containers but NOT to host machine
    ports:
      - "5433:5433"
    command: -p 5433
    restart: always
    ...

volumes:
  postgresPFC:

And there is my database's settings in settings.py :

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

I tried to replace the HOST parameter by 127.0.0.1, 172.23.0.1 (which was the db container ip, given with docker inspect), but none of them worked (if I use 172.23.0.1 I have a timeout)

I have the same problem to connect local pgadmin 4 to the containerized databse but I expect it to be solved by the same way.

1 Answers1

1

You can simplify the mapping port on docker-compose.yml, remapping the 5432 to 5433 to host.

With this configuration I'm able to connect to pfcd database with dbeaver

version: "3.7"
   
services:
  db:
    container_name: customer_platform_database_local
    image: postgres
 volumes:
      - postgresPFC:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=pfcdb
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    hostname: db
    ports:
      - 5433:5432
    restart: always
volumes:
  postgresPFC:

For the connection error I suspect the error is on the driver name not django.db.backends.postgresql but django.db.backends.postgresql_psycopg2 notice the final psycopg2

See this article and the official django host docs

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'pfcdb',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'localhost',
        'PORT': 5433,
    }
}

You must also check if you have another service or Postgres instance running on your host on the same port 5433.

Max
  • 6,821
  • 3
  • 43
  • 59
  • Thank you, I tried this but that does not change anything. – Nicolas Borowicz Aug 17 '21 at 15:26
  • 1
    Can you connect from your host to the Postgres database (maybe using dbeaver)? Have you some rules on your firewall? Antivirus? Network? Have you another service running on 5433 on your host? – Max Aug 17 '21 at 16:03
  • That was that, I had a postgres db that was running on port 5433. PgAdmin created it by default.... Could you edit your answer with this tips ? In order to valid it. Thanks a lot for your help ! – Nicolas Borowicz Aug 19 '21 at 06:37