Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django with PostgreSQL app on Heroku not synching

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

Getting Started with Django on Heroku

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

Syncing the database

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"?

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.

And I'm working on Mac OS 10.7.

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

Link to my code

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/

but nothing seems to work.

EDIT:

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() 
like image 271
Filipe Avatar asked Nov 03 '22 16:11

Filipe


1 Answers

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) line 3:

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

2) line 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) 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.

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 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.

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;

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

like image 68
Daniel Vérité Avatar answered Nov 12 '22 10:11

Daniel Vérité