Home > Enterprise >  Trouble hooking up postgres to django
Trouble hooking up postgres to django

Time:09-08

Following the documentation from Django Postgres DocumentationI added to my settings.py

in settings I set

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

Adjacent to settings.py I created pg_service.conf with

[my_service]
host=localhost
user=USER
dbname=NAME
port=5432

and adjacent to that I created mypg_pass

localhost:5432:PostgreSQL 14:postgres:LetMeIn

I am pretty sure I followed the documentation but on python manage.py check I got the following error.

 connection = Database.connect(**conn_params)
  File "C:\Users\oliver\base\correlator2v2\correlator2home\venv\lib\site-packages\psycopg2\__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)django.db.utils.OperationalError: definition of service "my_service" not found

CodePudding user response:

It is not able to find the service, but actually one is not required to use the service at all...

OP can simply

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'NAME', # database name
        'USER': 'USER',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

CodePudding user response:

Adjacent to settings.py I created pg_service.conf

That is not where the system looks for it, see the docs

"By default, the per-user service file is named ~/.pg_service.conf." Where ~ refers to your home directory, not your current directory (and surely doesn't refer to 'the same directory where settings.py is located', since PostgreSQL doesn't even know what that file is)

  • Related