How to save to a remote server with Django

隐身守侯 提交于 2019-12-12 16:16:08

问题


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, but unfortunately it requires Java for some of the NLP features (Stanford POS). Is there a way to parse and process the data on a local system and save it to the remote Postgres DB and serving the data from the remote server?

I've looked at some answers on here regarding SSH tunnelling, but I'm not sure this applies here? Alternatively would it be possible to save to the local database and periodically migrate the data to the remote database?


回答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.




回答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)



来源:https://stackoverflow.com/questions/49100598/how-to-save-to-a-remote-server-with-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!