Django does not sync with PostgreSQL application on Heroku

I try to run Django on Heroku after the tutorial:

Getting started with Django on Heroku

Everything went well until I got to the syncbd part:

Database synchronization

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 am currently using PostgreSQL from Homebrew and it works well:

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

The application server also works locally:

 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 am working on Mac OS 10.7.

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

Link to my code

I have already tried many possible solutions, such as:

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

but nothing works.

EDIT:

Looking around, I found this code and added it to the settings.py file, and it looks like it solved 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() 
+4
source share
3 answers

In settings.py in the source code that you linked , it seems that you have two conflicting declarations for setting DATABASES

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) In addition, the extra code of your last edit looks like another way to specify connection arguments, which probably again negates the consequences of previous declarations.

These methods should not stack on top of each other. You want to choose only one.

In addition, technically, as the initiator of a client connection to the db server, you should know if the server must be reached via TCP (in this case, its hostname or IP address plus port), or through a Unix domain socket file, and in this case its full path to the directory (starting with a slash). In both cases, this is part of the HOST parameters.

Postgres provides default values โ€‹โ€‹for all of them, but once you mix and match different pieces of software from different sources, these default values โ€‹โ€‹no longer help, and providing explicit values โ€‹โ€‹becomes a requirement.

If you doubt the socket path, inside psql when connecting as a postgres user, this path can be obtained with the SQL command:

 SHOW unix_socket_directory; 

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

+1
source

I just deployed a Django app in Heroku with posgresql and this is my code, it works fine, hope this 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 heroine:

 heroku addons:add heroku-postgresql:dev 

Display the env database variable. It will 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 click everything on the wizard and run syncdb

 heroku run python manage.py syncdb 

This can be useful Getting started with Django on Heroku . Let me know if something goes wrong or you need something else.

+1
source

You probably are not loading your PORT into the database. What does your code look like for loading a database connection?

0
source

Source: https://habr.com/ru/post/1432976/


All Articles