Is the server running on host "localhost" (127.0.0.1) - Reposting - django

Please do not close my question again if you are not sure that the answer provide to others fit my case.
I am deploying a django app for the first time to Heroku. I used the following:
heroku run python manage.py migrate
and it returns :
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection
connection = Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
In my settings.py i have :
DATABASES = {
'default': dj_database_url.config(
default=config('DATABASE_URL')
)
}
and at the bottom :
try:
from .local_settings import *
except ImportError:
pass
in the local_settings.py I have :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'newapp_development',
'USER': 'postgres',
'PASSWORD': '#####',
'HOST': 'localhost',
'PORT': '5432',
}
}
my .gitignore file looks like:
# byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
# tests and coverage
*.pytest_cache
.coverage
# database & logs
*.db
*.sqlite3
*.log
# venv
env
venv
# other
.DS_Store
# javascript
package-lock.json
staticfiles/*
!staticfiles/.gitkeep
!/core/local_settings.py
Do you know why is the error message please ?
Also, note that local_settings is added to . gitignore.
I did a few search on search engine and the responses that i found did not work for me or were not clear enough to me. This is the reason why I took the risk of asking publicly here.
Thank you for your help.

Solution found, the approach that I used works perfectly to manage two different db with two settings files.
What was wrong, was .env file which came with the template that I used. it had the following :
DATABASE_URL=jdbc:postgresql://localhost:5432/postgres
deleting this line makes it work both in production and locally.
Thank you guys for helping me eliminate the potential problems down to the main one.
And to those who downgraded my question,...not much to say actually.

Related

Server closed the connection unexpectedly sometimes

Project background:
Django project, I use gunicorn to run the project, in the project, i use python socket io to process some event
Postgresql, config like this:
DATABASES['default'] = {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': xxx,
'USER': xxx,
'PASSWORD': xxx,
'HOST': 'xxx',
'PORT': '5432',
'CONN_MAX_AGE': 60 * 10, # seconds
'OPTIONS': {
'connect_timeout': 20,
},
}
the python socket io will keep a thread to process some events. so the thread has its own postgresql database connection. sometimes, I find it will throw database connection problem, like this, I don't know why the database connection isn't be closed normally
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/engineio/server.py", line 520, in _trigger_event
return self.handlers[event](*args)
File "/usr/local/lib/python2.7/site-packages/socketio/server.py", line 590, in _handle_eio_message
self._handle_event(sid, pkt.namespace, pkt.id, pkt.data)
File "/usr/local/lib/python2.7/site-packages/socketio/server.py", line 526, in _handle_event
self._handle_event_internal(self, sid, data, namespace, id)
File "/usr/local/lib/python2.7/site-packages/socketio/server.py", line 529, in _handle_event_internal
r = server._trigger_event(data[0], namespace, sid, *data[1:])
File "/usr/local/lib/python2.7/site-packages/socketio/server.py", line 558, in _trigger_event
return self.handlers[namespace][event](*args)
File "/usr/src/app/async_worker/controllers/server/event_handler.py", line 61, in on_jira_handle_from_client_retrieve
ticket = Ticket.get_ticket_by_id(ticket_id)
File "/usr/src/app/review/models/ticket.py", line 144, in get_ticket_by_id
return Ticket.objects.filter(id=ticket_id).first()
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 564, in first
objects = list((self if self.ordered else self.order_by('pk'))[:1])
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 899, in execute_sql
raise original_exception
OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
This is what I get in the PostgreSQL log file:
LOG: could not receive data from client: Connection reset by peer
some ideas about the problem? I think if the connection keeps too long time (> CONN_MAX_AGE), the conn should be closed and set to None, but actually the connection is not null but the already closed
The error message you see is from the PostgreSQL client library.
Now both the PostgreSQL server and client complain that the other side suddenly went away, so this is almost certainly a nerwork problem.
My money is on an ill-configured firewall that drops idle connections after a while. I've always wondered if there is any valid use case to have a firewall do this...

Issue with django/postgresql when django started from cron

Edit to ask who thought that this question had anything to do with the "possibly related" one.
I have a fairly simple django (1.11) project using the rest_framework that works fine when I start it from the command line, typing
nohup python manage.py runserver 0.0.0.0:4448 &
on centos. Connects to a postgresql database, with
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'my_database',
'USER': 'my_user',
#'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
' PORT': '5432',
}
}
in my settings.py file. However if I set up the runserver command to run at boot time from a cron I get the following when I send a request to the application:
... lot of stuff
django.db.utils.OperationalError: could not connect to server: Connection
refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
and in my pg_hba file I have
IPv4 local connections:
host all all 127.0.0.1/32
trust # local host
host all all 10.2.11.53/32
trust
host all my_user 0.0.0.0/0 trust
and also I have
netstat -plunt | grep post
tcp 0 0 10.2.11.53:5432 0.0.0.0:* LISTEN
867/postmaster
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
867/postmaster
Any suggestion?
Thanks,
a
PS The full traceback:
Unhandled exception in thread started by <function wrapper at 0x1a11230>
Performing system checks...
[<RegexURLPattern batch-batch-done ^batch/batch-done/$>, <RegexURLPattern batch-load-urls ^batch/load-urls/$>, <RegexURLPattern batch-request-batch ^batch/request-batch/$>, <RegexURLPattern batch-schedule-job ^batch/schedule-job/$>]
System check identified no issues (0 silenced).
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 128, in inner_run
self.check_migrations()
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 422, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
File "/usr/lib/python2.7/site-packages/django/db/migrations/loader.py", line 52, in __init__
self.build_graph()
File "/usr/lib/python2.7/site-packages/django/db/migrations/loader.py", line 209, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/usr/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
self.ensure_schema()
File "/usr/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 254, in cursor
return self._cursor()
File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 229, in _cursor
self.ensure_connection()
File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/usr/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/lib/python2.7/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
connection = Database.connect(**conn_params)
File "/usr/lib64/python2.7/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Could you post the full traceback, as well as the cron job itself?
Also, if I understand your question correctly, you are trying to use cron to start your server on boot? If this is the case, you would be better served using something like supervisord to manage your server processes IMO.

Django Newbie: python manage.py syncdb error?

Working on the Django tutorial at the moment and got this error when I tried:
python manage.py syncdb
Using sqlite3
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/Library/Python/2.6/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "/Library/Python/2.6/site-packages/django/db/backends/__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Library/Python/2.6/site-packages/django/db/backends/sqlite3/base.py", line 281, in _cursor
self._sqlite_create_connection()
File "/Library/Python/2.6/site-packages/django/db/backends/sqlite3/base.py", line 271, in _sqlite_create_connection
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
My settings.py file reads:
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '/Users/username/django_tutorial/mysite',
Anyone know the problem here? it seems like I am putting in the complete file directory, but the databases still couldn't be created? ):
You should use the path to the file, including the file name:
'NAME': '/Users/username/django_tutorial/mysite/data.sqlite',
You can also just give it the file name:
'NAME': 'data.sqlite',
In that case it will be created in the same directory than the one from which you run manage.py.
The best practice is to pass an absolute path, but to ensure this absolute path is created relatively to the settings file. E.G:
import os
SETTINGS_DIR = os.path.dirname(os.path.abspath(__file__))
Then latter:
'NAME': os.path.join(SETTINGS_DIR, 'data.sqlite'),
Remember that the settings file is a Python file, and that you can use Python code in it.
There is a list of Django newbie mistakes on project's wiki. See this entry: https://code.djangoproject.com/wiki/NewbieMistakes#DjangosaysUnabletoOpenDatabaseFilewhenusingSQLite3
This specific error is common mistake among Django's newbies. See detailed description below.
PROBLEM
You're using SQLite3, your DATABASE_NAME is set to the
database file's full path, the database file is writeable by Apache,
but you still get the above error.
SOLUTION
Make sure Apache can also write to the parent directory of
the database. SQLite needs to be able to write to this directory.
Make sure each folder of your database file's full path does not start
with number, eg. /www/4myweb/db (observed on Windows 2000).
If DATABASE_NAME is set to something like
'/Users/yourname/Sites/mydjangoproject/db/db', make sure you've
created the 'db' directory first.
Make sure your /tmp directory is world-writable (an unlikely cause as
other thing on your system will also not work). ls /tmp -ald should
produce drwxrwxrwt ....
Make sure the path to the database specified in settings.py is a full
path.
If you working on windows make also sure that you have the path to the
db directory written with double backlashes
'C:\\django\\sqlite\\django.db'
i dont think you need the full path for the name, just put the file name ie "my_db.db". that is assuming you have sqlite3 installed.

Django Database Improperly Configured when function called outside of Django

I'm trying to call a python function that makes some queries into my django database from GNU mailman.
When mailman tries to deliver a message, it imports my python script. It later calls a function in my script to modify the message object. The error I'm getting is:
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation \
for more details.
Here's how I'm configuring the settings, at the very top of my file:
from django.core.management import setup_environ
from mysite import settings
setup_environ(settings)
When I run python manage.py syncdb, it seems to create the database fine. Here's my database configuration:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'django_db', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'root', # 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.
}
}
Further, I've commented out the entirety of my function such that it now looks like:
def f():
return
So I don't think this has to do with the function call.
Further, I've tested the setup_environ lines in the python console and everything works as expected.
Further, when I restart GNU mailman, I believe it has to load all its scripts, which means it necessarily has to import my file. This means that these "setup_environ" lines run when I restart mailman. And it's fine -- I get no errors.
It's only when GNU mailman tries to deliver a message that I have problems.
So I'm pretty stumped. I do run the mailman restart command as sudo with additional PYTHONPATH and DJANGO_SETTINGS_MODULE environmental variables, but I've manually added to the relevant parts to my sys.path and os.environ dict, which doesn't fix the problem either. Besides, the error doesn't suggest it's a problem with the path or being unable to find the settings module.
The full stack trace is:
Jun 04 12:06:11 2012 (5249) Uncaught runner exception: settings.DATABASES is improperly configured. Please supply the ENGINE val\
ue. Check settings documentation for more details.
Jun 04 12:06:11 2012 (5249) Traceback (most recent call last):
File "/var/lib/mailman/Mailman/Queue/Runner.py", line 100, in _oneloop
msg, msgdata = self._switchboard.dequeue(filebase)
File "/var/lib/mailman/Mailman/Queue/Switchboard.py", line 173, in dequeue
redirect_list(msg, data)
File "/home/ubuntu/djcode/mysite/mysite/apps/mailman/redirect.py", line 32, in redirect_list
File "/home/ubuntu/djcode/mysite/mysite/apps/mailman/redirect.py", line 45, in _get_real_listname
from mysite.apps.common.models import CustomUser
File "/home/ubuntu/djcode/mysite/mysite/apps/common/custom_user_manager.py", line 54, in get
email_object = Email.objects.get(email=kwargs['email'])
File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 131, in get
return self.get_query_set().get(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 361, in get
num = len(clone)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 85, in __len__
self._result_cache = list(self.iterator())
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 291, in iterator
for row in compiler.results_iter():
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 763, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 808, in execute_sql
sql, params = self.as_sql()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 71, in as_sql
out_cols = self.get_columns(with_col_aliases)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 218, in get_columns
col_aliases)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 306, in get_default_columns
r = '%s.%s' % (qn(alias), qn2(field.column))
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 49, in quote_name_unless_alias
r = self.connection.ops.quote_name(name)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/dummy/base.py", line 15, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation \
for more details.
Seems you have not specified anything in your setting.py file's DATABASES dictionary.Specify the following to connect to databse successfully(as per docs),
1) ENGINE -- Either 'django.db.backends.postgresql_psycopg2', 'django.db.backends.mysql', 'django.db.backends.sqlite3' or 'django.db.backends.oracle'. Other backends are also available.
2) NAME -- The name of your database. If you're using SQLite, the database will be a file on your computer; in that case, NAME should be the full absolute path, including filename, of that file. If the file doesn't exist, it will automatically be created when you synchronize the database for the first time (see below).
3) USER -- Your database username (not used for SQLite).
4) PASSWORD -- Your database password (not used for SQLite).
5) HOST -- The host your database is on. Leave this as an empty string if your database server is on the same physical machine (not used for SQLite).
(OR) click HERE

Django beginner problem: manage.py dbsync

I'm running ubuntu 9.04 32b and got django from Synaptics.
My settings.py is configured for a sqlite3 database.
I've been through this tutorial and got the following error when trying to run the command python manage.py syncdb :
Traceback (most recent call last):
File "manage.py", line 11, in
execute_manager(settings)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 340, in execute_manager
utility.execute()
File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 295, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 192, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 219, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 348, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/commands/syncdb.py", line 51, in handle_noargs
cursor = connection.cursor()
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 56, in cursor
cursor = self._cursor(settings)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/sqlite3/base.py", line 145, in _cursor
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
Do anyone have a clue on my problem ?
In settings.py are you using a relative path to the sqlite file?
If you are, try changing that to an absolute path.
i.e. instead of:
~/project/mydata.db
use
/home/user/project/mydata.db
This can also happen if your database name is the same as your project name. To fix it just change the name of your db e.g. by adding a .db to the NAME. So if your DATABASE NAME in settings.py was 'epic_project' change it to 'epic_project.db'
Long and boring explanation:
If you start your project by running:
django startproject epic_project
your folder structure will be like this:
/path/to/epic_project/
manage.py
epic_project/
settings.py
if then in your settings.py you set your database as:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'epic_project',
...
}
}
when
python manage.py syncdb
runs it tries to open or create a sqlite db file at /path/to/epic_project/epic_project, but there is a directory there, so it says "oh ok the path exists already, let's open it as an sqlite db", unfortunately for sqlite it's a directory and not a DB, so it cries and django presents these tears to you as "sqlite3.OperationalError: unable to open database file"
might be a permission problem, does your user have sufficient right to write on the folder? for example if you do
sudo python manage.py syncdb
instead, does it work?
For Google's sake:
The path to the database must be the full path to the file --- not just the directory where the file lives.
I had the same problem on Windows then realized that syncdb would not create the specified folder if it didn't already exist. I had specified c:/mysite/db/sqlite3.db in settings but the /db/ folder didn't exist. Created it in terminal then re-ran syncdb successfully.
I had the same problem two days ago. I solved it by setting the 'NAME' in the DATABASE dictionary (settings.py) to the absolute path.
For example.
settings.py
DATABASES = {
'default' : {
'ENGINE' : 'django.db.backends.sqlite3',
'NAME' : DATABASE_PATH,
}
}
here you can set the DATABASE_PATH at the top of the settings.py as such
(Not sure if this will work for windows)
SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.abspath(os.path.join(SETTINGS_DIR, os.pardir))
DATABASE_PATH = os.path.abspath(os.path.join(PROJECT_PATH, 'your-database-name'))
For Windows you might have to use the replace method. (Not sure .. But you can try it out as follows)
PROJECT_PATH = os.path.abspath(os.path.join(SETTINGS_DIR, os.pardir).replace('\\', '/'))
Same goes for the DATABASE_PATH.
PS. Correct me if I am wrong.
Similar to answer from user104264 - different perspective...
Currently following the YouTube tutorial I had the same error. Seems to me while running the manage.py in a virtualenv django project directory ...path to virtual env.../django_project/, the database name inside /myapp/settings.py was simply 'storage.db' so
(django-v)...path to virtual env project.../django_project/>python manage.py syncdb
created ...path to virtual env project.../django_project/storage.db
-Bill
If you are specifying a full path to db file and are still having issues, try putting an r before the string.
ie.
r'C:\home\usr\mysite\sqlite3.db'
The problem I was running into was a bootstrapping issue where some model lookups were being done at import (outside of any class or function). Per the docs, this is a bad idea.