How to change database url variable in heroku? - python

I'm trying to setup a simple Flask app using a postgresql database. I'm connecting to the database using psycopg2. Psycopg2 expect a dsn string for the connection, something like this :
dbname=mydb user=myuser password=1234 host=myhost.com
but heroku gives me, in my app config, a database url like this :
postgres://myuser:mypassword#myhost.com/mydb
I cannot directly change those config variables and I didn't find any way to specify their format in the heroku postgres dashboard.
Maybe there's a way to process it in python?

Related

An app deployed to Heroku can't access to MySQL database in cPanel remotely

i am deploying a python app to Heroku, an there is an error when accessing the app after deployment,
(pymysql.err.OperationalError) (2003, "Can't connect to MySQL server
obviously it is about my login system which is using MySQL to store the usernames and passwords.
Now this database sits in my cPanel, and i have already set the Remote MySQL Access hosts to a wild card "%" (trying to allow all incoming request from all ips to this database).
But now it seems this app on Heroku can't access to this database. Any solution for that?
Any help is appreciated.
if it helps create a php database query api script on your server that hosts the database and access it from your python code on heroku with requests package.

How to upload local mysql database to heroku app?

I have a web application written in Python-Flask with database created using MySQL. I have deployed my app using Heroku but it's only the frontend and backend part. I also need to upload database so that the app can interact with the database. I have searched many videos on YouTube but nothing came as useful. Most of them are using Postgre SQL both in local and heroku app but I've an MySQL database with me. I need help how can I connect my app to my database.
Heroku has native support for postgessql not mysql, so you might want to change to that or use the Clear DB addon as explained in the docs.
From heroku
"Heroku provides three managed data services to all customers:
Heroku Postgres, Heroku Redis, Apache Kafka on Heroku"
Also have a look at this previous answer that targets the same issue.

How do deploy a Flask app to Heroku without using SQLAlchemy?

I have built an an app with Flask, without using SQLAlchemy (so that I don't get an ORM, which for me give less transparency, and understanding), so just plain SQL with SQLite.
Now that I want to publish it online (now I want to test it with a free Heroku), all the tutorials I find seem to use SQLAlchemy to be able to use PostgreSQL instead of SQLite.
I am also using a schema.sql file containing all the code to create the schema (CREATE TABLE user, etc.) which is launched with the command flask init-db.
I am also a bit worried that I will need to change way more code than simply the database link.
Using the db.py code that is in the Flask tutorial, I changed the connect URL to use the one which Heroku gave me.
def get_db():
if 'db' not in g:
if os.environ.get('DATABASE_URL') is None:
g.db = sqlite3.connect(
current_app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
else:
DATABASE_URL = os.environ['DATABASE_URL']
g.db = psycopg2.connect(DATABASE_URL, sslmode='require')
return g.db
When pushing the code to Heroku with the initial commit, on the URL of my website I get a "Application error" message. And logs say:
2019-08-21T15:12:50.333541+00:00 app[init.1]: Usage: flask [OPTIONS] COMMAND [ARGS]...
2019-08-21T15:12:50.333564+00:00 app[init.1]: Try "flask --help" for help.
2019-08-21T15:12:50.333567+00:00 app[init.1]:
2019-08-21T15:12:50.333642+00:00 app[init.1]: Error: No such command "init_db".
2019-08-21T15:12:50.434989+00:00 heroku[init.1]: Process exited with status 2
2019-08-21T15:12:54.909027+00:00 heroku[init.1]: Starting process with command `FLASK_APP=myapp flask init_db`
2019-08-21T15:12:55.504819+00:00 heroku[init.1]: State changed from starting to up
2019-08-21T15:12:57.997833+00:00 heroku[init.1]: State changed from up to crashed
2019-08-21T15:12:57.888599+00:00 app[init.1]: Usage: flask [OPTIONS] COMMAND [ARGS]...
2019-08-21T15:12:57.888627+00:00 app[init.1]: Try "flask --help" for help.
2019-08-21T15:12:57.888630+00:00 app[init.1]:
2019-08-21T15:12:57.888639+00:00 app[init.1]: Error: No such command "init_db".
2019-08-21T15:12:57.978596+00:00 heroku[init.1]: Process exited with status 2
So can I easily deploy a Flask app to Heroku without using SQLAlchemy, or at least not an ORM, so that I can use my plain SQL commands?
You're conflating SQLAlchemy and PostgreSQL.
SQLAlchemy provides
a unified, relatively low-level interface for working with many different database backends via SQLAlchemy Core, and
an optional ORM
It can be used with both PostgreSQL and SQLite (and others).
PostgreSQL is a relational database management system, much like SQLite is.
You can use Postgres directly, e.g. with psycopg2, or with something like SQLAlchemy.
You can't use SQLite on Heroku, with or without SQLAlchemy. SQLite saves its data in a file on the filesystem, and this doesn't play nicely with Heroku's ephemeral filesystem.
To use Postgres without SQLAlchemy, simply
provision the add-on (this should happen automatically if you add psycopg2 to your dependencies),
and connect to it via the connection string given in the DATABASE_URL environment variable.
I am also using a schema.sql file containing all the code to create the schema (CREATE TABLE user, etc.) which is launched with the command flask init-db.
As long as the schema.sql is part of your application you should be able to do
heroku run flask init-db
If that doesn't work you should be able to do something like
cat schema.sql | heroku pg:psql
though personally I'd dig into why heroku run isn't doing what you want, first.
Finally, I strongly recommend switching to PostgreSQL in development as well.
SQLite and PostgreSQL are different products and don't always behave the same way. Some measure of uniformity can be achieved through an ORM or SQLAlchemy's Core (though even then I recommend using the same database everywhere), but if you're writing raw SQL the differences become more important.

What is the purpose of the postgresql database when using Heroku with Django?

I'm starting a new project using Django and hosting it on Heroku. I know that Django comes with a SQLite db ready to use for the application to store all of the app's data.
Heroku requires PostgreSQL - does this mean that my app will have to be configured locally to use this database INSTEAD OF the SQLite db - or does it use both? Is the PostgreSQL db just something that the Heroku app needs to work or do you actually need this to run your Django app in Heroku?
Thanks!
Found answer on https://devcenter.heroku.com/articles/sqlite3. Heroku uses/prefers postgres and not sqlite.

Can I connect a flask app to a database using MySQLdb-python vertica_python?

Is it possible to connect a Flask app to a database using MySQLdb-python and vertica_python? It seems that the recommended Flask library for accessing databases is Flask-SQLAlchemy. I have an app that connects to MySQL and Vertica databases, and have written a GUI wrapper for it in Flask using flask-wtforms, but am just getting an error when I try to test a Vertica or MySQL connection through the flask app. Is there a reason that I cannot use the prior libraries that I was using within my app?
Yes, it is possible. I was having difficulties debugging because of the opacity of the error, but ran it with app.run(debug=True), and managed to troubleshoot my problem.

Categories