如何解决在manage.py上运行sudo导致无法连接到PSQL的问题?

时间:2015-05-16 08:13:45

标签: python django python-3.x psycopg2

所以我从这个stackoverflow线程Burhan Khalid跟随command.py的解决方案,并在我的import os from django.core.management.base import BaseCommand, CommandError from chat.models import Account, Client, Trader class Command(BaseCommand): help = 'Does some magical work' def __str__(self): return "Your driver!" def handle(self, *args, **options): print(Client.objects.all())

中包含以下代码
py manage.py command

运行命令sudo py manage.py command时运行完美 但是,我试图将它连接到Twisted反应器,因此需要我使用sudo运行项目。现在,当我运行File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psycopg2/__init__.py", line 164, in connect conn = _connect(dsn, connection_factory=connection_factory, async=async) django.db.utils.OperationalError: FATAL: role "root" does not exist 时,它在我身上失败了,因为这是错误的最后一部分:

sudo

所以我假设psycopg2/psql混淆了settings.py?有没有办法解决?

这是我的>INSTALLED_APPS = ( > 'django.contrib.admin', > 'django.contrib.auth', > 'django.contrib.contenttypes', > 'django.contrib.sessions', > 'django.contrib.messages', > 'django.contrib.staticfiles', > > 'chat', >) > >MIDDLEWARE_CLASSES = ( > 'django.contrib.sessions.middleware.SessionMiddleware', > 'django.middleware.common.CommonMiddleware', > 'django.middleware.csrf.CsrfViewMiddleware', > 'django.contrib.auth.middleware.AuthenticationMiddleware', > 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', > 'django.contrib.messages.middleware.MessageMiddleware', > 'django.middleware.clickjacking.XFrameOptionsMiddleware', > 'django.middleware.security.SecurityMiddleware', >) >ROOT_URLCONF = 'esdctest.urls' >TEMPLATES = [ > { > 'BACKEND': 'django.template.backends.django.DjangoTemplates', > 'DIRS': [], > 'APP_DIRS': True, > 'OPTIONS': { > 'context_processors': [ > 'django.template.context_processors.debug', > 'django.template.context_processors.request', > 'django.contrib.auth.context_processors.auth', > 'django.contrib.messages.context_processors.messages', > ], > }, > }, > >WSGI_APPLICATION = 'esdctest.wsgi.application' >DATABASES = { > 'default': { > 'ENGINE': 'django.db.backends.postgresql_psycopg2', > 'NAME': 'db', > 'USER': '', > 'PASSWORD': '', > 'HOST': '127.0.0.1', > 'PORT': '5432', > } >} >LANGUAGE_CODE = 'en-us' >TIME_ZONE = 'UTC' >USE_I18N = True >USE_L10N = True >USE_TZ = True >STATIC_URL = '/static/' 文件:

@Controller
@RequestMapping(value = "/test")
 public class TestController {

@RequestMapping(value = "/add", method = RequestMethod.POST, headers = { "Accept=application/json",
        "Content-Type=application/json" })
public @ResponseBody String create(@RequestBody String body) {
    .....
}}

由于

1 个答案:

答案 0 :(得分:2)

您应填写USER字典中的DATABASES字段。像这样:

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

否则将使用当前用户的名称(例如root)。