postgresql Django 数据库连接错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12316819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:16:29  来源:igfitidea点击:

Django database connection error

djangopostgresql

提问by Jae

I ran these lines from "python manage.py shell":

我从“python manage.py shell”运行了这些行:

from django.db import connection
cursor = connection.cursor()

But got the following error:

但得到以下错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 306, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 177, in _cursor
    self.connection = Database.connect(**conn_params)
  File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
    connection_factory=connection_factory, async=async)
OperationalError: FATAL:  role "jay" does not exist

In settings.py I have

在 settings.py 我有

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', 
        'NAME': 'mysite',                     
        'USER': '',                     
        'PASSWORD': '',                  
        'HOST': '',                      
        'PORT': '',                     
    }
}

What am I doing wrong here? I installed postgresql and the adapter

我在这里做错了什么?我安装了 postgresql 和适配器

回答by Craig Ringer

FATAL: role "jay" does not exist

致命:角色“jay”不存在

Since USERis blank, it's using whatever your local username is (jay), and you haven't created a PostgreSQL user by that name.

由于USER为空,它使用您的本地用户名 ( jay),并且您尚未使用该名称创建 PostgreSQL 用户。

Try:

尝试:

psql -u postgres createuser -P jay

You'll probably have to put the password you enter at the prompt into settings.py, unless your pg_hba.confis using identas the auth method.

您可能必须将您在提示符下输入的密码放入settings.py,除非您pg_hba.conf正在使用ident作为身份验证方法。

回答by pacha

Since you haven't provided a username, postgresql is using the username of the user of your operating system under which django is being run to connect to the database. The error that you are getting says that that user (jay) is not defined in the database.

由于您尚未提供用户名,postgresql 将使用您的操作系统用户的用户名,在该用户名下运行 django 以连接到数据库。您收到的错误表明该用户 (jay) 未在数据库中定义。

A possible solution is to provide the name of the username and password of the owner of the mysitedatabase. Add also localhostas the host to ensure that postgresql uses user/password authentication instead of using the username that owns the django process.

一个可能的解决方案是提供mysite数据库所有者的用户名和密码。也添加localhost为主机以确保 postgresql 使用用户/密码身份验证而不是使用拥有 django 进程的用户名。