How to install app on heroku with postgres - python

I am trying to install an app in Heroku cloud. This python based app is built on postgresql. I have set up this app in my local machine(Ubuntu). I have following queries.
How to install postgresql for this app on heroku?
Do I have to make same set up again on Heroku as I did on local machine? Like setting up of Flask etc.?
Can I submit code in many stages? e.g I have pushed code, after some time modified same code and pushed again.
I have already gone through following url https://devcenter.heroku.com/articles/git#deploying-code but didn't got any satisfactory answer.
Thanks
Sanjeev Yadav

Assuming you have heroku belt installed on your computer, create git repository inside your project(which you probably already have) then create Heroku app:
heroku create --stack cedar <name_of_your_app>
First add Postgresql to your project:
heroku addons:add heroku-postgresql
after that check if it has been added succesfully:
heroku pg:info
This should return something like:
HEROKU_POSTGRESQL_WHITE_URL <== Database name
Plan: Dev
Status: available
Then promote the database so heroku knows which one you will be using (you can have several databases in one project).
heroku pg:promote HEROKU_POSTGRESQL_WHITE_URL
At this point if you want to configure your database connections you can either get all the connection info from Heroku page of your project or from environment variable DATABASE_URL which was created when you promoted the database. If you use the latter you don't have to provide any info yourself, the function that i use frequently is something in this lines:
if "DATABASE_URL" in os.environ:
urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.environ.get("DATABASE_URL"))
DATABASE = {
"database" : url.path[1:],
"user" : url.username,
"password": url.password,
"host": url.hostname,
"port": url.port
}
return PostgresqlDatabase(**DATABASE)
This is for peewee ORM , but you can adapt it to any other ORM like SQLAlchemy, just take what's in DATABASE and plug it in to whatever you want.
A far as setting up your application, you need to write requirements.txt file with all dependencies that your app uses, this will automatically be parsed and all libraries installed when you first push your project to Heroku, also you need Procfile file inside your project, this tells Heroku which processes to run at which stage, the most important part is web part for instance:
web: gunicorn main:app
will start gunicorn server serving your site. If you need to run any scripts, for instance script that creates the tables in your database simply type:
heroku run create_tables.py
from within your repository.
As far as last question goes - yes you can push your code in many stages, your app will be restarted and all new code should work same as on your local machine. Hope this helps.

Related

Railway.app: Running command (Flask-Migrate) in Deployed Service Environment

I am in the process of migrating a Python flask web app from Heroku to Railway. The app uses a set of Flask CLI commands to initialize and update a Postgres database schema.
In the development environment, for example, I would run "flask db migrate" in order to create a db model to be copied to other locations.
In order to copy the model into staging or production environments, I would then need to run the command "flask db upgrade" on the staging/production app. Heroku's CLI "run" command allows you to do this without SSH by running the following command:
heroku run flask db upgrade --app NAME-OF-STAGING/PRODUCTION-APP
As far as I know, Railway's CLI run command does not allow you to select an online environment on which to run the command in the same way. Nor do they allow SSH access.
Is there any way to run a CLI command or Python file on a Railway live app, so that I can run Flask-migrate's "db upgrade" and get my Postgres DB working?
If you're still looking for an answer, here is what I did: I didn't use flask migrate, but instead I downloaded my database as a CSV file and created my own migration script. The script reads through the CSV file row by row and adds the data to the new database (you need to delete the old one). I called the script from my main.py, and then I removed the script after the database was migrated. This is definitely not the best way to do this, especially if your DB has a lot of data in it, but it's a good workaround.

Deploy Django app to Heroku AFTER DATABASE?

I have deployed my django website to Heroku but since the website fields are dependent upon a database that is on my local machine. I've tried using Postgres but the database on Heroku doesn't populate with the data I need it to before the app runs. Has anyone experienced this? Do I need to use an exteranl database on AWS or something (in which case, what is the best way to do this?)
Use load data and dump data: https://docs.djangoproject.com/en/3.2/ref/django-admin/#loaddata
First dump the data on your local machine
python manage.py dumpdata ..other_options > data.json
add it to git and push to herkou
git add data.json
git commit -m "Added data"
git push heroku master
now on Heroku can use loaddata to load your data to database
heroku run python manage.py loaddata data.json
And your done.
I have deployed a Django project on Heroku where I had data in models even after the deployment the data remained in the models. I worked on SQLite in production. Heroku works with Postgres but on deploying also my data remained safe. I just used git push heroku main

Can I use Heroku as a Python server?

My web host does not have python and I am trying to build a machine learning application. I know that heroku lets you use python. I was wondering if I could use heroku as a python server? As in I would let heroku do all of the python processing for me and use my regular domain for everything else.
Yes, and it may be a pain at first but once it is set I would say Heroku is the easiest platform to continually deploy to. However, it is not intuitive - don't try and just 'take a stab' at it; follow a tutorial and try and understand why Heroku works the way it does.
Following the docs is a good bet; Heroku has great documentation for the most part.
Here's the generalized workflow for deploying to Heroku:
Locally, create your project and use virtualenv to install/manage
libraries.
Initialize a git repository in the base dir for your
Python project; create a heroku remote (heroku create)
Create a
procfile for Heroku to use when starting gunicorn (or see
the options for using waitress/etc); this is used by Heroku to start your process
cd to your base dir; freeze
your virtualenv (pip freeze > requirements.txt) and add/commit
requirements.txt. This tells Heroku what packages need to be installed, a requirement for your deployment to work. If you are trying to run a Python project and there are required packages missing, the app will be unable to start and Heroku will display an Internal Server Error.
Whenever changes are made, git commit your changes and git push heroku master to push all commits to Heroku. This will cause Heroku to restart the server application with your updated deployment. If there's a failure, you can use heroku rollback to just return to your last deployment.
In reality, it's not a pain in the ass, just particular. Knowing the rules of Heroku, you are able to manage your deployment with command-line git commands with ease.
One caveat - If deploying Django, Flask applications etc there are peculiarities to account for; specifically, non-project files (including assets) should NOT be stored on Heroku as Heroku periodically restarts your 'dyno' (server instance(s)), loading the whole project from the latest push to Heroku. With Django and Flask, this typically means serving assets/static/media files from an Amazon S3 bucket.
That being said, if you use virtualenv properly, provision your databases, and follow Heroku practices for serving files and commiting updates, it is (imho) the absolute best platform out there for ease of use, reliable uptime, and well-oiled rolling deployments.
One last tip - if you are creating a Django app, I'd suggest starting your project out of this boilerplate. I have a custom one I use for new projects and can start and publish a project in minutes.
Yes, you can use Heroku as a python server. I put a Python Flask server on Heroku but it was a pain: Heroku seemed to have some difficulties, and there were lots of conflicting advice on getting around those. I eventually got it working, can't remember what web page had the ultimate answer but you might look at this one: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xviii-deployment-on-the-heroku-cloud
Have you done your Python Server on Heroku by using twisted?
I don't know if this can help you.
I see the doc 'Getting Started on Heroku with Python' is about the Django.
It is sure that Heroku can use Twisted from docs
Pure Python applications, such as headless processes and evented web frameworks like Twisted, are fully supported.
django-twisted-server has twisted in django but it isn't on Heroku.

Download sqlite database from Heroku

I have a worker running python script every 2 hour on Heroku.
The problem is each time I 'pull' the changes from git.
There is no changes at all for the sqlite3 database.
But I am sure the program is running and the database has changed by looking at the log file.
heroku log
How to retrieve the .db file then ?
It sounds like you have a little misconception. Heroku's git support is effectively one-way; you can use it to push new code to be run on the server, but you can't use it to copy files from Heroku back to your local tree.
Unfortunately it looks like there's not a good easy way to copy a file from your app to your local machine; you can use heroku run console to get a bash shell, and then scp a file out, but you're "pushing" it out of Heroku, and thus run can only copy to things with valid IP addresses.
If you're really using sqlite for your app's storage, though, you're going to run into a bigger problem. The filesystem for your app on Heroku is ephemeral, in that changes you make can be wiped out at any time. Heroku will delete your app's local storage and start over fresh whenever it wants to.
The right way to do it is use Heroku's built-in Postgres support and store your application's data there. Not only will it persist, but you'll be able to access it directly using the Postgres command-line tools.
Accessing the heroku console can now be done with:
heroku run bash
then i downloaded the linux gdrive application and ran in locally in the folder to upload my file to google drive. https://olivermarshall.net/how-to-upload-a-file-to-google-drive-from-the-command-line/ (skip step 4 and run with ./ like this ./gdrive upload my_file.txt
the other suggestion of heroku run console did not work for me (running a python flask app)

How can I check out a github repo from Heroku?

I would like to be able to log the number of words in certain files in a Github repo whenever there is a new push to the repo. I have set up a hook on Github to hit a Django Heroku app url after each push, but I don't know how to run a git pull in python from a Django app running on Heroku. Is it possible to write to the local file system in Heroku?
Check out github repo from Heroku?
from the command line you can pull from heroku easily: git pull heroku master
have set up a hook on Github to hit a Django Heroku app url after each push, but I don't know how to run a git pull in python from a Django app running on Heroku?
Is it a different heroku App (from the one that was deployed) that will be doing the pull?
Yes? then you are going to have issues. Because the pull app needs permission (heroku login) to pull... and it wont have it. Also, b/c of the ephemeral filesystem, even if you login (via heroku run bash or the like) to it, the pull app will eventually lose its logged in session (see why below)
No? then don't pull. just use the os filesystem libraries to look into the application directory...
Is it even possible to write to the local file system in Heroku?
Yes and No. You can write to the local filesystem, but its going to get nuked. See: https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem
Also, with the EFS, each dyno is going to have a different EFS - so each web process is in a way sandboxed.

Categories