0

I'm getting this error while trying to run:

docker-compose run app python manage.py migrate

with Django.

this is the docker-compose.yml:

version: '3'

volumes:
  database: { }

services:
  app:
    build:
      context: .
      dockerfile: ./docker/app/dev/Dockerfile
    depends_on:
      - postgres
    volumes:
      - ./app:/app:z
    env_file:
      - ./.envs/.dev/.app
      - ./.envs/.dev/.postgres
    ports:
      - "8000:8000"
    command: "python manage.py runserver 0.0.0.0:8000"

  postgres:
    image: "postgres:13.1"
    volumes:
      - database:/var/lib/postgresql/data:Z
    env_file:
      - ./.envs/.dev/.postgres
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust
    ports:
      - "5432:5432"

I don't know why I'm getting this error. PostgreSQL is running correctly and all the settings are appropiately configured.

This is the full traceback error:.

Traceback (most recent call last):
  File "/app/manage.py", line 7, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 92, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 216, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 235, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
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 not connect to server: Cannot assign requested address
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?

I've searched for multiple answers and tried multiple solutions but they are not working.

They have asked me to put the .postgres note so here it is:

POSTGRES_USER=app
POSTGRES_DB=app
POSTGRES_PASSWORD=<set_me>

And also the database settings:

DATABASES = {
    'default': {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": 'DjangoCRM',
        "USER": 'postgres',
        "PASSWORD": 'xxxx',
        "HOST": 'localhost',
        "PORT": '5432',
    }
}
godtrianglefd
  • 179
  • 2
  • 12
  • can you paste `./.envs/.dev/.postgres`, please? – Paweł Kordowski Feb 07 '21 at 20:35
  • and the database related `settings.py` part? – Paweł Kordowski Feb 07 '21 at 20:37
  • Looks like you are trying to connect to postgres on localhost, but you should be using the container name `postgres`. `localhost` is the *container's* localhost, not your machine's localhost – C.Nivs Feb 07 '21 at 20:40
  • Can you check whether the postgresql is running on port 5432? `service postgresql status`, `sudo netstat -nl | grep postgres` – Ashok Feb 07 '21 at 20:43
  • Yes, already edited with .postgres and settings. Which is the container name ```postgres```? – godtrianglefd Feb 07 '21 at 21:05
  • The service name `postgres` in the `docker-compose.yml` file is usable as a host name; as @C.Nivs points out, "localhost" in Docker usually means "this container". [Networking in Compose](https://docs.docker.com/compose/networking/) in the Docker documentation has more details. – David Maze Feb 07 '21 at 21:07

0 Answers0