Could Not Connect To Server: Connection Refused

This is a common exception with Postgres. I am using Mac so it happens frequently when I have to switch between my cloud database and the local database. This can also happen when your Macbook shutdown because of an error or some system issue.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
  self.connect()
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
  return func(*args, **kwargs)
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/db/backends/base/base.py", line 197, in connect
  self.connection = self.get_new_connection(conn_params)
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
  return func(*args, **kwargs)
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
  connection = Database.connect(**conn_params)
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/psycopg2/__init__.py", line 126, 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 "127.0.0.1" and accepting
    TCP/IP connections on port 5432?

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
  self.run()
 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
  self._target(*self._args, **self._kwargs)
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
  fn(*args, **kwargs)
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
  self.check_migrations()
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/core/management/base.py", line 458, in check_migrations
  executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
  self.loader = MigrationLoader(self.connection)
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/db/migrations/loader.py", line 49, in __init__
  self.build_graph()
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/db/migrations/loader.py", line 212, in build_graph
  self.applied_migrations = recorder.applied_migrations()
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 76, in applied_migrations
  if self.has_table():
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 56, in has_table
  return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
  return func(*args, **kwargs)
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/db/backends/base/base.py", line 260, in cursor
  return self._cursor()
....
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
  connection = Database.connect(**conn_params)
 File "/Users/yourname/Documents/dev/django/lib/python3.8/site-packages/psycopg2/__init__.py", line 126, 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?

This can be solve by just running one command in your terminal window:

1
rm -f /usr/local/var/postgres/postmaster.pid

Your PostGres and Django Application should start up with no exceptions. Anytime you face the above problem, just run the above terminal command.

Related Posts

0 Comments

12345

    00