无法运行' python manage.py migrate'

时间:2016-03-20 19:38:51

标签: python django postgresql psycopg2

我是编程和学习Web开发的新手。我安装了:

-Django 1.9
 -postgresql 9.5
 -psycopg2(使用whl文件)db api用于Windows 7操作系统上的postgresql。

不是虚拟环境。

我已在我的 settings.py 文件中为我的项目设置了数据库连接,如下所示。

DATABASES =  {
    'default': {
        'ENGINE': 'django.db.backends.postgresql', 
        'NAME': 'Test', 
        'User': 'postgres', 
        'PASSWORD': 'admin', 
        'HOST': 'localhost', 
        'PORT': '5432', 
    }
} 

但是当我运行迁移时,我遇到了以下问题。我想将postgres连接到我的django

请参阅以下错误:

Traceback (most recent call last):
 File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 199, in ensure_connection
self.connect()
 File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\postgresql\base.py", line 175, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\psycopg2\__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)psycopg2.OperationalError
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
   execute_from_command_line(sys.argv)
 File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\core\management\__init__.py", line 353, in         execute_from_command_line
    utility.execute()
   File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
 -packages\django\core\management\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
 -packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
   File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
 -packages\django\core\management\base.py", line 399, in execute
output = self.handle(*args, **options)
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
 -packages\django\core\management\commands\migrate.py", line 89, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\loader.py", line 49, in __init__
self.build_graph()
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\loader.py", line 176, in build_graph
self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\recorder.py", line 65, in applied_migrations
self.ensure_schema()
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\recorder.py", line 52, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 204, in _cursor
self.ensure_connection()
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 199, in ensure_connection
self.connect()
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
  -packages\django\db\utils.py", line 95, in __exit__six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
 -packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
  -packages\django\db\backends\base\base.py", line 199, in ensure_connection
self.connect()
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\postgresql\base.py", line 175, in get_new_connection
   connection = Database.connect(**conn_params)
  File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\psycopg2\__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError

我已经用python脚本检查了psycopg2是否能够在postgresql中连接数据库,但是当我从django项目运行迁移时,它无法正常运行并出现上述错误。

1 个答案:

答案 0 :(得分:1)

您的数据库配置:

DATABASES =  {
    'default': {
        'ENGINE': 'django.db.backends.postgresql', 
        'NAME': 'Test', 
        'User': 'postgres', 
        'PASSWORD': 'admin', 
        'HOST': 'localhost', 
        'PORT': '5432', 
    }
} 

应该是:

DATABASES =  {
    'default': {
        'ENGINE': 'django.db.backends.postgresql', 
        'NAME': 'Test', 
        'USER': 'postgres', 
        'PASSWORD': 'admin', 
        'HOST': 'localhost', 
        'PORT': '5432', 
    }
} 

请注意,User应为USER