On my local development machine (macOS), I have placed .pg_service.conf and .pgpass in the home directory of the user running gunicorn, and configured the connection on settings.py like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'OPTIONS': {
'service' : 'mydb',
'passfile' : '.pgpass',
}
}
}
This works as expected. On the test server (FreeBSD with jails; database running in different jail), however, the PostgreSQL configuration files are not found in the user’s home directory (/home/www). I created the home directory for www manually, referenced it in /etc/passwd, set restrictive permissions on the configuration files and tested by logging in as www; psql service=mydb gets me right into the correct database without entering a password.
When placing pg_service.conf in /usr/local/etc/postgresql and specifying an absolute path for passfile (/home/www/.pgpass) in settings.py, the connection is available for my Django app, too – but why is the placement of the configuration files in /home/www not sufficient in this case? I start gunicorn with a --chdir parameter to the project’s root directory, but this should not make a difference.