How to deploy a FastAPI app using PostgreSQL as a database on Heroku

Viewed 1849

I developed a FastAPI app in a Virtual Environment using an SQLite database but deployed it on Heroku with a PostgresSQL database on Heroku as suggested in the tutorial. Although it worked on my PC, adding PostegresSQL as an addon & replacing the value of SQLALCHEMY_DATABASE_URL in the database.py broke everything. Note that I've properly frozen the dependencies on the requirements.txt file. Yet I can't figure out what went wrong.

For further clarification, I've pushed my code to GitHub & it can be accessed at this repository - Self_calculation.

2 Answers

If you are using Postgres Addon on Heroku, probably your solution is simple.

Use os.environ to get the connection parameters , don't try to connect directly, it's Heroku's recommended solution from Heroku Postgres

import os

DATABASE_URL = os.environ.get('DATABASE_URL')

I just need to run this:
1)heroku git:remote -a my_heroku_app_name

2)heroku logs --tail

after that I could see my problem.
In my case I forgot to change postgres url in alembic.ini file

Related