Reputation: 3408
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:
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:
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()
Upvotes: 5
Views: 3480
Reputation: 131
I just deployed and Django app in Heroku with posgresql and this is my code, it works perfectly, I hope it helps:
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())
Procfile
web: gunicorn <PROJECT_NAME>.wsgi
Make sure you have Postgres on your heroku:
heroku addons:add heroku-postgresql:dev
Figure out database url env variable. It's going to look something like this : 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'),
)
After that I just push all to the master and run syncdb
heroku run python manage.py syncdb
This may be helpful Getting Started with Django on Heroku. Let me know if I anything goes wrong or if you need something else.
Upvotes: 1
Reputation: 61506
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.
Upvotes: 1
Reputation: 5954
Its likely that you're not loading your PORT on the database. What does your code look like to load the database connection?
Upvotes: 0