'Heroku H10 error when deploying Flask application with Psycopg2

Runs locally, but not when deployed to Heroku (see error message).

I tried adding a runtime.txt file with the Python version, and tried most of suggestions to similar questions here on Stackowerflow: To change something about the Port or the Procfile. But I clearly missed something.

app.py


from flask import Flask, render_template, request, flash, session, Markup, redirect, url_for
import cgi
import psycopg2

connect = psycopg2.connect(
    host = "ec2-18-210-64-223.compute-1.amazonaws.com",
    dbname = "d871o598hou16e",
    user = "ntvbkqtnpwzevj",
    password = "882d0df267e2ce38ec994997b52e1a0bff78d949d3c3ad15b1b1e138b1fc4e6a",
    port = "5432")
# the above are credentials from Heroku Postgres add-on.


cur = connect.cursor()
#...

app = Flask(__name__)

@app.route("/", methods=["GET", "POST"])
def home():
    return render_template("index.html")

port = int(os.environ.get("PORT", 5000))
if __name__ == "__main__":
    app.secret_key="lol"
    app.run(host='0.0.0.0', port=port, debug=True)


connect.commit()
cur.close()
connect.close()

Procfile

web: gunicorn app:app 

requirements.txt

click==8.0.3
colorama==0.4.4
Flask==2.0.2
gunicorn==20.1.0
itsdangerous==2.0.1
Jinja2==3.0.3
MarkupSafe==2.0.1
psycopg2==2.9.3
Werkzeug==2.0.2     

Heroku error log (all there is)

2022-04-08T19:36:33.582752+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 242, in handle_chld
2022-04-08T19:36:33.582842+00:00 app[web.1]:     self.reap_workers()
2022-04-08T19:36:33.582852+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
2022-04-08T19:36:33.582967+00:00 app[web.1]:     raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2022-04-08T19:36:33.582997+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2022-04-08T19:36:33.744976+00:00 heroku[web.1]: Process exited with status 1
2022-04-08T19:36:33.871744+00:00 heroku[web.1]: State changed from up to crashed
2022-04-08T19:36:35.753303+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=movie-recommendation-service-o.herokuapp.com request_id=ad363000-ea61-432a-8b24-c0d0214aa899 fwd="5.33.58.163" dyno= connect= service= status=503 bytes= protocol=https
2022-04-08T19:36:36.100503+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=movie-recommendation-service-o.herokuapp.com request_id=2b41172e-8bd5-4ef0-ad1c-3b906d7686da fwd="5.33.58.163" dyno= connect= service= status=503 bytes= protocol=https
2022-04-08T19:36:45.395444+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/addMovie" host=movie-recommendation-service-o.herokuapp.com request_id=331c5ae1-73c0-4b14-b73a-53a12ef6f61f fwd="5.33.58.163" dyno= connect= service= status=503 bytes= protocol=https
2022-04-08T19:36:45.643071+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=movie-recommendation-service-o.herokuapp.com request_id=bc3d9cbe-e54a-47b7-aabd-ab2b75377e7e fwd="5.33.58.163" dyno= connect= service= status=503 bytes= protocol=https

I've seen all similar questions and tried the all solutions I understood without luck. As you can tell, Im new here so hope this post is ok. Please provide example text to clarify.



Solution 1:[1]

I had to create a postgres server that had the same credentials. For that I used pgAdmin.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rasmus