Connecting Heroku Database to SQLAlchemy - python

so as part of a Web Programming MOOC, I have to connect to a PostgreSQL Database hosted on Heroku using Python and SQLalchemy. I have spent a lot of hours trying to do this to no avail. My main problem is that I'm not being able to connect to the Database, because every time a try to run a script to update or just check the Database I get "Is the server running on host "ec2-174-129-35-61.compute-1.amazonaws.com" (174.129.35.61) and accepting
TCP/IP connections on port 5432?" in the command prompt.
I've done a lot of research trying to figure it out and it seems that maybe the URI of my Database is not set to accept public connections. But I tried setting ssl = require and still haven't been able to solve the problem. Maybe I didn't do it right?
I have come to think that maybe I am doing something wrong with the code, or that I should try to connect through Heroku's CLI and not the command prompt but I am not sure. This is my simple test code:
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
db.execute("INSERT INTO books VALUES (1, 1, 1, 1)")
db.commit()
I set the DATABASE_URL environment variable in the command prompt to the Database's URI but maybe I did it wrong? I also tried just using the URI in the code itself but still could not make it work.
This is my first time using Databases with Heroku so I am complete neewbie, maybe I am doing something else wrong?. I appreciate all the help that you guys can give. Thanks in advance

1) Check, did you copied all link, including postgres://
2) Maybe you had made a mistake during setting DATABASE_URL. So, try to add your URL directly to the script:
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = "postgres://ennjrjkrfsb____all_code_of_key_.....___compute.amazonaws.com:5432/ddo7541ka3hio7"
db = scoped_session(sessionmaker(bind=engine))
db.execute("INSERT INTO books VALUES (1, 1, 1, 1)")
db.commit()
3) if you are using linux, try to open your database, using command psql postgres://ennjrjkrfsb____all_code_of_key_.....___compute.amazonaws.com:5432/ddo7541ka3hio7
4) Try to access to your database in any way via other internet provider (may be your provider or sys admin in your office restricted access to Heroku)
Update: Try to use free online IDE with interpreter (based on Linux), for example, https://cs50.io - from this site you can access to Heroku postgres

postgres dialect does not support any more in SQLAlchemy v1.4.x above, and it turns into postgresql, so the final connection string must be postgresql://username:password#host:port/database
Here is my working connection from SQLAlchemy to Heroku Postgresql
database_url = os.environ('DATABASE_URL')
if database_url.startswith('postgres://'):
database_url.replace('postgres://', 'postgresql://')
engine = create_engine(database_url)
# rest of your codes...
here is my reference Heroku Issue

Related

Login to Flask app with database credentials

This may be more of a concept question as opposed to a "How do I" question. Currently, I am looking to create a flask app to help me create somewhat complex MongoDB entries. This worked well originally in my CLI based script when these entries were more simple. I am looking for a way to create a safer way to deploy this app on some docker containers without giving the application itself access to the database. The idea I am trying to work out is, can I create a flask app that has no access to the DB until the user logs in and, that users login credentials are the database credentials. I am struggling to work this out, if anyone could provide some insight I would be most thankful.
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
# sqlalchemy
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
In general practice, we setup environment variables before running the application so that the code itself do not explicitly show database url.
In mac and linux this is done using
export DATABASE_URL=yourdatabaseurl
flask run
In windows
set DATABASE_URL=yourdatabaseurl
flask run

How to connect to PostgreSQL using SQLAlchemy?

I am trying to connect to my PostgreSQL database using SLQAlchemy. I have installed psycopg2 and tried to connect to database with a raw python code and it worked but this time I am trying using SQLAlchemy. I read the documentation from SQLAlchemy docs. This might look very simple and I could not find a solution in the internet either for this specific one.
This is my code, so simple:
import psycopg2
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine('postgresql+psycopg2://nysa:mypassword#127.0.0.1:46015/nysa')
db = scoped_session(sessionmaker(bind=engine))
connection = engine.connect()
result = connection.execute('select*from table')
print(result)
When running the *.py file nothing happens and there are no errors and I tried to find the issue with pycharm's debugger and I noticed that in the line connection = engine.connect() gets stuck like in a infinite loop. I am pretty sure that my database credentials are correct. How do I connect to database?
Got it. When I opening pgAdmin it shows a different port and I thought that I had to put that port in the code, that is why It did not work. Worked with 127.0.0.1:5432.

"MySQL server has gone away" error after long idle

I know this issue is not a new one on SO but I'm unable to find a solution. Whenever I return to my desk after leaving my app running overnight, I get a MySQL server has gone away error that persists until I restart my uwsgi service. I've already done the following:
pool_recycle=some really large number in my create_engine() call
Added a ping_connection() after a #event.listens_for() decorator (and I can't use pool_pre_ping - that breaks my create_engine() call)
in /etc/my.cnf I added wait_timeout and interactive_timeout params with large values
but nothing has had any effect.
From the sqlalchemy doc located here, the pool_recycle feature is what you are looking for.
from sqlalchemy import create_engine
engine = create_engine("mysql://scott:tiger#localhost/test", pool_recycle=28700)
Set pool_recycle to a value < wait_timeout in your mysql configuration file my.cnf
MySQL default wait_time is 28800 (8 hrs)
Dont forget to restart your services (i.e. mysql, etc) if you do modify the conf files

Unknown database error after creating it (sqlalchemy)

I'm getting the next error while trying to run my Flask app:
sqlalchemy.exc.OperationalError
OperationalError: (_mysql_exceptions.OperationalError) (1049, "Unknown database '/home/gerardo/Documentos/python_web_dev/flask-intro/app2.db'")
so it seems like there is no database.. but I ran the next scripts using sqlalchemy_utils and everything was ok:
engine = create_engine("mysql://root:#localhost/home/gerardo/Documentos/python_web_dev/flask-intro/app2.db")
create_database(engine.url)
but still I get the error..
You have confused MySQL, a database server, with SQLite, a database in a file. You created a SQLite file, but are trying to tell MySQL to connect to it, which makes no sense.
Use the sqlite dialect in the connection string.
You can avoid typing the whole path (and tying the app to that path) by pointing to the file relative to the app's location.
import os
db_path = os.path.realpath(os.path.join(os.path.dirname(__file__), 'app2.db'))
engine = create_engine('sqlite://{}'.format(db_path))
Consider using Flask-SQLAlchemy rather than trying to manage the database yourself.

How to configure MongoEngine for Heroku

I have postgresql as my main database and I am using mongodb for 'audit-trail' purposes.
I am using mongoengine for my django app.
In localhost, in order to connect to mongodb database I did this:
connect('dbname')
and that is it, as documentation suggested.
But this is not working in heroku.
I added MongoLab add-on to my heroku application and they gave me this connections string:
mongodb://<dbuser>:<dbpassword>#ds037622.mongolab.com:37622/heroku_app30998840
Then I tried this:
connect('heroku_app30998840', username='username', password='pwd', host='mongodb://<dbuser>:<dbpassword>#ds037622.mongolab.com:37622/heroku_app30998840')
This did not work either.
How can I fix this?
Or maybe I should use something else other than MongoEngine? some other better way?
That is what worked for me:
connect('heroku_app30998840', username='username', password='pwd', host='#ds037622.mongolab.com:37622')
i.e remove everything except host and port number in host part.

Categories

Resources