Forums

(Pythonanywhere) is the server running on host <...> postgres.pythonanywhere-services.com

Hi, I seem to be having similar problems to others but cannot reslove them with the solutions they got provided. I'm trying to connect to my db from my Django project. Do you know what the issue is? My db settings i used credintials are below and also my pythonanywhere host, port and superuser name.

Killian

> Performing system checks...
> 
> System check identified no issues (0 silenced).

***

> ***Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000028AEEB07B70> Traceback (most
> recent call last): File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\backends\base\base.py",
> line 216, in ensure_connection self.connect() File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\backends\base\base.py",
> line 194, in connect self.connection =
> self.get_new_connection(conn_params) File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\backends\postgresql\base.py",
> line 178, in get_new_connection connection =
> Database.connect(**conn_params) File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\psycopg2\__init__.py",
> line 130, in connect conn = _connect(dsn,
> connection_factory=connection_factory, **kwasync)
> psycopg2.OperationalError: could not connect to server: Connection
> timed out (0x0000274C/10060) Is the server running on host
> "c15729771-1096.postgres.pythonanywhere-services.com" (10.0.0.46) and
> accepting TCP/IP connections on port 11096? The above exception was
> the direct cause of the following exception: Traceback (most recent
> call last): File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\utils\autoreload.py",
> line 225, in wrapper fn(*args, **kwargs) File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\core\management\commands\runserver.py",
> line 120, in inner_run self.check_migrations() File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\core\management\base.py",
> line 442, in check_migrations executor =
> MigrationExecutor(connections[DEFAULT_DB_ALIAS]) File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\migrations\executor.py",
> line 18, in __init__ self.loader = MigrationLoader(self.connection)
> File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\migrations\loader.py",
> line 49, in __init__ self.build_graph() File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\migrations\loader.py",
> line 212, in build_graph self.applied_migrations =
> recorder.applied_migrations() File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\migrations\recorder.py",
> line 61, in applied_migrations if self.has_table(): File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\migrations\recorder.py",
> line 44, in has_table return self.Migration._meta.db_table in
> self.connection.introspection.table_names(self.connection.cursor())
> File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\backends\base\base.py",
> line 255, in cursor return self._cursor() File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\backends\base\base.py",
> line 232, in _cursor self.ensure_connection() File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\backends\base\base.py",
> line 216, in ensure_connection self.connect() File
> "C:\Users\killi\PycharmProjects\Golfjournal\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\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\backends\base\base.py",
> line 216, in ensure_connection self.connect() File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\backends\base\base.py",
> line 194, in connect self.connection =
> self.get_new_connection(conn_params) File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\site-packages\django\db\backends\postgresql\base.py",
> line 178, in get_new_connection connection =
> Database.connect(**conn_params) File
> "C:\Users\killi\PycharmProjects\Golfjournal\venv\lib\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 timed out (0x0000274C/10060) Is the server running on host
> "c15729771-1096.postgres.pythonanywhere-services.com" (10.0.0.46) and
> accepting TCP/IP connections on port 11096?***


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'super',
        'USER': 'super',
        'HOST': 'c15729771-1096.postgres.pythonanywhere-services.com',
        'PASSWORD': '<<<>>>',
        'PORT': '11096',
    }
}

Address:     c15729771-1096.postgres.pythonanywhere-services.com
Port:  11096
Superuser role name:    super

It looks like you're trying to connect to your Postgres instance from outside PythonAnywhere. In order to do that, you need to use an SSH tunnel -- the servers aren't directly accessible from outside.

On Windows, the best way to do this is to use PuTTY, which you can download for free. If you start it in SSH tunnelling mode, then it will run as a server on your own computer which will look like a Postgres server but is in fact passing everything back and forth to your Postgres instance on PythonAnywhere.

To set it up, start up PuTTY, and then:

  • In the "Host name (or IP address)" field, enter ssh.pythonanywhere.com
  • In the left-hand side navigation tree, click Connection > SSH > Tunnels
  • In "Source port", enter 5432 (if you have a Postgres server running on your local machine, use 5434 or some other similar number)
  • In "Destination", enter c15729771-1096.postgres.pythonanywhere-services.com:11096
  • Click the "Add" button
  • Click "Open".
  • Enter your PythonAnywhere username, and the password you use to log on to our website.
  • Leave the SSH terminal open.

Now you'll need to change your Django settings so that the hostname is "localhost" and the port is the one you entered into "Source port" -- your code should then be able to connect.