1

I've been trying to set up a new postgresql database with a remote server (Ubuntu) a Django project for the last 2 days but keep having this same issue over and over again. What should I do?

Here's the error output that I'm getting when I try to make migrations to the database:

$ python manage.py makemigrations
Traceback (most recent call last):
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
    self.connect()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\postgresql\base.py", line 178, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\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 (0x0000274D/10061)
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
        Is the server running on host "localhost" (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 "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\core\management\commands\makemigrations.py", line 101, in handle
    loader.check_consistent_history(connection)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\migrations\loader.py", line 283, in check_consistent_history
    applied = recorder.applied_migrations()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations
    if self.has_table():
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\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 "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 256, in cursor
    return self._cursor()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 233, in _cursor
    self.ensure_connection()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
    self.connect()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
    self.connect()
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\site-packages\django\db\backends\postgresql\base.py", line 178, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\loicq\desktop\coding\uvergo\venv\lib\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 (0x0000274D/10061)
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?

And the code in my settings file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'myDB',
        'USER': 'loicq',
        'PASSWORD': '*********',
        'HOST': 'localhost',
        'PORT': '',
    }
}

I've run the following to but still getting the error:

sudo -i -u root
echo "listen_addresses = '*'" >> /etc/postgresql/*/main/postgresql.conf
echo 'host all all 0.0.0.0/0 md5' >> /etc/postgresql/*/main/pg_hba.conf
sudo /etc/init.d/postgresql restart

And I also created a new Server in PgAdmin with the same Ip Adress as my Droplet on Digital Ocean but still.

Please let me know if you have anything, thanks in advance!

locq
  • 301
  • 1
  • 5
  • 22

2 Answers2

2

Postgres uses PORT 5432 as standard. I would recommend to updated your settings.py this way:

   DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql',
          'NAME': 'dbname',
          'USER': 'postgres',
          'PASSWORD': 'password',
          'HOST': 'localhost',
          'PORT': '5432',
           }

     }
  • 1
    Thanks, not sure if it’s going to work but I’m going to give it a try. Should I add the _psycopg2 extension after backends.postrgesql or it is not necessary? – locq Apr 21 '19 at 11:59
2

Okay so I found the solution:

I replaced this code in my settings.py file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'myDB',
        'USER': 'loicq',
        'PASSWORD': '*********',
        'HOST': 'localhost',
        'PORT': '',
    }
}

With this:

DATABASES = {'default': dj_database_url.config(default='postgres://YOURUSER:YOURPASSWORD@YOURHOST:5432/YOURDATABASENAME')}

Installed dj-database-url with pip:

pip install dj_database_url

And now it work perfectly!

locq
  • 301
  • 1
  • 5
  • 22