1

I have a postgres database running inside a container with pgadmin connected to it, the docker-compose.yml is as follows:

postgres:
  image: postgres:13.0-alpine
  volumes:
    - postgres:/var/lib/postgresql/data
  ports:
    - "5432:5432"
  env_file:
    - $ENV_FILE

pgadmin:
  image: dpage/pgadmin4
  volumes:
    - pgadmin:/var/lib/pgadmin
  ports:
    - "${PGADMIN_PORT:-5050}:80"
  restart: unless-stopped
  depends_on:
    - postgres
  env_file:
    - $ENV_FILE

my django database settings are:

DATABASES = {
    "default": {
        "ENGINE": os.environ.get("POSTGRES_ENGINE", "django.db.backends.postgresql"),
        "NAME": os.environ.get("POSTGRES_NAME", "postgres"),
        "USER": os.environ.get("POSTGRES_USER", "admin"),
        "PASSWORD": os.environ.get("POSTGRES_PASS", "admin"),
        "HOST": os.environ.get("POSTGRES_HOST", "127.0.0.1"),
        "PORT": os.environ.get("POSTGRES_PORT", "5432"),
    }
}

The traceback is:

    Traceback (most recent call last):
  File "C:\Users\liam.obrien\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "C:\Users\liam.obrien\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\channels\management\commands\runserver.py", line 76, in inner_run
    self.check_migrations()
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\core\management\base.py", line 576, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\migrations\loader.py", line 58, in __init__
    self.build_graph()
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\migrations\loader.py", line 235, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\migrations\recorder.py", line 81, in applied_migrations
    if self.has_table():
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\migrations\recorder.py", line 57, in has_table
    with self.connection.cursor() as cursor:
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\backends\base\base.py", line 284, in cursor
    return self._cursor()
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\backends\base\base.py", line 260, in _cursor
    self.ensure_connection()
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\backends\base\base.py", line 243, in ensure_connection
    with self.wrap_database_errors:
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
    self.connect()
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\django\db\backends\postgresql\base.py", line 203, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\liam.obrien\AppData\Local\pypoetry\Cache\virtualenvs\tdd-framework-FOYpVOaj-py3.10\lib\site-packages\psycopg2\__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: connection to server at "127.0.0.1", port 5432 failed: FATAL:  password authentication failed for user "admin"

Absolutely no idea what is going on as my env file has the exact same password in it as the settings.py in my django project.

To be clear, i am running the postgres containers in docker and then trying to run the django test server locally on the host machine, and the error is thrown on startup.

liam
  • 223
  • 2
  • 10
  • 1
    What does the database server's log say about the connection attempt? If it doesn't mention it all, then you are probably trying to connect to some other database server than the one you think you are. – jjanes Nov 30 '22 at 01:52
  • yes you are right no log about a connection attepmt although im not sure how else to connect to it. – liam Nov 30 '22 at 01:57
  • i have also tried connecting via uri on my host machine with no luck `psql postgresql://admin:admin@localhost:5432/postgres` – liam Nov 30 '22 at 02:12

1 Answers1

1

So for anyone that finds this, i was helped along hugely by this thread; Connecting to Postgresql in a docker container from outside

The problem ended up being that for some reason port 5432 was in use, i think by django itself (although if anyone knows the real answer that knowledge would be greatly appreciated), and the solution was to change the port mapping to another port, in my case:

postgres:
  ports:
    - "6543:5432"

you can then test if you can acces your postgres db from a terminal on your host machine through a uri as follows:

psql postgresql://<postgres_user_name>:<postgres_pass>@localhost:6543/postgres

another problem i ran into is that to get the psql command, you need to add postgres to your system environment variables path as follows: %PROGRAMFILES%/PostgreSQL/<postgres_version_num>/bin/

liam
  • 223
  • 2
  • 10
  • 1
    Surely django isn't listening on 5432 pretending to be PostgreSQL. Probably when you installed the psql client on the host, you also installed the database server software and it configured you with a database server running on port 5432. This then blocks the container from mapping that port (on Linux, docker throws an error if that happens, but not on Windows) – jjanes Nov 30 '22 at 23:14
  • that may be as i had a database but even after i dropped that database port 5432 seems to be blocked as i get the same error – liam Dec 01 '22 at 00:14