Heroku 上带有 PostgreSQL 应用程序的 Django 不同步 [英] Django with PostgreSQL app on Heroku not synching

查看:89
本文介绍了Heroku 上带有 PostgreSQL 应用程序的 Django 不同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照以下教程在 Heroku 上运行 Django:

I'm trying to run the Django on Heroku following the tutorial at:

在 Heroku 上开始使用 Django

一切都运行良好,直到我到达同步部分:

Everything was running well untill I got to the syncbd part:

同步数据库

当我运行:heroku run python manage.py syncdb时,出现以下错误:

When I run: heroku run python manage.py syncdb, I get the following error:

psycopg2.OperationalError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

我目前正在使用 Homebrew 的 PostgreSQL,它运行良好:

I'm currently using PostgreSQL from Homebrew, and it's running well:

LOG:  database system is ready to accept connections
LOG: autovacuum launcher started

应用服务器也在本地运行:

The app server is also running localy:

Validating models...

0 errors found
Django version 1.4.1, using settings 'hellodjango.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

我正在使用 Mac OS 10.7.

And I'm working on Mac OS 10.7.

我正在部署到 Heroku 的代码可在此处获得:

The code I'm deploying to Heroku is available here:

链接到我的代码

我已经尝试了很多可能的解决方案,例如:

I've already tried a lot of possible solutions such as:

http://jeffammons.net/2011/09/fixing-postgres-on-mac-10-7-tiger-for-django/

但似乎没有任何效果.

环顾四周,我找到了这段代码,并将其添加到settings.py文件中,似乎解决了我的问题:

Looking around I've found this code, and added it to the settings.py file, and it seems to solve my problem:

# Register database schemes in URLs.
urlparse.uses_netloc.append('postgres')
urlparse.uses_netloc.append('mysql')

try:
    if 'DATABASES' not in locals():
        DATABASES = {}

    if 'DATABASE_URL' in os.environ:
        url = urlparse.urlparse(os.environ['DATABASE_URL'])

        # Ensure default database exists.
        DATABASES['default'] = DATABASES.get('default', {})

        # Update with environment configuration.
        DATABASES['default'].update({
            'NAME': url.path[1:],
            'USER': url.username,
            'PASSWORD': url.password,
            'HOST': url.hostname,
            'PORT': url.port,
        })
        if url.scheme == 'postgres':
            DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'

        if url.scheme == 'mysql':
            DATABASES['default']['ENGINE'] = 'django.db.backends.mysql'
except Exception:
    print 'Unexpected error:', sys.exc_info() 

推荐答案

settings.py中的原代码中你链接到,您的 DATABASES 设置似乎有两个相互矛盾的声明:

In the settings.py in the original code that you linked to, it seems that you have two contradictory declarations for your DATABASES setting:

1) 第 3 行:

DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}

2) 第 16 行:

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

3) 此外,您最新编辑的附加代码看起来像是另一种指定连接参数的方法,这可能会再次否定先前声明的效果.

3) Also, the additional code of your latest edit looks like yet another method to specify the connection arguments, that probably negates again the effects of the previous declarations.

这些方法不能相互叠加.您只想选择一个.

These methods are not meant to be piled onto each other. You want to choose only one.

此外,从技术上讲,作为到数据库服务器的客户端连接的发起者,您应该知道是否要通过 TCP 访问服务器(在这种情况下,它的主机名或 IP 地址加端口),或通过 Unix 域套接字文件,在这种情况下,它的完整目录路径(以斜杠开头).在这两种情况下,这都会进入连接参数的 HOST 部分.

Also, technically, as the initiator of a client-side connection to a db server, you're supposed to know if the server is to be reached through TCP (and in this case its hostname or IP address plus port), or through a Unix domain socket file, and in that case its full directory path (starting with a slash). In both cases, this goes into the HOST part of the connection parameters.

Postgres 为所有这些提供了默认值,但是一旦您混合和匹配来自不同来源的不同软件部分,这些默认值就不再有用,并且需要提供明确的值.

Postgres provides default values for all of these, but as soon as you mix and match different software parts from different sources, these defaults no longer help and giving explicit values becomes a requirement.

当对socket的路径有疑问时,在psql中以postgres用户身份连接时,可以通过SQL命令获取该路径:

When in doubt about the socket's path, inside psql when connected as the postgres user, this path can be obtained by the SQL command:

SHOW unix_socket_directory;

此设置也存在于服务器端 postgresql.conf 配置文件中.

This setting is also present in the server-side postgresql.conf configuration file.

这篇关于Heroku 上带有 PostgreSQL 应用程序的 Django 不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆