django postgres密码认证失败

时间:2018-12-26 11:27:22

标签: python django postgresql

我正在尝试将Django与postgres连接起来,但出现此错误

  

文件   “ /home/tbosss/Desktop/environment/test_api/venv/lib/python3.6/site-packages/psycopg2/init.py”,   连接线130       conn = _connect(dsn,connection_factory = connection_factory,** kwasync)django.db.utils.OperationalError:FATAL:用户“ tbosss”的密码身份验证失败FATAL:密码   用户“ tbosss”的身份验证失败

这是我的设置。py:

'default' : {
    'ENGINE' : 'django.db.backends.postgresql_psycopg2',
    'NAME' : 'login',
    'USERNAME' : 'postgres',
    'PASSWORD' :'123',
    'HOST' : 'localhost',
    'PORT' : '5432'
}

tbosss是root,应该是postgre用户。它没有连接到postgre。 这里是终端的数据库列表

                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------

 login     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres         +
           |          |          |             |             | postgres=CTc/postgres+
           |          |          |             |             | admin=CTc/postgres
 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

1 个答案:

答案 0 :(得分:1)

psql(PostgreSQL的默认客户端)在未指定用户的情况下尝试与当前OS用户连接,我认为psycopg2也会这样做。

  

PostgreSQL文档包含以下内容的完整列表:   支持的参数。另请注意,可以传递相同的参数   使用环境变量访问客户端库。

http://initd.org/psycopg/docs/module.html

https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS

当我查看psycopg2教程时,看到 USERNAME 选项必须更改为 USER

  

psycopg2.connect(“ dbname ='template1'user ='dbuser'host ='localhost'   password ='dbpass'“)

https://wiki.postgresql.org/wiki/Psycopg2_Tutorial