Django 1.4 syncdb + psycopg 时报时区错误

2 投票
1 回答
1520 浏览
提问于 2025-04-17 13:30

在MacOS上安装了Postgres.app之后,我创建了一个Python虚拟环境,并安装了Django、dj-database-url和psycopg2,但每次我执行:

/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 194, in _cursor
self.ops.set_time_zone_sql(), [tz])
psycopg2.DataError: invalid value for parameter "TimeZone": "UTC"

在我的Django应用中运行“python manage.py syncdb”时,都会出现问题。

有没有人知道这可能是什么原因呢?

1 个回答

3

当我查看base.py文件的第194行时,我注意到了以下代码:

tz = 'UTC' if settings.USE_TZ else settings_dict.get('TIME_ZONE')

我把settings.py文件中的USE_TZ参数改成'False'后,出现了以下错误:

psycopg2.DataError: invalid value for parameter "TimeZone": "Australia/Perth"

我在settings.py文件中设置的时区是"Australia/Perth",所以至少现在它能访问到我的时区了。

再次查看psycopg2的base.py文件时,我发现了以下代码:

if tz:
                try:
                    get_parameter_status = self.connection.get_parameter_status
                except AttributeError:
                    # psycopg2 < 2.0.12 doesn't have get_parameter_status
                    conn_tz = None
                else:
                    conn_tz = get_parameter_status('TimeZone')

                if conn_tz != tz:

在base.py中加了一个调试用的'print conn_tz',显示conn_tz(大概是postgres数据库的时区设置)是'Australia/West'。

把我的settings.py中的TIME_ZONE改成'Australia/West'后,syncdb就能正常运行了。

撰写回答