我安装postgres
了Django
按照本指南
这里我在命令行中创建了一个数据库和一个用户postgres
:
CREATE DATABASE myProject;
CREATE USER myUser WITH PASSWORD '1234';
当我尝试连接到该数据库时,无论是通过Django
还是直接通过,pscopg2
我都会收到错误的密码错误。
#connect through python
import psycopg2
psycopg2.connect("dbname=myProject user=myUser host=localhost password=1234 port=5432")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/data/project/venv/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL: password authentication failed for user "myUser"
FATAL: password authentication failed for user "myUser"
在这个问题中,我替换了我的实际项目名称、用户名和密码。但我已经仔细检查了两三遍,我写下的名称/密码都是正确的。
我怎么会收到错误的密码请求?我该如何解决?
编辑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 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
答案1
对于任何发现这个问题的人,请注意,显然数据库名称和用户名是总是以小写形式,即使您使用大写字母创建它们。
因此,创建如下数据库和用户:
CREATE DATABASE myProject;
CREATE USER myUser WITH PASSWORD '1234';
含义与这样做完全相同:
CREATE DATABASE myproject;
CREATE USER myuser WITH PASSWORD '1234';
答案2
尝试为 Postgres 创建用户
postgres=# create user username with encrypted password 'password';
添加权限
postgres=# grant all on database db_name to username;
其他有用的命令可能会有帮助
sudo -u postgres psql
\du+ #list of user
\l+ # list of DB
致谢 这篇文章