Postgresql User does not connect to database (Nginx Django Gunicorn)

Almost a month I have been struggling with this problem. Whenever I try to access my Django Admin page when creating, I get the following error:

OperationalError at /admin/login/
FATAL:  password authentication failed for user "vpusr"
FATAL:  password authentication failed for user "vpusr"

My production.py file looks like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'vpdb',
        'USER': 'vpusr',
        'PASSWORD': os.environ["VP_DB_PASS"],
        'HOST': 'localhost',
    }
}

NOTE: the environment variable is working correctly. even if I put a regular password with hard code there, it does not work.

Here is a list of databases with its owner:

                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 vpdb      | vpusr    | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/vpusr            +
           |          |          |             |             | vpusr=CTc/vpusr

And here is the list of users :

                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 vpusr     | Superuser, Create DB                                       | {}

As you can see, I also tried adding the superuser and Create DB roles in vpusr, but this had no effect.

Even when I try to connect through the terminal, I get the same error:

sudo -u postgres psql -U vpusr vpdb

: psql: FATAL: Peer authentication failed for user "vpusr"

:

psql -U vpusr -h localhost vpdb

psql vpusr.

: , . , . Gunicorn, Nginx, Virtualenv, Django, Postgres Ubuntu Digital Ocean.

, , !

: , ! , django postgres ?

EDIT: . postgres :

    2017-09-09 18:09:55 UTC [29909-2] LOG:  received fast shutdown request
2017-09-09 18:09:55 UTC [29909-3] LOG:  aborting any active transactions
2017-09-09 18:09:55 UTC [29914-2] LOG:  autovacuum launcher shutting down
2017-09-09 18:09:55 UTC [29911-1] LOG:  shutting down
2017-09-09 18:09:55 UTC [29911-2] LOG:  database system is shut down
2017-09-09 18:09:56 UTC [2711-1] LOG:  database system was shut down at 2017-09-09 18:09:55 UTC
2017-09-09 18:09:56 UTC [2711-2] LOG:  MultiXact member wraparound protections are now enabled
2017-09-09 18:09:56 UTC [2710-1] LOG:  database system is ready to accept connections
2017-09-09 18:09:56 UTC [2715-1] LOG:  autovacuum launcher started
2017-09-09 18:09:57 UTC [2717-1] [unknown]@[unknown] LOG:  incomplete startup packet
2017-09-09 18:10:17 UTC [2740-1] tony@vpdb LOG:  provided user name (tony) and authenticated user name (postgres) do not match
2017-09-09 18:10:17 UTC [2740-2] tony@vpdb FATAL:  Peer authentication failed for user "tony"
2017-09-09 18:10:17 UTC [2740-3] tony@vpdb DETAIL:  Connection matched pg_hba.conf line 90: "local   all             all                                     peer"

EDIT:

pg_hba.conf :

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            password
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

?

+6
3

PostgreSQL , pg_hba.conf md5, md5.

2017-09-01 11:42:17 UTC [16320-1] vpusr@vpdb FATAL:  password authentication failed for user "vpusr"
2017-09-01 11:42:17 UTC [16320-2] vpusr@vpdb DETAIL:  Connection matched pg_hba.conf line 92: "host    all             all             127.0.0.1/32            md5"

pg_hba.conf PostgreSQL, vim pg_hba.conf

host    all             all             127.0.0.1/32            md5

host    all             all             127.0.0.1/32            password

PostgreSQL

[root@server] service postgresql restart

, , : sudo -u postgres psql -U vpusr vpdb -h <host>,

local    all             all             127.0.0.1/32            <method>

, , -h <host>,

host    all             all             127.0.0.1/32            password

,

sudo -u postgres -c "PGPASSWORD=<password>;psql -h localhost -U vpusr vpdb"

EDIT: , pg_hba.conf

+2

:

db_user_namespace (boolean)

. . postgresql.conf .

, @dbname. , @ , , , . : , @ SQL, .

. @ , . @. @ , .

db_user_namespace , . , , . md5 , , md5 db_user_namespace.

, psql , .

, psycopg2 libpq, FIPS OpenSSL. md5, OpenSSL md5. , .

. . -, psycopg2 .

- . , ascii, abcdefghijkl. Django , LANG_* LC_* .

+1

fox fix password authentication failed for user "vpusr"try adding the password as is in settingsand test foros.environ["VP_DB_PASS"],

change engine

'ENGINE': 'django.db.backends.postgresql_psycopg2'

install if necessary:

pip install psycopg2

for fix psql: FATAL: Peer authentication failed for user "vpusr"try adding a host

psql -h localhost -U vpusr vpdb
#    ^^^^^^^^^^^^    
0
source

All Articles