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
Related
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.
I have deployed an app in Heroku using Django. The Django program uses a SQLite database db.sqlite3 on root directory to populate its page. Separately, there is also a Node.js scraper program that inserts to that database.
The problem is that the hard-refreshed webpage shows the same data even after the content of the database changed. Curiously, this does not happen when it is tested locally with python manage.py runserver. How can I fix this problem?
Thank you in advance!
For reference, here is my requirements.txt file:
Django==1.10.6
gunicorn==19.7.1
Pillow==4.0.0
selenium==3.3.1
whitenoise==3.3.0
You cannot use sqlite on Heroku.
An sqlite db is stored as a file on the local filesystem. But in Heroku the filesystem is ephemeral and is not shared between dynos. Every time you redeploy your app, or scale your process, or in your case launch a worker, you get a new filesystem with a different copy of the db file.
Use the proper Postgres support via the add-ons.
Say I have deployed a django website on heroku through a github repository. (For deployment I simply clicked on the deploy button here - https://github.com/AmmsA/theresumator#theresumator---using-django-resumator.) I now update the repository with new commits.
Q: How can I make changes in the deployed website from the repository without losing the data already present on the repository.
When you are pushing the fresh commits git push heroku master or via git hook git push origin master -- these nothing to do with heroku database.
But this will run this command when build python manage.py migrate so if you are changed something in the migrations definetly db schema get alter not the values stored in there.
Just set your remote project:
heroku git:remote -a
And then, push your project on Heroku:
git push heroku main
I've written an article on it. You can see that. https://medium.com/#sreebash/how-to-update-previous-deployed-project-on-heroku-c778d555cd8a
I want to update a field in my users table on my django project hosted on heroku.
Is there a way I can run a script(if so from where?) using what?
That allows me to update a field in the database? I could do this manually in the django admin but it would take way to long as there are large number of users.
Any advice is appreciated.
i suggest you update the data in your local then make a fixture, commit and push it in your heroku. then do load the data using the terminal
update data (locally)
make a fixture (manage.py dumpdata)
commit and push to heroku
login via terminal (heroku login)
load the data (heroku run python manage.py loaddata .json)
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.