【问题标题】:Using Postgres Fields in Django在 Django 中使用 Postgres 字段
【发布时间】:2016-04-05 15:08:43
【问题描述】:

我见过 How to use ArrayField in Django using PostgreSQL DB? 但是当我打电话时我得到的当前错误发生 python manage.py migrate 我得到了错误

`  Applying game.0001_initial...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/Library/Python/2.7/site-packages/django/core/management/commands/migrate.py", line 221, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 147, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Library/Python/2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Library/Python/2.7/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
    schema_editor.create_model(model)
  File "/Library/Python/2.7/site-packages/django/db/backends/base/schema.py", line 282, in create_model
    self.execute(sql, params or None)
  File "/Library/Python/2.7/site-packages/django/db/backends/base/schema.py", line 107, in execute
    cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py", line 316, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: near "[10]": syntax error`

咨询后 https://docs.djangoproject.com/en/1.9/ref/contrib/postgres/fields/ 我仍然无法弄清楚如何在 OS X 上使用 Django 1.8 中可用的 PostgresSQL 字段。这是我的第一个 Django 应用程序,因此将不胜感激。谢谢!

settings.py 的数据库看起来像

`DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}`

【问题讨论】:

  • 但是你需要告诉我们你是如何使用ArrayField的?没有任何代码,我们无法猜测您做错了什么。
  • 您能否向我们展示您的settings.py 部分以及您的数据库设置?当然没有用户名/密码。从"/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py" 看来,您使用了错误的数据库后端驱动程序。
  • 我见过这个,但我只是在放入一个 ArrayField 后才得到这个错误。 stackoverflow.com/questions/33270297/… 除了INSTALLED_APPS = ( 'django.contrib.admin', ... ... ... 'django.contrib.postgres', 'game', ),我在settings.py 中什么都没改变

标签: django postgresql


【解决方案1】:

问题是你没有设置 django 来使用 Postgres,目前它使用默认的 sqlite3。如果您将 settings.py 数据库部分修改为类似于此的内容,您应该不会再看到该错误。

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

或者,如果您想要一个在断开连接时自动重新连接的 postgres 后端,您可以使用名为 postgreconnect 的自定义 postgres 后端。相同的指令适用于两者,但只需使用 django-postgreconnect 而不是 django.db.backends.postgresql_psycopg2

【讨论】:

  • 我相信默认值为 5432。如​​果这不起作用,您可以查看 $PGDATA 目录中的 postgresql.conf
  • 我正在使用 5432 获取 django.db.utils.OperationalError: could not connect to server: No such file or directory。:/
  • 您是否在设置中填写了HOST 部分?你的 postgres 服务器在运行吗?它是在收听localhost 还是0.0.0.0?您可以通过运行netstat -na | grep 5432 来验证这一点
  • 它正在监听 localhost.5432。我不知道名称和用户要使用什么
  • 好的,您是否将localhost 作为HOST 放入您的数据库配置中?此外,如果您还没有配置 postgres,则可能需要配置。我不是这方面的专家,因此您可能必须找到正确设置 postgres 的指南。 digitalocean.com/community/tutorials/… 可能会对您有所帮助,Create a Database and Database User 部分应该为您指明正确的方向。
猜你喜欢
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
  • 1970-01-01
  • 2015-10-06
  • 2017-10-15
  • 2021-06-25
  • 2018-11-07
相关资源
最近更新 更多