Skip to content Skip to sidebar Skip to footer

How To Save To A Remote Server With Django

I'm fairly new to Python and Django. I've recently got a Django app working on localhost in Linux Mint 18.3 with a Postgresql database. I've uploaded the app to PythonAnywhere, bu

Solution 1:

Yes -- to access your PythonAnywhere Postgres DB from your local machine, you'll need to use SSH tunnelling. If you're using a Unix-like operating system (eg. Linux or Mac) on your local machine, the "Manual SSH tunnelling" instructions at the bottom of this help page will handle that, with a couple of tweaks.

The SSH command on the help page is:

ssh -L 3306:username.mysql.pythonanywhere-services.com:3306 username@ssh.pythonanywhere.com

...which, as you can see, is for MySQL. To make it work for your PythonAnywhere Postgres server, replace:

  • The first 3306 with 5432 (which means that on your local machine, it will use the default Postgres port)
  • The username.mysql.pythonanywhere-services.com with the Postgres server hostname from the "Postgres" tab on the "Databases" page inside PythonAnywhere.
  • The second 3306 with the port from the "Postgres" tab on the "Databases" page inside PythonAnywhere.

So you'll wind up with something like

ssh -L 5432:username-123.postgres.pythonanywhere-services.com:10123 username@ssh.pythonanywhere.com

...with different values for username, 123 and and 10123.

When you run that command (and entered your PythonAnywhere login password, which it will prompt you for), a process will start up on your machine that looks like a Postgres server to all local processes, but is actually just forwarding everything back and forth to the PythonAnywhere-hosted database server. So you can run your parsing and processing code locally, and it will work transparently.

Solution 2:

Yes you just have to connect your local env to your remote DB using Django DATABASES settings https://docs.djangoproject.com/en/2.0/ref/settings/#databases

Be careful to use your Django version in the doc (here 2.0)

That way you can write from your local env, and read from your remote server (using the same settings to connect to your remote DB)

Post a Comment for "How To Save To A Remote Server With Django"