Django 5.1 + Postgresql (debian server)

Trying to connect to posgresql base as Django wrote in its docs: https://docs.djangoproject.com/en/5.1/ref/databases/#postgresql-notes

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "OPTIONS": {
            "service": "my_service",
            "passfile": ".my_pgpass",
        },
    }
}

I've created 2 files in my home directory .pg_service.conf

[my_service]
host=/var/run/postgresql
user=dbms
dbname=dbms_db
port=5432

.pgpass

/var/run/postgresql:5432:dbms_db:dbms:my_password

Such command as test of .pgpass:

psql -h localhost -U dbms dbms_db

is working. But the connect doesn't work:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "OPTIONS": {
            "service": "my_service",
            "passfile": ".pgpass",
        },
    }
}

with such error

Traceback (most recent call last): File "/home/www/projects/amodulesu/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 279, in ensure_connection self.connect() File "/home/www/projects/amodulesu/venv/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/www/projects/amodulesu/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 256, in connect self.connection = self.get_new_connection(conn_params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/www/projects/amodulesu/venv/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/www/projects/amodulesu/venv/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 332, in get_new_connection connection = self.Database.connect(**conn_params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/www/projects/amodulesu/venv/lib/python3.11/site-packages/psycopg/connection.py", line 119, in connect raise last_ex.with_traceback(None) psycopg.OperationalError: connection failed: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "dbms" ... File "/home/www/projects/amodulesu/venv/lib/python3.11/site-packages/psycopg/connection.py", line 119, in connect raise last_ex.with_traceback(None) django.db.utils.OperationalError: connection failed: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "dbms"

whats wrong with my code?

Trying to use export vars in debian - but it doesnt work too..

With psql you are connecting to localhost whereas with your django app you are trying to connect to /var/run/postgresql/ (or rather a unit socket in that directory). These are different methods and listed separately in pg_hba.conf.

The localhost (127.0.0.1) connection is using passwords ("scram-sha-256") for authentication.

The unix-socket ("local") is using peer authentication. This means that your system user should match your database user ("dbms").

If you try connecting with psql without a -h parameter it should default to local unix-socket connections and show the same problem.

If you try connecting to localhost ("127.0.0.1") in your django configuration then it should work.

Вернуться на верх