Heroku/Django Connection refused: Is the server running on host “localhost” (127.0.0.1) and accepting TCP/IP connections on port 5432?

4,633 views
Skip to first unread message

Marco Mera

unread,
Jul 6, 2017, 7:47:58 PM7/6/17
to Django users
Hi all,


I just started learning Django using http://www.gettingstartedwithdjango.com/introduction-and-launch.html as a resource. I'm stuck on this problem of connecting to psql through the heroku server. THANKS IN ADVANCE!!

I'm on MacOX but I'm using vagrant to ssh to Ubuntu 12.04 machine.


I'm running:

$psql --version
psql (PostgreSQL) 9.4.12

$which psql
/usr/bin/psql

Things I've tried:

1.

$service postgresql status
9.4/main (port 5432): online

2.

$sudo netstat -nl | grep postgres
unix  2      [ ACC ]     STREAM     LISTENING     88800    /var/run/postgresql/.s.PGSQL.5432

3.

In my postgresql.conf file set: `listen_addresses = '*'

    4.

In my settings/local.py :

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'microblog',
            'USER': 'vagrant',
            'PASSWORD': 'vagrant',
            'HOST': 'localhost',
            'PORT': '5432'
    }

settings/base.py :

DATABASES = {
    'default': dj_database_url.config(default='postgres://vagrant:vagrant@localhost:5432/microblog')
}

The stack trace I'm getting is:

2017-07-06T19:55:10.932781+00:00 heroku[web.1]: Starting process with command `python manage.py runserver 0.0.0.0:5303 --noreload`
2017-07-06T19:55:13.876394+00:00 app[web.1]: Performing system checks...
2017-07-06T19:55:13.876416+00:00 app[web.1]: 
2017-07-06T19:55:13.904755+00:00 app[web.1]: System check identified some issues:
2017-07-06T19:55:13.904757+00:00 app[web.1]: 
2017-07-06T19:55:13.904758+00:00 app[web.1]: WARNINGS:
2017-07-06T19:55:13.904773+00:00 app[web.1]: ?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DEBUG.
2017-07-06T19:55:13.904774+00:00 app[web.1]: 
2017-07-06T19:55:13.904775+00:00 app[web.1]: System check identified 1 issue (0 silenced).
2017-07-06T19:55:13.928308+00:00 app[web.1]: Traceback (most recent call last):
2017-07-06T19:55:13.928310+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
2017-07-06T19:55:13.928486+00:00 app[web.1]:     self.connect()
2017-07-06T19:55:13.928488+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect
2017-07-06T19:55:13.928614+00:00 app[web.1]:     self.connection = self.get_new_connection(conn_params)
2017-07-06T19:55:13.928615+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
2017-07-06T19:55:13.928745+00:00 app[web.1]:     connection = Database.connect(**conn_params)
2017-07-06T19:55:13.928746+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
2017-07-06T19:55:13.928863+00:00 app[web.1]:     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
2017-07-06T19:55:13.928866+00:00 app[web.1]: psycopg2.OperationalError: could not connect to server: Connection refused
2017-07-06T19:55:13.928867+00:00 app[web.1]:    Is the server running on host "localhost" (127.0.0.1) and accepting
2017-07-06T19:55:13.928867+00:00 app[web.1]:    TCP/IP connections on port 5432?
2017-07-06T19:55:13.928868+00:00 app[web.1]: 
2017-07-06T19:55:13.928870+00:00 app[web.1]: 
2017-07-06T19:55:13.928870+00:00 app[web.1]: The above exception was the direct cause of the following exception:
2017-07-06T19:55:13.928871+00:00 app[web.1]: 
2017-07-06T19:55:13.928872+00:00 app[web.1]: Traceback (most recent call last):
2017-07-06T19:55:13.928898+00:00 app[web.1]:   File "manage.py", line 22, in <module>
2017-07-06T19:55:13.928981+00:00 app[web.1]:     execute_from_command_line(sys.argv)
2017-07-06T19:55:13.928983+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
2017-07-06T19:55:13.929156+00:00 app[web.1]:     utility.execute()
2017-07-06T19:55:13.929157+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
2017-07-06T19:55:13.929322+00:00 app[web.1]:     self.fetch_command(subcommand).run_from_argv(self.argv)
2017-07-06T19:55:13.929324+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
2017-07-06T19:55:13.929478+00:00 app[web.1]:     self.execute(*args, **cmd_options)
2017-07-06T19:55:13.929480+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 62, in execute
2017-07-06T19:55:13.929569+00:00 app[web.1]:     super(Command, self).execute(*args, **options)
2017-07-06T19:55:13.929571+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
2017-07-06T19:55:13.929732+00:00 app[web.1]:     output = self.handle(*args, **options)
2017-07-06T19:55:13.929734+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 101, in handle
2017-07-06T19:55:13.929831+00:00 app[web.1]:     self.run(**options)
2017-07-06T19:55:13.929833+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 112, in run
2017-07-06T19:55:13.929932+00:00 app[web.1]:     self.inner_run(None, **options)
2017-07-06T19:55:13.929933+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 128, in inner_run
2017-07-06T19:55:13.930036+00:00 app[web.1]:     self.check_migrations()
2017-07-06T19:55:13.930038+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 422, in check_migrations
2017-07-06T19:55:13.930232+00:00 app[web.1]:     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
2017-07-06T19:55:13.930233+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 20, in __init__
2017-07-06T19:55:13.930318+00:00 app[web.1]:     self.loader = MigrationLoader(self.connection)
2017-07-06T19:55:13.930319+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/loader.py", line 52, in __init__
2017-07-06T19:55:13.930405+00:00 app[web.1]:     self.build_graph()
2017-07-06T19:55:13.930407+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/loader.py", line 209, in build_graph
2017-07-06T19:55:13.930540+00:00 app[web.1]:     self.applied_migrations = recorder.applied_migrations()
2017-07-06T19:55:13.930541+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
2017-07-06T19:55:13.930630+00:00 app[web.1]:     self.ensure_schema()
2017-07-06T19:55:13.930631+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
2017-07-06T19:55:13.930716+00:00 app[web.1]:     if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
2017-07-06T19:55:13.930718+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 254, in cursor
2017-07-06T19:55:13.930879+00:00 app[web.1]:     return self._cursor()
2017-07-06T19:55:13.930881+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 229, in _cursor
2017-07-06T19:55:13.931014+00:00 app[web.1]:     self.ensure_connection()
2017-07-06T19:55:13.931015+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
2017-07-06T19:55:13.931145+00:00 app[web.1]:     self.connect()
2017-07-06T19:55:13.931146+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
2017-07-06T19:55:13.931242+00:00 app[web.1]:     six.reraise(dj_exc_type, dj_exc_value, traceback)
2017-07-06T19:55:13.931243+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
2017-07-06T19:55:13.931501+00:00 app[web.1]:     raise value.with_traceback(tb)
2017-07-06T19:55:13.931503+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
2017-07-06T19:55:13.931634+00:00 app[web.1]:     self.connect()
2017-07-06T19:55:13.931636+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect
2017-07-06T19:55:13.931755+00:00 app[web.1]:     self.connection = self.get_new_connection(conn_params)
2017-07-06T19:55:13.931756+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
2017-07-06T19:55:13.931874+00:00 app[web.1]:     connection = Database.connect(**conn_params)
2017-07-06T19:55:13.931875+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
2017-07-06T19:55:13.931988+00:00 app[web.1]:     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
2017-07-06T19:55:13.931992+00:00 app[web.1]: django.db.utils.OperationalError: could not connect to server: Connection refused
2017-07-06T19:55:13.931993+00:00 app[web.1]:    Is the server running on host "localhost" (127.0.0.1) and accepting
2017-07-06T19:55:13.931994+00:00 app[web.1]:    TCP/IP connections on port 5432? 

Constantine Covtushenko

unread,
Jul 6, 2017, 10:52:44 PM7/6/17
to django...@googlegroups.com
Hi Marco,

Welcome to community!

As suggested in Deploying Python and Django Apps on Heroku article
you should add heroku-postgresql addon to your heroku application. With this your application will get connection string in 'DATABASE_URL' environment variable. For more info please see that article. With this variable you should use 'dj_database_url' as following:

--------------
# Update database configuration with $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
--------------

As I see from your message you trying to get access from heroku server in the cloud to your 'localhost'. It is impossible as for heroku server 'localhost' is heroku server and not your Ubuntu 12.04.

Does it make any sense?

Regards,
Constantine C.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/951772e0-e513-45cc-9825-c3f3d7fa3c3d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Sincerely yours,
Constantine C
Reply all
Reply to author
Forward
0 new messages