django postgreSQL连接问题

发布于 2024-10-30 08:22:04 字数 2483 浏览 1 评论 0原文

我在 Django 中连接 PostgreSql 时遇到问题

>>python manage.py shell
>>> from django.db import connection
>>> cursor = connection.cursor()
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 250, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 148, in _cursor
    cursor.execute("SET TIME ZONE %s", [settings_dict['TIME_ZONE']])
DataError: unrecognized time zone name: "America/Chicago"

,但是当我尝试时,

>>> c=connection.cursor()
>>> c

>>> c.execute("select * from test")
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
    return self.cursor.execute(query, args)
DatabaseError: current transaction is aborted, commands ignored until end of transaction block

>>> connection.commit()
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 202, in commit
    self.set_clean()
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 147, in set_clean
    raise TransactionManagementError("This code isn't under transaction management")
TransactionManagementError: This code isn't under transaction management
>>> c.execute("select * from test")
>>> c.fetchone()
(1, 100, u'abd')

我不知道问题出在哪里
我谷歌了一下,但没有帮助,有人可以帮忙吗?非常感谢。
我的设置是:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'mydb',                      # Or path to database file if using sqlite3.
        'USER': 'postgres',                      # Not used with sqlite3.
        'PASSWORD': '123',                  # Not used with sqlite3.
        'HOST': '/tmp',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '5432',                      # Set to empty string for default. Not used with sqlite3.
        #'OPTIONS':True,
    }
}


I got a problem in connecting the PostgreSql in Django

>>python manage.py shell
>>> from django.db import connection
>>> cursor = connection.cursor()
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 250, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 148, in _cursor
    cursor.execute("SET TIME ZONE %s", [settings_dict['TIME_ZONE']])
DataError: unrecognized time zone name: "America/Chicago"

but, when I tried

>>> c=connection.cursor()
>>> c

>>> c.execute("select * from test")
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
    return self.cursor.execute(query, args)
DatabaseError: current transaction is aborted, commands ignored until end of transaction block

>>> connection.commit()
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 202, in commit
    self.set_clean()
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 147, in set_clean
    raise TransactionManagementError("This code isn't under transaction management")
TransactionManagementError: This code isn't under transaction management
>>> c.execute("select * from test")
>>> c.fetchone()
(1, 100, u'abd')

I have no idea where the problem lies
I did google but it didn't help,Can anyone help ?Thanks a lot.
my setting is :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'mydb',                      # Or path to database file if using sqlite3.
        'USER': 'postgres',                      # Not used with sqlite3.
        'PASSWORD': '123',                  # Not used with sqlite3.
        'HOST': '/tmp',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '5432',                      # Set to empty string for default. Not used with sqlite3.
        #'OPTIONS':True,
    }
}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2024-11-06 08:22:04

看起来时区设置不正确。检查您的 postgresql 配置是否支持值“America/Chicago”(它正在我的测试服务器上运行...)

尝试将其设置为 None - 也许它会帮助您:

TIME_ZONE = None

looks like timezone settings are incorrect. check if the value 'America/Chicago' is supported by your postgresql configuration (it is working my test server...)

Try to set it to None - maybe it will help you:

TIME_ZONE = None
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文