带有Heroku上PostgreSQL应用程序的Django无法同步

Fil*_*ipe 5 python django postgresql heroku

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

在Heroku上使用Django入门

一切运行良好,直到我进入syncbd部分:

同步资料库

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

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"?
Run Code Online (Sandbox Code Playgroud)

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

LOG:  database system is ready to accept connections
LOG: autovacuum launcher started
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)

我正在Mac OS 10.7上工作。

我要部署到Heroku的代码在这里可用:

链接到我的代码

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

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

但似乎没有任何效果。

编辑:

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

# 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() 
Run Code Online (Sandbox Code Playgroud)

Dan*_*ité 1

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

1)第3行:

DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
Run Code Online (Sandbox Code Playgroud)

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.
    }
}
Run Code Online (Sandbox Code Playgroud)

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

这些方法并不意味着可以相互叠加。您只想选择一个。

另外,从技术上讲,作为与数据库服务器的客户端连接的发起者,您应该知道是否通过 TCP(在本例中是其主机名或 IP 地址加端口)或通过Unix 域套接字文件,在这种情况下是其完整目录路径(以斜杠开头)。在这两种情况下,这都属于HOST连接参数的部分。

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

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

SHOW unix_socket_directory;
Run Code Online (Sandbox Code Playgroud)

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