How to save to a remote server with Django - 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, 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?

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.

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)

Related

Database not exist in docker compose on windows debian subsystem

I want to try to dockerize my Django project, but I face a problem that the database doesn't exist. I run docker on WSL2. I use PostgreSQL.
TLDR: Try adding - DATABASE_HOST=db on line 28
Even though you are running all the containers on your host. They do not share localhost nor 127.0.0.1. Docker creates its own network and every containers have its own IP address(es) and Network interface(s).
When using Docker Compose, you can use the service name in this case db and web to point to a container. You can also use host.docker.internal to point at the actual host.
In your case, Django is trying to connect to a database that runs on the web container but there is none.

How to run a Django's application server on a VPS server

I have an application that should run via VPS terminal-based so that the web app can be online permanently.
The command: python manage.py runserver 173.249.8.237:8000 should be executed via the VPS terminal because when I use putty via my laptop, whenever I switch off my putty software via my laptop, the application won't be accessible.
Please, suggest to me a way open a terminal in order to run the Django application server.
Or, is there any method that can allow me to my Django web application on VPS?
Thanks in advence
Don't do that.
runserver according Django Docs
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone
through security audits or performance tests. (And that’s how it’s
gonna stay. We’re in the business of making web frameworks, not web
servers, so improving this server to be able to handle a production
environment is outside the scope of Django.)
You need to configure your Django application to run in production environments. You change choose docker for that.
Or simple running with gunicorn.
Access your VPS through SSH or Putty.
Copy your application code inside VPS
Configure your application to run with gunicorn or docker
Access your VPS address in your browser.
And turn of the DEBUG mode, by setting DEBUG=False

Heroku: installing Postgres without local db | Django

I've already pushed my Django files to Heroku via Git and now I'd like to configure at Heroku server the Postgres database. After that, I need to transfer my local sqlite database to Heroku Postgres.
All of this issue is because I don't have admin rights on my local PC (corporate unit) to install Postgres.
So basically:
1. Configure remotely Postegres at Heroku;
2. Migrate local database (sqlite) to Heroku (Postgres).
I don't know if there is another path to go on...
Thank you!
That's some crazy gymnastics you're trying to do. Getting a grasp of PostgreSQL setup is hard as it is. There are several user/table privileges you have to maintain. Sqlite3 has very simple settings in the settings.py file in Django, while Postgres requires a username, password, Host, and Port.
Also the sql you export from sqlite3, might not ingest directly into the PostgreSQL. See this. You'll have to install Postgres on your local machine, if you plan to have any sort of ok workflow as far as I can see. You can probably explore docker to create a local dev environment on your corporate PC.

cannot access a development server on a server im ssh'd into

I am deploying a django app from a Centos server. When i do a python3.6 manage.py runserver 8000 command it starts a development server no problem. I am not able to access this page from my local computer to test it.
so the steps i take are: i ssh into the server by doing ssh <user>#url.com and then run the dev server with the above command. I then go to the browser on my laptop and type url.com:8000 and will come up with Unable to connect
I also have this problem when running my apache server for production. i would have no problems putting up the server on the server im ssh'd into but cannot access the webpage.
I know this is very little information to go on but does this sound like a server side issue at url.com? Should i be contacting the administrators with this, or is this something on my end possibly?
Maybe i need to configure the address my settings.py in my django app?
You probably want to run it so it listens on any interface. From the documentation:
Note that the default IP address, 127.0.0.1, is not accessible from
other machines on your network. To make your development server
viewable to other machines on the network, use its own IP address
(e.g. 192.168.2.1) or 0.0.0.0 or :: (with IPv6 enabled).
By example, you should start the server with
python3.6 manage.py runserver 0.0.0.0:8000
In general, it is probably not wise to keep such a thing running on the web, particularly with debug on. From the same documentation link:
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone
through security audits or performance tests. (And that’s how it’s
gonna stay. We’re in the business of making Web frameworks, not Web
servers, so improving this server to be able to handle a production
environment is outside the scope of Django.)

Django on Vagrant, Postgres both locally and on Heroku, could not connect to server: No such file or directory

I finally got my Django app to deploy on Heroku, using Vagrant and Postgres for both local and production. The localhost is up and running, and I'm in the admin, adding users. But when I run
heroku run python manage.py syncdb
it barfs up this error:
psycopg2.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Same thing happens when I try to access the admin online: http://vast-sierra-7949.herokuapp.com/admin/
I'm new to Heroku, and I've tried just about every getting started tutorial I could find, including
http://gettingstartedwithdjango.com/en/lessons/introduction-and-launch/
https://devcenter.heroku.com/articles/django,
https://github.com/callmephilip/django-heroku-bootstrap, and
https://github.com/jpadilla/django-project-template
This last github link actually allowed me to deploy, before I was just getting an error when I ran
git push heroku master
and that error was: manage.py: error: no such option: --noreload
I know there are several posts with this error, but I've looked through as many as I could find with no luck on resolving the issue.
Thank you in advance,
Anthony
Be sure to setup heroku DB settings
Check out this blog post: http://jamie.ideasasylum.com/2012/09/connecting-navicat-to-postgresql-on-vagrant/
The author talks about how you have to modify pg_hba.conf by adding the following lines to allow a host machine (in this case, you Heroku instance) to connect to a postgres server installed on a guest VM within the host.
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 10.0.2.2/32 md5
I'm not sure if you can access these files on a Heroku instance, but it's a place to start. Good luck!