【问题标题】:Django with PostgreSQL app on Heroku not synchingHeroku上带有PostgreSQL应用程序的Django不同步
【发布时间】:2012-09-01 12:17:14
【问题描述】:

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

Getting Started with Django on Heroku

在我进入 syncbd 部分之前,一切都运行良好:

Syncing the database

当我运行: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"?

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

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

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

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。

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

Link to my code

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

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() 

【问题讨论】:

    标签: python django postgresql heroku


    【解决方案1】:

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

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

    这些方法并不是要相互叠加。您只想选择一个。

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

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

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

    SHOW unix_socket_directory;
    

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

    【讨论】:

      【解决方案2】:

      我刚刚使用 posgresql 在 Heroku 中部署了 Django 应用程序,这是我的代码,它运行良好,希望对您有所帮助:

      requirements.txt

      Django==1.7.4
      dj-database-url==0.3.0
      dj-static==0.0.6
      gunicorn==19.2.1
      psycopg2==2.6
      six==1.9.0
      static3==0.5.1
      wsgiref==0.1.2
      

      wsgi.py

      import os
      os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<PROJECT_NAME>.settings")
      
      from django.core.wsgi import get_wsgi_application
      from dj_static import Cling
      
      application = Cling(get_wsgi_application())
      

      过程文件

      web: gunicorn <PROJECT_NAME>.wsgi
      

      确保你的 Heroku 上有 Postgres:

      heroku addons:add heroku-postgresql:dev
      

      找出数据库 url 环境变量。它看起来像这样:HEROKU_POSTGRESQL__URL

      heroku config | grep POSTGRESQL
      

      settings.py

      import dj_database_url
      POSTGRES_URL = "HEROKU_POSTGRESQL_<DB_NAME>_URL"
      DATABASES = {'default': dj_database_url.config(default=os.environ[POSTGRES_URL])}
      
      # Honor the 'X-Forwarded-Proto' header for request.is_secure()
      SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
      
      # Allow all host headers
      ALLOWED_HOSTS = ['*']
      
      # Static asset configuration
      import os
      BASE_DIR = os.path.dirname(os.path.abspath(__file__))
      STATIC_ROOT = 'staticfiles'
      STATIC_URL = '/static/'
      
      STATICFILES_DIRS = (
          os.path.join(BASE_DIR, 'static'),
      )
      

      之后我只是将所有内容推送到主服务器并运行同步数据库

      heroku run python manage.py syncdb
      

      这可能会有所帮助Getting Started with Django on Heroku。让我知道我是否有任何问题或您是否需要其他东西。

      【讨论】:

        【解决方案3】:

        您可能没有将 PORT 加载到数据库中。加载数据库连接的代码是什么样的?

        【讨论】:

        • 这是命令行。我提供的链接中提供了 python 代码。
        猜你喜欢
        • 1970-01-01
        • 2018-12-13
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-02
        相关资源
        最近更新 更多